cpp-peglib icon indicating copy to clipboard operation
cpp-peglib copied to clipboard

Changing root feature

Open micrenda opened this issue 1 year ago • 1 comments

I would like to ask if it is possible to pass a specific target rule instead of using the main priority chain when parsing a string.

Let me clarify with an example:

Suppose I have the following rule set:

species    <- molecule ( '(' excitatopm ')' )?
molecule <- # Description of a molecule
excitation <- excitation_ele / excitation_vib / excitation_rot
excitation_ele <- # something
excitation_vib <- # something
excitation_rot <- # something

Usually, in my code, I would do something like this:

pegParser = peg::parser();
pegParser.load_grammar(s);
std::any result;
pegParser.parse("H2O(2V1)", result);

This works fine. However, in my unit tests or in other parts of the code, I might want to parse according to a specific rule. In that case, I would like to do something like this:

pegParser = peg::parser();
pegParser.load_grammar(s);
std::any result;
pegParser.parse("2V1", "excitation_vib", result);

This way, I would use excitation_vib as the root rule and expect an exception if excitation_vib does not fully consume the input.

Is this possible? With the current implementation, to achieve something like this, I would need to change the grammar by making the target rule the new root. However, I was wondering if there is a better way to do it.

micrenda avatar Aug 23 '24 05:08 micrenda