@@ -4,8 +4,14 @@ use super::{
4
4
raise_expr:: RaiseExpressionParser , ExpressionParser , FunctionParser ,
5
5
} ;
6
6
use crate :: {
7
- parser:: ParsingError , BinaryOp , Expression , IdentifierParser , Keyword , LiteralValue , Parser ,
8
- TokenType , UnaryOp ,
7
+ expression:: {
8
+ between_expr:: BetweenExpressionParser , in_expr:: InExpressionParser ,
9
+ is_expr:: IsExpressionParser , like_expr:: LikeExpressionParser ,
10
+ regexp_match_expr:: RegexpMatchExpressionParser ,
11
+ } ,
12
+ parser:: ParsingError ,
13
+ BinaryOp , Expression , IdentifierParser , Keyword , LiteralValue , Parser , TokenType ,
14
+ UnaryMatchingExpression , UnaryOp ,
9
15
} ;
10
16
11
17
pub trait PrattParser {
@@ -29,12 +35,22 @@ pub trait PrattParser {
29
35
impl < ' a > PrattParser for Parser < ' a > {
30
36
/// Parse an expression using Pratt's parsing algorithm
31
37
fn parse_expression_pratt ( & mut self , precedence : u8 ) -> Result < Expression , ParsingError > {
38
+ dbg ! ( "parse_expression_pratt" ) ;
32
39
let mut expression = self . parse_prefix ( ) ?;
33
40
41
+ dbg ! ( "prefix expression: {:?}" , & expression) ;
34
42
loop {
43
+ println ! ( "precedence: {:?}" , precedence) ;
35
44
let current_token = self . peek_token ( ) ?;
36
45
let next_precedence = get_precedence ( & current_token. token_type ) ;
37
46
47
+ dbg ! ( "current_token: {:?}" , & current_token) ;
48
+ dbg ! (
49
+ "precedence: {:?}, next_precedence: {:?}" ,
50
+ & precedence,
51
+ & next_precedence
52
+ ) ;
53
+
38
54
if precedence >= next_precedence {
39
55
break ;
40
56
}
@@ -50,17 +66,78 @@ impl<'a> PrattParser for Parser<'a> {
50
66
left : Expression ,
51
67
precedence : u8 ,
52
68
) -> Result < Expression , ParsingError > {
69
+ dbg ! ( "parse_infix" ) ;
70
+
53
71
let token = self . peek_token ( ) ?;
54
- let operator = BinaryOp :: try_from ( & token. token_type ) ?;
55
- // Consume the operator token
56
- self . consume_as ( token. token_type ) ?;
57
-
58
- let right = self . parse_expression_pratt ( precedence) ?;
59
- Ok ( Expression :: BinaryOp (
60
- Box :: new ( left) ,
61
- operator,
62
- Box :: new ( right) ,
63
- ) )
72
+
73
+ let binary_operator_result = BinaryOp :: try_from ( & token. token_type ) ;
74
+
75
+ if let Ok ( binary_operator) = binary_operator_result {
76
+ // Consume the operator token
77
+ self . consume_as ( token. token_type ) ?;
78
+
79
+ let right = self . parse_expression_pratt ( precedence) ?;
80
+ Ok ( Expression :: BinaryOp (
81
+ Box :: new ( left) ,
82
+ binary_operator,
83
+ Box :: new ( right) ,
84
+ ) )
85
+ } else if let Ok ( Keyword :: Between ) = self . peek_as_keyword ( ) {
86
+ return BetweenExpressionParser :: parse_between_expression ( self , left, false ) ;
87
+ } else if let Ok ( Keyword :: Like ) = self . peek_as_keyword ( ) {
88
+ return LikeExpressionParser :: parse_like_expression ( self , left, false ) ;
89
+ } else if let Ok ( Keyword :: Is ) = self . peek_as_keyword ( ) {
90
+ return IsExpressionParser :: parse_is_expression ( self , left) ;
91
+ } else if let Ok ( Keyword :: In ) = self . peek_as_keyword ( ) {
92
+ return InExpressionParser :: parse_in_expression ( self , left, false ) ;
93
+ } else if let Ok ( Keyword :: Glob ) = self . peek_as_keyword ( ) {
94
+ return RegexpMatchExpressionParser :: parse_regexp_match_expression ( self , left, false ) ;
95
+ } else if let Ok ( Keyword :: Regexp ) = self . peek_as_keyword ( ) {
96
+ return RegexpMatchExpressionParser :: parse_regexp_match_expression ( self , left, false ) ;
97
+ } else if let Ok ( Keyword :: Match ) = self . peek_as_keyword ( ) {
98
+ return RegexpMatchExpressionParser :: parse_regexp_match_expression ( self , left, false ) ;
99
+ } else if let Ok ( Keyword :: Isnull ) = self . peek_as_keyword ( ) {
100
+ self . consume_as_keyword ( Keyword :: Isnull ) ?;
101
+ Ok ( Expression :: UnaryMatchingExpression (
102
+ Box :: new ( left) ,
103
+ UnaryMatchingExpression :: IsNull ,
104
+ ) )
105
+ } else if let Ok ( Keyword :: Notnull ) = self . peek_as_keyword ( ) {
106
+ self . consume_as_keyword ( Keyword :: Notnull ) ?;
107
+ Ok ( Expression :: UnaryMatchingExpression (
108
+ Box :: new ( left) ,
109
+ UnaryMatchingExpression :: IsNotNull ,
110
+ ) )
111
+ } else if let Ok ( Keyword :: Not ) = self . peek_as_keyword ( ) {
112
+ self . consume_as_keyword ( Keyword :: Not ) ?;
113
+
114
+ if let Ok ( nested_keyword) = self . peek_as_keyword ( ) {
115
+ match nested_keyword {
116
+ Keyword :: Null => {
117
+ self . consume_as_keyword ( Keyword :: Null ) ?;
118
+ Ok ( Expression :: UnaryMatchingExpression (
119
+ Box :: new ( left) ,
120
+ UnaryMatchingExpression :: IsNotNull ,
121
+ ) )
122
+ }
123
+ Keyword :: Between => {
124
+ BetweenExpressionParser :: parse_between_expression ( self , left, true )
125
+ }
126
+ Keyword :: Like => LikeExpressionParser :: parse_like_expression ( self , left, true ) ,
127
+ Keyword :: Glob | Keyword :: Regexp | Keyword :: Match => {
128
+ RegexpMatchExpressionParser :: parse_regexp_match_expression ( self , left, true )
129
+ }
130
+ Keyword :: In => InExpressionParser :: parse_in_expression ( self , left, true ) ,
131
+ _ => {
132
+ return Err ( ParsingError :: UnexpectedKeyword ( nested_keyword) ) ;
133
+ }
134
+ }
135
+ } else {
136
+ Err ( ParsingError :: UnexpectedKeyword ( Keyword :: Not ) )
137
+ }
138
+ } else {
139
+ Ok ( left)
140
+ }
64
141
}
65
142
66
143
/// Parse a prefix expression
@@ -157,6 +234,12 @@ impl<'a> PrattParser for Parser<'a> {
157
234
let expression = self . parse_expression_pratt ( pr) ?;
158
235
Ok ( Expression :: UnaryOp ( UnaryOp :: Plus , Box :: new ( expression) ) )
159
236
}
237
+ TokenType :: BitNot => {
238
+ self . consume_as ( TokenType :: BitNot ) ?;
239
+ let pr = get_precedence ( & TokenType :: BitNot ) ;
240
+ let expression = self . parse_expression_pratt ( pr) ?;
241
+ Ok ( Expression :: UnaryOp ( UnaryOp :: BitNot , Box :: new ( expression) ) )
242
+ }
160
243
TokenType :: Blob ( value) => {
161
244
self . consume_token ( ) ?;
162
245
Ok ( Expression :: LiteralValue ( LiteralValue :: Blob (
@@ -178,6 +261,7 @@ impl<'a> PrattParser for Parser<'a> {
178
261
}
179
262
}
180
263
264
+ /// Parse an expression which starts with the NOT keyword
181
265
fn parse_not_expression ( & mut self ) -> Result < Expression , ParsingError > {
182
266
self . consume_as_keyword ( Keyword :: Not ) ?;
183
267
@@ -503,7 +587,7 @@ mod unary_matching_expression_tests {
503
587
#[ test]
504
588
fn notnull ( ) {
505
589
run_sunny_day_test (
506
- "SELECT 1 NOT NULL ;" ,
590
+ "SELECT 1 NOTNULL ;" ,
507
591
select_expr ( unary_matching_expression (
508
592
numeric_expr ( "1" ) ,
509
593
UnaryMatchingExpression :: IsNotNull ,
@@ -512,7 +596,7 @@ mod unary_matching_expression_tests {
512
596
) ;
513
597
514
598
run_sunny_day_test (
515
- "SELECT 1 NOTNULL ;" ,
599
+ "SELECT 1 NOT NULL ;" ,
516
600
select_expr ( unary_matching_expression (
517
601
numeric_expr ( "1" ) ,
518
602
UnaryMatchingExpression :: IsNotNull ,
0 commit comments