feophantlib/engine/
sql_parser.rs

1//! Top Level of the sql parsing engine
2
3mod common;
4mod create;
5mod insert;
6mod select;
7
8use self::select::parse_select;
9
10use super::objects::ParseTree;
11use create::parse_create_table;
12use insert::parse_insert;
13use nom::branch::alt;
14use nom::bytes::complete::tag;
15use nom::combinator::{complete, opt};
16use nom::error::{convert_error, ContextError, ParseError, VerboseError};
17use nom::sequence::tuple;
18use nom::Finish;
19use nom::IResult;
20use thiserror::Error;
21
22pub struct SqlParser {}
23
24impl SqlParser {
25    pub fn parse(input: &str) -> Result<ParseTree, SqlParserError> {
26        match SqlParser::nom_parse::<VerboseError<&str>>(input).finish() {
27            Ok((_, cmd)) => Ok(cmd),
28            Err(e) => Err(SqlParserError::ParseError(convert_error(input, e))),
29        }
30    }
31
32    fn nom_parse<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
33        input: &'a str,
34    ) -> IResult<&'a str, ParseTree, E> {
35        //TODO Had to remove all consuming since it was throwing EOF issues
36        let (input, (result, _)) = complete(tuple((
37            alt((parse_create_table, parse_insert, parse_select)),
38            opt(tag(";")),
39        )))(input)?;
40        Ok((input, result))
41    }
42}
43
44#[derive(Debug, Error)]
45pub enum SqlParserError {
46    #[error("SQL Parse Error {0}")]
47    ParseError(String),
48    #[error("Got an incomplete on {0} which shouldn't be possible")]
49    Incomplete(String),
50}