mirror of
https://github.com/fluencelabs/lalrpop
synced 2025-03-16 17:00:53 +00:00
Add exemple for state parameters
This commit is contained in:
parent
847adbc5df
commit
df0c7f1a8d
34
doc/calculator/src/calculator7.lalrpop
Normal file
34
doc/calculator/src/calculator7.lalrpop
Normal file
@ -0,0 +1,34 @@
|
||||
use std::str::FromStr;
|
||||
use ast::{Expr, Opcode};
|
||||
|
||||
grammar(scale: i32);
|
||||
|
||||
pub Expr: Box<Expr> = { // (1)
|
||||
Expr ExprOp Factor => Box::new(Expr::Op(<>)), // (2)
|
||||
Factor,
|
||||
};
|
||||
|
||||
ExprOp: Opcode = { // (3)
|
||||
"+" => Opcode::Add,
|
||||
"-" => Opcode::Sub,
|
||||
};
|
||||
|
||||
Factor: Box<Expr> = {
|
||||
Factor FactorOp Term => Box::new(Expr::Op(<>)),
|
||||
Term,
|
||||
};
|
||||
|
||||
FactorOp: Opcode = {
|
||||
"*" => Opcode::Mul,
|
||||
"/" => Opcode::Div,
|
||||
};
|
||||
|
||||
Term: Box<Expr> = {
|
||||
Num => Box::new(Expr::Number(<>)),
|
||||
"(" <Expr> ")"
|
||||
};
|
||||
|
||||
Num: i32 = {
|
||||
r"[0-9]+" => i32::from_str(<>).unwrap()*scale,
|
||||
};
|
||||
|
@ -125,6 +125,17 @@ fn calculator6() {
|
||||
assert_eq!(errors.len(), 4);
|
||||
}
|
||||
|
||||
lalrpop_mod!(pub calculator7);
|
||||
|
||||
#[test]
|
||||
fn calculator7() {
|
||||
let scale = 2;
|
||||
let expr = calculator7::ExprParser::new()
|
||||
.parse(scale,"11 * 22 + 33")
|
||||
.unwrap();
|
||||
assert_eq!(&format!("{:?}", expr), "((22 * 44) + 66)");
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
Loading…
x
Reference in New Issue
Block a user