diff options
author | Araq <rumpf_a@web.de> | 2015-04-19 14:25:16 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2015-04-19 14:25:16 +0200 |
commit | 9abfc60db441e6d21edbc99f0ef38075fba85f36 (patch) | |
tree | a61779753e60e219516ca74578f603e1b538d9f1 | |
parent | 89cbf092b23b901cd6a485ab670b1e374c31559d (diff) | |
download | Nim-9abfc60db441e6d21edbc99f0ef38075fba85f36.tar.gz |
parse 'of' branches for macros properly
-rw-r--r-- | compiler/parser.nim | 11 | ||||
-rw-r--r-- | tests/macros/tlexerex.nim | 16 |
2 files changed, 25 insertions, 2 deletions
diff --git a/compiler/parser.nim b/compiler/parser.nim index d2831ea46..d600f0f85 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1139,9 +1139,11 @@ proc parseMacroColon(p: var TParser, x: PNode): PNode = result = makeCall(result) getTok(p) skipComment(p, result) + let stmtList = newNodeP(nkStmtList, p) if p.tok.tokType notin {tkOf, tkElif, tkElse, tkExcept}: let body = parseStmt(p) - addSon(result, makeStmtList(body)) + stmtList.add body + #addSon(result, makeStmtList(body)) while sameInd(p): var b: PNode case p.tok.tokType @@ -1164,8 +1166,13 @@ proc parseMacroColon(p: var TParser, x: PNode): PNode = eat(p, tkColon) else: break addSon(b, parseStmt(p)) - addSon(result, b) + addSon(stmtList, b) 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 = #| exprStmt = simpleExpr diff --git a/tests/macros/tlexerex.nim b/tests/macros/tlexerex.nim new file mode 100644 index 000000000..d348a4bcc --- /dev/null +++ b/tests/macros/tlexerex.nim @@ -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" |