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
Allow returning match result of a specific expression in a rule without an action #427
Comments
Love that idea! This could work on non-nested rules too: WhiteSpacedIdentifier = WhiteSpace? identifier:Identifier WhiteSpace {return identifier;}
// becomes
WhiteSpacedIdentifier = WhiteSpace? ^Identifier WhiteSpace? |
Very readable! Presumably, use of multiple
Would return Likewise, seems to make sense that if mixed with named captures, the
Would return only c. |
Oh that multiples idea is excellent. Mixing with named captures should be On Tue, Jul 5, 2016, 01:07 Graham Wakefield notifications@github.com
|
I totally agree the described pattern is a quite common. Having a way to express it without an action makes sense. What I’m not so sure about the proposed solution (the I’ll think about this more deeply after 1.0.0. |
Some more food for thought: since
There, no new syntax (we have
|
In that case more consistent will mark with empty "labels" those expressions which will need to be returned as a result. It, by the way, not to break the existing semantics: the label exists, but it is unnamed; as labels are introduced for access to result, it is quite logical that unnamed labels automatically become result. Simultaneous existence of automatic and concrete labels shall be forbidden. If only one automatic label exist, then the single result, but not an array with one element must be returned since such behavior is more demanded. |
Why not just return any label? On the other hand, why rules like @alanmimms I like this idea. We don't have to create a name (a variable/label) just to return simple value. |
@nedzadarek because if you give a name to expression it is more likely that you wont to use it in some no-trivial expression. At least, the name is important for you, otherwise you wouldn't give it, truly? Also, mixing named and unnamed labels more likely are mistake than conscious action, so it will be safer if it will be forbidden. If you give one name why not provide another? Unfortunately, it is necessary to recognize that automatic labels in that look in what they are offered by @opatut, it is impossible to implement since it creates ambiguity in grammar. The elementary example: start = a :b;// `a` - it is rule reference or label?
a = .;
b = .; So, for this purpose need select another character. At the moment there is a choice from: Another solution -- introduce some pseudo-actions -- a shortcuts for creation of simple functions for return, for example,
|
Yes, the name is important => I want to use it => I want to return it.
Yes. |
It's very common to need to return a value from one of the non-terminals in a rule or inside of a parenthesized sub-rule. For example:
In this case, I needed the
expr
labeled ase
inside theinit
parenthesis level for an optional phrase in the language. I didn't need the "noise word"EQ
as part of the returned value.If the PEGjs language had a symbol to be used to mark terminals like the
expr
above so that they, and only they, are the value returned from a grammar rule or sub-rule this case would be simpler.To rewrite my example above:
Note the use of the
^
to mark theexpr
value inside theinit
parenthesized optional phrase sub-rule to designate what is bound toinit
. This simplifies many situations both with and without the parenthesized sub-rule shown in this example.Thanks for making such a wonderfully simple, elegant, and powerful tool. I love PEGjs! 😄
The text was updated successfully, but these errors were encountered: