elm-community / parser-combinators

A parser combinator library for Elm.

Home Page:http://package.elm-lang.org/packages/elm-community/parser-combinators/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Injecting macros

andre-dietrich opened this issue · comments

Hi,
I am building a simple parser for an extended Markdown format. One thing I try to include is macro support, which are stored within the state as Dict String String, with name and code to inject. Instead of running multiple string replacements, I would like to add the code in front of the current input stream, if the macro pattern is detected, and then go on with straight forward parsing ...

Is the "primitive" function the appropriate method and if so, how can I use it?

Kind regards,

André

Hi,

found a solution. Could you please add the following function? Using this, it is possible to modify the InputStream at parsing time. I use it to define macros, that change the parser state and if a macro pattern is detected also the input stream.

{-| Modify the parser's InputStream.
-}
modifyStream : (String -> String) -> Parser s ()
modifyStream f =
Parser <|
\state stream ->
app (succeed ()) state { stream | input = f stream.input }

Could you provide a pull request with a unit test?