I am trying to learn the scala fast parse library. Towards this I have written the following code
import fastparse.noApi._
import fastparse.WhitespaceApi
object FastParsePOC {
val White = WhitespaceApi.Wrapper{
import fastparse.all._
NoTrace(" ".rep)
}
def print(input : Parsed[String]): Unit = {
input match {
case Parsed.Success(value, index) => println(s"Success: $value $index")
case f @ Parsed.Failure(error, line, col) => println(s"Error: $error $line $col ${f.extra.traced.trace}")
}
}
def main(args: Array[String]) : Unit = {
import White._
val parser = P("Foo" ~ "(" ~ AnyChar.rep(1).! ~ ")")
val input1 = "Foo(Bar(10), Baz(20))"
print(parser.parse(input1))
}
}
But I get error
Error: ")" 21 Extra(Foo(Bar(10), Baz(20)), [traced - not evaluated]) parser:1:1 / (AnyChar | ")"):1:21 ...""
My expected output was "Bar(10), Baz(20)". it seems the parser above does not like the ending ")".
AnyChar.rep(1)
also includes)
symbol at the end of the input string, as a result the end)
at~ ")")
isn't reached. If)
symbol weren't used in Bar and Baz, then this could be solved by excluding)
fromAnyChar
like this:To make Bar and Baz work with
)
symbol you could define separate parsers for each of them (also excluding)
symbol fromAnyChar
. The following solution is a bit more flexible as it allows more occurrences of Bar and Baz but I hope that you get the idea.Result:
Success: Bar(10), Baz(20) 21