yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
879 stars 112 forks source link

Issue in parser reuse #260

Closed 95A31 closed 1 year ago

95A31 commented 1 year ago

Hello,

I am developing a frontend of a small language. For debugging purpose, I would like to print the AST and then get the result of the parsing obtained with my actions. To my understanding, it is not possible doing that with the same parser instance because the method enable_ast() adds its own actions. I am wondering if it is safe to introduce a method like the following:

void reset_actions()
{
  for (auto & [_, rule]  :  *grammar_)
  {
    ~rule.action;
    rule.action = Action();
  }
}

Cheers

yhirose commented 1 year ago

@95A31, thanks for your feedback.

To my understanding, it is not possible doing that with the same parser instance because the method enable_ast() adds its own actions.

You are correct. I recommend to have two parser instances with the same grammar and use one of them as a debug parser with AST mode. I think it's better since the parser with AST mode should be slower since it has lots of overhead which is not necessary for the production parser.

Your reset method may work though, ~rule looks suspicious to me. It doesn't reset the object state, since it sets 'true' instead of 'false' to ignoreSemanticValue member variable in a Definition object.

Hope it helps!