I have a working parser, but I've just realised I do not cater for comments. In the DSL I am parsing, comments start with a ;
character. If a ;
is encountered, the rest of the line is ignored (not all of it however, unless the first character is ;
).
I am extending RegexParsers
for my parser and ignoring whitespace (the default way), so I am losing the new line characters anyway. I don't wish to modify each and every parser I have to cater for the possibility of comments either, because statements can span across multiple lines (thus each part of each statement may end with a comment). Is there any clean way to acheive this?
Just filter out all the comments with a regex before you pass the code into your parser.
One thing that may influence your choice is whether comments can be found within your valid parsers. For instance let's say you have something like:
which would parse something like
( abc )
but because of comments you could actually encounter something like:Then I would recommend using a TokenParser or one of its subclass. It's more work because you have to provide a lexical parser that will do a first pass to discard the comments. But it is also more flexible if you have nested comments or if the
;
can be escaped or if the;
can be inside a string literal like:On the other hand, you could also try to override the value of whitespace to be something like
Or something along those lines. For instance using the example from the RegexParsers scaladoc:
This prints: