jexl_parser/
ast.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#[derive(Debug, PartialEq)]
6pub enum Expression {
7    Number(f64),
8    String(String),
9    Boolean(bool),
10    Array(Vec<Box<Expression>>),
11    Object(Vec<(String, Box<Expression>)>),
12    Identifier(String),
13    Null,
14
15    BinaryOperation {
16        operation: OpCode,
17        left: Box<Expression>,
18        right: Box<Expression>,
19    },
20    Transform {
21        name: String,
22        subject: Box<Expression>,
23        args: Option<Vec<Box<Expression>>>,
24    },
25    DotOperation {
26        subject: Box<Expression>,
27        ident: String,
28    },
29    IndexOperation {
30        subject: Box<Expression>,
31        index: Box<Expression>,
32    },
33
34    Conditional {
35        left: Box<Expression>,
36        truthy: Box<Expression>,
37        falsy: Box<Expression>,
38    },
39
40    Filter {
41        ident: String,
42        op: OpCode,
43        right: Box<Expression>,
44    },
45}
46
47#[derive(Debug, PartialEq, Eq, Copy, Clone)]
48pub enum OpCode {
49    Add,
50    Subtract,
51    Multiply,
52    Divide,
53    FloorDivide,
54    Less,
55    LessEqual,
56    Greater,
57    GreaterEqual,
58    Equal,
59    NotEqual,
60    And,
61    Or,
62    Modulus,
63    Exponent,
64    In,
65}
66
67impl std::fmt::Display for OpCode {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        write!(
70            f,
71            "{}",
72            match self {
73                OpCode::Add => "Add",
74                OpCode::Subtract => "Subtract",
75                OpCode::Multiply => "Multiply",
76                OpCode::Divide => "Divide",
77                OpCode::FloorDivide => "Floor division",
78                OpCode::Less => "Less than",
79                OpCode::LessEqual => "Less than or equal to",
80                OpCode::Greater => "Greater than",
81                OpCode::GreaterEqual => "Greater than or equal to",
82                OpCode::Equal => "Equal",
83                OpCode::NotEqual => "Not equal",
84                OpCode::And => "Bitwise And",
85                OpCode::Or => "Bitwise Or",
86                OpCode::Modulus => "Modulus",
87                OpCode::Exponent => "Exponent",
88                OpCode::In => "In",
89            }
90        )
91    }
92}