Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
build/
out/
project/target/
project/project
target/
drafts/
node_modules/
Expand All @@ -12,6 +13,7 @@ licenses/
.bsp/
bin
.metals/
project/metals.sbt
.vscode/
.bloop/
*.tgz
Expand Down
17 changes: 17 additions & 0 deletions effekt/jvm/src/test/scala/effekt/ParserTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,23 @@ class ParserTests extends munit.FunSuite {
parseMatchPattern("_"),
IgnorePattern(Span.missing))
parseMatchPattern("Cons(x, Cons(x, Nil()))")
parseMatchPattern("[]")
parseMatchPattern("[1]")
parseMatchPattern("[_, 2, (3, 4)]")
parseMatchPattern("[[1, 2], [3, [4, (6, [7, 8])]]]")

{
def cons(head: MatchPattern, tail: MatchPattern) =
TagPattern(IdRef(Nil, "Cons", Span.missing), List(head, tail), Span.missing)
def nil = TagPattern(IdRef(Nil, "Nil", Span.missing), Nil, Span.missing)
def lit(l: Int) =
LiteralPattern(Literal(l, symbols.builtins.TInt, Span.missing), Span.missing)

assertEqualModuloSpans(
parseMatchPattern("[1, [2, 3, 4], [5]]"),
cons(lit(1), cons(cons(lit(2), cons(lit(3), cons(lit(4), nil))), cons(cons(lit(5), nil), nil)))
)
}

{
val (source, pos) =
Expand Down
7 changes: 7 additions & 0 deletions effekt/shared/src/main/scala/effekt/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,15 @@ class Parser(tokens: Seq[Token], source: Source) {
case Many(p :: Nil , _) => fail("Pattern matching on tuples requires more than one element")
case Many(ps, _) => TagPattern(IdRef(List("effekt"), s"Tuple${ps.size}", span().synthesized), ps, span())
}
case `[` => manyTrailing(matchPattern, `[`, `,`, `]`).foldRight(NilPattern)(ConsPattern)
case k => fail("pattern", k)
}

private def NilPattern: MatchPattern =
TagPattern(IdRef(Nil, "Nil", span().synthesized), Nil, span())

private def ConsPattern(head: MatchPattern, tail: MatchPattern): MatchPattern =
TagPattern(IdRef(Nil, "Cons", span().synthesized), List(head, tail), span())

def matchExpr(scrutinee: Term): Term =
val clauses = `match` ~> braces { manyWhile(matchClause(), `case`) }
Expand Down
3 changes: 3 additions & 0 deletions examples/neg/parsing/invalid_lit_list_pattern.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[error] examples/neg/parsing/invalid_lit_list_pattern.effekt:3:17: Expected ] but got )
case [1, [2, 3)] => ()
^
5 changes: 5 additions & 0 deletions examples/neg/parsing/invalid_lit_list_pattern.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
val _ = [] match {
case [] => ()
case [1, [2, 3)] => ()
case _ => ()
}
Loading