-
Notifications
You must be signed in to change notification settings - Fork 105
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
Capture values from enum tokens #227
Comments
Ugly hack: peg::parser!{ grammar list_parser() for [MyToken] {
use MyToken::*;
rule main() -> Vec<i64>
= [OpenBracket] a:int() [Comma] b:int() [CloseBracket] { vec![a, b] }
rule int() -> i64
= n:$[Int(_)] {
match n[0] {
Int(inner) => inner,
_ => unreachable!(),
}
}
} } |
Thanks, I'm surprised there wasn't already an issue open for this. The syntax in your first comment is what I'd like too but isn't supported yet. Currently a PEG expression expands to a Rust expression, which means any variables captured are not in scope at the position of the action block. That is,
To keep the variable in scope, instead of returning An alternative syntax I've considered that would be easier to support but not as nice would be |
The alternative syntax seems great. Easier to support, clearer, more flexible. For example, looks like it would support
Could require A third alternative would be to use an match input.parse_elem(pos) {
RuleResult::Matched(npos, n@pat) => RuleResult::Matched(npos, n)
_ => RuleResult::Failed
} Would you take a PR for this? In any case, it would be great to add a note to the rustdoc until this is fixed. Wasted a couple of hours tonight before I found this issue. Thanks much for an interesting crate! |
You'd have to do For the third option, I think you're basically suggesting making |
Been thinking about this for a while. I think Option 0 (make the match available to the action) is the only way that can make sense. One could also qualify the match in the description with variables it must match, and check for them, if desired. I looked a providing a PR and decided the difficulty was above what I could handle. Thanks for looking at this! |
This changes the expansion of pattern expressions when used in a labeled sequence such that the remainder of the sequence is lexically within the match arm so that the variable is in scope. Literal expressions are also included for the sake of generating nicer code by not generating RuleResult in a `match` that is itself immediately matched upon. Fixes #227
This changes the expansion of pattern expressions when used in a labeled sequence such that the remainder of the sequence is lexically within the match arm so that the variable is in scope. Literal expressions are also included for the sake of generating nicer code by not generating RuleResult in a `match` that is itself immediately matched upon. Fixes #227
Missed this earlier — apologies for not noticing. Thanks much! |
How can I get
inner
?The text was updated successfully, but these errors were encountered: