elm-community / parser-combinators

A parser combinator library for Elm.
http://package.elm-lang.org/packages/elm-community/parser-combinators/latest
BSD 3-Clause "New" or "Revised" License
104 stars 13 forks source link

Injecting macros #28

Open andre-dietrich opened 6 years ago

andre-dietrich commented 6 years ago

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é

andre-dietrich commented 6 years ago

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 }

stil4m commented 6 years ago

Could you provide a pull request with a unit test?