Skip to content

Commit

Permalink
fix: Removed regularizer
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Jul 28, 2024
1 parent 98d8057 commit 1a6c637
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 282 deletions.
24 changes: 0 additions & 24 deletions lykiadb-lang/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use self::program::Program;

use super::ast::expr::{Expr, Operation};
use super::ast::stmt::Stmt;
use crate::ast::AstNode;
use crate::tokenizer::token::{
Keyword::*, SqlKeyword, SqlKeyword::*, Symbol::*, Token, TokenType, TokenType::*,
};
Expand Down Expand Up @@ -44,8 +43,6 @@ macro_rules! binary {
id: $self.get_expr_id(),
};

$self.register_expr_node(&expr, vec![token.clone()]);

current_expr = Box::new(expr);
}
return Ok(current_expr);
Expand All @@ -60,20 +57,13 @@ macro_rules! match_next {
}
};
}

#[derive(Clone)]
pub struct NodeMetadata {
pub tokens: Vec<Token>,
}

pub struct Parser<'a> {
tokens: &'a Vec<Token>,
current: usize,
expr_id: usize,
in_select_depth: usize,
in_array_depth: usize,
in_object_depth: usize,
node_metadata: FxHashMap<usize, NodeMetadata>,
}

impl<'a> Parser<'a> {
Expand All @@ -85,7 +75,6 @@ impl<'a> Parser<'a> {
in_array_depth: 0,
in_object_depth: 0,
expr_id: 0,
node_metadata: FxHashMap::default(),
}
}

Check warning on line 79 in lykiadb-lang/src/parser/mod.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-lang/src/parser/mod.rs#L70-L79

Added lines #L70 - L79 were not covered by tests

Expand All @@ -100,27 +89,17 @@ impl<'a> Parser<'a> {
in_array_depth: 0,
in_object_depth: 0,
expr_id: 0,
node_metadata: FxHashMap::default(),
};
let program = parser.program()?;
Ok(Program::new(program))
}

fn register_expr_node(&mut self, node: &Expr, tokens: Vec<Token>) {
self.node_metadata
.insert(node.get_id(), NodeMetadata { tokens });
}

fn get_expr_id(&mut self) -> usize {
let id = self.expr_id;
self.expr_id += 1;
id
}

pub fn get_metadata(&mut self) -> FxHashMap<usize, NodeMetadata> {
self.node_metadata.clone()
}

pub fn program(&mut self) -> ParseResult<Box<Stmt>> {
let mut statements: Vec<Stmt> = Vec::new();
while !self.is_at_end() {
Expand Down Expand Up @@ -335,7 +314,6 @@ impl<'a> Parser<'a> {
span: var_tok.span,
id: self.get_expr_id(),
};
self.register_expr_node(&node, vec![var_tok.clone()]);
Box::new(node)
}
};
Expand Down Expand Up @@ -404,8 +382,6 @@ impl<'a> Parser<'a> {
id: self.get_expr_id(),
};

self.register_expr_node(&node, tokens);

Ok(Box::new(node))
}

Expand Down
260 changes: 2 additions & 258 deletions lykiadb-playground/src/regularizer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use lykiadb_lang::{
ast::{expr::Expr, stmt::Stmt, visitor::VisitorMut}, parser::NodeMetadata, tokenizer::token::{Token, TokenType}, Literal, Span
};
use rustc_hash::FxHashMap;
use lykiadb_lang::{tokenizer::token::{Token, TokenType}, Span};
use serde::{Deserialize, Serialize};

#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]

Check warning on line 4 in lykiadb-playground/src/regularizer.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-playground/src/regularizer.rs#L4

Added line #L4 was not covered by tests
Expand All @@ -11,18 +8,9 @@ pub struct Tree {
pub span: Span,
}

pub struct TreeBuilder {
node_metadata: FxHashMap<usize, NodeMetadata>,
}
pub struct TreeBuilder;

impl TreeBuilder {
pub fn new(node_metadata: FxHashMap<usize, NodeMetadata>) -> Self {
Self { node_metadata }
}
pub fn build(&mut self, stmt: &Stmt) -> Tree {
self.visit_stmt(stmt).unwrap()
}

pub fn token_to_tree(token: Token) -> Tree {
match token.tok_type {
TokenType::Identifier { .. } => Tree {
Expand Down Expand Up @@ -77,248 +65,4 @@ impl TreeBuilder {
}

Check warning on line 65 in lykiadb-playground/src/regularizer.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-playground/src/regularizer.rs#L14-L65

Added lines #L14 - L65 were not covered by tests
}
}

Check warning on line 67 in lykiadb-playground/src/regularizer.rs

View check run for this annotation

Codecov / codecov/patch

lykiadb-playground/src/regularizer.rs#L67

Added line #L67 was not covered by tests

pub fn get_children(&self, id: usize) -> Option<Vec<Tree>> {
if self.node_metadata.contains_key(&id) {
let children: Vec<Tree> = self.node_metadata[&id]
.tokens
.iter()
.map(|t| Self::token_to_tree(t.clone()))
.collect();
return Some(children);
}
return None;
}

pub fn get_subtree(&self, name: &str, span: &Span, id: usize, mut children: Vec<Tree>) -> Tree {
let token_children = self.get_children(id);
if let Some(c) = token_children {
children.extend(c);
}
Tree {
name: name.to_owned(),
children: Some(children),
span: *span,
}
}
}

impl<'a> VisitorMut<Tree, ()> for TreeBuilder {
fn visit_expr(&mut self, e: &Expr) -> Result<Tree, ()> {
let tree = match e {
Expr::Literal {
raw: _,
span,
value,
id,
} => {
let mut children: Vec<Tree> = vec![];
self.get_children(*id).map(|c| children.extend(c));
Tree {
name: match value {
Literal::Str(_) => "String".to_string(),
Literal::Num(_) => "Number".to_string(),
Literal::Bool(_) => "Boolean".to_string(),
Literal::Array(exprs) => {
for expr in exprs {
children.push(self.visit_expr(expr)?);
}
"Array".to_string()
}
Literal::Object(exprs) => {
for (key, value) in exprs {
// children.push(self.visit_expr(key)?);
children.push(self.visit_expr(value)?);
}
"Object".to_string()
}
Literal::Null => "Null".to_string(),
Literal::Undefined => "Undefined".to_string(),
Literal::NaN => "NaN".to_string(),
},
children: Some(children),
span: *span,
}
}
Expr::Variable { name, span, id } => {
self.get_subtree("Variable", span, *id, vec![Tree {
name: "Identifier".to_string(),
children: None,
span: name.span,
}])
}
Expr::Assignment {
dst,
expr,
span,
id,
} => {
let children = vec![Tree {
name: "Identifier".to_string(),
children: None,
span: dst.span,
}, self.visit_expr(expr)?];
self.get_subtree("Assignment", span, *id, children)
}
Expr::Logical {
left,
operation,
right,
span,
id,
} => {
let children = vec![
self.visit_expr(left)?,
self.visit_expr(right)?
];
self.get_subtree(&format!("Logical/{:?}", operation), span, *id, children)
}
Expr::Call {
callee,
args,
span,
id,
} => {
let mut children = vec![self.visit_expr(callee)?];
for arg in args {
children.push(self.visit_expr(arg)?);
}
self.get_subtree("Call", span, *id, children)
}
Expr::Get {
object,
name,
span,
id,
} => {
let children = vec![Tree {
name: "Identifier".to_string(),
children: None,
span: name.span,
}, self.visit_expr(object)?];
self.get_subtree("Get", span, *id, children)
}
Expr::Set {
object,
name,
value,
span,
id,
} => {
let children = vec![Tree {
name: "Identifier".to_string(),
children: None,
span: name.span,
}, self.visit_expr(object)?, self.visit_expr(value)?];
self.get_subtree("Set", span, *id, children)
}
Expr::Grouping { expr, span, id } => {
let children = vec![self.visit_expr(expr)?];
self.get_subtree("Grouping", span, *id, children)
}
Expr::Function {
name,
parameters,
body,
span,
id,
} => {
let mut children = vec![];

if let Some(n) = name {
children.push(Tree {
name: "Identifier".to_string(),
children: None,
span: n.span,
});
}

for param in parameters {
children.push(Tree {
name: "Identifier".to_string(),
children: None,
span: param.span,
});
}

self.get_subtree("Function", span, *id, children)
}
Expr::Binary {
left,
operation,
right,
span,
id,
} => {
let children = vec![
self.visit_expr(left)?,
self.visit_expr(right)?
];
self.get_subtree(&format!("Binary/{:?}", operation), span, *id, children)
}
Expr::Unary {
operation,
expr,
span,
id,
} => {
let children = vec![self.visit_expr(expr)?];
self.get_subtree(&format!("Unary/{:?}", operation), span, *id, children)
}
Expr::Insert { command, span, id } => Tree {
name: format!("{:?}", command),
children: self.get_children(*id),
span: *span,
},
Expr::Delete { command, span, id } => Tree {
name: format!("{:?}", command),

children: self.get_children(*id),
span: *span,
},
Expr::Update { command, span, id } => Tree {
name: format!("{:?}", command),

children: self.get_children(*id),
span: *span,
},
Expr::Select { query, span, id } => Tree {
name: "Select".to_string(),
children: self.get_children(*id),
span: *span,
},
};
Ok(tree)
}

fn visit_stmt(&mut self, s: &Stmt) -> Result<Tree, ()> {
let tree = match s {
Stmt::Program { body: stmts, span } => {
let mut children = vec![];
for stmt in stmts {
children.push(self.visit_stmt(stmt)?);
}
Tree {
name: "Program".to_string(),
children: Some(children),
span: *span,
}
}
Stmt::Expression { expr, span } => {
let mut children = vec![];
children.push(self.visit_expr(expr)?);
Tree {
name: "Expression".to_string(),
children: Some(children),
span: *span,
}
}
_ => Tree {
name: "Unknown".to_string(),
children: None,
span: Span::default(),
},
};
Ok(tree)
}
}

0 comments on commit 1a6c637

Please sign in to comment.