Skip to content

Commit

Permalink
allow full commands and blocks in type sections
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 23, 2021
1 parent 1b143f5 commit 28aaa25
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
x, y, z: int
Baz = object
```
- Full command syntax and block arguments i.e. `foo a, b: c` are now allowed
for the right-hand side of type definitions in type sections. Previously
they would error with "invalid indentation".

## Compiler changes

Expand Down
23 changes: 16 additions & 7 deletions compiler/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -821,14 +821,14 @@ proc primarySuffix(p: var Parser, r: PNode,
of tkParLe:
# progress guaranteed
if p.tok.strongSpaceA > 0:
# inside type sections, expressions such as `ref (int, bar)`
# are parsed as a nkCommand with a single tuple argument (nkPar)
result = commandExpr(p, result, mode)
# type sections allow full command syntax
if mode == pmTypeDef:
result = newNodeP(nkCommand, p)
result.add r
result.add primary(p, pmNormal)
else:
result = commandExpr(p, result, mode)
var isFirstParam = false
while p.tok.tokType == tkComma:
getTok(p)
optInd(p, result)
result.add(commandParam(p, isFirstParam, mode))
break
result = namedParams(p, result, nkCall, tkParRi)
if result.len > 1 and result[1].kind == nkExprColonExpr:
Expand Down Expand Up @@ -869,9 +869,18 @@ proc primarySuffix(p: var Parser, r: PNode,
# 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)
if mode == pmTypeDef:
var isFirstParam = false
while p.tok.tokType == tkComma:
getTok(p)
optInd(p, result)
result.add(commandParam(p, isFirstParam, mode))
break
else:
break
# type sections allow post-expr blocks
if mode == pmTypeDef:
result = postExprBlocks(p, result)

proc parseOperators(p: var Parser, headNode: PNode,
limit: int, mode: PrimaryMode): PNode =
Expand Down
210 changes: 210 additions & 0 deletions tests/parser/ttypesectioncalls.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
discard """
nimout: '''
StmtList
TypeSection
TypeDef
Ident "A"
Empty
Call
Ident "call"
IntLit 1
TypeSection
TypeDef
Ident "B"
Empty
Command
Ident "call"
IntLit 2
TypeDef
Ident "C"
Empty
Call
Ident "call"
StmtList
IntLit 3
TypeDef
Ident "D"
Empty
Call
Ident "call"
StmtList
IntLit 4
TypeSection
TypeDef
Ident "E"
Empty
Call
Ident "call"
IntLit 5
IntLit 6
TypeDef
Ident "F"
Empty
Command
Ident "call"
IntLit 7
IntLit 8
TypeDef
Ident "G"
Empty
Call
Ident "call"
IntLit 9
StmtList
IntLit 10
TypeDef
Ident "H"
Empty
Call
Ident "call"
IntLit 11
StmtList
IntLit 12
TypeDef
Ident "I"
Empty
Command
Ident "call"
IntLit 13
StmtList
IntLit 14
TypeDef
Ident "J"
Empty
Command
Ident "call"
IntLit 15
StmtList
IntLit 16
TypeSection
TypeDef
Ident "K"
Empty
Call
Ident "call"
IntLit 17
IntLit 18
IntLit 19
TypeDef
Ident "L"
Empty
Command
Ident "call"
IntLit 20
IntLit 21
IntLit 22
TypeDef
Ident "M"
Empty
Call
Ident "call"
IntLit 23
IntLit 24
StmtList
IntLit 25
TypeDef
Ident "N"
Empty
Command
Ident "call"
IntLit 26
IntLit 27
StmtList
IntLit 28
TypeDef
Ident "O"
Empty
Command
Ident "call"
IntLit 29
IntLit 30
StmtList
IntLit 31
a: IntLit 1
a: IntLit 2
a: StmtList
IntLit 3
a: StmtList
IntLit 4
a: IntLit 5
b: IntLit 6
a: IntLit 7
b: IntLit 8
a: IntLit 9
b: StmtList
IntLit 10
a: IntLit 11
b: StmtList
IntLit 12
a: IntLit 13
b: StmtList
IntLit 14
a: IntLit 15
b: StmtList
IntLit 16
a: IntLit 17
b: IntLit 18
c: IntLit 19
a: IntLit 20
b: IntLit 21
c: IntLit 22
a: IntLit 23
b: IntLit 24
c: StmtList
IntLit 25
a: IntLit 26
b: IntLit 27
c: StmtList
IntLit 28
a: IntLit 29
b: IntLit 30
c: StmtList
IntLit 31
'''
"""
import macros

macro call(a): untyped =
echo "a: ", a.treeRepr
result = ident"int"
macro call(a, b): untyped =
echo "a: ", a.treeRepr
echo "b: ", b.treeRepr
result = ident"int"
macro call(a, b, c): untyped =
echo "a: ", a.treeRepr
echo "b: ", b.treeRepr
echo "c: ", c.treeRepr
result = ident"int"

macro sections(x): untyped =
echo x.treeRepr
result = newStmtList(x)
for ts in x:
for td in ts:
let t = td[0]
result.add quote do:
doAssert `t` is int

sections:
type A = call(1)
type
B = call 2
C = call: 3
D = call(): 4
type
E = call(5, 6)
F = call 7, 8
G = call(9): 10
H = call(11):
12
I = call 13: 14
J = call 15:
16
type
K = call(17, 18, 19)
L = call 20, 21, 22
M = call(23, 24): 25
N = call 26, 27: 28
O = call 29, 30:
31

0 comments on commit 28aaa25

Please sign in to comment.