Skip to content

Commit 0cfef70

Browse files
metagndsrw
authored andcommitted
parseExpr/parseStmt accept filename, fixes nim-lang#13540 (nim-lang#19182)
1 parent b0712a6 commit 0cfef70

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
filename argument for more informative errors.
1616
- Module `colors` expanded with missing colors from the CSS color standard.
1717
- Fixed `lists.SinglyLinkedList` being broken after removing the last node ([#19353](https://github.com/nim-lang/Nim/pull/19353)).
18+
- `macros.parseExpr` and `macros.parseStmt` now accept an optional
19+
filename argument for more informative errors.
20+
21+
## `std/smtp`
1822

1923

2024
## Language changes

compiler/vm.nim

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
17601760
elif instr.opcode == opcNHint:
17611761
message(c.config, info, hintUser, a.strVal)
17621762
of opcParseExprToAst:
1763-
decodeB(rkNode)
1764-
# c.debug[pc].line.int - countLines(regs[rb].strVal) ?
1763+
decodeBC(rkNode)
17651764
var error: string
17661765
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
1767-
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
1766+
regs[rc].node.strVal, 0,
17681767
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
17691768
if error.len == 0 and msg <= errMax:
17701769
error = formatMsg(conf, info, msg, arg))
@@ -1776,10 +1775,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
17761775
else:
17771776
regs[ra].node = ast[0]
17781777
of opcParseStmtToAst:
1779-
decodeB(rkNode)
1778+
decodeBC(rkNode)
17801779
var error: string
17811780
let ast = parseString(regs[rb].node.strVal, c.cache, c.config,
1782-
toFullPath(c.config, c.debug[pc]), c.debug[pc].line.int,
1781+
regs[rc].node.strVal, 0,
17831782
proc (conf: ConfigRef; info: TLineInfo; msg: TMsgKind; arg: string) {.nosinks.} =
17841783
if error.len == 0 and msg <= errMax:
17851784
error = formatMsg(conf, info, msg, arg))

compiler/vmgen.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,9 +1229,9 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
12291229
unused(c, n, dest)
12301230
genBinaryStmtVar(c, n, opcAddSeqElem)
12311231
of mParseExprToAst:
1232-
genUnaryABC(c, n, dest, opcParseExprToAst)
1232+
genBinaryABC(c, n, dest, opcParseExprToAst)
12331233
of mParseStmtToAst:
1234-
genUnaryABC(c, n, dest, opcParseStmtToAst)
1234+
genBinaryABC(c, n, dest, opcParseStmtToAst)
12351235
of mTypeTrait:
12361236
let tmp = c.genx(n[1])
12371237
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
@@ -516,27 +516,29 @@ proc lineInfo*(arg: NimNode): string =
516516
## Return line info in the form `filepath(line, column)`.
517517
$arg.lineInfoObj
518518

519-
proc internalParseExpr(s: string): NimNode {.
519+
proc internalParseExpr(s, filename: string): NimNode {.
520520
magic: "ParseExprToAst", noSideEffect.}
521521

522-
proc internalParseStmt(s: string): NimNode {.
522+
proc internalParseStmt(s, filename: string): NimNode {.
523523
magic: "ParseStmtToAst", noSideEffect.}
524524

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

529-
proc parseExpr*(s: string): NimNode {.noSideEffect.} =
529+
proc parseExpr*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
530530
## Compiles the passed string to its AST representation.
531531
## Expects a single expression. Raises `ValueError` for parsing errors.
532-
result = internalParseExpr(s)
532+
## A filename can be given for more informative errors.
533+
result = internalParseExpr(s, filename)
533534
let x = internalErrorFlag()
534535
if x.len > 0: raise newException(ValueError, x)
535536

536-
proc parseStmt*(s: string): NimNode {.noSideEffect.} =
537+
proc parseStmt*(s: string; filename: string = ""): NimNode {.noSideEffect.} =
537538
## Compiles the passed string to its AST representation.
538539
## Expects one or more statements. Raises `ValueError` for parsing errors.
539-
result = internalParseStmt(s)
540+
## A filename can be given for more informative errors.
541+
result = internalParseStmt(s, filename)
540542
let x = internalErrorFlag()
541543
if x.len > 0: raise newException(ValueError, x)
542544

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)