Skip to content

Commit

Permalink
refactor: change locaation to only range
Browse files Browse the repository at this point in the history
  • Loading branch information
yazaldefilimone committed Aug 10, 2024
1 parent e041736 commit 4e29337
Show file tree
Hide file tree
Showing 46 changed files with 1,717 additions and 5,271 deletions.
262 changes: 131 additions & 131 deletions src/ast/ast.rs

Large diffs are not rendered by default.

86 changes: 43 additions & 43 deletions src/ast/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,71 @@

use serde::{Deserialize, Serialize};

use crate::utils::location::Location;
use crate::utils::range::Range;

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Token {
pub kind: TokenKind,
pub location: Location,
pub range: Range,
}

impl Token {
pub fn new(kind: TokenKind, location: Location) -> Token {
Token { kind, location }
pub fn new(kind: TokenKind, range: Range) -> Token {
Token { kind, range }
}

pub fn new_end_of_file(location: Location) -> Token {
Token::new(TokenKind::EOF, location)
pub fn new_end_of_file(range: Range) -> Token {
Token::new(TokenKind::EOF, range)
}

pub fn new_identifier(location: Location, identifier: String) -> Token {
Token::new(TokenKind::Identifier(identifier), location)
pub fn new_identifier(range: Range, identifier: String) -> Token {
Token::new(TokenKind::Identifier(identifier), range)
}

pub fn new_number(location: Location, number: String) -> Token {
Token::new(TokenKind::Number(number), location)
pub fn new_number(range: Range, number: String) -> Token {
Token::new(TokenKind::Number(number), range)
}

pub fn new_string(location: Location, string: String) -> Token {
Token::new(TokenKind::String(string), location)
pub fn new_string(range: Range, string: String) -> Token {
Token::new(TokenKind::String(string), range)
}

pub fn new_comment(location: Location, comment: String) -> Token {
Token::new(TokenKind::Comment(comment), location)
pub fn new_comment(range: Range, comment: String) -> Token {
Token::new(TokenKind::Comment(comment), range)
}

pub fn new_block_comment(location: Location, comment: String) -> Token {
Token::new(TokenKind::BlockComment(comment), location)
pub fn new_block_comment(range: Range, comment: String) -> Token {
Token::new(TokenKind::BlockComment(comment), range)
}

pub fn new_keyword_or_identifier(location: Location, keyword: String) -> Token {
pub fn new_keyword_or_identifier(range: Range, keyword: String) -> Token {
match keyword.as_str() {
"function" => Token::new(TokenKind::Function, location),
"local" => Token::new(TokenKind::Local, location),
"if" => Token::new(TokenKind::If, location),
"then" => Token::new(TokenKind::Then, location),
"else" => Token::new(TokenKind::Else, location),
"elseif" => Token::new(TokenKind::ElseIf, location),
"end" => Token::new(TokenKind::End, location),
"while" => Token::new(TokenKind::While, location),
"do" => Token::new(TokenKind::Do, location),
"for" => Token::new(TokenKind::For, location),
"in" => Token::new(TokenKind::In, location),
"repeat" => Token::new(TokenKind::Repeat, location),
"until" => Token::new(TokenKind::Until, location),
"return" => Token::new(TokenKind::Return, location),
"break" => Token::new(TokenKind::Break, location),
"true" => Token::new(TokenKind::True, location),
"false" => Token::new(TokenKind::False, location),
"nil" => Token::new(TokenKind::Nil, location),
"type" => Token::new(TokenKind::Type, location),
"enum" => Token::new(TokenKind::Enum, location),
"continue" => Token::new(TokenKind::Continue, location),
"and" => Token::new(TokenKind::And, location),
"or" => Token::new(TokenKind::Or, location),
"not" => Token::new(TokenKind::Not, location),
"require" => Token::new(TokenKind::Require, location),
_ => Token::new(TokenKind::Identifier(keyword), location),
"function" => Token::new(TokenKind::Function, range),
"local" => Token::new(TokenKind::Local, range),
"if" => Token::new(TokenKind::If, range),
"then" => Token::new(TokenKind::Then, range),
"else" => Token::new(TokenKind::Else, range),
"elseif" => Token::new(TokenKind::ElseIf, range),
"end" => Token::new(TokenKind::End, range),
"while" => Token::new(TokenKind::While, range),
"do" => Token::new(TokenKind::Do, range),
"for" => Token::new(TokenKind::For, range),
"in" => Token::new(TokenKind::In, range),
"repeat" => Token::new(TokenKind::Repeat, range),
"until" => Token::new(TokenKind::Until, range),
"return" => Token::new(TokenKind::Return, range),
"break" => Token::new(TokenKind::Break, range),
"true" => Token::new(TokenKind::True, range),
"false" => Token::new(TokenKind::False, range),
"nil" => Token::new(TokenKind::Nil, range),
"type" => Token::new(TokenKind::Type, range),
"enum" => Token::new(TokenKind::Enum, range),
"continue" => Token::new(TokenKind::Continue, range),
"and" => Token::new(TokenKind::And, range),
"or" => Token::new(TokenKind::Or, range),
"not" => Token::new(TokenKind::Not, range),
"require" => Token::new(TokenKind::Require, range),
_ => Token::new(TokenKind::Identifier(keyword), range),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/checker/check_assign_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{ast::ast::AssignStatement, diagnostics::Diagnostic, types::Type};
impl<'a> Checker<'a> {
pub fn check_assign_statement(&mut self, assign: &AssignStatement) -> Result<Type, Diagnostic> {
let right_type = self.check_expression(&assign.value)?;
let value_location = assign.get_location();
self.declare_variables(&assign.values, right_type, false, value_location)?;
let value_range = assign.get_range();
self.declare_variables(&assign.values, right_type, false, value_range)?;
Ok(Type::Nil)
}
}
10 changes: 5 additions & 5 deletions src/checker/check_binary_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
ast::ast,
diagnostics::{Diagnostic, TypeError},
types::Type,
utils::location::Location,
utils::range::Range,
};

impl<'a> Checker<'a> {
Expand All @@ -14,19 +14,19 @@ impl<'a> Checker<'a> {
if left_t.supports_operator(&binary_expr.operator) && right_t.supports_operator(&binary_expr.operator) {
return Ok(left_t.get_operator_result_type(&right_t, &binary_expr.operator));
}
let location = binary_expr.get_location();
let range = binary_expr.get_range();

let diagnostic = TypeError::UnsupportedOperator(
left_t.to_string(),
right_t.to_string(),
binary_expr.operator.to_str().to_owned(),
Some(location),
Some(range),
);

Err(self.create_diagnostic(diagnostic))
}

pub fn check_add_expression(&mut self, left_t: Type, right_t: Type, loc: Location) -> Result<Type, Diagnostic> {
pub fn check_add_expression(&mut self, left_t: Type, right_t: Type, range: Range) -> Result<Type, Diagnostic> {
if left_t.supports_operator(&ast::BinaryOperator::Add) && right_t.supports_operator(&ast::BinaryOperator::Add) {
return Ok(Type::Number);
}
Expand All @@ -35,7 +35,7 @@ impl<'a> Checker<'a> {
left_t.to_string(),
right_t.to_string(),
ast::BinaryOperator::Add.to_str().to_owned(),
Some(loc),
Some(range),
);

Err(self.create_diagnostic(dignostic))
Expand Down
16 changes: 8 additions & 8 deletions src/checker/check_call_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a> Checker<'a> {

let (defined, scope_idx) = self.ctx.defined_in_any_scope(name);
if !defined {
let diagnostic = TypeError::UndeclaredVariable(name.to_string(), Some(call_expr.name.location.clone()));
let diagnostic = TypeError::UndeclaredVariable(name.to_string(), Some(call_expr.name.range.clone()));
return Err(self.create_diagnostic(diagnostic));
}

Expand All @@ -21,23 +21,23 @@ impl<'a> Checker<'a> {
return Ok(return_t);
}

let diagnostic = TypeError::UndeclaredVariable(name.to_string(), Some(call_expr.name.location.clone()));
let diagnostic = TypeError::UndeclaredVariable(name.to_string(), Some(call_expr.name.range.clone()));
Err(self.create_diagnostic(diagnostic))
}

pub fn check_call_arguments(&mut self, args_call: &ast::Expression, params_tt: &[Type]) -> Result<(), Diagnostic> {
if let ast::Expression::Grouped(ast::GroupedExpression { expressions, location }) = args_call {
if let ast::Expression::Grouped(ast::GroupedExpression { expressions, range }) = args_call {
if expressions.len() != params_tt.len() {
let diagnostic = TypeError::FunctionArityMismatch(params_tt.len(), expressions.len(), Some(location.clone()));
let diagnostic = TypeError::FunctionArityMismatch(params_tt.len(), expressions.len(), Some(range.clone()));
return Err(self.create_diagnostic(diagnostic));
}

for (arg_expr, param_t) in expressions.iter().zip(params_tt.iter()) {
let param_t = self.check_type(param_t.clone())?;
let arg_t = self.check_expression(arg_expr)?;
if !arg_t.check_match(&param_t) {
let location = arg_expr.get_location();
let diagnostic = TypeError::MismatchedTypes(param_t.to_string(), arg_t.to_string(), Some(location.clone()));
let range = arg_expr.get_range();
let diagnostic = TypeError::MismatchedTypes(param_t.to_string(), arg_t.to_string(), Some(range.clone()));
return Err(self.create_diagnostic(diagnostic));
}
}
Expand All @@ -47,8 +47,8 @@ impl<'a> Checker<'a> {
let arg_t = self.check_expression(args_call)?;

if params_tt.len() != 1 {
let location = args_call.get_location();
let diagnostic = TypeError::FunctionArityMismatch(params_tt.len(), 1, Some(location));
let range = args_call.get_range();
let diagnostic = TypeError::FunctionArityMismatch(params_tt.len(), 1, Some(range));
return Err(self.create_diagnostic(diagnostic));
}

Expand Down
2 changes: 1 addition & 1 deletion src/checker/check_function_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> Checker<'a> {
for (param, ty) in arguments.iter() {
let arg_type = ty.clone().unwrap_or(Type::Unknown);
self.ctx.declare_variable(param.lexeme(), arg_type.clone());
self.ctx.set_variable_location(param.lexeme(), param.location.clone());
self.ctx.set_variable_range(param.lexeme(), param.range.clone());
params.push(arg_type);
}
Ok(params)
Expand Down
2 changes: 1 addition & 1 deletion src/checker/check_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl<'a> Checker<'a> {
pub fn check_identifier(&mut self, ident: &ast::Identifier) -> Result<Type, Diagnostic> {
let (defined, scope_idx) = self.ctx.defined_in_any_scope(&ident.name);
if !defined {
let diagnostic = TypeError::UndeclaredVariable(ident.name.to_string(), Some(ident.location.clone()));
let diagnostic = TypeError::UndeclaredVariable(ident.name.to_string(), Some(ident.range.clone()));
return Err(self.create_diagnostic(diagnostic));
}
self.ctx.use_variable_in_scope(&ident.name, scope_idx);
Expand Down
4 changes: 2 additions & 2 deletions src/checker/check_if_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ impl<'a> Checker<'a> {
pub fn check_if_statement(&mut self, if_stmt: &ast::IfStatement) -> Result<Type, Diagnostic> {
let condition_type = self.check_expression(&if_stmt.condition)?;
if !condition_type.check_match(&Type::Boolean) {
let condition_location = if_stmt.condition.get_location();
return Err(self.create_type_mismatch(Type::new_boolean(), condition_type, condition_location));
let condition_range = if_stmt.condition.get_range();
return Err(self.create_type_mismatch(Type::new_boolean(), condition_type, condition_range));
}

let then_type = self.check_statement(&if_stmt.then_body)?;
Expand Down
38 changes: 16 additions & 22 deletions src/checker/check_require_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,38 @@ use crate::{
diagnostics::{Diagnostic, TypeError},
parser::parser::Parser,
types::Type,
utils::location::Location,
utils::range::Range,
};

impl<'a> Checker<'a> {
pub fn check_require_expression(&mut self, require: &ast::RequireExpression) -> Result<Type, Diagnostic> {
let module_name = require.module_name.lexeme();
self.check_module(module_name, require.location.clone())
let name = require.module_name.lexeme();
self.check_module(name, require.range.clone())
}

pub fn check_module(&mut self, module_name: &str, location: Location) -> Result<Type, Diagnostic> {
let module_path = self.resolve_module_path(module_name, &location)?;
let content = self.load_module_content(&module_path, &location)?;
let return_module_type = self.analyze_module_content(module_name, &module_path, &content, location)?;
pub fn check_module(&mut self, name: &str, range: Range) -> Result<Type, Diagnostic> {
let path = self.resolve_path(name, &range)?;
let content = self.load_module(&path, &range)?;
let return_module_type = self.analyze_module(name, &path, &content, range)?;
Ok(return_module_type)
}

fn resolve_module_path(&mut self, module_name: &str, location: &Location) -> Result<std::path::PathBuf, Diagnostic> {
self.resolver.resolve(module_name).map_err(|_| {
let diagnostic = TypeError::ModuleNotFound(module_name.to_string(), Some(location.clone()));
fn resolve_path(&mut self, name: &str, range: &Range) -> Result<std::path::PathBuf, Diagnostic> {
self.resolver.resolve(name).map_err(|_| {
let diagnostic = TypeError::ModuleNotFound(name.to_string(), Some(range.clone()));
self.create_diagnostic(diagnostic)
})
}

fn load_module_content(&mut self, module_path: &PathBuf, location: &Location) -> Result<String, Diagnostic> {
self.loader.load_module_from_path(module_path).map_err(|_| {
let diagnostic = TypeError::ModuleNotFound(module_path.to_string_lossy().to_string(), Some(location.clone()));
fn load_module(&mut self, path: &PathBuf, range: &Range) -> Result<String, Diagnostic> {
self.loader.load_module_from_path(path).map_err(|_| {
let diagnostic = TypeError::ModuleNotFound(path.to_string_lossy().to_string(), Some(range.clone()));
self.create_diagnostic(diagnostic)
})
}

fn analyze_module_content(
&mut self,
module_name: &str,
module_path: &PathBuf,
content: &str,
location: Location,
) -> Result<Type, Diagnostic> {
let path_name = module_path.to_str().unwrap();
fn analyze_module(&mut self, name: &str, path: &PathBuf, content: &str, range: Range) -> Result<Type, Diagnostic> {
let path_name = path.to_str().unwrap();

let mut parser = Parser::new(content, path_name);

Expand All @@ -54,7 +48,7 @@ impl<'a> Checker<'a> {
let _ = checker.check(&program)?;

let return_module_type = checker.ctx.get_last_return().ok_or_else(|| {
let diagnostic = TypeError::ModuleNotExported(module_name.to_string(), Some(location.clone()));
let diagnostic = TypeError::ModuleNotExported(name.to_string(), Some(range.clone()));
self.create_diagnostic(diagnostic)
})?;

Expand Down
10 changes: 5 additions & 5 deletions src/checker/check_return_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
ast::ast,
diagnostics::{Diagnostic, TypeError},
types::{replace_type, Type},
utils::location::Location,
utils::range::Range,
};

impl<'a> Checker<'a> {
Expand All @@ -12,7 +12,7 @@ impl<'a> Checker<'a> {
let mut grup_return_type = Type::new_grup(return_types);

if let Some(expected_t) = self.ctx.get_return_param_type() {
if !self.validate_return_type(&grup_return_type, &expected_t, &return_stmt.location)? {
if !self.validate_return_type(&grup_return_type, &expected_t, &return_stmt.range)? {
return Ok(Type::Unknown);
}

Expand All @@ -32,14 +32,14 @@ impl<'a> Checker<'a> {
values.iter().map(|value| self.check_expression(value)).collect()
}

fn validate_return_type(&self, return_t: &Type, expected_t: &Type, location: &Location) -> Result<bool, Diagnostic> {
fn validate_return_type(&self, return_t: &Type, expected_t: &Type, range: &Range) -> Result<bool, Diagnostic> {
if return_t.is_grup() && expected_t.is_grup() && !return_t.same_grup_length(expected_t) {
let diagnostic = TypeError::MismatchedTypes(expected_t.to_string(), return_t.to_string(), Some(location.clone()));
let diagnostic = TypeError::MismatchedTypes(expected_t.to_string(), return_t.to_string(), Some(range.clone()));
return Err(self.create_diagnostic(diagnostic));
}

if !return_t.check_match(expected_t) {
let diagnostic = TypeError::MismatchedTypes(expected_t.to_string(), return_t.to_string(), Some(location.clone()));
let diagnostic = TypeError::MismatchedTypes(expected_t.to_string(), return_t.to_string(), Some(range.clone()));
return Err(self.create_diagnostic(diagnostic));
}

Expand Down
Loading

0 comments on commit 4e29337

Please sign in to comment.