use std::str::FromStr; use ast::{self, WithLocation}; grammar; pub Program: ast::Program = { Expression => ast::Program(<>), }; Expression: Box> = { "+" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::Add, Box::new(v1), Box::new(v2)) })), "-" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::Sub, Box::new(v1), Box::new(v2)) })), => <>, }; ExpressionMul: Box> = { "*" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::Mul, Box::new(v1), Box::new(v2)) })), "/" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::Div, Box::new(v1), Box::new(v2)) })), "//" => Box::new(e1.join_map(*e2, |v1, v2| { ast::Expression::BinOp(ast::BinOp::FloorDiv, Box::new(v1), Box::new(v2)) })), => <>, }; ExpressionSign: Box> = { "+" => Box::new(WithLocation::new( ast::Expression::UnaryOp(ast::UnaryOp::Pos, e), l, r )), "-" => Box::new(WithLocation::new( ast::Expression::UnaryOp(ast::UnaryOp::Neg, e), l, r )), => <>, }; ExpressionBase: Box> = { Num => Box::new(<>.map(|v| ast::Expression::Number(v))), Spanned => Box::new(<>.map(|v| ast::Expression::Nil)), "(" ")" => <>, }; Num: WithLocation = > => e.map(|v| u64::from_str(v).unwrap()); Spanned: WithLocation = { => WithLocation::new(v, l, r) };