poetix

this time for sure

Monadic Parser Combinators in Python

OK, ok, I know this is insane…

I’ve translated some code from Hutton & Meijer’s paper on monadic parser combinators from Haskell into Python.

You can write things like:

> token = isalpha |seq| many(isdigit)

> tokens = token |sepBy| whitespace

> runParser("p123 q456 hello world", tokens)

['p123', 'q456']

Please don’t try to use this to do anything serious.

So here it is: Source code for monadic parser combinators in Python.

Probably the only useful bit of this is the multi-reader buffered stream class at the start. It allows you to have multiple forward-only readers on a single stream. The stream is buffered so that readers lagging behind the front-runner can access values that have already been pulled from the stream. Any values that are no longer needed by any reader are removed from the buffer.