Collecting elements between branches #394
|
I have a requirement like this in my grammar: if it matches branch a, collect the variables of branch a, if it matches branch b, collect the variables of branch b, and finally merge them together Roughly similar to the following hypothetical grammar: struct Expr {
a: Option<char>,
b: Option<char>,
}
peg::parser!( grammar example() for str {
pub rule select() -> Expr
= a:a() / b:b() {
Expr {
a: Some(a),
b: Some(b),
}
}
rule a() -> char = "a" { 'a' }
rule b() -> char = "b" { 'b' }
});I would like to know how to desugar my requirement in In actual use, there are much more complex nested grammar, such as |
Answered by
kevinmehall
Dec 26, 2024
Replies: 2 comments 1 reply
|
I'd probably write that as struct Expr {
a: Option<char>,
b: Option<char>,
}
peg::parser!( grammar example() for str {
pub rule select() -> Expr
= a:a() { Expr { a: Some(a), b: None } }
/ b:b() { Expr { a: None, b: Some(b) } }
rule a() -> char = "a" { 'a' }
rule b() -> char = "b" { 'b' }
}); |
1 reply
Answer selected by
oovm
|
OK, I found that all cases can be solved by two means
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd probably write that as