Skip to content

Commit

Permalink
fix: TokenType removed from AST
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 22, 2023
1 parent c8a3efd commit 5b42b3b
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 123 deletions.
58 changes: 51 additions & 7 deletions server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
use std::rc::Rc;

use serde::{Deserialize, Serialize};

use super::{sql::SqlSelect, stmt::StmtId, Literal};
use crate::lang::token::{Span, Spanned, Token, TokenType};
use crate::lang::token::{Keyword, Span, Spanned, Symbol, Token, TokenType};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum Operation {
Add,
Subtract,
Multiply,
Divide,
Equal,
NotEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
And,
Or,
Not,
}

pub fn tok_type_to_op(tok_t: TokenType) -> Operation {
match tok_t {
TokenType::Symbol(sym) => match sym {
Symbol::Plus => Operation::Add,
Symbol::Minus => Operation::Subtract,
Symbol::Star => Operation::Multiply,
Symbol::Slash => Operation::Divide,
Symbol::EqualEqual => Operation::Equal,
Symbol::BangEqual => Operation::NotEqual,
Symbol::Greater => Operation::Greater,
Symbol::GreaterEqual => Operation::GreaterEqual,
Symbol::Less => Operation::Less,
Symbol::LessEqual => Operation::LessEqual,
Symbol::Bang => Operation::Not,
_ => unreachable!(),
},
TokenType::Keyword(kw) => match kw {
Keyword::And => Operation::And,
Keyword::Or => Operation::Or,
_ => unreachable!(),
},
_ => unreachable!(),
}
}

#[derive(Debug, Eq, PartialEq)]
pub enum Expr {
Expand Down Expand Up @@ -30,12 +74,12 @@ pub enum Expr {
},
Binary {
left: ExprId,
operator: TokenType,
operation: Operation,
right: ExprId,
span: Span,
},
Unary {
operator: TokenType,
operation: Operation,
expr: ExprId,
span: Span,
},
Expand All @@ -46,7 +90,7 @@ pub enum Expr {
},
Logical {
left: ExprId,
operator: TokenType,
operation: Operation,
right: ExprId,
span: Span,
},
Expand Down Expand Up @@ -87,12 +131,12 @@ impl Spanned for Expr {
}
| Expr::Binary {
left: _,
operator: _,
operation: _,
right: _,
span,
}
| Expr::Unary {
operator: _,
operation: _,
expr: _,
span,
}
Expand All @@ -103,7 +147,7 @@ impl Spanned for Expr {
}
| Expr::Logical {
left: _,
operator: _,
operation: _,
right: _,
span,
}
Expand Down
10 changes: 5 additions & 5 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::ast::expr::{Expr, ExprId};
use super::ast::expr::{tok_type_to_op, Expr, ExprId};
use super::ast::sql::{
SqlCollectionSubquery, SqlCompoundOperator, SqlDistinct, SqlExpr, SqlJoin, SqlJoinType,
SqlLimitClause, SqlOrderByClause, SqlOrdering, SqlProjection, SqlSelect, SqlSelectCompound,
Expand Down Expand Up @@ -50,7 +50,7 @@ macro_rules! binary {

current_expr = $self.arena.expression(Expr::Binary {
left,
operator: token.tok_type,
operation: tok_type_to_op(token.tok_type),
right,
span: $self.get_merged_span(
$self.arena.get_expression(left),
Expand Down Expand Up @@ -409,7 +409,7 @@ impl<'a> Parser<'a> {
let right = self.and()?;
return Ok(self.arena.expression(Expr::Logical {
left: expr,
operator: op.tok_type.clone(),
operation: tok_type_to_op(op.tok_type.clone()),
right,
span: self.get_merged_span(
self.arena.get_expression(expr),
Expand All @@ -427,7 +427,7 @@ impl<'a> Parser<'a> {
let right = self.equality()?;
return Ok(self.arena.expression(Expr::Logical {
left: expr,
operator: op.tok_type.clone(),
operation: tok_type_to_op(op.tok_type.clone()),
right,
span: self.get_merged_span(
self.arena.get_expression(expr),
Expand Down Expand Up @@ -468,7 +468,7 @@ impl<'a> Parser<'a> {
let token = (*self.peek_bw(1)).clone();
let unary = self.unary()?;
return Ok(self.arena.expression(Expr::Unary {
operator: token.tok_type,
operation: tok_type_to_op(token.tok_type),
expr: unary,
span: self
.get_merged_span(&token.span, &self.arena.get_expression(unary).get_span()),
Expand Down
14 changes: 7 additions & 7 deletions server/src/lang/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a> ImmutableVisitor<Value, ()> for ProgramSerializer<'a> {
.map(|x| {
json!({
"core": self.visit_sql_select_core(&x.core),
"operator": format!("{:?}", x.operator),
"operation": format!("{:?}", x.operator),
})
})
.collect();
Expand Down Expand Up @@ -200,26 +200,26 @@ impl<'a> ImmutableVisitor<Value, ()> for ProgramSerializer<'a> {
})
}
Expr::Unary {
operator,
operation,
expr,
span: _,
} => {
json!({
"type": "Expr::Unary",
"operator": operator,
"operation": operation,
"expr": self.visit_expr(*expr)?,
})
}
Expr::Binary {
operator,
operation,
left,
right,
span: _,
} => {
json!({
"type": "Expr::Binary",
"left": self.visit_expr(*left)?,
"operator": operator,
"operation": operation,
"right": self.visit_expr(*right)?,
})
}
Expand All @@ -238,14 +238,14 @@ impl<'a> ImmutableVisitor<Value, ()> for ProgramSerializer<'a> {
}
Expr::Logical {
left,
operator,
operation,
right,
span: _,
} => {
json!({
"type": "Expr::Logical",
"left": self.visit_expr(*left)?,
"operator": operator,
"operation": operation,
"right": self.visit_expr(*right)?,
})
}
Expand Down
4 changes: 1 addition & 3 deletions server/src/lang/tests/generic/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ assert_parsing! {
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Binary",
"operation": "Add",
"left": {
"type": "Expr::Literal",
"value": "Num(1.0)",
"raw": "1"
},
"operator": {
"Symbol": "Plus"
},
"right": {
"type": "Expr::Literal",
"value": "Num(2.0)",
Expand Down
16 changes: 4 additions & 12 deletions server/src/lang/tests/generic/grouping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,44 @@ assert_parsing! {
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Binary",
"operation": "Multiply",
"left": {
"type": "Expr::Grouping",
"expr": {
"type": "Expr::Binary",
"operation": "Add",
"left": {
"raw": "1",
"type": "Expr::Literal",
"value": "Num(1.0)"
},
"operator": {
"Symbol": "Plus"
},
"right": {
"raw": "2",
"type": "Expr::Literal",
"value": "Num(2.0)"
}
}
},
"operator": {
"Symbol": "Star"
},
"right": {
"type": "Expr::Grouping",
"expr": {
"type": "Expr::Binary",
"operation": "Divide",
"left": {
"raw": "3",
"type": "Expr::Literal",
"value": "Num(3.0)"
},
"operator": {
"Symbol": "Slash"
},
"right": {
"type": "Expr::Grouping",
"expr": {
"type": "Expr::Binary",
"operation": "Subtract",
"left": {
"raw": "4",
"type": "Expr::Literal",
"value": "Num(4.0)"
},
"operator": {
"Symbol": "Minus"
},
"right": {
"raw": "7",
"type": "Expr::Literal",
Expand Down
4 changes: 1 addition & 3 deletions server/src/lang/tests/generic/unary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ assert_parsing! {
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Unary",
"operator": {
"Symbol": "Minus"
},
"operation": "Subtract",
"expr": {
"type": "Expr::Literal",
"value": "Num(1.0)",
Expand Down
55 changes: 55 additions & 0 deletions server/src/lang/tests/sql/select_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,60 @@ assert_parsing! {
}
]
}
},
constraint: {
"SELECT * from users inner join orders on users.id = orders.user_id inner join order_items on orders.id = carts.order_id;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Select",
"value": {
"core": {
"from": {
"type": "Join",
"subquery": {
"type": "Group",
"subqueries": [{
"type": "Collection",
"alias": null,
"name": "users",
"namespace": null,
}]
},
"joins": [{
"type": "Inner",
"subquery": {
"type": "Collection",
"alias": null,
"name": "orders",
"namespace": null,
},
"constraint": null
},{
"type": "Inner",
"subquery": {
"type": "Collection",
"alias": null,
"name": "order_items",
"namespace": null,
},
"constraint": null
}]
},
"projection": [{
"type": "All",
"collection": null
}]
},
"compound": [],
"limit": null,
"order_by": null
}
}
}
]
}
}
}
16 changes: 4 additions & 12 deletions server/src/lang/tests/sql/select_projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ assert_parsing! {
"value": "Num(5.0)",
"raw": "5"
},
"operator": {
"Symbol": "Plus"
},
"operation": "Add",
"right": {
"type": "Expr::Literal",
"value": "Num(27.0)",
Expand All @@ -220,9 +218,7 @@ assert_parsing! {
"value": "Num(4.0)",
"raw": "4"
},
"operator": {
"Symbol": "Slash"
},
"operation": "Divide",
"right": {
"type": "Expr::Literal",
"value": "Num(2.0)",
Expand Down Expand Up @@ -261,9 +257,7 @@ assert_parsing! {
"value": "Num(5.0)",
"raw": "5"
},
"operator": {
"Symbol": "Plus"
},
"operation": "Add",
"right": {
"type": "Expr::Literal",
"value": "Num(27.0)",
Expand All @@ -281,9 +275,7 @@ assert_parsing! {
"value": "Num(4.0)",
"raw": "4"
},
"operator": {
"Symbol": "Slash"
},
"operation": "Divide",
"right": {
"type": "Expr::Literal",
"value": "Num(2.0)",
Expand Down
Loading

0 comments on commit 5b42b3b

Please sign in to comment.