yhirose / cpp-peglib

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

Various issue with recursive tokens #257

Closed fdedinec closed 1 year ago

fdedinec commented 1 year ago

Hello,

I suspect that issue 44 is a consequence of this one.

Below is my grammar (sorry for the mess, work in progress). I have two issues with the various typeName->name One is #44 : I have to remove the whitespaces in a c++ post-prrocessing pass. The second is that the following code doesn't work:

    DAGparser["entityName"] = [](const peg::SemanticValues &vs) {
        cerr << this << endl; // if this doesn't work, none of my class methods will
        return  removeSpaces(vs.token_to_string());
    };

(...) error: ‘this’ was not captured for this lambda function (which prevents these functions to do the housekeeping they were intended for)

while DAGparser["nameValuePair"] = [&](const peg::SemanticValues &vs) { cerr << this << endl; (...) } compiles OK.

I am not sure I understand why some rules are special. If it is a feature of PEG (I come from yacc then antlr) please close this bug stating so. I'll be avoiding this situation in between, it makes the grammar less verbose but also less explicit.

Thanks for cpp-peglib. Florent

-- Bug reports are love letters.

dag <- command command <- parameterDeclaration / fileName / operatorDeclaration / InputDeclaration / OutputDeclaration / Assignment / LineComment/ EndOfLine fileName <- 'Name' entityName ';' parameterDeclaration <- 'Param' paramName '=' number ';' operatorDeclaration <- 'Operator' instanceName ':' entityName nameValuePair ';' nameValuePair <- name '=' value InputDeclaration <- 'Input' signalName ':' typeName '(' value ',' value ')' ';' OutputDeclaration <- 'Output' signalName ':' typeName '(' value ',' value ')' ';'

Assignment <- signalName '=' instance ';' instance <- instanceName '(' arg (',' arg)* ')' arg <- instance / signalName

typeName <- name instanceName <- name entityName <- name paramName <- name signalName <- name value <- string / number / paramValue string <- < '"' (!'"' .) '"' > number <- < '-'? [0-9]+ > name <- < [a-zA-Z] [a-zA-Z0-9-_] > paramValue <- '$' name

%whitespace <- [ \t\n] Endl <- EndOfLine / EndOfFile EndOfLine <- '\r\n' / '\n' / '\r' EndOfFile <- !. LineComment <- ('#' / '//') (!Endl .) &Endl

yhirose commented 1 year ago

@fdedinec, thank you for the feedback. I read your comment, but I still don't understand this issue, yet... Could you please show me the smallest possible PEG grammar and the shortest source text for the grammar to reveal the problem that you experienced? That will help me to understand this issue fully and I can give you a more meaningful comment. Thanks a lot!

fdedinec commented 1 year ago

I might be wasting your time.

Upon attempting to build a minimal example, it seems the problem occurs because I sometimes have DAGparser["name"] = [](const peg::SemanticValues &vs) {...} instead of DAGparser["name"] = [&](const peg::SemanticValues &vs) {...} (missing the &) This is what happens when you cutnpaste unfinished student code. Sorry. So your minimal example would be DAGparser["name"] = [](const peg::SemanticValues &vs) { cerr << this << endl; return 0; }; but you won't care. I was tricked because the empty function DAGparser["name"] = [](const peg::SemanticValues &vs) { return 0; }; compiles OK:

Thanks again for peglib.

From: "yhirose" @.> To: "yhirose/cpp-peglib" @.> Cc: "fdedinec" @.>, "Mention" @.> Sent: Saturday, 10 December, 2022 16:05:11 Subject: Re: [yhirose/cpp-peglib] Various issue with recursive tokens (Issue #257)

[ https://github.com/fdedinec | @fdedinec ] , thank you for the feedback. I read your comment, but I still don't understand it... Could you please show me the smallest possible PEG grammar and the shortest source text for the grammar? That will help me to understand fully what you are talking about. Thanks a lot!

— Reply to this email directly, [ https://github.com/yhirose/cpp-peglib/issues/257#issuecomment-1345283246 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AD4UX3L4VLOP6ANWQS5ENGLWMSL2PANCNFSM6AAAAAAS2JLP5I | unsubscribe ] . You are receiving this because you were mentioned. Message ID: <yhirose/cpp-peglib/issues/257/1345283246 @ github . com>

yhirose commented 1 year ago

@fdedinec sorry that I still don't understand what the problem is... Anyway, it seems like it's not an issue anymore. Hope your project will go smoothly!