I am trying to write a parser in scala using Parser Combiners. If I match recursively,
def body: Parser[Body] = ("begin" ~> statementList ) ^^ { case s => { new Body(s); } } def statementList : Parser[List[Statement]] = ("end" ^^ { _ => List() } )| (statement ~ statementList ^^ { case statement ~ statementList => statement :: statementList })
then I get good errormessages whenever there is an error in the statement. However, this is an ugly long code. Therefore, I would like to write the following:
def body: Parser[Body] = ("begin" ~> statementList <~ "end" ) ^^ { case s => { new Body(s); } } def statementList : Parser[List[Statement]] = rep(statement)
This code works, but only prints meaningful messages if there is an error in the FIRST instruction. If this is in a later statement, the message becomes painfully unusable because the parser wants to see that the entire erroneous instruction is replaced with the "end" marker:
Exception in thread "main" java.lang.RuntimeException: [4.2] error: "end" expected but "let" found let b : string = x(3,b,"WHAT???",!ERRORHERE!,7 ) ^
My question is: is there a way to get rep and repsep to work in conjunction with meaningful error messages that put the carriage in the right place instead of starting a repeating fragment?
source share