Skip to content

Commit 187a328

Browse files
Allow trailing comma in list literals (#900)
Fixes #873
1 parent 8c3e696 commit 187a328

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

effekt/jvm/src/test/scala/effekt/RecursiveDescentTests.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ class RecursiveDescentTests extends munit.FunSuite {
125125
parseExpr("fun() { foo(()) }")
126126

127127
parseExpr("10.seconds")
128+
129+
parseExpr("[1,2,3]")
130+
parseExpr("[3,2,1,]")
131+
parseExpr("[]")
132+
parseExpr("[,]")
133+
intercept[Throwable] { parseExpr("[,1]") }
128134
}
129135

130136
test("Boxing") {

effekt/shared/src/main/scala/effekt/RecursiveDescent.scala

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ class RecursiveDescent(positions: Positions, tokens: Seq[Token], source: Source)
993993
}
994994
def listLiteral(): Term =
995995
nonterminal:
996-
many(expr, `[`, `,`, `]`).foldRight(NilTree) { ConsTree }
996+
manyTrailing(expr, `[`, `,`, `]`).foldRight(NilTree) { ConsTree }
997997

998998
private def NilTree: Term =
999999
Call(IdTarget(IdRef(List(), "Nil")), Nil, Nil, Nil)
@@ -1406,6 +1406,29 @@ class RecursiveDescent(positions: Positions, tokens: Seq[Token], source: Source)
14061406
components.toList
14071407
}
14081408

1409+
inline def manyTrailing[T](p: () => T, before: TokenKind, sep: TokenKind, after: TokenKind): List[T] =
1410+
consume(before)
1411+
if (peek(after)) {
1412+
consume(after)
1413+
Nil
1414+
} else if (peek(sep)) {
1415+
consume(sep)
1416+
consume(after)
1417+
Nil
1418+
} else {
1419+
val components: ListBuffer[T] = ListBuffer.empty
1420+
components += p()
1421+
while (peek(sep)) {
1422+
consume(sep)
1423+
1424+
if (!peek(after)) {
1425+
components += p()
1426+
}
1427+
}
1428+
consume(after)
1429+
components.toList
1430+
}
1431+
14091432

14101433
// Positions
14111434

0 commit comments

Comments
 (0)