Skip to content

Commit 037ebb0

Browse files
committed
Refactoring
1 parent 2e20b15 commit 037ebb0

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

examples/acme_parser.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,24 @@ impl SQLTokenizer<AcmeToken> for AcmeTokenizer {
6262
}
6363

6464
struct AcmeParser {
65-
ansi_parser: Arc<Mutex<SQLParser<AcmeToken, AcmeExpr>>>
65+
tokenizer: Arc<Mutex<SQLTokenizer<AcmeToken>>>
6666
}
6767

68+
impl AcmeParser {
69+
70+
pub fn new(tokenizer: Arc<Mutex<SQLTokenizer<AcmeToken>>>) -> Self {
71+
AcmeParser { tokenizer: tokenizer.clone() }
72+
}
73+
74+
}
6875
impl SQLParser<AcmeToken, AcmeExpr> for AcmeParser {
6976

70-
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Box<SQLExpr<AcmeExpr>>, ParserError<AcmeToken>> {
71-
//TODO: add custom overrides
72-
self.ansi_parser.lock().unwrap().parse_prefix(chars)
77+
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Option<Box<SQLExpr<AcmeExpr>>>, ParserError<AcmeToken>> {
78+
Ok(None)
7379
}
7480

7581
fn parse_infix(&mut self, chars: &mut CharSeq, left: &SQLExpr<AcmeExpr>, precedence: usize) -> Result<Option<Box<SQLExpr<AcmeExpr>>>, ParserError<AcmeToken>> {
76-
//TODO: add custom overrides
77-
self.ansi_parser.lock().unwrap().parse_infix(chars, left, precedence)
82+
Ok(None)
7883
}
7984
}
8085

@@ -90,14 +95,18 @@ fn main() {
9095
ansi_tokenizer: ansi_tokenizer.clone()
9196
}));
9297

98+
// Create parsers
99+
let ansi_parser = Arc::new(Mutex::new(ANSISQLParser::new(acme_tokenizer.clone())));
100+
let acme_parser = Arc::new(Mutex::new(AcmeParser::new(acme_tokenizer.clone())));
101+
102+
//let parser_list: Vec<Arc<Mutex<SQLParser<>>>> = vec![acme_parser, ansi_parser];
103+
93104
// Custom ACME parser
94-
let acme_parser: Arc<Mutex<SQLParser<AcmeToken, AcmeExpr>>> = Arc::new(Mutex::new(AcmeParser {
95-
ansi_parser: Arc::new(Mutex::new(ANSISQLParser::new(acme_tokenizer)))
96-
}));
105+
// let acme_parser: Arc<Mutex<SQLParser<AcmeToken, AcmeExpr>>> = Arc::new(Mutex::new(AcmeParser {
106+
// ansi_parser: Arc::new(Mutex::new(ANSISQLParser::new(acme_tokenizer)))
107+
// }));
97108

98109
// let expr = parse_expr(acme_parser).unwrap();
99110
//
100111
// println!("Parsed: {:?}", expr);
101-
102-
103112
}

src/ansi/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<TokenType> ANSISQLParser<TokenType> where TokenType: Debug + PartialEq {
2222
impl<TokenType, ExprType> SQLParser<TokenType, ExprType> for ANSISQLParser<TokenType>
2323
where TokenType: Debug + PartialEq, ExprType: Debug {
2424

25-
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Box<SQLExpr<ExprType>>, ParserError<TokenType>> {
25+
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>> {
2626

2727
match self.tokenizer.lock().unwrap().next_token(chars)? {
2828
Some(SQLToken::Keyword(ref k)) => match k.to_uppercase().as_ref() {

src/ansi/tokenizer.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::cmp::PartialEq;
22
use std::fmt::Debug;
3-
use std::iter::Peekable;
4-
use std::str::Chars;
53

64
use super::super::tokenizer::*;
75

src/parser.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::cmp::PartialEq;
22
use std::fmt::Debug;
3-
use std::rc::Rc;
4-
use std::sync::{Arc, Mutex};
53

64
use super::tokenizer::*;
75

@@ -110,7 +108,7 @@ pub trait SQLParser<TokenType, ExprType>
110108
where TokenType: Debug + PartialEq, ExprType: Debug {
111109

112110
/// parse the prefix and stop once an infix operator is reached
113-
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Box<SQLExpr<ExprType>>, ParserError<TokenType>> ;
111+
fn parse_prefix(&mut self, chars: &mut CharSeq) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>> ;
114112
/// parse the next infix expression, returning None if the precedence has changed
115113
fn parse_infix(&mut self, chars: &mut CharSeq, left: &SQLExpr<ExprType>, precedence: usize) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>>;
116114
}

0 commit comments

Comments
 (0)