Skip to content

Commit 375671e

Browse files
committed
Refactoring
1 parent a1696cc commit 375671e

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

examples/acme_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn main() {
107107

108108
let mut pratt_parser = PrattParser {
109109
chars: CharSeq::new(sql),
110-
parser: acme_parser
110+
parsers: vec![acme_parser, ansi_parser]
111111
};
112112

113113
let expr = pratt_parser.parse_expr().unwrap();

src/parser.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,26 @@ pub trait SQLParser<TokenType, ExprType>
116116

117117
pub struct PrattParser<TokenType, ExprType> {
118118
pub chars: CharSeq,
119-
pub parser: Arc<Mutex<SQLParser<TokenType, ExprType>>>
119+
pub parsers: Vec<Arc<Mutex<SQLParser<TokenType, ExprType>>>>
120120
}
121121

122122
impl<TokenType, ExprType> PrattParser<TokenType, ExprType> where TokenType: Debug + PartialEq, ExprType: Debug {
123123

124124
pub fn parse_expr(&mut self) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>> {
125125

126-
let mut p = self.parser.lock().unwrap();
126+
for i in 0..self.parsers.len() {
127+
let mut p = self.parsers[i].lock().unwrap();
128+
let expr = p.parse_prefix(&mut self.chars)?;
127129

128-
p.parse_prefix(&mut self.chars)
130+
// return as soon as we have a match
131+
match expr {
132+
Some(_) => return Ok(expr),
133+
_ => {}
134+
}
135+
}
136+
137+
// found no valid token
138+
Ok(None)
129139
}
130140

131141
}

0 commit comments

Comments
 (0)