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 |
Beta Was this translation helpful? Give feedback.
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' }
}); |
Beta Was this translation helpful? Give feedback.
-
OK, I found that all cases can be solved by two means
|
Beta Was this translation helpful? Give feedback.
I'd probably write that as