Skip to content

Commit

Permalink
fix: Refactored Spans
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 7, 2023
1 parent dc8ea59 commit 405f721
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
50 changes: 49 additions & 1 deletion src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;

use crate::{
lang::token::{Span, Token, TokenType},
lang::token::{Span, Spanned, Token, TokenType},
runtime::types::RV,
};

Expand Down Expand Up @@ -61,6 +61,54 @@ pub enum Expr {
},
}

impl Spanned for Expr {
fn get_span(&self) -> Span {
match self {
Expr::Select { query: _, span }
| Expr::Variable { name: _, span }
| Expr::Grouping { expr: _, span }
| Expr::Literal {
value: _,
raw: _,
span,
}
| Expr::Function {
name: _,
parameters: _,
body: _,
span,
}
| Expr::Binary {
left: _,
symbol: _,
right: _,
span,
}
| Expr::Unary {
symbol: _,
expr: _,
span,
}
| Expr::Assignment {
dst: _,
expr: _,
span,
}
| Expr::Logical {
left: _,
symbol: _,
right: _,
span,
}
| Expr::Call {
callee: _,
args: _,
span,
} => *span,
}
}
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ExprId(pub usize);
18 changes: 14 additions & 4 deletions src/lang/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::ast::sql::SqlTableSubquery;
use super::ast::stmt::Stmt;
use super::ast::stmt::StmtId;
use super::ast::ParserArena;
use super::token::Spanned;

pub struct Parser<'a> {
tokens: &'a Vec<Token>,
Expand Down Expand Up @@ -60,7 +61,7 @@ macro_rules! binary {
left,
symbol: token.tok_type,
right,
span: Span { start: 0, end: 0, line: 0, line_end: 0 }
span: Span::default()
});
}
return Ok(current_expr);
Expand Down Expand Up @@ -391,7 +392,10 @@ impl<'a> Parser<'a> {
left: expr,
symbol: op.tok_type.clone(),
right,
span: Span::default(),
span: self.get_span(
self.arena.get_expression(expr),
self.arena.get_expression(right),
),
}));
}
Ok(expr)
Expand Down Expand Up @@ -655,6 +659,12 @@ impl<'a> Parser<'a> {
}
false
}

fn get_span(&self, left: &impl Spanned, right: &impl Spanned) -> Span {
let left_span = &left.get_span();
let right_span = &right.get_span();
left_span.merge(right_span)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -831,15 +841,15 @@ mod test {
#[test]
fn test_parse_variable_expression() {
compare_parsed_to_expected(
"a;",
"$a;",
json!({
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"value": {
"type": "Expr::Variable",
"value": "a",
"value": "$a",
}
}
]
Expand Down
6 changes: 5 additions & 1 deletion src/lang/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,12 @@ pub struct Span {
pub line_end: u32,
}

pub trait Spanned {
fn get_span(&self) -> Span;
}

impl Span {
pub fn default () -> Span {
pub fn default() -> Span {
Span {
start: 0,
end: 0,
Expand Down
6 changes: 1 addition & 5 deletions src/runtime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,7 @@ pub fn report_error(filename: &str, source: &str, error: ExecutionError) {
);
}*/
ExecutionError::Interpret(InterpretError::Other { message }) => {
print(
&message,
"",
Span::default(),
);
print(&message, "", Span::default());
}
_ => {}
}
Expand Down

0 comments on commit 405f721

Please sign in to comment.