parse 'of' branches for macros properly
This commit is contained in:
parent
89cbf092b2
commit
9abfc60db4
2 changed files with 25 additions and 2 deletions
|
|
@ -1139,9 +1139,11 @@ proc parseMacroColon(p: var TParser, x: PNode): PNode =
|
||||||
result = makeCall(result)
|
result = makeCall(result)
|
||||||
getTok(p)
|
getTok(p)
|
||||||
skipComment(p, result)
|
skipComment(p, result)
|
||||||
|
let stmtList = newNodeP(nkStmtList, p)
|
||||||
if p.tok.tokType notin {tkOf, tkElif, tkElse, tkExcept}:
|
if p.tok.tokType notin {tkOf, tkElif, tkElse, tkExcept}:
|
||||||
let body = parseStmt(p)
|
let body = parseStmt(p)
|
||||||
addSon(result, makeStmtList(body))
|
stmtList.add body
|
||||||
|
#addSon(result, makeStmtList(body))
|
||||||
while sameInd(p):
|
while sameInd(p):
|
||||||
var b: PNode
|
var b: PNode
|
||||||
case p.tok.tokType
|
case p.tok.tokType
|
||||||
|
|
@ -1164,8 +1166,13 @@ proc parseMacroColon(p: var TParser, x: PNode): PNode =
|
||||||
eat(p, tkColon)
|
eat(p, tkColon)
|
||||||
else: break
|
else: break
|
||||||
addSon(b, parseStmt(p))
|
addSon(b, parseStmt(p))
|
||||||
addSon(result, b)
|
addSon(stmtList, b)
|
||||||
if b.kind == nkElse: break
|
if b.kind == nkElse: break
|
||||||
|
if stmtList.len == 1 and stmtList[0].kind == nkStmtList:
|
||||||
|
# to keep backwards compatibility (see tests/vm/tstringnil)
|
||||||
|
result.add stmtList[0]
|
||||||
|
else:
|
||||||
|
result.add stmtList
|
||||||
|
|
||||||
proc parseExprStmt(p: var TParser): PNode =
|
proc parseExprStmt(p: var TParser): PNode =
|
||||||
#| exprStmt = simpleExpr
|
#| exprStmt = simpleExpr
|
||||||
|
|
|
||||||
16
tests/macros/tlexerex.nim
Normal file
16
tests/macros/tlexerex.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
|
macro match*(s: cstring|string; pos: int; sections: untyped): untyped =
|
||||||
|
for sec in sections.children:
|
||||||
|
expectKind sec, nnkOfBranch
|
||||||
|
expectLen sec, 2
|
||||||
|
result = newStmtList()
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
var input = "the input"
|
||||||
|
var pos = 0
|
||||||
|
match input, pos:
|
||||||
|
of r"[a-zA-Z_]\w+": echo "an identifier"
|
||||||
|
of r"\d+": echo "an integer"
|
||||||
|
of r".": echo "something else"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue