| 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 |
pub mod ast; |
| 6 |
#[rustfmt::skip] |
| 7 |
mod parser; |
| 8 |
|
| 9 |
pub use lalrpop_util::ParseError; |
| 10 |
|
| 11 |
pub use crate::parser::Token; |
| 12 |
|
| 13 |
pub struct Parser {} |
| 14 |
|
| 15 |
impl Parser { |
| 16 |
pub fn parse(input: &str) -> Result<ast::Expression, ParseError<usize, Token, &str>> { |
| 17 |
Ok(*parser::ExpressionParser::new().parse(input)?) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[cfg(test)] |
| 22 |
mod tests { |
| 23 |
use super::*; |
| 24 |
use crate::ast::{Expression, OpCode}; |
| 25 |
|
| 26 |
#[test] |
| 27 |
fn literal() { |
| 28 |
assert_eq!(Parser::parse("1"), Ok(Expression::Number(1.0))); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn binary_expression() { |
| 33 |
assert_eq!( |
| 34 |
Parser::parse("1+2"), |
| 35 |
Ok(Expression::BinaryOperation { |
| 36 |
operation: OpCode::Add, |
| 37 |
left: Box::new(Expression::Number(1.0)), |
| 38 |
right: Box::new(Expression::Number(2.0)), |
| 39 |
}), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn binary_expression_whitespace() { |
| 45 |
assert_eq!(Parser::parse("1 + 2 "), Parser::parse("1+2"),); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn transform_simple_no_args() { |
| 50 |
let exp = "'T_T'|lower"; |
| 51 |
let parsed = Parser::parse(exp).unwrap(); |
| 52 |
assert_eq!( |
| 53 |
parsed, |
| 54 |
Expression::Transform { |
| 55 |
name: "lower".to_string(), |
| 56 |
subject: Box::new(Expression::String("T_T".to_string())), |
| 57 |
args: None |
| 58 |
} |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn transform_multiple_args() { |
| 64 |
let exp = "'John Doe'|split(' ')"; |
| 65 |
let parsed = Parser::parse(exp).unwrap(); |
| 66 |
assert_eq!( |
| 67 |
parsed, |
| 68 |
Expression::Transform { |
| 69 |
name: "split".to_string(), |
| 70 |
subject: Box::new(Expression::String("John Doe".to_string())), |
| 71 |
args: Some(vec![Box::new(Expression::String(" ".to_string()))]) |
| 72 |
} |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn trasform_way_too_many_args() { |
| 78 |
let exp = "123456|math(12, 35, 100, 31, 90)"; |
| 79 |
let parsed = Parser::parse(exp).unwrap(); |
| 80 |
assert_eq!( |
| 81 |
parsed, |
| 82 |
Expression::Transform { |
| 83 |
name: "math".to_string(), |
| 84 |
subject: Box::new(Expression::Number(123_456f64)), |
| 85 |
args: Some(vec![ |
| 86 |
Box::new(Expression::Number(12f64)), |
| 87 |
Box::new(Expression::Number(35f64)), |
| 88 |
Box::new(Expression::Number(100f64)), |
| 89 |
Box::new(Expression::Number(31f64)), |
| 90 |
Box::new(Expression::Number(90f64)), |
| 91 |
]) |
| 92 |
} |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn test_index_op_ident() { |
| 98 |
let exp = "foo[0]"; |
| 99 |
let parsed = Parser::parse(exp).unwrap(); |
| 100 |
assert_eq!( |
| 101 |
parsed, |
| 102 |
Expression::IndexOperation { |
| 103 |
subject: Box::new(Expression::Identifier("foo".to_string())), |
| 104 |
index: Box::new(Expression::Number(0f64)) |
| 105 |
} |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_index_op_array_literal() { |
| 111 |
let exp = "[1, 2, 3][0]"; |
| 112 |
let parsed = Parser::parse(exp).unwrap(); |
| 113 |
assert_eq!( |
| 114 |
parsed, |
| 115 |
Expression::IndexOperation { |
| 116 |
subject: Box::new(Expression::Array(vec![ |
| 117 |
Box::new(Expression::Number(1f64)), |
| 118 |
Box::new(Expression::Number(2f64)), |
| 119 |
Box::new(Expression::Number(3f64)), |
| 120 |
])), |
| 121 |
index: Box::new(Expression::Number(0f64)) |
| 122 |
} |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_dot_op_ident() { |
| 128 |
let exp = "foo.bar"; |
| 129 |
let parsed = Parser::parse(exp).unwrap(); |
| 130 |
assert_eq!( |
| 131 |
parsed, |
| 132 |
Expression::DotOperation { |
| 133 |
subject: Box::new(Expression::Identifier("foo".to_string())), |
| 134 |
ident: "bar".to_string() |
| 135 |
} |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn test_dot_op_equality_with_null() { |
| 141 |
let exp = "foo.bar == null"; |
| 142 |
let parsed = Parser::parse(exp).unwrap(); |
| 143 |
assert_eq!( |
| 144 |
parsed, |
| 145 |
Expression::BinaryOperation { |
| 146 |
operation: OpCode::Equal, |
| 147 |
left: Box::new(Expression::DotOperation { |
| 148 |
subject: Box::new(Expression::Identifier("foo".to_string())), |
| 149 |
ident: "bar".to_string() |
| 150 |
}), |
| 151 |
right: Box::new(Expression::Null), |
| 152 |
} |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_dot_op_object_literal() { |
| 158 |
let exp = "{'foo': 1}.foo"; |
| 159 |
let parsed = Parser::parse(exp).unwrap(); |
| 160 |
assert_eq!( |
| 161 |
parsed, |
| 162 |
Expression::DotOperation { |
| 163 |
subject: Box::new(Expression::Object(vec![( |
| 164 |
"foo".to_string(), |
| 165 |
Box::new(Expression::Number(1f64)) |
| 166 |
)])), |
| 167 |
ident: "foo".to_string() |
| 168 |
} |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn test_parsing_null() { |
| 174 |
assert_eq!(Parser::parse("null"), Ok(Expression::Null)); |
| 175 |
} |
| 176 |
} |