From 61051ed11b9168c9d1ca225499bb20a230294b04 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 15 Mar 2024 16:59:28 -0500 Subject: [PATCH] Parsing refactor pt4 --- src/main.rs | 1 - src/syntax/binding.rs | 9 ++++----- src/syntax/utils.rs | 15 ++++++--------- src/utils/mod.rs | 15 --------------- 4 files changed, 10 insertions(+), 30 deletions(-) delete mode 100644 src/utils/mod.rs diff --git a/src/main.rs b/src/main.rs index 0752f78..3852a10 100755 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ mod lexic; mod semantic; // Transforms an AST to JS mod codegen; -mod utils; mod error_handling; diff --git a/src/syntax/binding.rs b/src/syntax/binding.rs index 8baf49c..e9cd56a 100644 --- a/src/syntax/binding.rs +++ b/src/syntax/binding.rs @@ -3,7 +3,6 @@ use super::utils::{parse_token_type, try_operator}; use super::{expression, ParseResult, ParsingError}; use crate::error_handling::SyntaxError; use crate::lexic::token::{Token, TokenType}; -use crate::utils::Result3; pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult { let mut current_pos = pos; @@ -64,8 +63,8 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult * Equal (=) operator */ let equal_operator = match try_operator(tokens, current_pos, String::from("=")) { - Result3::Ok(t) => t, - Result3::Err(t) => { + Ok((t, _)) => t, + Err(ParsingError::Mismatch(t)) => { // The parser found a token, but it's not the `=` operator return ParseResult::Err(SyntaxError { reason: format!("There should be an equal sign `=` after the identifier"), @@ -73,7 +72,7 @@ pub fn try_parse<'a>(tokens: &'a Vec, pos: usize) -> ParseResult error_end: t.get_end_position(), }); } - Result3::None => { + _ => { // The parser didn't find the `=` operator after the identifier return ParseResult::Err(SyntaxError { reason: format!("There should be an equal sign `=` after the identifier",), @@ -141,7 +140,7 @@ mod tests { #[test] fn should_parse_operator() { let tokens = get_tokens(&String::from("=")).unwrap(); - let token = *try_operator(&tokens, 0, String::from("=")).unwrap(); + let (token, _) = try_operator(&tokens, 0, String::from("=")).unwrap(); assert_eq!("=", token.value); } diff --git a/src/syntax/utils.rs b/src/syntax/utils.rs index 7c37d0e..6557b7f 100644 --- a/src/syntax/utils.rs +++ b/src/syntax/utils.rs @@ -1,7 +1,4 @@ -use crate::{ - lexic::token::{Token, TokenType}, - utils::Result3, -}; +use crate::lexic::token::{Token, TokenType}; use super::{ParsingError, ParsingResult}; @@ -34,14 +31,14 @@ impl Tokenizer for Vec { } /// Expects the token at `pos` to be an operator of value `operator`. Doesn't ignore whitespace or newlines -pub fn try_operator(tokens: &Vec, pos: usize, operator: String) -> Result3<&Token> { +pub fn try_operator(tokens: &Vec, pos: usize, operator: String) -> ParsingResult<&Token> { match tokens.get(pos) { - Some(t) if t.token_type == TokenType::Operator && t.value == operator => Result3::Ok(t), + Some(t) if t.token_type == TokenType::Operator && t.value == operator => Ok((t, pos + 1)), Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => { - Result3::None + Err(ParsingError::Unmatched) } - Some(t) => Result3::Err(t), - None => Result3::None, + Some(t) => Err(ParsingError::Mismatch(t)), + None => Err(ParsingError::Unmatched), } } diff --git a/src/utils/mod.rs b/src/utils/mod.rs deleted file mode 100644 index 2f52b2a..0000000 --- a/src/utils/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -pub enum Result3 { - Ok(T), - Err(T), - None, -} - -#[allow(dead_code)] -impl Result3 { - pub fn unwrap(&self) -> &T { - match self { - Result3::Ok(t) => t, - _ => panic!(""), - } - } -}