Skip to content

Commit c1856a7

Browse files
metagnJaremy Creechley
authored andcommitted
parseExpr/parseStmt accept filename, fixes nim-lang#13540 (nim-lang#19182)
1 parent 30286fb commit c1856a7

File tree

6 files changed

+32
-13
lines changed

6 files changed

+32
-13
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
## Standard library additions and changes
1515

16+
- `macros.parseExpr` and `macros.parseStmt` now accept an optional
17+
filename argument for more informative errors.
18+
1619
## `std/smtp`
1720

1821
- Sends `ehlo` first. If the mail server does not understand, it sends `helo` as a fallback.

compiler/vm.nim

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,11 +1748,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
17481748
elif instr.opcode == opcNHint:
17491749
message(c.config, info, hintUser, a.strVal)
17501750
of opcParseExprToAst:
1751-
decodeB(rkNode)
1752-
# c.debug[pc].line.int - countLines(regs[rb].strVal) ?
1751+
decodeBC(rkNode)
17531752
var error: string
17541753
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
1755-
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
1754+
regs[rc].node.strVal, 0,
17561755
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
17571756
if error.len == 0 and msg <= errMax:
17581757
error = formatMsg(conf, info, msg, arg))
@@ -1764,10 +1763,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
17641763
else:
17651764
regs[ra].node = ast[0]
17661765
of opcParseStmtToAst:
1767-
decodeB(rkNode)
1766+
decodeBC(rkNode)
17681767
var error: string
17691768
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
1770-
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
1769+
regs[rc].node.strVal, 0,
17711770
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
17721771
if error.len == 0 and msg <= errMax:
17731772
error = formatMsg(conf, info, msg, arg))

compiler/vmgen.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
12251225
unused(c, n, dest)
12261226
genBinaryStmtVar(c, n, opcAddSeqElem)
12271227
of mParseExprToAst:
1228-
genUnaryABC(c, n, dest, opcParseExprToAst)
1228+
genBinaryABC(c, n, dest, opcParseExprToAst)
12291229
of mParseStmtToAst:
1230-
genUnaryABC(c, n, dest, opcParseStmtToAst)
1230+
genBinaryABC(c, n, dest, opcParseStmtToAst)
12311231
of mTypeTrait:
12321232
let tmp = c.genx(n[1])
12331233
if dest < 0: dest = c.getTemp(n.typ)

lib/core/macros.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,27 +511,29 @@ proc lineInfo*(arg: NimNode): string =
511511
## Return line info in the form `filepath(line, column)`.
512512
$arg.lineInfoObj
513513

514-
proc internalParseExpr(s: string): NimNode {.
514+
proc internalParseExpr(s, filename: string): NimNode {.
515515
magic: "ParseExprToAst", noSideEffect.}
516516

517-
proc internalParseStmt(s: string): NimNode {.
517+
proc internalParseStmt(s, filename: string): NimNode {.
518518
magic: "ParseStmtToAst", noSideEffect.}
519519

520520
proc internalErrorFlag*(): string {.magic: "NError", noSideEffect.}
521521
## Some builtins set an error flag. This is then turned into a proper
522522
## exception. **Note**: Ordinary application code should not call this.
523523

524-
proc parseExpr*(s: string): NimNode {.noSideEffect.} =
524+
proc parseExpr*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
525525
## Compiles the passed string to its AST representation.
526526
## Expects a single expression. Raises `ValueError` for parsing errors.
527-
result = internalParseExpr(s)
527+
## A filename can be given for more informative errors.
528+
result = internalParseExpr(s, filename)
528529
let x = internalErrorFlag()
529530
if x.len > 0: raise newException(ValueError, x)
530531

531-
proc parseStmt*(s: string): NimNode {.noSideEffect.} =
532+
proc parseStmt*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
532533
## Compiles the passed string to its AST representation.
533534
## Expects one or more statements. Raises `ValueError` for parsing errors.
534-
result = internalParseStmt(s)
535+
## A filename can be given for more informative errors.
536+
result = internalParseStmt(s, filename)
535537
let x = internalErrorFlag()
536538
if x.len > 0: raise newException(ValueError, x)
537539

tests/macros/mparsefile.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
let a = 1
2+
let b = 2
3+
let c =
4+
let d = 4

tests/macros/tparsefile.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import macros
2+
3+
static:
4+
let fn = "mparsefile.nim"
5+
var raised = false
6+
try:
7+
discard parseStmt(staticRead(fn), filename = fn)
8+
except ValueError as e:
9+
raised = true
10+
doAssert e.msg == "mparsefile.nim(4, 1) Error: invalid indentation"
11+
doAssert raised

0 commit comments

Comments
 (0)