-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changing root feature #304
Comments
Added PR #305 which implement this feature: it may need some rework. |
@micrenda thanks for the feedback, but I don't understand the example grammar... The grammar isn't valid. ('excitatopm' is not defined, and 'excitation' is not referenced.) So |
Hello In the example I wrote I just omitted the actual implementation, because it was not important (and I also made a typos!). Let me give you a valid grammar:
In my code, now I can do something like this:
And it will work perfectly. However, using the PR #305, it is now possible to also do this in unit testing or in other section of code:
For me this is a life saver :-) |
Thanks for the clear explanation. I now fully understand what you would like to do. (By the way, I put comments in your pull request to fix problems that I found, and the following sample uses the revised version.) Unfortunately, there are some situations where the parser doesn't work properly with this. // sample.cc
#include <iostream>
#include <peglib.h>
using namespace peg;
int main(void) {
parser parser(R"(
Start <- A
A <- B (',' B)*
B <- '[one]' / '[two]'
%whitespace <- [ \t\n]*
)");
std::cout << std::boolalpha;
std::cout << parser.parse("[one],[two]") << std::endl;
std::cout << parser.parse(" [one] , [two] ") << std::endl;
std::cout << parser.parse("[one],[two]", nullptr, "A") << std::endl;
std::cout << parser.parse(" [one] , [two] ", nullptr, "A") << std::endl;
} > ./sample
true
true
true
false As you can see, Line 3992 in 5ef7180
|
@micrenda I made a change to allow users to specify the start definition rule name in the parser constructor and auto grammar = R"(
Start <- A
A <- B (',' B)*
B <- '[one]' / '[two]'
%whitespace <- [ \t\n]*
)";
peg::parser parser(grammar, "A"); // Start Rule is "A"
or
peg::parser parser;
parser.load_grammar(grammar, "A"); // Start Rule is "A"
parser.parse(" [one] , [two] "); // OK Could you take a look at it when you have time? Thanks! |
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:
Usually, in my code, I would do something like this:
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:
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.
The text was updated successfully, but these errors were encountered: