How to maintain a state in uu-parsinglib Haskell parser combinator

196 views Asked by At

I would like to be able to use get and put functions from State Monad while writing uu-parsinglib parser combinator.

How can this be done? Can I somehow create state parser using this library?

1

There are 1 answers

1
Levi Pearson On BEST ANSWER

Sjoerd is right in his comment; the basic parse descriptor type in uu-parsinglib is P st a, where st is a type that maintains input/parse state and a is the output type. This is defined in Text.ParserCombinators.UU.Core.

Some of the basic combinators in UU.Core put some constraints on what st can be, namely that it must have instances for the Eof, StoresErrors, and HasPosition classes also defined in UU.Core. Other instances may be required for full functionality.

The Text.ParserCombinators.UU.BasicInstances package supplies appropriate instances for all of these that allow creation of parsers over streams of the ListLike class that contain Char and provide error and position state.

If you want to make parsers over something other than ListLike containers of Char with position and error state, for example to parse based on some Token type or to store arbitrary user-supplied state, you will have to create a module similar to Text.ParserCombinators.UU.BasicInstances in which you provide your own instances that meet your needs along with the needs of the parsing library.

For your request, I think you will need to augment the Str a s loc data type (which is used for the st type in P st a) with the necessary fields to implement the MonadState interface, then provide (in addition to the instances already in UU.BasicInstances) an instance for MonadState (P <your new type> a) that uses your augmented Str type to provide get and set. UU.Core already provides the Monad instance for P st a, so this should not be too difficult if you constrain the MonadState instance to your particular state type.