Skip to content

Commit

Permalink
feat: New test format
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 21, 2023
1 parent 24abfd3 commit f55f4b6
Show file tree
Hide file tree
Showing 21 changed files with 371 additions and 225 deletions.
4 changes: 4 additions & 0 deletions server/src/lang/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_hash::FxHashMap;

use self::{
expr::{Expr, ExprId},
sql::SqlSelect,
stmt::{Stmt, StmtId},
};
pub mod expr;
Expand All @@ -25,11 +26,14 @@ pub enum Literal {
impl Eq for Literal {}

pub trait Visitor<T, Q> {
// TODO(vck): Implement visit_select
// fn visit_select(&mut self, e: SqlSelect) -> Result<T, Q>;
fn visit_expr(&mut self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&mut self, e: StmtId) -> Result<T, Q>;
}

pub trait ImmutableVisitor<T, Q> {
fn visit_select(&self, e: &SqlSelect) -> Result<T, Q>;
fn visit_expr(&self, e: ExprId) -> Result<T, Q>;
fn visit_stmt(&self, e: StmtId) -> Result<T, Q>;
}
Expand Down
6 changes: 3 additions & 3 deletions server/src/lang/ast/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum SqlCollectionSubquery {
}

#[derive(Debug, Eq, PartialEq)]
pub struct SelectCore {
pub struct SqlSelectCore {
pub distinct: SqlDistinct,
pub projection: Vec<SqlProjection>,
pub from: Option<SqlCollectionSubquery>,
Expand All @@ -79,8 +79,8 @@ pub struct SelectCore {

#[derive(Debug, Eq, PartialEq)]
pub struct SqlSelect {
pub core: SelectCore,
pub compound: Vec<(SqlCompoundOperator, SelectCore)>,
pub core: SqlSelectCore,
pub compound: Vec<(SqlCompoundOperator, SqlSelectCore)>,
pub order_by: Option<Vec<(SqlExpr, SqlOrdering)>>,
pub limit: Option<(SqlExpr, Option<SqlExpr>)>,
}
24 changes: 5 additions & 19 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::ast::expr::{Expr, ExprId};
use super::ast::sql::{
SelectCore, SqlCollectionSubquery, SqlCompoundOperator, SqlDistinct, SqlExpr, SqlJoin,
SqlJoinType, SqlOrdering, SqlProjection, SqlSelect,
SqlCollectionSubquery, SqlCompoundOperator, SqlDistinct, SqlExpr, SqlJoin, SqlJoinType,
SqlOrdering, SqlProjection, SqlSelect, SqlSelectCore,
};
use super::ast::stmt::{Stmt, StmtId};
use super::ast::{Literal, ParserArena};
Expand Down Expand Up @@ -481,7 +481,7 @@ impl<'a> Parser<'a> {
return self.call();
}
let core = self.sql_select_core()?;
let mut compounds: Vec<(SqlCompoundOperator, SelectCore)> = vec![];
let mut compounds: Vec<(SqlCompoundOperator, SqlSelectCore)> = vec![];
while self.match_next_one_of(&[skw!(Union), skw!(Intersect), skw!(Except)]) {
let op = self.peek_bw(1);
let compound_op = if op.tok_type == skw!(Union) && self.match_next(skw!(All)) {
Expand Down Expand Up @@ -551,7 +551,7 @@ impl<'a> Parser<'a> {
}))
}

fn sql_select_core(&mut self) -> ParseResult<SelectCore> {
fn sql_select_core(&mut self) -> ParseResult<SqlSelectCore> {
self.expected(skw!(Select))?;
let distinct = if self.match_next(skw!(Distinct)) {
SqlDistinct::Distinct
Expand All @@ -571,7 +571,7 @@ impl<'a> Parser<'a> {
None
};

Ok(SelectCore {
Ok(SqlSelectCore {
distinct,
projection,
from,
Expand Down Expand Up @@ -924,17 +924,3 @@ impl<'a> Parser<'a> {
left_span.merge(right_span)
}
}

#[cfg(test)]
mod test {
use crate::parse_tests;

parse_tests!(
generic / binary,
unary,
grouping,
number_literal,
variable,
function_call
);
}
64 changes: 62 additions & 2 deletions server/src/lang/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use serde_json::{json, Value};
use super::{
ast::{
expr::{Expr, ExprId},
sql::{SqlExpr, SqlSelect},
stmt::{Stmt, StmtId},
ImmutableVisitor,
},
parser::Program,
};
use std::rc::Rc;
use std::{borrow::BorrowMut, rc::Rc};

impl Program {
pub fn to_json(&self) -> Value {
Expand All @@ -26,14 +27,73 @@ impl ToString for Program {
}

impl ImmutableVisitor<Value, ()> for Program {
fn visit_select(&self, select: &SqlSelect) -> Result<Value, ()> {
let order_by: Value = select
.order_by
.as_ref()
.map(|x| {
x.iter()
.map(|order| {
let expr = if let SqlExpr::Default(eidx) = order.0 {
self.visit_expr(eidx).unwrap()
} else {
panic!("Not implemented");
};
let val = json!({
"expr": expr,
"ordering": format!("{:?}", order.1),
});
val
})
.collect()
})
.unwrap();

let limit: Option<Value> = select.limit.as_ref().map(|x| {
let limit_part = if let SqlExpr::Default(eidx) = x.0 {
self.visit_expr(eidx).unwrap()
} else {
panic!("Not implemented");
};

let offset_part = if x.1.is_some() {
if let SqlExpr::Default(eidx) = x.1.as_ref().unwrap() {
self.visit_expr(*eidx).unwrap()
} else {
panic!("Not implemented");
}
} else {
json!("None")
};

json!({
"limit_part": limit_part,
"offset_part": offset_part
})
});
/*
{
pub core: SqlSelectCore,
pub compound: Vec<(SqlCompoundOperator, SqlSelectCore)>,
pub order_by: Option<Vec<(SqlExpr, SqlOrdering)>>,
pub limit: Option<(SqlExpr, Option<SqlExpr>)>,
}
*/
Ok(json!({
"order_by": order_by,
"limit": limit
}))
}

fn visit_expr(&self, eidx: ExprId) -> Result<Value, ()> {
// TODO: Remove clone here
let a = Rc::clone(&self.arena);
let e = a.get_expression(eidx);

let matched: Value = match e {
Expr::Select { span: _, query: _ } => json!({
Expr::Select { span: _, query } => json!({
"type": "Expr::Select",
"value": self.visit_select(query).unwrap(),
// TODO(vck): Implement rest of the select
}),
Expr::Literal {
Expand Down
27 changes: 0 additions & 27 deletions server/src/lang/tests/generic/binary.json

This file was deleted.

38 changes: 38 additions & 0 deletions server/src/lang/tests/generic/binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[cfg(test)]
use crate::lang::tests::helpers::compare_parsed_to_expected;

#[cfg(test)]
use serde_json::json;

#[cfg(test)]
use crate::assert_parsing;

#[cfg(test)]
assert_parsing! {
one_plus_two: {
"1 + 2;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Binary",
"left": {
"type": "Expr::Literal",
"value": "Num(1.0)",
"raw": "1"
},
"operator": {
"Symbol": "Plus"
},
"right": {
"type": "Expr::Literal",
"value": "Num(2.0)",
"raw": "2"
}
}
}
]
}
}
}
25 changes: 0 additions & 25 deletions server/src/lang/tests/generic/function_call.json

This file was deleted.

36 changes: 36 additions & 0 deletions server/src/lang/tests/generic/function_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[cfg(test)]
use crate::lang::tests::helpers::compare_parsed_to_expected;

#[cfg(test)]
use serde_json::json;

#[cfg(test)]
use crate::assert_parsing;

#[cfg(test)]
assert_parsing! {
print_50: {
"print(50);" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Call",
"callee": {
"type": "Expr::Variable",
"name": "print"
},
"args": [
{
"type": "Expr::Literal",
"value": "Num(50.0)",
"raw": "50"
}
]
}
}
]
}
}
}
69 changes: 0 additions & 69 deletions server/src/lang/tests/generic/grouping.json

This file was deleted.

Loading

0 comments on commit f55f4b6

Please sign in to comment.