Skip to content

Commit

Permalink
fixes #19392 #19806; support dot-like method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Jun 24, 2022
1 parent d33e112 commit db92926
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
19 changes: 16 additions & 3 deletions compiler/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ proc dotExpr(p: var Parser, a: PNode): PNode =
exprColonEqExprListAux(p, tkParRi, y)
result = y

proc simpleExpr(p: var Parser, mode = pmNormal): PNode
proc parseGStrLit(p: var Parser, a: PNode): PNode

proc dotLikeExpr(p: var Parser, a: PNode): PNode =
var info = p.parLineInfo
result = newNodeI(nkInfix, info)
Expand All @@ -479,7 +482,19 @@ proc dotLikeExpr(p: var Parser, a: PNode): PNode =
getTok(p)
result.add(opNode)
result.add(a)
result.add(parseSymbol(p, smAfterDot))
if p.tok.tokType in {tkSymbol, tkAccent}:
let symbol = parseSymbol(p, smAfterDot)

if p.tok.tokType == tkParLe and p.tok.strongSpaceA <= 0:
let callNode = newNodeI(nkCall, p.parLineInfo)
callNode.add symbol
exprColonEqExprListAux(p, tkParRi, callNode)
result.add callNode
else:
result.add symbol
result = parseGStrLit(p, result)
else:
result.add simpleExpr(p)

proc qualifiedIdent(p: var Parser): PNode =
#| qualifiedIdent = symbol ('.' optInd symbol)?
Expand Down Expand Up @@ -551,7 +566,6 @@ proc parseGStrLit(p: var Parser, a: PNode): PNode =
result = a

proc complexOrSimpleStmt(p: var Parser): PNode
proc simpleExpr(p: var Parser, mode = pmNormal): PNode
proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode

proc semiStmtList(p: var Parser, result: PNode) =
Expand Down Expand Up @@ -864,7 +878,6 @@ proc primarySuffix(p: var Parser, r: PNode,
if isDotLike2 and p.lex.config.isDefined("nimPreviewDotLikeOps"):
# synchronize with `tkDot` branch
result = dotLikeExpr(p, result)
result = parseGStrLit(p, result)
else:
if isDotLike2:
parMessage(p, warnDotLikeOps, "dot-like operators will be parsed differently with `-d:nimPreviewDotLikeOps`")
Expand Down
41 changes: 41 additions & 0 deletions tests/misc/trfc_341_dotlike_call.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
discard """
nimout: '''
ArgList
Ident "foo"
Call
Ident "bar"
IntLit 1
ArgList
Ident "foo"
Call
Ident "bar"
IntLit 1
'''
"""

import std/macros
macro `?`(a: varargs[untyped]): untyped =
echo a.treerepr

foo?bar(1)

macro `.?`(a: varargs[untyped]): untyped =
echo a.treerepr

foo.?bar(1)

static: doAssert defined(nimPreviewDotLikeOps)

iterator `.++`[T](a: T, b: T): T =
## Increment each output by 2
var res: T = a
while res <= b:
yield res
inc res
inc res

var collector: seq[int] = @[]
for i in 0 .++ 8:
collector.add i

doAssert collector == @[0, 2, 4, 6, 8]

0 comments on commit db92926

Please sign in to comment.