parent
ff39f6e260
commit
c7c6b13a32
6 changed files with 32 additions and 13 deletions
|
|
@ -13,6 +13,9 @@
|
||||||
|
|
||||||
## Standard library additions and changes
|
## Standard library additions and changes
|
||||||
|
|
||||||
|
- `macros.parseExpr` and `macros.parseStmt` now accept an optional
|
||||||
|
filename argument for more informative errors.
|
||||||
|
|
||||||
## `std/smtp`
|
## `std/smtp`
|
||||||
|
|
||||||
- Sends `ehlo` first. If the mail server does not understand, it sends `helo` as a fallback.
|
- Sends `ehlo` first. If the mail server does not understand, it sends `helo` as a fallback.
|
||||||
|
|
|
||||||
|
|
@ -1748,11 +1748,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
|
||||||
elif instr.opcode == opcNHint:
|
elif instr.opcode == opcNHint:
|
||||||
message(c.config, info, hintUser, a.strVal)
|
message(c.config, info, hintUser, a.strVal)
|
||||||
of opcParseExprToAst:
|
of opcParseExprToAst:
|
||||||
decodeB(rkNode)
|
decodeBC(rkNode)
|
||||||
# c.debug[pc].line.int - countLines(regs[rb].strVal) ?
|
|
||||||
var error: string
|
var error: string
|
||||||
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
|
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
|
||||||
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
|
regs[rc].node.strVal, 0,
|
||||||
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
|
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
|
||||||
if error.len == 0 and msg <= errMax:
|
if error.len == 0 and msg <= errMax:
|
||||||
error = formatMsg(conf, info, msg, arg))
|
error = formatMsg(conf, info, msg, arg))
|
||||||
|
|
@ -1764,10 +1763,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
|
||||||
else:
|
else:
|
||||||
regs[ra].node = ast[0]
|
regs[ra].node = ast[0]
|
||||||
of opcParseStmtToAst:
|
of opcParseStmtToAst:
|
||||||
decodeB(rkNode)
|
decodeBC(rkNode)
|
||||||
var error: string
|
var error: string
|
||||||
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
|
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
|
||||||
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
|
regs[rc].node.strVal, 0,
|
||||||
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
|
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
|
||||||
if error.len == 0 and msg <= errMax:
|
if error.len == 0 and msg <= errMax:
|
||||||
error = formatMsg(conf, info, msg, arg))
|
error = formatMsg(conf, info, msg, arg))
|
||||||
|
|
|
||||||
|
|
@ -1225,9 +1225,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
|
||||||
unused(c, n, dest)
|
unused(c, n, dest)
|
||||||
genBinaryStmtVar(c, n, opcAddSeqElem)
|
genBinaryStmtVar(c, n, opcAddSeqElem)
|
||||||
of mParseExprToAst:
|
of mParseExprToAst:
|
||||||
genUnaryABC(c, n, dest, opcParseExprToAst)
|
genBinaryABC(c, n, dest, opcParseExprToAst)
|
||||||
of mParseStmtToAst:
|
of mParseStmtToAst:
|
||||||
genUnaryABC(c, n, dest, opcParseStmtToAst)
|
genBinaryABC(c, n, dest, opcParseStmtToAst)
|
||||||
of mTypeTrait:
|
of mTypeTrait:
|
||||||
let tmp = c.genx(n[1])
|
let tmp = c.genx(n[1])
|
||||||
if dest < 0: dest = c.getTemp(n.typ)
|
if dest < 0: dest = c.getTemp(n.typ)
|
||||||
|
|
|
||||||
|
|
@ -511,27 +511,29 @@ proc lineInfo*(arg: NimNode): string =
|
||||||
## Return line info in the form `filepath(line, column)`.
|
## Return line info in the form `filepath(line, column)`.
|
||||||
$arg.lineInfoObj
|
$arg.lineInfoObj
|
||||||
|
|
||||||
proc internalParseExpr(s: string): NimNode {.
|
proc internalParseExpr(s, filename: string): NimNode {.
|
||||||
magic: "ParseExprToAst", noSideEffect.}
|
magic: "ParseExprToAst", noSideEffect.}
|
||||||
|
|
||||||
proc internalParseStmt(s: string): NimNode {.
|
proc internalParseStmt(s, filename: string): NimNode {.
|
||||||
magic: "ParseStmtToAst", noSideEffect.}
|
magic: "ParseStmtToAst", noSideEffect.}
|
||||||
|
|
||||||
proc internalErrorFlag*(): string {.magic: "NError", noSideEffect.}
|
proc internalErrorFlag*(): string {.magic: "NError", noSideEffect.}
|
||||||
## Some builtins set an error flag. This is then turned into a proper
|
## Some builtins set an error flag. This is then turned into a proper
|
||||||
## exception. **Note**: Ordinary application code should not call this.
|
## exception. **Note**: Ordinary application code should not call this.
|
||||||
|
|
||||||
proc parseExpr*(s: string): NimNode {.noSideEffect.} =
|
proc parseExpr*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
|
||||||
## Compiles the passed string to its AST representation.
|
## Compiles the passed string to its AST representation.
|
||||||
## Expects a single expression. Raises `ValueError` for parsing errors.
|
## Expects a single expression. Raises `ValueError` for parsing errors.
|
||||||
result = internalParseExpr(s)
|
## A filename can be given for more informative errors.
|
||||||
|
result = internalParseExpr(s, filename)
|
||||||
let x = internalErrorFlag()
|
let x = internalErrorFlag()
|
||||||
if x.len > 0: raise newException(ValueError, x)
|
if x.len > 0: raise newException(ValueError, x)
|
||||||
|
|
||||||
proc parseStmt*(s: string): NimNode {.noSideEffect.} =
|
proc parseStmt*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
|
||||||
## Compiles the passed string to its AST representation.
|
## Compiles the passed string to its AST representation.
|
||||||
## Expects one or more statements. Raises `ValueError` for parsing errors.
|
## Expects one or more statements. Raises `ValueError` for parsing errors.
|
||||||
result = internalParseStmt(s)
|
## A filename can be given for more informative errors.
|
||||||
|
result = internalParseStmt(s, filename)
|
||||||
let x = internalErrorFlag()
|
let x = internalErrorFlag()
|
||||||
if x.len > 0: raise newException(ValueError, x)
|
if x.len > 0: raise newException(ValueError, x)
|
||||||
|
|
||||||
|
|
|
||||||
4
tests/macros/mparsefile.nim
Normal file
4
tests/macros/mparsefile.nim
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
let a = 1
|
||||||
|
let b = 2
|
||||||
|
let c =
|
||||||
|
let d = 4
|
||||||
11
tests/macros/tparsefile.nim
Normal file
11
tests/macros/tparsefile.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import macros
|
||||||
|
|
||||||
|
static:
|
||||||
|
let fn = "mparsefile.nim"
|
||||||
|
var raised = false
|
||||||
|
try:
|
||||||
|
discard parseStmt(staticRead(fn), filename = fn)
|
||||||
|
except ValueError as e:
|
||||||
|
raised = true
|
||||||
|
doAssert e.msg == "mparsefile.nim(4, 1) Error: invalid indentation"
|
||||||
|
doAssert raised
|
||||||
Loading…
Add table
Add a link
Reference in a new issue