TheLartians / PEGParser

💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
BSD 3-Clause "New" or "Revised" License
240 stars 21 forks source link

Use separator in grammar #56

Closed DaOnlyOwner closed 2 years ago

DaOnlyOwner commented 4 years ago

Hello a third question today :D

Is it possible to use the separator in the grammar too? For example like this:

[...]
g.setSeparator(g["WS"]); // <- whitespace
g["ConcatExpr"] << "AltExpr WS AltExpr";  
[...]
TheLartians commented 4 years ago

Yes, the separator can be used just as a normal rule. However, in the current implementation setting a separator will transform any following rules to R -> S* R S*, meaning that the separator will be implicitly parsed zero or more times before and after each rule. In other words, adding the separator rule as an actual rule in parsing will not parse anything.

However, you can change or deactivate the separator at any point, by calling setSeparator again before adding new rules. ~To deactivate the separator, call setSeparator with and empty pointer, e.g. g.setSeparator(std::shared_ptr<peg_parser::grammar::Rule>()).~

DaOnlyOwner commented 4 years ago

Hello and thank you again ;) if I call setSeparator with an empty pointer, I get a nullptr exception because rule->hidden is accessed.

TheLartians commented 4 years ago

Oh I should have tested or looked at the code before saying that, sorry!😅 You should be able to use g.unsetSeparatorRule() instead.