fixes long standing parsing issue with command syntax

This commit is contained in:
Araq 2015-03-22 01:38:01 +01:00
commit ca6906d85b
3 changed files with 39 additions and 18 deletions

View file

@ -666,11 +666,11 @@ proc namedParams(p: var TParser, callee: PNode,
proc parseMacroColon(p: var TParser, x: PNode): PNode proc parseMacroColon(p: var TParser, x: PNode): PNode
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?
#| | doBlocks #| | doBlocks
#| | '.' optInd symbol generalizedLit? #| | '.' optInd symbol generalizedLit?
#| | '[' optInd indexExprList optPar ']' #| | '[' optInd indexExprList optPar ']'
#| | '{' optInd indexExprList optPar '}' #| | '{' optInd indexExprList optPar '}'
#| | &( '`'|IDENT|literal|'cast') expr # command syntax #| | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
result = r result = r
while p.tok.indent < 0 or while p.tok.indent < 0 or
(p.tok.tokType == tkDot and p.tok.indent >= baseIndent): (p.tok.tokType == tkDot and p.tok.indent >= baseIndent):
@ -696,21 +696,22 @@ proc primarySuffix(p: var TParser, r: PNode, baseIndent: int): PNode =
of tkCurlyLe: of tkCurlyLe:
if p.strongSpaces and p.tok.strongSpaceA > 0: break if p.strongSpaces and p.tok.strongSpaceA > 0: break
result = namedParams(p, result, nkCurlyExpr, tkCurlyRi) result = namedParams(p, result, nkCurlyExpr, tkCurlyRi)
of tkSymbol, tkAccent, tkIntLit..tkCharLit, tkNil, tkCast: of tkSymbol, tkAccent, tkIntLit..tkCharLit, tkNil, tkCast, tkAddr, tkType:
if p.inPragma == 0: if p.inPragma == 0:
# actually parsing {.push hints:off.} as {.push(hints:off).} is a sweet # actually parsing {.push hints:off.} as {.push(hints:off).} is a sweet
# solution, but pragmas.nim can't handle that # solution, but pragmas.nim can't handle that
let a = result let a = result
result = newNodeP(nkCommand, p) result = newNodeP(nkCommand, p)
addSon(result, a) addSon(result, a)
addSon result, parseExpr(p) when true:
when false: addSon result, parseExpr(p)
else:
while p.tok.tokType != tkEof: while p.tok.tokType != tkEof:
let a = parseExpr(p) let x = parseExpr(p)
addSon(result, a) addSon(result, x)
if p.tok.tokType != tkComma: break if p.tok.tokType != tkComma: break
getTok(p) getTok(p)
optInd(p, a) optInd(p, x)
if p.tok.tokType == tkDo: if p.tok.tokType == tkDo:
parseDoBlocks(p, result) parseDoBlocks(p, result)
else: else:
@ -1162,9 +1163,7 @@ proc parseExprStmt(p: var TParser): PNode =
#| doBlocks #| doBlocks
#| / macroColon #| / macroColon
#| ))? #| ))?
inc p.inPragma
var a = simpleExpr(p) var a = simpleExpr(p)
dec p.inPragma
if p.tok.tokType == tkEquals: if p.tok.tokType == tkEquals:
getTok(p) getTok(p)
optInd(p, result) optInd(p, result)
@ -1173,8 +1172,20 @@ proc parseExprStmt(p: var TParser): PNode =
addSon(result, a) addSon(result, a)
addSon(result, b) addSon(result, b)
else: else:
if p.tok.indent < 0 and isExprStart(p): # simpleExpr parsed 'p a' from 'p a, b'?
result = newNode(nkCommand, a.info, @[a]) if p.tok.indent < 0 and p.tok.tokType == tkComma and a.kind == nkCommand:
result = a
while true:
getTok(p)
optInd(p, result)
var e = parseExpr(p)
addSon(result, e)
if p.tok.tokType != tkComma: break
elif p.tok.indent < 0 and isExprStart(p):
if a.kind == nkCommand:
result = a
else:
result = newNode(nkCommand, a.info, @[a])
while true: while true:
var e = parseExpr(p) var e = parseExpr(p)
addSon(result, e) addSon(result, e)

View file

@ -1,7 +1,8 @@
discard """ discard """
output: '''140 output: '''140
5-120-120 5-120-120
359''' 359
77'''
""" """
#import math #import math
@ -16,8 +17,10 @@ proc foo(x, y: int): int = x-y
let x = optarg foo 7.foo let x = optarg foo 7.foo
let y = singlearg foo(1, foo 8) let y = singlearg foo(1, foo 8)
let z = singlearg 1.foo foo 8 let z = singlearg 1.foo foo 8
echo x, y, z echo x, y, z
let a = [2,4,8].map do (d:int) -> int: d + 1 let a = [2,4,8].map do (d:int) -> int: d + 1
echo a[0], a[1], a[2] echo a[0], a[1], a[2]
echo(foo 8, foo 8)

View file

@ -17,3 +17,10 @@ echo(type(10).name()) # doesn't work
echo(name(type(10))) # works echo(name(type(10))) # works
echo((type(10)).name()) # works echo((type(10)).name()) # works
# test that 'addr' still works
proc poo(x, y: ptr int) = discard
var someInt: int
poo(addr someInt, addr someInt)