Passing state parameter
By default, the parser doesn't take any argument other than the input. When building the AST, it might be useful to pass parameters to the parser, which might be needed to the construction of the tree.
Going back to the calculator4 example it is possible to pass an argument to the parser :
#![allow(unused)] fn main() { grammar(scale: i32); }
#![allow(unused)] fn main() { Num: i32 = { r"[0-9]+" => i32::from_str(<>).unwrap()*scale, }; }
Here the parser will accept a scale parameter that will scale every number encountered.
We can then call the parser with the state parameter :
#![allow(unused)] fn main() { #[test] fn calculator8() { let scale = 2; let expr = calculator8::ExprParser::new() .parse(scale,"11 * 22 + 33") .unwrap(); assert_eq!(&format!("{:?}", expr), "((22 * 44) + 66)"); } }
For a more practical example with a custom tree structure, check out this parser using this structure to build the AST.
Note: The state parameter must implement the Copy trait. For types that don't implement Copy, you should pass them as a reference instead.