Search code examples
parsinghaskellpascalparsec

Re-define "stringLiteral" token in Parsec.Token


I am developing Pascal language parser in Haskell using Parsec library and I need to re-define some tokens defined in Parsec.Token class.

Speeking of it, here is my case:

I need to change how stringLiteral token is matched. In default definition, it is something between char '"' (see this), but I need it to be between '\'' (apostrophes). How can I do this modification to Parsec behavior?

Thanks!!!


Solution

  • You are talking about adjusting the field of a data type named GenTokenParser. It looks like you are using a function that automatically fills in the data type with sensible defaults and you just want to adjust one thing, here you go:

     myMakeTokenParser langDef =
        let default = makeTokenParser langDef
        in default { stringLiteral = newStringLit }
      where
      newStringLit = lexeme (
                          do{ str <- between (char '\'')
                                             (char '\'' <?> "end of string")
                                             (many stringChar)
                            ; return (foldr (maybe id (:)) "" str)
                            }
                          <?> "literal string")