Refactoring

This commit is contained in:
Andy Grove 2018-09-03 08:04:20 -06:00
parent a1696ccdb8
commit 375671e208
2 changed files with 14 additions and 4 deletions

View file

@ -107,7 +107,7 @@ fn main() {
let mut pratt_parser = PrattParser { let mut pratt_parser = PrattParser {
chars: CharSeq::new(sql), chars: CharSeq::new(sql),
parser: acme_parser parsers: vec![acme_parser, ansi_parser]
}; };
let expr = pratt_parser.parse_expr().unwrap(); let expr = pratt_parser.parse_expr().unwrap();

View file

@ -116,16 +116,26 @@ pub trait SQLParser<TokenType, ExprType>
pub struct PrattParser<TokenType, ExprType> { pub struct PrattParser<TokenType, ExprType> {
pub chars: CharSeq, pub chars: CharSeq,
pub parser: Arc<Mutex<SQLParser<TokenType, ExprType>>> pub parsers: Vec<Arc<Mutex<SQLParser<TokenType, ExprType>>>>
} }
impl<TokenType, ExprType> PrattParser<TokenType, ExprType> where TokenType: Debug + PartialEq, ExprType: Debug { impl<TokenType, ExprType> PrattParser<TokenType, ExprType> where TokenType: Debug + PartialEq, ExprType: Debug {
pub fn parse_expr(&mut self) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>> { pub fn parse_expr(&mut self) -> Result<Option<Box<SQLExpr<ExprType>>>, ParserError<TokenType>> {
let mut p = self.parser.lock().unwrap(); for i in 0..self.parsers.len() {
let mut p = self.parsers[i].lock().unwrap();
let expr = p.parse_prefix(&mut self.chars)?;
p.parse_prefix(&mut self.chars) // return as soon as we have a match
match expr {
Some(_) => return Ok(expr),
_ => {}
}
}
// found no valid token
Ok(None)
} }
} }