12

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.

ePak
  • 484
  • 5
  • 12
  • I know this does not answer your question, but Mike Lischke made an open source delphi parser and lexer, known as [DCC](http://www.soft-gems.net/index.php?option=com_content&task=view&id=25&Itemid=33). – LU RD Dec 05 '11 at 14:24

1 Answers1

5

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}
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
  • I had a quick try, it didn't seem to work, but I could be doing something else wrong. I'll spend a bit more time on it. Just to confirm, if there is another parser in the `originalTokenParser` which is defined with the original `whiteSpace`, would this parser automatically use `myWhiteSpaceParser` once I have applied `fixWhiteSpace`? – ePak Dec 06 '11 at 08:20
  • 1
    @epak: No. All the values are immutable, so defining "newParser = fixWhiteSpace blach" does not affect originalTokenParser. – Chris Kuklewicz Dec 06 '11 at 16:04
  • @ChrisKuklewicz: Thanks for clearing that up for me and in fact that is the underlying problem. The `lexeme` parser is defined in terms of `whiteSpace` and a whole lot more parsers, including `identifier`, are defined in terms of `lexeme`. Once I have replaced the `identifier` parser with my definition of `lexeme` and `whiteSpace` like @John suggested, it would handle multiple types of comment blocks. – ePak Dec 08 '11 at 22:23