From 9af450eaa0a97a48e29e9e39d17abc9f7c59c593 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sun, 1 Oct 2023 18:41:00 -0500 Subject: [PATCH] Refactor --- src/syntax/functions/function_call.rs | 7 ++- .../{ => functions}/function_declaration.rs | 2 +- src/syntax/functions/mod.rs | 1 + src/syntax/grammar.md | 9 +++- src/syntax/mod.rs | 16 +------ src/syntax/utils.rs | 47 +------------------ 6 files changed, 18 insertions(+), 64 deletions(-) rename src/syntax/{ => functions}/function_declaration.rs (99%) diff --git a/src/syntax/functions/function_call.rs b/src/syntax/functions/function_call.rs index a81abdc..cfe338e 100644 --- a/src/syntax/functions/function_call.rs +++ b/src/syntax/functions/function_call.rs @@ -35,7 +35,12 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult { /// The parsing was a success @@ -84,7 +70,7 @@ fn next_construct<'a>( current_pos: usize, ) -> ParseResult { None.or_else( - || match function_declaration::try_parse(tokens, current_pos) { + || match functions::function_declaration::try_parse(tokens, current_pos) { ParseResult::Ok(declaration, next_pos) => Some(ParseResult::Ok( TopLevelDeclaration::FunctionDeclaration(declaration), next_pos, diff --git a/src/syntax/utils.rs b/src/syntax/utils.rs index 6309bfb..1f5ed7a 100644 --- a/src/syntax/utils.rs +++ b/src/syntax/utils.rs @@ -4,7 +4,7 @@ use crate::{ utils::Result3, }; -use super::{ParseResult, SyntaxResult}; +use super::ParseResult; /// Expects the token at `pos` to be of type `token_type` pub fn try_token_type(tokens: &Vec, pos: usize, token_type: TokenType) -> Result3<&Token> { @@ -58,48 +58,3 @@ pub fn try_operator(tokens: &Vec, pos: usize, operator: String) -> Result None => Result3::None, } } - -pub fn _try_operator_w<'a>( - tokens: &'a Vec, - pos: usize, - operator: String, - error_message: String, - prev_token: &Token, -) -> Result<(&'a Token, usize), Option> { - let mut current_pos = pos; - - // Ignore all whitespace and newlines - while let Some(t) = tokens.get(current_pos) { - if t.token_type == TokenType::INDENT - || t.token_type == TokenType::DEDENT - || t.token_type == TokenType::NewLine - { - current_pos += 1; - } else { - break; - } - } - - match tokens.get(current_pos) { - Some(t) if t.token_type == TokenType::Operator && t.value == operator => { - Ok((t, current_pos + 1)) - } - Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => { - Err(Some(SyntaxResult::Err(SyntaxError { - reason: error_message, - error_start: prev_token.position, - error_end: prev_token.get_end_position(), - }))) - } - Some(t) => Err(Some(SyntaxResult::Err(SyntaxError { - reason: error_message, - error_start: t.position, - error_end: t.get_end_position(), - }))), - None => Err(Some(SyntaxResult::Err(SyntaxError { - reason: error_message, - error_start: prev_token.position, - error_end: prev_token.get_end_position(), - }))), - } -}