I'm currently writing a Parser
which extends JavaTokenParsers
in Scala
which should amongst other things parse following Grammar
:
list = "[" [ { expr "," } expr ] "]"
My Parser Class:
class ExpParser extends JavaTokenParsers {
def expr: Parser[Expression] = int | list | bool | id
[...]
private def list: Parser[Liste] = "[" ~> repsep(expr, ",") ~ expr <~ "]" ^^ {
case el ~ e => Liste(List(el, e))
} // error
[..]
}
object ParseProgram extends ExpParser {
def parse(s: String): ParseResult[Expression] = {
parseAll(expr, s)
}
}
My Case Class:
sealed trait Expression
[...]
case class Liste(l: List[Expression]) extends Expression
[...]
I tried to create a new List
and pass it to the Liste Constructor
but i get following error message:
Type mismatch, expected: List[Expression], actual: List[Object]
So how can i pass the list
of Expressions
which i get from repsep(expr, ",")
and the expression
which i get from ~ expr
as one list
to the Liste Constructor
?
repsep(expr, ",")
gives you a Parser[List[Expression]]
and isn't that enough?
private def list: Parser[Liste] = "[" ~> repsep(expr, ",") <~ "]" ^^ {
case el => Liste(el)
}
your original parse means to have a list like [True,FalseTrue]
, is that what you want?