Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
chumsky = "0.10.1"
logos = "0.15.0"
rowan = "0.16.1"
thiserror = "2.0.12"
26 changes: 21 additions & 5 deletions parser/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub enum SyntaxKind {
GROUP_BY,

CREATE,
ALTER,
DROP,
TRUNCATE,

TABLE,

Expand All @@ -72,31 +75,44 @@ pub enum SyntaxKind {
PARENTHESES_END,
VALUES,
DEFINITION,

EQUAL,
GT,
LT,
LEQT,
GEQT,

AND,
OR,
NOT,

TRUE,
FALSE,
BOOLEAN_OP,

GROUP,
ORDER,
BY,
DESC,
ASC,
DISTINCT,

COMPARE,
GREATER,
LESS,
COMPARISON,
EMPTY,
ROOT,
}

impl SyntaxKind {
pub fn is_dql(&self) -> bool {
(2..=3).contains(&(*self as u16))
//(2..=3).contains(&(*self as u16))
*self == SELECT
}
pub fn is_ddl(&self) -> bool {
(4..=4).contains(&(*self as u16))
//(4..=4).contains(&(*self as u16))
match *self {
CREATE | ALTER | DROP | TRUNCATE => true,
_ => false,
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions parser/src/parser/ast/ast_trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::parser::ast::ast::SyntaxKind;
use crate::parser::ast::errors::AstErr;
use crate::parser::lexer::Token;
use std::fmt::Display;

pub trait AstNode: Display + Eq {
fn add_child<A>(&mut self, child: &A) -> Result<(), AstErr>
where
A: AstNode;
fn is_compatible<A>(&self, child: &A) -> Result<(), AstErr>
where
A: AstNode;
fn get_parent<A>(&self) -> Option<A>
where
A: AstNode;
fn get_kind<A>(&self) -> SyntaxKind;
}

pub trait AstToken: AstNode {
fn get_token(&self) -> Token;
}

pub trait AstNodeContainer: AstNode {
fn get_children<A>(&self) -> Vec<Box<A>>
where
A: AstNode;
}

pub trait Ast<N>: Iterator
where
N: AstNode,
{
fn diff(&self, other: &Self) -> Self;
fn as_node(&self) -> N;
fn from(root: &N) -> Self;
}
4 changes: 4 additions & 0 deletions parser/src/parser/ast/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum AstErr {}
5 changes: 5 additions & 0 deletions parser/src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod ast;
pub mod ast_trait;
pub mod errors;
pub mod nodes;
pub mod operators;
12 changes: 12 additions & 0 deletions parser/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub enum Token {
#[regex("(?i)OR")]
Or,

#[regex("(?i)NOT")]
Not,

#[regex("(?i)GROUP")]
Group,

Expand Down Expand Up @@ -66,6 +69,12 @@ pub enum Token {
#[token("=")]
Equal,

#[token(">=")]
GEQT,

#[token("<=")]
LEQT,

#[token(">")]
GT,

Expand Down Expand Up @@ -104,8 +113,11 @@ impl Token {
Token::Where => (SyntaxKind::WHERE, String::from("WHERE")),
Token::LT => (SyntaxKind::LT, String::from("<")),
Token::GT => (SyntaxKind::GT, String::from(">")),
Token::LEQT => (SyntaxKind::LT, String::from("<=")),
Token::GEQT => (SyntaxKind::GT, String::from(">=")),
Token::And => (SyntaxKind::AND, String::from("AND")),
Token::Or => (SyntaxKind::OR, String::from("OR")),
Token::Not => (SyntaxKind::NOT, String::from("NOT")),
Token::Group => (SyntaxKind::GROUP, String::from("GROUP")),
Token::By => (SyntaxKind::BY, String::from("BY")),
Token::Desc => (SyntaxKind::DESC, String::from("DESC")),
Expand Down
4 changes: 1 addition & 3 deletions parser/src/parser/sql/group_by.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::parser::ast::SyntaxKind::*;
use crate::parser::grammar::Grammar::{self, *};
use crate::parser::grammar::GrammarType::*;

pub const GROUP_BY_GRAMMAR: Grammar =
Children(&[GROUP, BY], GROUP_BY, &[List(&[IDENTIFIER])]);
pub const GROUP_BY_GRAMMAR: Grammar = Children(&[GROUP, BY], GROUP_BY, &[List(&[IDENTIFIER])]);