Debian Codegraph @

third_party/rust/jexl-parser/src/parser.lalrpop

raw
1 use std::str::FromStr;
2
3 use crate::ast::{Expression, OpCode};
4
5 grammar;
6
7 pub Expression: Box<Expression> = Expr00;
8
9 Expr00: Box<Expression> = {
10 <left: Expression> <operation: Op10> <right: Expr10> => Box::new(Expression::BinaryOperation { left, right, operation }),
11 Expr10,
12 };
13
14 Expr10: Box<Expression> = {
15 <left: Expr10> <operation: Op20> <right: Expr20> => Box::new(Expression::BinaryOperation { left, right, operation }),
16 Expr20,
17 };
18
19 Expr20: Box<Expression> = {
20 <left: Expr20> <operation: Op30> <right: Expr30> => Box::new(Expression::BinaryOperation { left, right, operation }),
21 Expr30,
22 };
23
24 Expr30: Box<Expression> = {
25 <left: Expr30> <operation: Op40> <right: Expr40> => Box::new(Expression::BinaryOperation { left, right, operation }),
26 Expr40,
27 };
28
29 Expr40: Box<Expression> = {
30 <left: Expr40> <operation: Op50> <right: Expr50> => Box::new(Expression::BinaryOperation { left, right, operation }),
31 Expr50,
32 };
33
34 Expr50: Box<Expression> = {
35 <left: Expr50> "?" <truthy: Expr60> ":" <falsy: Expr60> => Box::new(Expression::Conditional {left, truthy, falsy}),
36 Expr60,
37 }
38
39 Expr60: Box<Expression> = {
40 <subject: Expr60> "|" <name: Identifier> <args: Args?> => Box::new(Expression::Transform{name, subject, args}),
41 Expr70
42 };
43
44
45 /// Expression for dereferencing.
46 /// Used for dereferencing object literals, array literals, and the context
47 /// There are two types of operations here:
48 /// - 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)
49 /// - Or an `index` operation, taking an expression on the left hand side, and another expression inside square ("[]") brackets.
50 ///
51 /// # Examples:
52 ///
53 /// Assume our context is the following
54 /// ```
55 ///{
56 /// "foo":
57 /// {
58 /// "bar": [{"baz": 1}, {"bobo": [13, 12]}]
59 // }
60 // }
61 /// ```
62 ///
63 /// `foo.bar == [{"baz": 1}, {"bobo": [13, 12]]`
64 /// `foo.bar[0] == {"baz": 1}`
65 /// `foo.bar[1].bobo[0] == 13`
66 /// `[1, 2, 3][1] == 2`
67 Expr70: Box<Expression> = {
68 <subject: Expr70> <index: Index> => Box::new(Expression::IndexOperation{subject, index}),
69 <subject: Expr70> "." <ident: Identifier> => Box::new(Expression::DotOperation{subject, ident}),
70 Expr80
71 };
72
73 Expr80: Box<Expression> = {
74 Number => Box::new(Expression::Number(<>)),
75 Boolean => Box::new(Expression::Boolean(<>)),
76 String => Box::new(Expression::String(<>)),
77 Array => Box::new(Expression::Array(<>)),
78 Object => Box::new(Expression::Object(<>)),
79 Null => Box::new(Expression::Null),
80 Identifier => Box::new(Expression::Identifier(<>)),
81 "(" <Expression> ")",
82 };
83
84 Args: Vec<Box<Expression>> = {
85 "(" <Comma<Expression>> ")"
86 };
87
88 Op10: OpCode = {
89 "&&" => OpCode::And,
90 "||" => OpCode::Or,
91 };
92
93 Op20: OpCode = {
94 "==" => OpCode::Equal,
95 "!=" => OpCode::NotEqual,
96 ">=" => OpCode::GreaterEqual,
97 "<=" => OpCode::LessEqual,
98 ">" => OpCode::Greater,
99 "<" => OpCode::Less,
100 "in" => OpCode::In,
101 };
102
103 Op30: OpCode = {
104 "+" => OpCode::Add,
105 "-" => OpCode::Subtract,
106 };
107
108 Op40: OpCode = {
109 "*" => OpCode::Multiply,
110 "//" => OpCode::FloorDivide,
111 "/" => OpCode::Divide,
112 };
113
114 Op50: OpCode = {
115 "%" => OpCode::Modulus,
116 "^" => OpCode::Exponent,
117 };
118
119 Number: f64 = {
120 r"[0-9]+" => f64::from_str(<>).unwrap(),
121 r"[0-9]+\.[0-9]*" => f64::from_str(<>).unwrap(),
122 r"\.[0-9]+" => f64::from_str(<>).unwrap(),
123 };
124
125 String: String = {
126 <s: r#""([^"\\]*(\\")?)*""#> => s[1..s.len() - 1].to_string().replace("\\\"", "\""),
127 <s: r#"'([^'\\]*(\\')?)*'"#> => s[1..s.len() - 1].to_string().replace("\\'", "'"),
128 };
129
130 Null: Option<Box<Expression>> = {
131 "null" => None,
132 }
133
134 Identifier: String = {
135 r#"[a-zA-Z_][a-zA-Z0-9_]*"# => <>.to_string()
136 }
137
138 Index: Box<Expression> = {
139 "[" "." <ident: Identifier> <op: Op20> <right: Expr80> "]" => Box::new(Expression::Filter {ident, op, right}),
140 "[" <Expression> "]",
141 }
142
143
144 Boolean: bool = {
145 "true" => true,
146 "false" => false,
147 }
148
149 Comma<T>: Vec<T> = {
150 <v: (<T> ",")*> <e:T?> => match e {
151 None => v,
152 Some(e) => {
153 let mut v = v;
154 v.push(e);
155 v
156 }
157 }
158 };
159
160 Array: Vec<Box<Expression>> = {
161 "[" <Comma<Expression>> "]"
162 }
163
164 Object: Vec<(String, Box<Expression>)> = {
165 "{" <Comma<(<ObjectIdentifier> ":" <Expression>)>> "}",
166 "{}" => vec![],
167 }
168
169 ObjectIdentifier: String = {
170 String,
171 Identifier
172 }