1
1
use std:: cmp:: PartialEq ;
2
2
use std:: fmt:: Debug ;
3
- //use std::iter::Peekable;
4
- //use std::str::Chars;
5
-
6
- use std:: sync:: { Arc , Mutex } ;
3
+ //use std::rc::Rc;
4
+ //use std::sync::{Arc, Mutex};
7
5
6
+ use super :: tokenizer:: ANSISQLTokenizer ;
8
7
use super :: super :: tokenizer:: * ;
9
8
use super :: super :: parser:: * ;
10
9
11
- pub struct ANSISQLParser < TokenType > {
12
- tokenizer : Arc < Mutex < SQLTokenizer < TokenType > > >
10
+ pub struct ANSISQLParser {
11
+ tokenizer : Box < SQLTokenizer >
13
12
}
14
13
15
- impl < TokenType > ANSISQLParser < TokenType > where TokenType : Debug + PartialEq {
14
+ impl ANSISQLParser where {
16
15
17
- pub fn new ( tokenizer : Arc < Mutex < SQLTokenizer < TokenType > > > ) -> Self {
18
- ANSISQLParser { tokenizer : tokenizer. clone ( ) }
16
+ pub fn parse ( sql : & str ) -> Result < Option < Box < SQLExpr > > , ParserError > {
17
+ let mut parser = ANSISQLParser { tokenizer : Box :: new ( ANSISQLTokenizer :: new ( sql) ) } ;
18
+ parser. parse_expr ( )
19
19
}
20
20
}
21
21
22
- impl < TokenType , ExprType > SQLParser < TokenType , ExprType > for ANSISQLParser < TokenType >
23
- where TokenType : Debug + PartialEq , ExprType : Debug {
22
+ impl SQLParser for ANSISQLParser {
23
+
24
+ fn parse_expr ( & mut self ) -> Result < Option < Box < SQLExpr > > , ParserError > {
25
+
26
+ let precedence: usize = 0 ;
27
+
28
+ let mut e = self . parse_prefix ( ) ?;
29
+
30
+ match e {
31
+ Some ( mut expr) => {
32
+ while let Some ( token) = self . tokenizer . peek_token ( ) ? {
33
+ let next_precedence = self . tokenizer . precedence ( & token) ;
34
+
35
+ if precedence >= next_precedence {
36
+ break ;
37
+ }
38
+
39
+ expr = self . parse_infix ( & expr, next_precedence) ?. unwrap ( ) ; //TODO: fix me
40
+ }
41
+
42
+ Ok ( Some ( expr) )
43
+ }
44
+ _ => {
45
+ Ok ( None )
46
+ }
47
+ }
48
+
49
+ }
24
50
25
- fn parse_prefix ( & mut self , chars : & mut CharSeq ) -> Result < Option < Box < SQLExpr < ExprType > > > , ParserError < TokenType > > {
51
+ fn parse_prefix ( & mut self ) -> Result < Option < Box < SQLExpr > > , ParserError > {
26
52
27
- match self . tokenizer . lock ( ) . unwrap ( ) . next_token ( chars ) ? {
53
+ match self . tokenizer . next_token ( ) ? {
28
54
Some ( SQLToken :: Keyword ( ref k) ) => match k. to_uppercase ( ) . as_ref ( ) {
29
55
"INSERT" => unimplemented ! ( ) ,
30
56
"UPDATE" => unimplemented ! ( ) ,
@@ -37,7 +63,7 @@ impl<TokenType, ExprType> SQLParser<TokenType, ExprType> for ANSISQLParser<Token
37
63
}
38
64
}
39
65
40
- fn parse_infix ( & mut self , _chars : & mut CharSeq , _left : & SQLExpr < ExprType > , _precedence : usize ) -> Result < Option < Box < SQLExpr < ExprType > > > , ParserError < TokenType > > {
66
+ fn parse_infix ( & mut self , _left : & SQLExpr , _precedence : usize ) -> Result < Option < Box < SQLExpr > > , ParserError > {
41
67
unimplemented ! ( )
42
68
}
43
69
}
0 commit comments