Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Parser Traversal? #158
The fastparse DSL is just fantastic to the developer!
Here I'm wondering about some mechanism to traverse the parser instance to construct a representation (say, some simplified pseudo EBNF), mainly targeted at the less-expert user, and for not-necessarily-formal documentation.
Say, operating on the
val number: P[Int] = P( CharIn('0'to'9').rep(1).!.map(_.toInt) ) val parens: P[Int] = P( "(" ~/ addSub ~ ")" ) val factor: P[Int] = P( number | parens ) val divMul: P[Int] = P( factor ~ (CharIn("*/").! ~/ factor).rep ).map(eval) val addSub: P[Int] = P( divMul ~ (CharIn("+-").! ~/ divMul).rep ).map(eval) val expr: P[Int] = P( addSub ~ End )
generate something like:
Anybody have any hints on how to accomplish this? Perhaps worth considering adding support for this in the library itself? Thx.
Basically, we need to let people recurse over the
Apart from letting us pretty-print it, we could use that info to perform transformations/optimizations of the parser tree (parser tree, not the parse tree), and probably other things.
This isn't hard in niche cases: most parsers are made of typical
I imagine a good approach may be to
This is rough and not nearly sufficiently fleshed out, but I think it could work if anyone wants to take a crack at it