Skip to content

Commit

Permalink
fix: Serialization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jan 6, 2024
1 parent 914cd67 commit 957615c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 546 deletions.
16 changes: 12 additions & 4 deletions server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::rc::Rc;

use id_arena::Id;
use serde::{Deserialize, Serialize};
use serde::Serialize;

use super::{sql::{SqlSelect, SqlInsert, SqlDelete, SqlUpdate}, stmt::StmtId, Literal};
use crate::lang::token::{Span, Spanned, Token};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
pub enum Operation {
Add,
Subtract,
Expand All @@ -23,7 +23,7 @@ pub enum Operation {
Not,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum Expr {
Select {
query: SqlSelect,
Expand Down Expand Up @@ -162,4 +162,12 @@ impl Spanned for Expr {
}
}

pub type ExprId = Id<Expr>;
#[repr(transparent)]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct ExprId(pub Id<Expr>);

impl Serialize for ExprId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.index().serialize(serializer)
}
}
13 changes: 7 additions & 6 deletions server/src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::rc::Rc;
use serde::Serialize;

use id_arena::Arena;
use rustc_hash::FxHashMap;
Expand All @@ -12,7 +13,7 @@ pub mod expr;
pub mod sql;
pub mod stmt;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum Literal {
Str(Rc<String>),
Num(f64),
Expand Down Expand Up @@ -73,24 +74,24 @@ pub struct ParserArena {
impl ParserArena {
pub fn new() -> ParserArena {
ParserArena {
expressions: Arena::<Expr>::new(),
expressions:Arena::<Expr>::new(),
statements: Arena::<Stmt>::new(),
}
}

pub fn expression(&mut self, expr: Expr) -> ExprId {
self.expressions.alloc(expr)
ExprId(self.expressions.alloc(expr))
}

pub fn statement(&mut self, stmt: Stmt) -> StmtId {
self.statements.alloc(stmt)
StmtId(self.statements.alloc(stmt))
}

pub fn get_expression(&self, idx: ExprId) -> &Expr {
&self.expressions[idx]
&self.expressions[idx.0]
}

pub fn get_statement(&self, idx: StmtId) -> &Stmt {
&self.statements[idx]
&self.statements[idx.0]
}
}
37 changes: 19 additions & 18 deletions server/src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
use crate::lang::token::Token;
use serde::Serialize;

use super::expr::ExprId;

// Enums

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlDistinct {
ImplicitAll,
All,
Distinct,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlJoinType {
Left,
LeftOuter,
Right,
Inner,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlCompoundOperator {
Union,
UnionAll,
Intersect,
Except,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlOrdering {
Asc,
Desc,
}

//
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlCollectionIdentifier {
pub namespace: Option<Token>,
pub name: Token,
pub alias: Option<Token>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlExpr {
Default(ExprId),
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlProjection {
All { collection: Option<Token> },
Expr { expr: SqlExpr, alias: Option<Token> },
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlLimitClause {
pub count: SqlExpr,
pub offset: Option<SqlExpr>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlOrderByClause {
pub expr: SqlExpr,
pub ordering: SqlOrdering,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlSelectCompound {
pub operator: SqlCompoundOperator,
pub core: SqlSelectCore,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlJoin {
pub join_type: SqlJoinType,
pub subquery: SqlCollectionSubquery,
pub join_constraint: Option<SqlExpr>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlCollectionSubquery {
Group(Vec<SqlCollectionSubquery>),
Join(Box<SqlCollectionSubquery>, Vec<SqlJoin>),
Expand All @@ -88,7 +89,7 @@ pub enum SqlCollectionSubquery {
},
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlSelectCore {
pub distinct: SqlDistinct,
pub projection: Vec<SqlProjection>,
Expand All @@ -98,34 +99,34 @@ pub struct SqlSelectCore {
pub having: Option<SqlExpr>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlSelect {
pub core: SqlSelectCore,
pub compound: Vec<SqlSelectCompound>,
pub order_by: Option<Vec<SqlOrderByClause>>,
pub limit: Option<SqlLimitClause>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub enum SqlValues {
Values(Vec<SqlExpr>),
Select(SqlSelect)
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlInsert {
pub collection: SqlCollectionIdentifier,
pub values: SqlValues,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlUpdate {
pub collection: SqlCollectionIdentifier,
pub assignments: Vec<SqlExpr>,
pub r#where: Option<SqlExpr>,
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct SqlDelete {
pub collection: SqlCollectionIdentifier,
pub r#where: Option<SqlExpr>,
Expand Down
12 changes: 11 additions & 1 deletion server/src/lang/ast/stmt.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::Serialize;

use id_arena::Id;

use crate::lang::token::{Span, Spanned, Token};
Expand Down Expand Up @@ -63,4 +65,12 @@ impl Spanned for Stmt {
}
}

pub type StmtId = Id<Stmt>;
#[repr(transparent)]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct StmtId(pub Id<Stmt>);

impl Serialize for StmtId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.index().serialize(serializer)
}
}
Loading

0 comments on commit 957615c

Please sign in to comment.