Skip to content

Commit

Permalink
make grammar a bit more honest (nim-lang#21655)
Browse files Browse the repository at this point in the history
* test if expr parsing expr

refs nim-lang#19802

* in any case

* just be honest

* fix symbol/keyword issue too
  • Loading branch information
metagn authored and capocasa committed May 15, 2023
1 parent 714cd61 commit 34abbf7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
19 changes: 10 additions & 9 deletions compiler/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ proc checkBinary(p: Parser) {.inline.} =
#| operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
#| | 'or' | 'xor' | 'and'
#| | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from'
#| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
#| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | '..'
#|
#| prefixOperator = operator
#|
Expand Down Expand Up @@ -362,7 +362,8 @@ template setEndInfo() =

proc parseSymbol(p: var Parser, mode = smNormal): PNode =
#| symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
#| | IDENT | KEYW
#| | IDENT | 'addr' | 'type' | 'static'
#| symbolOrKeyword = symbol | KEYW
case p.tok.tokType
of tkSymbol:
result = newIdentNodeP(p.tok.ident, p)
Expand Down Expand Up @@ -539,7 +540,7 @@ proc dotLikeExpr(p: var Parser, a: PNode): PNode =
result.add(parseSymbol(p, smAfterDot))

proc qualifiedIdent(p: var Parser): PNode =
#| qualifiedIdent = symbol ('.' optInd symbol)?
#| qualifiedIdent = symbol ('.' optInd symbolOrKeyword)?
result = parseSymbol(p)
if p.tok.tokType == tkDot: result = dotExpr(p, result)

Expand Down Expand Up @@ -869,8 +870,8 @@ proc isDotLike(tok: Token): bool =
proc primarySuffix(p: var Parser, r: PNode,
baseIndent: int, mode: PrimaryMode): PNode =
#| primarySuffix = '(' (exprColonEqExpr comma?)* ')'
#| | '.' optInd symbol ('[:' exprList ']' ( '(' exprColonEqExpr ')' )?)? generalizedLit?
#| | DOTLIKEOP optInd symbol generalizedLit?
#| | '.' optInd symbolOrKeyword ('[:' exprList ']' ( '(' exprColonEqExpr ')' )?)? generalizedLit?
#| | DOTLIKEOP optInd symbolOrKeyword generalizedLit?
#| | '[' optInd exprColonEqExprList optPar ']'
#| | '{' optInd exprColonEqExprList optPar '}'
# XXX strong spaces need to be reflected above
Expand Down Expand Up @@ -1005,7 +1006,7 @@ proc parsePragma(p: var Parser): PNode =

proc identVis(p: var Parser; allowDot=false): PNode =
#| identVis = symbol OPR? # postfix position
#| identVisDot = symbol '.' optInd symbol OPR?
#| identVisDot = symbol '.' optInd symbolOrKeyword OPR?
var a = parseSymbol(p)
if p.tok.tokType == tkOpr:
when defined(nimpretty):
Expand Down Expand Up @@ -1717,9 +1718,9 @@ proc parseIfOrWhen(p: var Parser, kind: TNodeKind): PNode =
setEndInfo()

proc parseIfOrWhenExpr(p: var Parser, kind: TNodeKind): PNode =
#| condExpr = expr colcom expr optInd
#| ('elif' expr colcom expr optInd)*
#| 'else' colcom expr
#| condExpr = expr colcom stmt optInd
#| ('elif' expr colcom stmt optInd)*
#| 'else' colcom stmt
#| ifExpr = 'if' condExpr
#| whenExpr = 'when' condExpr
result = newNodeP(kind, p)
Expand Down
19 changes: 10 additions & 9 deletions doc/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ colcom = ':' COMMENT?
operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
| 'or' | 'xor' | 'and'
| 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from'
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | '..'
prefixOperator = operator
optInd = COMMENT? IND?
optPar = (IND{>} | IND{=})?
Expand All @@ -26,13 +26,14 @@ operatorB = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9 |
'div' | 'mod' | 'shl' | 'shr' | 'in' | 'notin' |
'is' | 'isnot' | 'not' | 'of' | 'as' | 'from' | '..' | 'and' | 'or' | 'xor'
symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
| IDENT | KEYW
| IDENT | 'addr' | 'type' | 'static'
symbolOrKeyword = symbol | KEYW
exprColonEqExpr = expr (':'|'=' expr)?
exprEqExpr = expr ('=' expr)?
exprList = expr ^+ comma
optionalExprList = expr ^* comma
exprColonEqExprList = exprColonEqExpr (comma exprColonEqExpr)* (comma)?
qualifiedIdent = symbol ('.' optInd symbol)?
qualifiedIdent = symbol ('.' optInd symbolOrKeyword)?
setOrTableConstr = '{' ((exprColonEqExpr comma)* | ':' ) '}'
castExpr = 'cast' ('[' optInd typeDesc optPar ']' '(' optInd expr optPar ')') /
parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try'
Expand All @@ -58,13 +59,13 @@ identOrLiteral = generalizedLit | symbol | literal
tupleConstr = '(' optInd (exprColonEqExpr comma?)* optPar ')'
arrayConstr = '[' optInd (exprColonEqExpr comma?)* optPar ']'
primarySuffix = '(' (exprColonEqExpr comma?)* ')'
| '.' optInd symbol ('[:' exprList ']' ( '(' exprColonEqExpr ')' )?)? generalizedLit?
| DOTLIKEOP optInd symbol generalizedLit?
| '.' optInd symbolOrKeyword ('[:' exprList ']' ( '(' exprColonEqExpr ')' )?)? generalizedLit?
| DOTLIKEOP optInd symbolOrKeyword generalizedLit?
| '[' optInd exprColonEqExprList optPar ']'
| '{' optInd exprColonEqExprList optPar '}'
pragma = '{.' optInd (exprColonEqExpr comma?)* optPar ('.}' | '}')
identVis = symbol OPR? # postfix position
identVisDot = symbol '.' optInd symbol OPR?
identVisDot = symbol '.' optInd symbolOrKeyword OPR?
identWithPragma = identVis pragma?
identWithPragmaDot = identVisDot pragma?
declColonEquals = identWithPragma (comma identWithPragma)* comma?
Expand Down Expand Up @@ -135,9 +136,9 @@ condStmt = expr colcom stmt COMMENT?
(IND{=} 'else' colcom stmt)?
ifStmt = 'if' condStmt
whenStmt = 'when' condStmt
condExpr = expr colcom expr optInd
('elif' expr colcom expr optInd)*
'else' colcom expr
condExpr = expr colcom stmt optInd
('elif' expr colcom stmt optInd)*
'else' colcom stmt
ifExpr = 'if' condExpr
whenExpr = 'when' condExpr
whileStmt = 'while' expr colcom stmt
Expand Down

0 comments on commit 34abbf7

Please sign in to comment.