@@ -64,32 +64,23 @@ impl Parser {
64
64
}
65
65
66
66
/// Parse tokens until the precedence changes
67
- fn parse_expr ( & mut self , precedence : u8 ) -> Result < ASTNode , ParserError > {
68
- // println!("parse_expr() precendence = {}", precedence);
69
-
67
+ pub fn parse_expr ( & mut self , precedence : u8 ) -> Result < ASTNode , ParserError > {
70
68
let mut expr = self . parse_prefix ( ) ?;
71
- // println!("parsed prefix: {:?}", expr);
72
-
73
69
loop {
74
70
let next_precedence = self . get_next_precedence ( ) ?;
75
71
if precedence >= next_precedence {
76
- // println!("break on precedence change ({} >= {})", precedence, next_precedence);
77
72
break ;
78
73
}
79
74
80
75
if let Some ( infix_expr) = self . parse_infix ( expr. clone ( ) , next_precedence) ? {
81
- // println!("parsed infix: {:?}", infix_expr);
82
76
expr = infix_expr;
83
77
}
84
78
}
85
-
86
- // println!("parse_expr() returning {:?}", expr);
87
-
88
79
Ok ( expr)
89
80
}
90
81
91
82
/// Parse an expression prefix
92
- fn parse_prefix ( & mut self ) -> Result < ASTNode , ParserError > {
83
+ pub fn parse_prefix ( & mut self ) -> Result < ASTNode , ParserError > {
93
84
match self . next_token ( ) {
94
85
Some ( t) => {
95
86
match t {
@@ -150,7 +141,7 @@ impl Parser {
150
141
}
151
142
152
143
/// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
153
- fn parse_cast_expression ( & mut self ) -> Result < ASTNode , ParserError > {
144
+ pub fn parse_cast_expression ( & mut self ) -> Result < ASTNode , ParserError > {
154
145
let expr = self . parse_expr ( 0 ) ?;
155
146
self . consume_token ( & Token :: Keyword ( "AS" . to_string ( ) ) ) ?;
156
147
let data_type = self . parse_data_type ( ) ?;
@@ -162,7 +153,7 @@ impl Parser {
162
153
}
163
154
164
155
/// Parse an expression infix (typically an operator)
165
- fn parse_infix (
156
+ pub fn parse_infix (
166
157
& mut self ,
167
158
expr : ASTNode ,
168
159
precedence : u8 ,
@@ -206,7 +197,7 @@ impl Parser {
206
197
}
207
198
208
199
/// Convert a token operator to an AST operator
209
- fn to_sql_operator ( & self , tok : & Token ) -> Result < SQLOperator , ParserError > {
200
+ pub fn to_sql_operator ( & self , tok : & Token ) -> Result < SQLOperator , ParserError > {
210
201
match tok {
211
202
& Token :: Eq => Ok ( SQLOperator :: Eq ) ,
212
203
& Token :: Neq => Ok ( SQLOperator :: NotEq ) ,
@@ -226,7 +217,7 @@ impl Parser {
226
217
}
227
218
228
219
/// Get the precedence of the next token
229
- fn get_next_precedence ( & self ) -> Result < u8 , ParserError > {
220
+ pub fn get_next_precedence ( & self ) -> Result < u8 , ParserError > {
230
221
if self . index < self . tokens . len ( ) {
231
222
self . get_precedence ( & self . tokens [ self . index ] )
232
223
} else {
@@ -235,7 +226,7 @@ impl Parser {
235
226
}
236
227
237
228
/// Get the precedence of a token
238
- fn get_precedence ( & self , tok : & Token ) -> Result < u8 , ParserError > {
229
+ pub fn get_precedence ( & self , tok : & Token ) -> Result < u8 , ParserError > {
239
230
//println!("get_precedence() {:?}", tok);
240
231
241
232
match tok {
@@ -252,7 +243,7 @@ impl Parser {
252
243
}
253
244
254
245
/// Peek at the next token
255
- fn peek_token ( & mut self ) -> Option < Token > {
246
+ pub fn peek_token ( & mut self ) -> Option < Token > {
256
247
if self . index < self . tokens . len ( ) {
257
248
Some ( self . tokens [ self . index ] . clone ( ) )
258
249
} else {
@@ -261,7 +252,7 @@ impl Parser {
261
252
}
262
253
263
254
/// Get the next token and increment the token index
264
- fn next_token ( & mut self ) -> Option < Token > {
255
+ pub fn next_token ( & mut self ) -> Option < Token > {
265
256
if self . index < self . tokens . len ( ) {
266
257
self . index = self . index + 1 ;
267
258
Some ( self . tokens [ self . index - 1 ] . clone ( ) )
@@ -271,7 +262,7 @@ impl Parser {
271
262
}
272
263
273
264
/// Get the previous token and decrement the token index
274
- fn prev_token ( & mut self ) -> Option < Token > {
265
+ pub fn prev_token ( & mut self ) -> Option < Token > {
275
266
if self . index > 0 {
276
267
Some ( self . tokens [ self . index - 1 ] . clone ( ) )
277
268
} else {
@@ -280,7 +271,7 @@ impl Parser {
280
271
}
281
272
282
273
/// Look for an expected keyword and consume it if it exists
283
- fn parse_keyword ( & mut self , expected : & ' static str ) -> bool {
274
+ pub fn parse_keyword ( & mut self , expected : & ' static str ) -> bool {
284
275
match self . peek_token ( ) {
285
276
Some ( Token :: Keyword ( k) ) => {
286
277
if expected. eq_ignore_ascii_case ( k. as_str ( ) ) {
@@ -295,7 +286,7 @@ impl Parser {
295
286
}
296
287
297
288
/// Look for an expected sequence of keywords and consume them if they exist
298
- fn parse_keywords ( & mut self , keywords : Vec < & ' static str > ) -> bool {
289
+ pub fn parse_keywords ( & mut self , keywords : Vec < & ' static str > ) -> bool {
299
290
let index = self . index ;
300
291
for keyword in keywords {
301
292
//println!("parse_keywords aborting .. expecting {}", keyword);
@@ -312,7 +303,7 @@ impl Parser {
312
303
//TODO: this function is inconsistent and sometimes returns bool and sometimes fails
313
304
314
305
/// Consume the next token if it matches the expected token, otherwise return an error
315
- fn consume_token ( & mut self , expected : & Token ) -> Result < bool , ParserError > {
306
+ pub fn consume_token ( & mut self , expected : & Token ) -> Result < bool , ParserError > {
316
307
match self . peek_token ( ) {
317
308
Some ( ref t) => if * t == * expected {
318
309
self . next_token ( ) ;
@@ -329,7 +320,7 @@ impl Parser {
329
320
}
330
321
331
322
/// Parse a SQL CREATE statement
332
- fn parse_create ( & mut self ) -> Result < ASTNode , ParserError > {
323
+ pub fn parse_create ( & mut self ) -> Result < ASTNode , ParserError > {
333
324
if self . parse_keywords ( vec ! [ "TABLE" ] ) {
334
325
match self . next_token ( ) {
335
326
Some ( Token :: Identifier ( id) ) => {
@@ -398,7 +389,7 @@ impl Parser {
398
389
}
399
390
400
391
/// Parse a literal integer/long
401
- fn parse_literal_int ( & mut self ) -> Result < i64 , ParserError > {
392
+ pub fn parse_literal_int ( & mut self ) -> Result < i64 , ParserError > {
402
393
match self . next_token ( ) {
403
394
Some ( Token :: Number ( s) ) => s. parse :: < i64 > ( ) . map_err ( |e| {
404
395
ParserError :: ParserError ( format ! ( "Could not parse '{}' as i64: {}" , s, e) )
@@ -408,19 +399,19 @@ impl Parser {
408
399
}
409
400
410
401
/// Parse a literal string
411
- // fn parse_literal_string(&mut self) -> Result<String, ParserError> {
412
- // match self.next_token() {
413
- // Some(Token::String(ref s)) => Ok(s.clone()),
414
- // other => parser_err!(format!("Expected literal string, found {:?}", other)),
415
- // }
416
- // }
402
+ pub fn parse_literal_string ( & mut self ) -> Result < String , ParserError > {
403
+ match self . next_token ( ) {
404
+ Some ( Token :: String ( ref s) ) => Ok ( s. clone ( ) ) ,
405
+ other => parser_err ! ( format!( "Expected literal string, found {:?}" , other) ) ,
406
+ }
407
+ }
417
408
418
409
/// Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
419
- fn parse_data_type ( & mut self ) -> Result < SQLType , ParserError > {
410
+ pub fn parse_data_type ( & mut self ) -> Result < SQLType , ParserError > {
420
411
match self . next_token ( ) {
421
412
Some ( Token :: Keyword ( k) ) => match k. to_uppercase ( ) . as_ref ( ) {
422
413
"BOOLEAN" => Ok ( SQLType :: Boolean ) ,
423
- "FLOAT" => Ok ( SQLType :: Float ( self . parse_precision ( ) ?) ) ,
414
+ "FLOAT" => Ok ( SQLType :: Float ( self . parse_optional_precision ( ) ?) ) ,
424
415
"REAL" => Ok ( SQLType :: Real ) ,
425
416
"DOUBLE" => Ok ( SQLType :: Double ) ,
426
417
"SMALLINT" => Ok ( SQLType :: SmallInt ) ,
@@ -433,12 +424,12 @@ impl Parser {
433
424
}
434
425
}
435
426
436
- fn parse_precision ( & mut self ) -> Result < usize , ParserError > {
427
+ pub fn parse_precision ( & mut self ) -> Result < usize , ParserError > {
437
428
//TODO: error handling
438
429
Ok ( self . parse_optional_precision ( ) ?. unwrap ( ) )
439
430
}
440
431
441
- fn parse_optional_precision ( & mut self ) -> Result < Option < usize > , ParserError > {
432
+ pub fn parse_optional_precision ( & mut self ) -> Result < Option < usize > , ParserError > {
442
433
if self . consume_token ( & Token :: LParen ) ? {
443
434
let n = self . parse_literal_int ( ) ?;
444
435
//TODO: check return value of reading rparen
@@ -450,7 +441,7 @@ impl Parser {
450
441
}
451
442
452
443
/// Parse a SELECT statement
453
- fn parse_select ( & mut self ) -> Result < ASTNode , ParserError > {
444
+ pub fn parse_select ( & mut self ) -> Result < ASTNode , ParserError > {
454
445
let projection = self . parse_expr_list ( ) ?;
455
446
456
447
let relation: Option < Box < ASTNode > > = if self . parse_keyword ( "FROM" ) {
@@ -509,7 +500,7 @@ impl Parser {
509
500
}
510
501
511
502
/// Parse a comma-delimited list of SQL expressions
512
- fn parse_expr_list ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
503
+ pub fn parse_expr_list ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
513
504
let mut expr_list: Vec < ASTNode > = vec ! [ ] ;
514
505
loop {
515
506
expr_list. push ( self . parse_expr ( 0 ) ?) ;
@@ -528,7 +519,7 @@ impl Parser {
528
519
}
529
520
530
521
/// Parse a comma-delimited list of SQL ORDER BY expressions
531
- fn parse_order_by_expr_list ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
522
+ pub fn parse_order_by_expr_list ( & mut self ) -> Result < Vec < ASTNode > , ParserError > {
532
523
let mut expr_list: Vec < ASTNode > = vec ! [ ] ;
533
524
loop {
534
525
let expr = self . parse_expr ( 0 ) ?;
@@ -575,7 +566,7 @@ impl Parser {
575
566
}
576
567
577
568
/// Parse a LIMIT clause
578
- fn parse_limit ( & mut self ) -> Result < Option < Box < ASTNode > > , ParserError > {
569
+ pub fn parse_limit ( & mut self ) -> Result < Option < Box < ASTNode > > , ParserError > {
579
570
if self . parse_keyword ( "ALL" ) {
580
571
Ok ( None )
581
572
} else {
@@ -845,6 +836,21 @@ mod tests {
845
836
//TODO: assertions
846
837
}
847
838
839
+ #[ test]
840
+ fn parse_literal_string ( ) {
841
+ let sql = "SELECT 'one'" ;
842
+ match parse_sql ( & sql) {
843
+ ASTNode :: SQLSelect { ref projection, .. } => {
844
+ assert_eq ! (
845
+ projection[ 0 ] ,
846
+ ASTNode :: SQLLiteralString ( "one" . to_string( ) )
847
+ ) ;
848
+ }
849
+ _ => panic ! ( ) ,
850
+ }
851
+
852
+ }
853
+
848
854
#[ test]
849
855
fn parse_select_version ( ) {
850
856
let sql = "SELECT @@version" ;
0 commit comments