I'm trying to translate a grammar written in Haskell using Parsec into Scala's parser combinators.
The translation of the actual matching expressions is pretty straightforward and, at least in my opinion, even a little easier in Scala. What's not at all clear to me is how to handle the statefulness that Parsec passes around using monads.
A Scala parser reads in Input and produces a ParseResult[T].
In contrast, a GenParser in Haskell reads in input and a state and produces another parser. Passing that state around in Scala has me confused.
Does anyone have an example of stateful parsing in Scala that they'd be willing to share?
The only way I know of to handle state-fullness in Scala Parsers Combinators is through the into
method, also known as >>
and flatMap
(and, yes, you can use it in for-comprehensions). However, it passes state (or, more precisely, parse result) into a parser, and not along the next parsers, which seems to be what you are describing.
Not knowing Haskell's Parsec, it is difficult for me to guess at how that can be used to translate your grammar.
See also this question. There was a very interesting paper about Scala parser combinators, but I was not able to find it. Some spelunking on Scala Lang might turn it up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With