I would split it a bit and drag the case analysis into | . This is one of the advantages of combinators and, in essence, LL (*) parsing:
def factor: Parser[ExprTree] = ( wholeNumber ^^ { Number(_.toInt) } | "(" ~> expr <~ ")" | ident ^^ { Variable(_) } )
I apologize if you are not familiar with the underscore syntax. Basically, it simply means "replace the nth parameter with the value of the enable function." Thus, { Variable(_) } equivalent to { x => Variable(x) } .
Another bit of syntactic magic is the operators ~> and <~ instead of ~ . These operators mean that the parsing of this term should include the syntax of both parameters, but the result should be determined only by the result of expr . Thus, "(" ~> expr <~ ")" matches exactly the same as "(" ~ expr ~ ")" , but an additional case analysis is not required to obtain the internal value of the result from expr .
Daniel Spiewak
source share