Backwards-compatible support for keyword arguments in the command syntax

This commit is contained in:
Zahary Karadjov 2018-04-29 13:30:08 +03:00 • committed by Andreas Rumpf
commit b0d85b0adf
3 changed files with 19 additions and 7 deletions

View file

@ -95,11 +95,13 @@
- ``nil`` for strings/seqs is finally gone. Instead the default value for - ``nil`` for strings/seqs is finally gone. Instead the default value for
these is ``"" / @[]``. these is ``"" / @[]``.
- Accessing the binary zero terminator in Nim's native strings - Accessing the binary zero terminator in Nim's native strings
is now invalid. Internally a Nim string still has the trailing zero for is now invalid. Internally a Nim string still has the trailing zero for
zero-copy interoperability with ``cstring``. Compile your code with the zero-copy interoperability with ``cstring``. Compile your code with the
new switch ``--laxStrings:on`` if you need a transition period. new switch ``--laxStrings:on`` if you need a transition period.
- The command syntax now supports keyword arguments after the first comma.
### Tool changes ### Tool changes

View file

@ -702,10 +702,17 @@ proc namedParams(p: var TParser, callee: PNode,
# progress guaranteed # progress guaranteed
exprColonEqExprListAux(p, endTok, result) exprColonEqExprListAux(p, endTok, result)
proc commandParam(p: var TParser): PNode = proc commandParam(p: var TParser, isFirstParam: var bool): PNode =
result = parseExpr(p) result = parseExpr(p)
if p.tok.tokType == tkDo: if p.tok.tokType == tkDo:
result = postExprBlocks(p, result) result = postExprBlocks(p, result)
elif p.tok.tokType == tkEquals and not isFirstParam:
let lhs = result
result = newNodeP(nkExprEqExpr, p)
getTok(p)
addSon(result, lhs)
addSon(result, parseExpr(p))
isFirstParam = false
proc primarySuffix(p: var TParser, r: PNode, baseIndent: int): PNode = proc primarySuffix(p: var TParser, r: PNode, baseIndent: int): PNode =
#| primarySuffix = '(' (exprColonEqExpr comma?)* ')' doBlocks? #| primarySuffix = '(' (exprColonEqExpr comma?)* ')' doBlocks?
@ -748,10 +755,11 @@ proc primarySuffix(p: var TParser, r: PNode, baseIndent: int): PNode =
let a = result let a = result
result = newNodeP(nkCommand, p) result = newNodeP(nkCommand, p)
addSon(result, a) addSon(result, a)
var isFirstParam = true
when true: when true:
# progress NOT guaranteed # progress NOT guaranteed
p.hasProgress = false p.hasProgress = false
addSon result, commandParam(p) addSon result, commandParam(p, isFirstParam)
if not p.hasProgress: break if not p.hasProgress: break
else: else:
while p.tok.tokType != tkEof: while p.tok.tokType != tkEof:
@ -1303,17 +1311,18 @@ proc parseExprStmt(p: var TParser): PNode =
addSon(result, b) addSon(result, b)
else: else:
# simpleExpr parsed 'p a' from 'p a, b'? # simpleExpr parsed 'p a' from 'p a, b'?
var isFirstParam = false
if p.tok.indent < 0 and p.tok.tokType == tkComma and a.kind == nkCommand: if p.tok.indent < 0 and p.tok.tokType == tkComma and a.kind == nkCommand:
result = a result = a
while true: while true:
getTok(p) getTok(p)
optInd(p, result) optInd(p, result)
addSon(result, commandParam(p)) addSon(result, commandParam(p, isFirstParam))
if p.tok.tokType != tkComma: break if p.tok.tokType != tkComma: break
elif p.tok.indent < 0 and isExprStart(p): elif p.tok.indent < 0 and isExprStart(p):
result = newNode(nkCommand, a.info, @[a]) result = newNode(nkCommand, a.info, @[a])
while true: while true:
addSon(result, commandParam(p)) addSon(result, commandParam(p, isFirstParam))
if p.tok.tokType != tkComma: break if p.tok.tokType != tkComma: break
getTok(p) getTok(p)
optInd(p, result) optInd(p, result)

View file

@ -46,7 +46,8 @@ proc hasRegularArgs(x: int, y: string) =
echo "x: ", x, ", y: ", y echo "x: ", x, ", y: ", y
templateForwarding(hasRegularArgs, true, 1, "test 1") templateForwarding(hasRegularArgs, true, 1, "test 1")
templateForwarding(hasKeywordArgs, true, 2, "test 2") templateForwarding hasKeywordArgs, true, 2, "test 2"
templateForwarding(hasKeywordArgs, true, y = "test 3")
templateForwarding(hasKeywordArgs, true, y = "test 4", x = 4) templateForwarding(hasKeywordArgs, true, y = "test 3")
templateForwarding hasKeywordArgs, true, y = "test 4", x = 4