dlang-community / Pegged

A Parsing Expression Grammar (PEG) module, using the D programming language.
534 stars 66 forks source link

Implement longest match parser. Closes PhilippeSigaud/Pegged#218. #222

Closed veelo closed 7 years ago

veelo commented 7 years ago

The longest_match!() parser is like the or!() parser, except that all rules are tried and the one producing the longest match is taken. I have extended the PEG grammar to let | denote the longest match choice operator, besides the standard / prioritised choice. Unfortunately, the Expression* rules in peggedgrammar.d have become a tad convoluted; at first I used negative lookahead, but this I think is better. Alternatively, I could have used Expression <- Expression | LongestExpression.

I hope you think this is worth it. With this change I am able to parse a complete EP source file that is currently in production, with non-standard language extensions. The epgrammar.d changes for this I will commit later. You may bump the version before or after that, but maybe let me document this extension first?

PhilippeSigaud commented 7 years ago

Yes, I find it worthwhile, if only because with it you're able to compile your EP grammar :-) (but not only for that, I'm all for offering choices to the grammar creator).

I'll merge this PR and wait for your documentation update, and then but the version with a small explanation text.

veelo commented 7 years ago

Thanks! I'll let you know.