Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dot-like method calls; alternative to #19384 #19919; fixes #19392 #19921

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, tokKeywordLow..tokKeywordHigh}: # if the tok is a symbol
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
50 changes: 50 additions & 0 deletions tests/misc/trfc_341_dotlike_call.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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

block:
var collector: seq[int] = @[]
for i in 0 .++ 8:
collector.add i
ringabout marked this conversation as resolved.
Show resolved Hide resolved

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

block:
var collector: seq[int] = @[]
let x = 0
let y = 8
for i in x .++ y:
collector.add i
doAssert collector == @[0, 2, 4, 6, 8]