* fix RFC #341: dot-like operators are now parsed with same precedence as `.` * fixup * [skip ci] address comment in changelog * address comment * update grammmar * add manual entry * fixup * -d:nimPreviewDotLikeOps * address comment to unblock PR: move nimPreviewDotLikeOps out of config/config.nims
This commit is contained in:
parent
3d1bba04ab
commit
3aa16c1de0
7 changed files with 74 additions and 5 deletions
|
|
@ -46,6 +46,7 @@ type
|
|||
warnCannotOpenFile = "CannotOpenFile", warnOctalEscape = "OctalEscape",
|
||||
warnXIsNeverRead = "XIsNeverRead", warnXmightNotBeenInit = "XmightNotBeenInit",
|
||||
warnDeprecated = "Deprecated", warnConfigDeprecated = "ConfigDeprecated",
|
||||
warnDotLikeOps = "DotLikeOps",
|
||||
warnSmallLshouldNotBeUsed = "SmallLshouldNotBeUsed", warnUnknownMagic = "UnknownMagic",
|
||||
warnRstRedefinitionOfLabel = "RedefinitionOfLabel",
|
||||
warnRstUnknownSubstitutionX = "UnknownSubstitutionX",
|
||||
|
|
@ -116,6 +117,7 @@ const
|
|||
warnXmightNotBeenInit: "'$1' might not have been initialized",
|
||||
warnDeprecated: "$1",
|
||||
warnConfigDeprecated: "config file '$1' is deprecated",
|
||||
warnDotLikeOps: "$1",
|
||||
warnSmallLshouldNotBeUsed: "'l' should not be used as an identifier; may look like '1' (one)",
|
||||
warnUnknownMagic: "unknown magic '$1' might crash the compiler",
|
||||
warnRstRedefinitionOfLabel: "redefinition of label '$1'",
|
||||
|
|
|
|||
|
|
@ -466,6 +466,17 @@ proc dotExpr(p: var Parser, a: PNode): PNode =
|
|||
exprColonEqExprListAux(p, tkParRi, y)
|
||||
result = y
|
||||
|
||||
proc dotLikeExpr(p: var Parser, a: PNode): PNode =
|
||||
#| dotLikeExpr = expr DOTLIKEOP optInd symbol
|
||||
var info = p.parLineInfo
|
||||
result = newNodeI(nkInfix, info)
|
||||
optInd(p, result)
|
||||
var opNode = newIdentNodeP(p.tok.ident, p)
|
||||
getTok(p)
|
||||
result.add(opNode)
|
||||
result.add(a)
|
||||
result.add(parseSymbol(p, smAfterDot))
|
||||
|
||||
proc qualifiedIdent(p: var Parser): PNode =
|
||||
#| qualifiedIdent = symbol ('.' optInd symbol)?
|
||||
result = parseSymbol(p)
|
||||
|
|
@ -788,10 +799,15 @@ proc commandExpr(p: var Parser; r: PNode; mode: PrimaryMode): PNode =
|
|||
p.hasProgress = false
|
||||
result.add commandParam(p, isFirstParam, mode)
|
||||
|
||||
proc isDotLike(tok: Token): bool =
|
||||
result = tok.tokType == tkOpr and tok.ident.s.len > 1 and
|
||||
tok.ident.s[0] == '.' and tok.ident.s[1] != '.'
|
||||
|
||||
proc primarySuffix(p: var Parser, r: PNode,
|
||||
baseIndent: int, mode: PrimaryMode): PNode =
|
||||
#| primarySuffix = '(' (exprColonEqExpr comma?)* ')'
|
||||
#| | '.' optInd symbol generalizedLit?
|
||||
#| | DOTLIKEOP optInd symbol generalizedLit?
|
||||
#| | '[' optInd exprColonEqExprList optPar ']'
|
||||
#| | '{' optInd exprColonEqExprList optPar '}'
|
||||
#| | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax
|
||||
|
|
@ -840,11 +856,19 @@ proc primarySuffix(p: var Parser, r: PNode,
|
|||
# `foo ref` or `foo ptr`. Unfortunately, these two are also
|
||||
# used as infix operators for the memory regions feature and
|
||||
# the current parsing rules don't play well here.
|
||||
if p.inPragma == 0 and (isUnary(p.tok) or p.tok.tokType notin {tkOpr, tkDotDot}):
|
||||
# actually parsing {.push hints:off.} as {.push(hints:off).} is a sweet
|
||||
# solution, but pragmas.nim can't handle that
|
||||
result = commandExpr(p, result, mode)
|
||||
break
|
||||
let isDotLike2 = p.tok.isDotLike
|
||||
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`")
|
||||
if p.inPragma == 0 and (isUnary(p.tok) or p.tok.tokType notin {tkOpr, tkDotDot}):
|
||||
# actually parsing {.push hints:off.} as {.push(hints:off).} is a sweet
|
||||
# solution, but pragmas.nim can't handle that
|
||||
result = commandExpr(p, result, mode)
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue