-
Notifications
You must be signed in to change notification settings - Fork 66
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
Automatic return values #302
Comments
See https://github.com/arcanis/arpege#automatic-return-values for the original idea. |
Luckily we're programming in JS, where you can have an array that is also an object. // rule = a:number b:number
const a = [1, 2];
Object.assign(a, {a: 1, b: 2}); We can also define a getter/setter on the array, so that the first time someone accesses its elements (with a number), there is a Also TypeScript supports named fields in tuples, and using arrays instead of objects is semi-common performance optimization nowadays. Probably in future there should be a flag to not generate objects instead of arrays there. My proposal for semantics:
(Also, IMO, |
That won't work quite as-is, because that's currently legal and useful for predicates: foo = @n:$[0-9]+ ! { return n[0] === '0' } // Currently returns "19" but not "019". (this is just a quick example, not a good grammar.)
That's interesting. I'd like to use a word that isn't a valid rule name, or even better, a |
I'm not sure about the arrays with extra properties on them yet, I'll need to think about that a little. However it is a very elegant solution. |
IIRC, proposed solution with rule = @label:expression was considered when plucking implementing, but then came to the conclusion that this may not be quite obvious (pegjs/pegjs#235) |
I see Qix as the only one who said it was too magical, in this comment. I think now that we've lived with |
Oh god forbid.
It's quite standard for disjoint unions to have a For something that is not often used, I'd suggest |
Now that I thought about serializing AST to JSON, I'm not sure if array-object should be serialized as an array, or if it should have a custom |
That works for me.
Yeah, this is one of several issues we'd need to figure out. I'll note that the other way we could go is to return a plain object with properties for each of the array indexes as well as each label. However, the compatibility of that approach may not be good enough to be worth the extra complexity. |
If you want to return a single piece of your rule definition, or you want to return an array, plucking works really well. If you would like to return an object, you have to write a rule action that includes JS.
Examples:
It would be cool if rules without an action could generate an object without the JS action. Proposal:
This is a slight break in backward-compat (today, this generates
['4', '6']
), but this syntax is currently allowed and really only useful for predicates at the moment, so it shouldn't break too many things. An alternate approach:has similar compatibility issues (today, that generates
['4', '5', '6']
), but doesn't have as much flexibility.If we're worried about back-compat, we can add a flag to the compiler to turn on this behavior.
The text was updated successfully, but these errors were encountered: