Search code examples
delphihaskellparsec

How to define multiple type of comment block in Parsec


I am trying to learn how to use Parsec to write a Delphi parser, but I am getting stuck at defining the LanguageDef.

In Delphi, there are two types of comment blocks, (* comments *) and { comments }. But the types of commentStart & commentEnd of LanguageDef are String, not [String], so I could only put in one or the other.

So, I tried to make my own whiteSpace parser, but I'm not sure I could actually pass it into makeTokenParser.

Any help would be appreciated.

Thanks


John and Chris have helped me to understand and get around the problem, but the solution involves replacing a huge number of parsers that makeTokenParser provides, so it's not exactly desirable.

I will post again if I could find a better solution.


Solution

  • My reading of the Text.ParserCombinators.Parsec.Language file is that this cannot be done directly using a LanguageDef.

    I believe you are on the right track to write your own whiteSpace parser. In order to use it successfully, you need to overwrite the whiteSpace parser that is generated by makeTokenParser. The TokenParser created by makeTokenParser is a record with each field containing a parser. We can create a new copy of the record with one of those fields replaced as follows:

    -- ask GCHi for the type actual type signature constraints
    -- Type sig is approx. fixWhiteSpace :: TokenParser -> Parser -> TokenParser
    fixWhiteSpace originalTokenParser myWhiteSpaceParser = 
      originalTokenParser {whiteSpace = myWhiteSpaceParser}