Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions commons/src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::Position;
pub type PositionedResult<K> = Result<K, PositionedError>;

/// An error that has a position
#[derive(Debug)]
pub struct PositionedError {
pub start: Position,
pub end: Position,
Expand All @@ -19,26 +20,31 @@ pub struct PositionedError {

impl PositionedError {
pub fn new(start: Position, end: Position, reason: String) -> Self {
return PositionedError { start, end, reason }

let err = PositionedError { start, end, reason };

println!("{}", err);

return err;
}
}

impl fmt::Display for PositionedError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{} at {}", "ERR".bright_red().bold(), self.start);
writeln!(f, "{} at {}", "ERR".bright_red().bold(), self.start)?;

let line = match self.start.get_line_content() {
Ok(v) => v,
Err(e) => format!("{}","Couldn't read file contents!".red().bold())
};

let before = &line[self.start.col - 1..];
let target = &line[self.start.col..self.end.col - 1].cyan().underline();
let before = &line[0..self.start.col - 1];
let target = &line[self.start.col - 1..self.end.col].cyan().underline();
let after = &line[self.end.col..];

writeln!(f, "{}{}{}", before, target, after);
writeln!(f, "");
writeln!(f, "{}", self.reason.bright_red());
writeln!(f, "{}{}{}", before, target, after)?;
writeln!(f, "")?;
writeln!(f, "{}", self.reason.bright_red())?;

Ok(())
}
Expand Down
23 changes: 16 additions & 7 deletions lexer/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,44 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult<Vec<LexerToken>>
let mut i: usize = 0;

let mut line: usize = 1;
let mut col: usize = 0;

let mut last_line_break: usize = 0;

while i < contents.len() {
let c: char = contents.chars().nth(i).unwrap();

col += 1;

if c == '\n' {
i += c.len_utf8();
last_line_break = i;
line += 1;
continue;
}

if c.is_numeric() {
let col = i - last_line_break + 1;
tokens.push(parse_number_token(&contents, &mut i, Position::new(file_path.to_string(), line, col))?);
continue;
}

if c == '"' {
let col = i - last_line_break + 1;

tokens.push(parse_string_token(&contents, &mut i, Position::new(file_path.to_string(), line, col)));
continue;
}

if c.is_alphabetic() {
let col = i - last_line_break + 1;

tokens.push(parse_keyword(&contents, &mut i, Position::new(file_path.to_string(), line, col)));
continue;
}

i += c.len_utf8();


let col = i - last_line_break + 1;

let pos = Position::new(file_path.to_string(), line, col);

match c {
Expand All @@ -89,7 +98,7 @@ pub fn lexer_parse_file(file_path: &String) -> LexerParseResult<Vec<LexerToken>>

}

tokens.push(LexerToken::make_single_sized(Position::new(file_path.to_string(), line, col), LexerTokenType::END_OF_FILE));
tokens.push(LexerToken::make_single_sized(Position::new(file_path.to_string(), line, i - last_line_break + 1), LexerTokenType::END_OF_FILE));

Ok(tokens)
}
Expand All @@ -114,7 +123,7 @@ fn parse_number_token(str: &String, ind: &mut usize, start_pos: Position) -> Lex

*ind = end;

let endpos = start_pos.increment_by(start - end);
let endpos = start_pos.increment_by(end - start);
return Ok(LexerToken::new(start_pos, endpos, LexerTokenType::INT_LIT(num)));
}

Expand All @@ -135,7 +144,7 @@ fn parse_string_token(str: &String, ind: &mut usize, start_pos: Position) -> Lex

*ind = end;

let endpos: Position = start_pos.increment_by(start - end);
let endpos: Position = start_pos.increment_by(end - start);
return LexerToken::new(start_pos, endpos, LexerTokenType::STRING_LIT(slice.to_string()));
}

Expand Down Expand Up @@ -176,6 +185,6 @@ fn parse_keyword(str: &String, ind: &mut usize, start_pos: Position) -> LexerTok
_ => LexerTokenType::KEYWORD(slice.to_string(), hash)
};

let endpos: Position = start_pos.increment_by(start - end);
let endpos: Position = start_pos.increment_by(end - start);
return LexerToken::new(start_pos, endpos, token_type);
}
3 changes: 2 additions & 1 deletion parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2024"

[dependencies]
utils = { path = "../utils" }
lexer = { path = "../lexer" }
lexer = { path = "../lexer" }
commons = { path = "../commons" }
29 changes: 15 additions & 14 deletions parser/src/ast/cond/operators.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lexer::token::LexerToken;
use commons::err::PositionedResult;
use lexer::token::{LexerToken, LexerTokenType};

use crate::{ParserError, ParserResult};

Expand All @@ -14,38 +15,38 @@ pub enum ConditionOperator {
LOWEREQ // A <= B
}

pub fn parse_condition_operator(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<ConditionOperator> {
match &tokens[*ind] {
LexerToken::EQUAL_SIGN => {
pub fn parse_condition_operator(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<ConditionOperator> {
match &tokens[*ind].tok_type {
LexerTokenType::EQUAL_SIGN => {
*ind += 1;

if tokens[*ind] == LexerToken::EQUAL_SIGN {
return Ok(ConditionOperator::EQUAL);
}
tokens[*ind].expects(LexerTokenType::EQUAL_SIGN);

return Ok(ConditionOperator::EQUAL)
},

LexerToken::EXCLAMATION_MARK => {
LexerTokenType::EXCLAMATION_MARK => {
*ind += 1;

if tokens[*ind] == LexerToken::EQUAL_SIGN {
if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN {
return Ok(ConditionOperator::NOT_EQUAL)
}
},

LexerToken::ANGEL_BRACKET_OPEN => {
LexerTokenType::ANGEL_BRACKET_OPEN => {
*ind += 1;

if tokens[*ind] == LexerToken::EQUAL_SIGN {
if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN {
return Ok(ConditionOperator::LOWEREQ);
}

return Ok(ConditionOperator::LOWER);
},

LexerToken::ANGEL_BRACKET_CLOSE => {
LexerTokenType::ANGEL_BRACKET_CLOSE => {
*ind += 1;

if tokens[*ind] == LexerToken::EQUAL_SIGN {
if tokens[*ind].tok_type == LexerTokenType::EQUAL_SIGN {
return Ok(ConditionOperator::HIGHEREQ);
}

Expand All @@ -55,5 +56,5 @@ pub fn parse_condition_operator(tokens: &Vec<LexerToken>, ind: &mut usize) -> Pa
_ => {}
}

Err(ParserError::new(String::from("Pattern doesn't represent a valid condition operator!"), 0))
Err(tokens[*ind].make_err("Token doesn't make a valid condition operator"))
}
27 changes: 8 additions & 19 deletions parser/src/ast/control/forloop.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
use lexer::token::LexerToken;
use commons::err::PositionedResult;
use lexer::token::{LexerToken, LexerTokenType};

use crate::{ParserError, ParserResult, ast::{func::parse_node_body, parse_ast_node, parse_ast_value, tree::ASTTreeNode, var::decl::parse_variable_declaration}};

pub fn parse_for_loop(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<Box<ASTTreeNode>> {
pub fn parse_for_loop(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<Box<ASTTreeNode>> {
*ind += 1;

if tokens[*ind] != LexerToken::PAREN_OPEN {
return Err(ParserError::new(String::from("Requires ("), 0));
}
tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?;

let initial = parse_variable_declaration(tokens, ind)?;

if tokens[*ind] != LexerToken::COMMA {
return Err(ParserError::new(String::from("Requires for bodies to seperated by commas!"), 0));
}
tokens[*ind].expects(LexerTokenType::COMMA)?;

*ind += 1;
let cond = parse_ast_value(tokens, ind)?;

if tokens[*ind] != LexerToken::COMMA {
return Err(ParserError::new(String::from("Requires for bodies to seperated by commas!"), 0));
}
tokens[*ind].expects(LexerTokenType::COMMA)?;
*ind += 1;


let increment = parse_ast_node(tokens, ind)?;

*ind += 1;

if tokens[*ind] != LexerToken::PAREN_CLOSE {
return Err(ParserError::new(String::from("Requires )"), 0));
}

tokens[*ind].expects(LexerTokenType::PAREN_CLOSE)?;
*ind += 1;

if tokens[*ind] != LexerToken::BRACKET_OPEN {
return Err(ParserError::new(String::from("Requires {"), 0));
}
tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?;

let body = parse_node_body(tokens, ind)?;

Expand Down
31 changes: 12 additions & 19 deletions parser/src/ast/control/ifelse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,30 @@
//! Parsing for if and else statements
//!

use lexer::token::LexerToken;
use commons::err::PositionedResult;
use lexer::token::{LexerToken, LexerTokenType};

use crate::{ParserError, ParserResult, ast::{func::parse_node_body, parse_ast_value, tree::ASTTreeNode}};

pub fn parse_condition_member(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<Box<ASTTreeNode>> {
if tokens[*ind] != LexerToken::PAREN_OPEN {
return Err(ParserError::new(String::from("If statements must be followed by condition!"), 0));
}
pub fn parse_condition_member(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<Box<ASTTreeNode>> {
tokens[*ind].expects(LexerTokenType::PAREN_OPEN)?;

*ind += 1;
let cond = parse_ast_value(tokens, ind)?;

if tokens[*ind] != LexerToken::PAREN_CLOSE {
return Err(ParserError::new(String::from("Conditions must be closed by paren!"), 0));
}
tokens[*ind].expects(LexerTokenType::PAREN_CLOSE)?;

*ind += 1;

return Ok(cond);
}

pub fn parse_if_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<Box<ASTTreeNode>> {
pub fn parse_if_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<Box<ASTTreeNode>> {
*ind += 1;

let cond = parse_condition_member(tokens, ind)?;

if tokens[*ind] != LexerToken::BRACKET_OPEN {
return Err(ParserError::new(String::from("Condition must be followed by body!"), 0));
}
tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?;

let body = match parse_node_body(tokens, ind) {
Ok(v) => v,
Expand All @@ -39,7 +34,7 @@ pub fn parse_if_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserRe

let mut elseStatement = None;

if tokens[*ind + 1] == LexerToken::ELSE {
if tokens[*ind + 1].tok_type == LexerTokenType::ELSE {
*ind += 1;

elseStatement = Some(parse_else_statement(tokens, ind)?);
Expand All @@ -48,19 +43,17 @@ pub fn parse_if_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserRe
return Ok(Box::new(ASTTreeNode::IfStatement { cond, body, elseStatement }));
}

pub fn parse_else_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<Box<ASTTreeNode>> {
pub fn parse_else_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<Box<ASTTreeNode>> {
*ind += 1;

let mut cond = None;

if tokens[*ind] == LexerToken::IF {
if tokens[*ind].tok_type == LexerTokenType::IF {
*ind += 1;
cond = Some(parse_condition_member(tokens, ind)?);
}

if tokens[*ind] != LexerToken::BRACKET_OPEN {
return Err(ParserError::new(String::from("Condition must be followed by body!"), 0));
}
tokens[*ind].expects(LexerTokenType::BRACKET_OPEN)?;

let body = match parse_node_body(tokens, ind) {
Ok(v) => v,
Expand All @@ -70,7 +63,7 @@ pub fn parse_else_statement(tokens: &Vec<LexerToken>, ind: &mut usize) -> Parser
if cond.is_some() {
let mut elseStatement = None;

if tokens[*ind + 1] == LexerToken::ELSE {
if tokens[*ind + 1].tok_type == LexerTokenType::ELSE {
*ind += 1;

elseStatement = Some(parse_else_statement(tokens, ind)?);
Expand Down
7 changes: 3 additions & 4 deletions parser/src/ast/control/whileblock.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use commons::err::PositionedResult;
use lexer::token::LexerToken;

use crate::{ParserError, ParserResult, ast::{control::ifelse::parse_condition_member, func::parse_node_body, tree::ASTTreeNode}};

pub fn parse_while_block(tokens: &Vec<LexerToken>, ind: &mut usize) -> ParserResult<Box<ASTTreeNode>> {
pub fn parse_while_block(tokens: &Vec<LexerToken>, ind: &mut usize) -> PositionedResult<Box<ASTTreeNode>> {
*ind += 1;

let cond = parse_condition_member(tokens, ind)?;

if tokens[*ind] != LexerToken::BRACKET_OPEN {
return Err(ParserError::new(String::from("Expected block body!"), 0));
}
tokens[*ind].expects(lexer::token::LexerTokenType::BRACKET_OPEN)?;

let body = match parse_node_body(tokens, ind) {
Ok(v) => v,
Expand Down
Loading