use std::str::FromStr; use crate::ast::{Expression, OpCode}; grammar; pub Expression: Box = Expr00; Expr00: Box = { => Box::new(Expression::BinaryOperation { left, right, operation }), Expr10, }; Expr10: Box = { => Box::new(Expression::BinaryOperation { left, right, operation }), Expr20, }; Expr20: Box = { => Box::new(Expression::BinaryOperation { left, right, operation }), Expr30, }; Expr30: Box = { => Box::new(Expression::BinaryOperation { left, right, operation }), Expr40, }; Expr40: Box = { => Box::new(Expression::BinaryOperation { left, right, operation }), Expr50, }; Expr50: Box = { "?" ":" => Box::new(Expression::Conditional {left, truthy, falsy}), Expr60, } Expr60: Box = { "|" => Box::new(Expression::Transform{name, subject, args}), Expr70 }; /// Expression for dereferencing. /// Used for dereferencing object literals, array literals, and the context /// There are two types of operations here: /// - Either a `dot` operation, taking an expression on the left hand side, and an identifier on the right hand side (a string without the quotations) /// - Or an `index` operation, taking an expression on the left hand side, and another expression inside square ("[]") brackets. /// /// # Examples: /// /// Assume our context is the following /// ``` ///{ /// "foo": /// { /// "bar": [{"baz": 1}, {"bobo": [13, 12]}] // } // } /// ``` /// /// `foo.bar == [{"baz": 1}, {"bobo": [13, 12]]` /// `foo.bar[0] == {"baz": 1}` /// `foo.bar[1].bobo[0] == 13` /// `[1, 2, 3][1] == 2` Expr70: Box = { => Box::new(Expression::IndexOperation{subject, index}), "." => Box::new(Expression::DotOperation{subject, ident}), Expr80 }; Expr80: Box = { Number => Box::new(Expression::Number(<>)), Boolean => Box::new(Expression::Boolean(<>)), String => Box::new(Expression::String(<>)), Array => Box::new(Expression::Array(<>)), Object => Box::new(Expression::Object(<>)), Null => Box::new(Expression::Null), Identifier => Box::new(Expression::Identifier(<>)), "(" ")", }; Args: Vec> = { "(" > ")" }; Op10: OpCode = { "&&" => OpCode::And, "||" => OpCode::Or, }; Op20: OpCode = { "==" => OpCode::Equal, "!=" => OpCode::NotEqual, ">=" => OpCode::GreaterEqual, "<=" => OpCode::LessEqual, ">" => OpCode::Greater, "<" => OpCode::Less, "in" => OpCode::In, }; Op30: OpCode = { "+" => OpCode::Add, "-" => OpCode::Subtract, }; Op40: OpCode = { "*" => OpCode::Multiply, "//" => OpCode::FloorDivide, "/" => OpCode::Divide, }; Op50: OpCode = { "%" => OpCode::Modulus, "^" => OpCode::Exponent, }; Number: f64 = { r"[0-9]+" => f64::from_str(<>).unwrap(), r"[0-9]+\.[0-9]*" => f64::from_str(<>).unwrap(), r"\.[0-9]+" => f64::from_str(<>).unwrap(), }; String: String = { => s[1..s.len() - 1].to_string().replace("\\\"", "\""), => s[1..s.len() - 1].to_string().replace("\\'", "'"), }; Null: Option> = { "null" => None, } Identifier: String = { r#"[a-zA-Z_][a-zA-Z0-9_]*"# => <>.to_string() } Index: Box = { "[" "." "]" => Box::new(Expression::Filter {ident, op, right}), "[" "]", } Boolean: bool = { "true" => true, "false" => false, } Comma: Vec = { ",")*> => match e { None => v, Some(e) => { let mut v = v; v.push(e); v } } }; Array: Vec> = { "[" > "]" } Object: Vec<(String, Box)> = { "{" ":" )>> "}", "{}" => vec![], } ObjectIdentifier: String = { String, Identifier }