|
1 | 1 | use crate::parser::errors::ParsingError;
|
2 |
| -use crate::{BinaryMatchingExpression, Expression, Keyword, Parser}; |
| 2 | +use crate::{ |
| 3 | + Expression, GlobExpression, Keyword, MatchExpression, Parser, RegexpMatchingExpression, |
| 4 | +}; |
3 | 5 |
|
4 | 6 | use super::ExpressionParser;
|
5 | 7 |
|
@@ -30,91 +32,96 @@ impl RegexpMatchExpressionParser for Parser<'_> {
|
30 | 32 | let pattern = self.parse_expression()?;
|
31 | 33 |
|
32 | 34 | let matching_expression = match match_type {
|
33 |
| - Keyword::Glob => BinaryMatchingExpression::Glob(Box::new(pattern)), |
34 |
| - Keyword::Regexp => BinaryMatchingExpression::Regexp(Box::new(pattern)), |
35 |
| - Keyword::Match => BinaryMatchingExpression::Match(Box::new(pattern)), |
| 35 | + Keyword::Glob => Expression::GlobExpression(GlobExpression { |
| 36 | + expression: Box::new(expression), |
| 37 | + not: is_not, |
| 38 | + pattern: Box::new(pattern), |
| 39 | + }), |
| 40 | + Keyword::Regexp => Expression::RegexpExpression(RegexpMatchingExpression { |
| 41 | + expression: Box::new(expression), |
| 42 | + not: is_not, |
| 43 | + pattern: Box::new(pattern), |
| 44 | + }), |
| 45 | + Keyword::Match => Expression::MatchExpression(MatchExpression { |
| 46 | + expression: Box::new(expression), |
| 47 | + not: is_not, |
| 48 | + pattern: Box::new(pattern), |
| 49 | + }), |
36 | 50 | _ => unreachable!(),
|
37 | 51 | };
|
38 | 52 |
|
39 |
| - if is_not { |
40 |
| - Ok(Expression::BinaryMatchingExpression( |
41 |
| - Box::new(expression), |
42 |
| - BinaryMatchingExpression::Not(Box::new(matching_expression)), |
43 |
| - )) |
44 |
| - } else { |
45 |
| - Ok(Expression::BinaryMatchingExpression( |
46 |
| - Box::new(expression), |
47 |
| - matching_expression, |
48 |
| - )) |
49 |
| - } |
| 53 | + Ok(matching_expression) |
50 | 54 | }
|
51 | 55 | }
|
52 | 56 |
|
53 | 57 | #[cfg(test)]
|
54 | 58 | mod regexp_match_expression_tests {
|
55 | 59 | use crate::parser::test_utils::run_sunny_day_test;
|
56 | 60 | use crate::select::test_utils::select_expr;
|
57 |
| - use crate::{BinaryMatchingExpression, Expression, Keyword}; |
| 61 | + use crate::{Expression, GlobExpression, MatchExpression, RegexpMatchingExpression}; |
58 | 62 |
|
59 | 63 | use crate::parser::expression::test_utils::*;
|
60 | 64 |
|
61 |
| - fn binary_matching_expr(pattern: Expression, keyword: Keyword) -> BinaryMatchingExpression { |
62 |
| - match keyword { |
63 |
| - Keyword::Glob => BinaryMatchingExpression::Glob(Box::new(pattern)), |
64 |
| - Keyword::Regexp => BinaryMatchingExpression::Regexp(Box::new(pattern)), |
65 |
| - Keyword::Match => BinaryMatchingExpression::Match(Box::new(pattern)), |
66 |
| - _ => panic!("Invalid keyword: {}", keyword), |
| 65 | + fn glob_expr(expression: Expression, pattern: Expression) -> GlobExpression { |
| 66 | + GlobExpression { |
| 67 | + expression: Box::new(expression), |
| 68 | + not: false, |
| 69 | + pattern: Box::new(pattern), |
67 | 70 | }
|
68 | 71 | }
|
69 | 72 |
|
70 |
| - fn regexp_match_expr( |
71 |
| - expression: Expression, |
72 |
| - pattern: Expression, |
73 |
| - keyword: Keyword, |
74 |
| - is_not: bool, |
75 |
| - ) -> Expression { |
76 |
| - let binary_matching_expression = if is_not { |
77 |
| - BinaryMatchingExpression::Not(Box::new(binary_matching_expr(pattern, keyword))) |
78 |
| - } else { |
79 |
| - binary_matching_expr(pattern, keyword) |
80 |
| - }; |
| 73 | + fn regexp_expr(expression: Expression, pattern: Expression) -> RegexpMatchingExpression { |
| 74 | + RegexpMatchingExpression { |
| 75 | + expression: Box::new(expression), |
| 76 | + not: false, |
| 77 | + pattern: Box::new(pattern), |
| 78 | + } |
| 79 | + } |
81 | 80 |
|
82 |
| - Expression::BinaryMatchingExpression(Box::new(expression), binary_matching_expression) |
| 81 | + fn match_expr(expression: Expression, pattern: Expression) -> MatchExpression { |
| 82 | + MatchExpression { |
| 83 | + expression: Box::new(expression), |
| 84 | + not: false, |
| 85 | + pattern: Box::new(pattern), |
| 86 | + } |
83 | 87 | }
|
84 | 88 |
|
85 | 89 | #[test]
|
86 |
| - fn regexp_match() { |
87 |
| - let keywords = vec![Keyword::Glob, Keyword::Regexp, Keyword::Match]; |
88 |
| - |
89 |
| - for keyword in keywords { |
90 |
| - run_sunny_day_test( |
91 |
| - &format!("SELECT 1 {} 'a*';", keyword), |
92 |
| - select_expr(regexp_match_expr( |
93 |
| - numeric_expr("1"), |
94 |
| - string_expr("'a*'"), |
95 |
| - keyword, |
96 |
| - false, |
97 |
| - )) |
98 |
| - .into(), |
99 |
| - ); |
100 |
| - } |
| 90 | + fn glob_expr_test() { |
| 91 | + let expected = glob_expr(numeric_expr("1"), string_expr("'a*'")); |
| 92 | + run_sunny_day_test("SELECT 1 GLOB 'a*';", select_expr(expected.into()).into()); |
| 93 | + |
| 94 | + let mut expected = glob_expr(numeric_expr("1"), string_expr("'a*'")); |
| 95 | + expected.not = true; |
| 96 | + run_sunny_day_test( |
| 97 | + "SELECT 1 NOT GLOB 'a*';", |
| 98 | + select_expr(expected.into()).into(), |
| 99 | + ); |
101 | 100 | }
|
102 | 101 |
|
103 | 102 | #[test]
|
104 |
| - fn not_regexp_match() { |
105 |
| - let keywords = vec![Keyword::Glob, Keyword::Regexp, Keyword::Match]; |
106 |
| - |
107 |
| - for keyword in keywords { |
108 |
| - run_sunny_day_test( |
109 |
| - &format!("SELECT 1 NOT {} 'a*';", keyword), |
110 |
| - select_expr(regexp_match_expr( |
111 |
| - numeric_expr("1"), |
112 |
| - string_expr("'a*'"), |
113 |
| - keyword, |
114 |
| - true, |
115 |
| - )) |
116 |
| - .into(), |
117 |
| - ); |
118 |
| - } |
| 103 | + fn regexp_expr_test() { |
| 104 | + let expected = regexp_expr(numeric_expr("1"), string_expr("'a*'")); |
| 105 | + run_sunny_day_test("SELECT 1 REGEXP 'a*';", select_expr(expected.into()).into()); |
| 106 | + |
| 107 | + let mut expected = regexp_expr(numeric_expr("1"), string_expr("'a*'")); |
| 108 | + expected.not = true; |
| 109 | + run_sunny_day_test( |
| 110 | + "SELECT 1 NOT REGEXP 'a*';", |
| 111 | + select_expr(expected.into()).into(), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn match_expr_test() { |
| 117 | + let expected = match_expr(numeric_expr("1"), string_expr("'a*'")); |
| 118 | + run_sunny_day_test("SELECT 1 MATCH 'a*';", select_expr(expected.into()).into()); |
| 119 | + |
| 120 | + let mut expected = match_expr(numeric_expr("1"), string_expr("'a*'")); |
| 121 | + expected.not = true; |
| 122 | + run_sunny_day_test( |
| 123 | + "SELECT 1 NOT MATCH 'a*';", |
| 124 | + select_expr(expected.into()).into(), |
| 125 | + ); |
119 | 126 | }
|
120 | 127 | }
|
0 commit comments