Skip to content

Commit 032da90

Browse files
authored
implement parser for new case objects (#24885)
refs nim-lang/RFCs#559 Parses as an `nkIdentDefs` with an `nkEmpty` name. Pragma is allowed, can remove this if necessary. Fine to close and postpone for later
1 parent 5aaba21 commit 032da90

File tree

3 files changed

+101
-4
lines changed

3 files changed

+101
-4
lines changed

compiler/parser.nim

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,12 +2110,28 @@ proc parseObjectCase(p: var Parser): PNode =
21102110
#| objectBranches = objectBranch (IND{=} objectBranch)*
21112111
#| (IND{=} 'elif' expr colcom objectPart)*
21122112
#| (IND{=} 'else' colcom objectPart)?
2113-
#| objectCase = 'case' declColonEquals ':'? COMMENT?
2113+
#| objectCase = 'case' (declColonEquals / pragma)? ':'? COMMENT?
21142114
#| (IND{>} objectBranches DED
21152115
#| | IND{=} objectBranches)
21162116
result = newNodeP(nkRecCase, p)
2117-
getTokNoInd(p)
2118-
var a = parseIdentColonEquals(p, {withPragma})
2117+
getTok(p)
2118+
if p.tok.tokType != tkOf:
2119+
# of case will be handled later
2120+
if p.tok.indent >= 0: parMessage(p, errInvalidIndentation)
2121+
var a: PNode
2122+
if p.tok.tokType in {tkSymbol, tkAccent}:
2123+
a = parseIdentColonEquals(p, {withPragma})
2124+
else:
2125+
a = newNodeP(nkIdentDefs, p)
2126+
if p.tok.tokType == tkCurlyDotLe:
2127+
var prag = newNodeP(nkPragmaExpr, p)
2128+
prag.add(p.emptyNode)
2129+
prag.add(parsePragma(p))
2130+
a.add(prag)
2131+
else:
2132+
a.add(p.emptyNode)
2133+
a.add(p.emptyNode)
2134+
a.add(p.emptyNode)
21192135
result.add(a)
21202136
if p.tok.tokType == tkColon: getTok(p)
21212137
flexComment(p, result)

doc/grammar.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ objectBranch = 'of' exprList colcom objectPart
181181
objectBranches = objectBranch (IND{=} objectBranch)*
182182
(IND{=} 'elif' expr colcom objectPart)*
183183
(IND{=} 'else' colcom objectPart)?
184-
objectCase = 'case' declColonEquals ':'? COMMENT?
184+
objectCase = 'case' (declColonEquals / pragma)? ':'? COMMENT?
185185
(IND{>} objectBranches DED
186186
| IND{=} objectBranches)
187187
objectPart = IND{>} objectPart^+IND{=} DED

tests/parser/tparsenewcaseobject.nim

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
discard """
2+
nimout: '''
3+
StmtList
4+
TypeSection
5+
TypeDef
6+
Ident "Node"
7+
Empty
8+
RefTy
9+
ObjectTy
10+
Empty
11+
Empty
12+
RecList
13+
RecCase
14+
IdentDefs
15+
Empty
16+
Empty
17+
Empty
18+
OfBranch
19+
Ident "AddOpr"
20+
Ident "SubOpr"
21+
Ident "MulOpr"
22+
Ident "DivOpr"
23+
RecList
24+
IdentDefs
25+
Ident "a"
26+
Ident "b"
27+
Ident "Node"
28+
Empty
29+
OfBranch
30+
Ident "Value"
31+
RecList
32+
NilLit
33+
IdentDefs
34+
Ident "info"
35+
Ident "LineInfo"
36+
Empty
37+
RecCase
38+
IdentDefs
39+
PragmaExpr
40+
Empty
41+
Pragma
42+
ExprColonExpr
43+
Ident "size"
44+
IntLit 1
45+
Empty
46+
Empty
47+
OfBranch
48+
Ident "Foo"
49+
NilLit
50+
51+
type
52+
Node = ref object
53+
case
54+
of AddOpr, SubOpr, MulOpr, DivOpr:
55+
a, b: Node
56+
of Value:
57+
nil
58+
info: LineInfo
59+
case {.size: 1.}
60+
of Foo:
61+
nil
62+
'''
63+
"""
64+
65+
import std/macros
66+
67+
macro foo(x: untyped) =
68+
echo x.treeRepr
69+
echo x.repr
70+
71+
foo:
72+
type
73+
Node = ref object
74+
case
75+
of AddOpr, SubOpr, MulOpr, DivOpr:
76+
a, b: Node
77+
of Value:
78+
discard
79+
info: LineInfo
80+
case {.size: 1.}
81+
of Foo: discard

0 commit comments

Comments
 (0)