From e8ac8717fa4765af375f80612502f1217d214437 Mon Sep 17 00:00:00 2001 From: Robert-M-Lucas <100799838+Robert-M-Lucas@users.noreply.github.com> Date: Fri, 2 Aug 2024 11:03:58 +0100 Subject: [PATCH] All parser errors have an implementation --- .idea/workspace.xml | 31 ++--- main.why | 2 +- src/root/errors/parser_errors.rs | 7 +- .../parser/parse_function/parse_evaluable.rs | 122 ++++++++++++------ src/root/parser/parse_function/parse_line.rs | 4 +- 5 files changed, 98 insertions(+), 68 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8a32208..c306b79 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -7,12 +7,7 @@ - - - - - - + @@ -719,7 +714,6 @@ - @@ -744,7 +738,8 @@ - diff --git a/main.why b/main.why index 43290b7..9411b23 100644 --- a/main.why +++ b/main.why @@ -11,7 +11,7 @@ impl Test { fn main() -> int { let x: Test = Test { a: 3 }; - +; + ::a; return 8; } diff --git a/src/root/errors/parser_errors.rs b/src/root/errors/parser_errors.rs index 5de9099..664c97c 100644 --- a/src/root/errors/parser_errors.rs +++ b/src/root/errors/parser_errors.rs @@ -29,12 +29,9 @@ pub fn create_custom_error_tree(e: String, l: Span) -> ErrorTree { pub fn to_error_tree<'a>(e: nom::Err>, s: Span<'a>) -> ErrorTree<'a> { match e { nom::Err::Incomplete(i) => { - create_custom_error_tree( - "Expected more characters".to_string(), - s - ) + create_custom_error_tree("Expected more characters".to_string(), s) } nom::Err::Error(e) => e, nom::Err::Failure(f) => f, } -} \ No newline at end of file +} diff --git a/src/root/parser/parse_function/parse_evaluable.rs b/src/root/parser/parse_function/parse_evaluable.rs index 95b6b09..e8c8e50 100644 --- a/src/root/parser/parse_function/parse_evaluable.rs +++ b/src/root/parser/parse_function/parse_evaluable.rs @@ -357,34 +357,50 @@ pub fn parse_evaluable<'a, 'b>( match eval { TempEvaluableTokensOne::StaticAccess(n) => { let Some((token, t2_span)) = new_evaluables.pop() else { - todo!() // Must have previous + return Err(create_custom_error( + "Must have something to perform a static access on".to_string(), + t1_span + )); }; - match token { - TempEvaluableTokensTwo::Operator(_)=> todo!(), // Can't be operator - TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push( - (TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + match token { + TempEvaluableTokensTwo::Operator(_) => return Err(create_custom_error( + "Cannot perform a static access on an operator".to_string(), + t2_span + )), + TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push(( + TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { location: e.location.clone(), token: EvaluableTokens::StaticAccess(b!(e), n), - }), t1_span)) + }), + t1_span, + )), } } TempEvaluableTokensOne::DynamicAccess(n) => { let Some((token, t2_span)) = new_evaluables.pop() else { - todo!() // Must have previous + return Err(create_custom_error( + "Must have something to perform a dynamic access on".to_string(), + t1_span + )); }; match token { - TempEvaluableTokensTwo::Operator(_) => todo!(), // Can't be operator - TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push( - (TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + TempEvaluableTokensTwo::Operator(_) => return Err(create_custom_error( + "Cannot perform a dynamic access on an operator".to_string(), + t2_span + )), + TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push(( + TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { location: e.location.clone(), token: EvaluableTokens::DynamicAccess(b!(e), n), - }), t1_span)) + }), + t1_span, + )), } } - TempEvaluableTokensOne::FunctionCall(n, c, a) => { - new_evaluables.push((TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + TempEvaluableTokensOne::FunctionCall(n, c, a) => new_evaluables.push(( + TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { location: n.location().clone(), token: EvaluableTokens::FunctionCall( b!(EvaluableToken { @@ -393,47 +409,66 @@ pub fn parse_evaluable<'a, 'b>( }), a, ), - }), t1_span)) - } + }), + t1_span, + )), TempEvaluableTokensOne::DynamicFunctionCall(n, a) => { let Some((token, t2_span)) = new_evaluables.pop() else { - todo!() // Must have previous + return Err(create_custom_error( + "Must have something to perform a method call on".to_string(), + t1_span + )); }; match token { - TempEvaluableTokensTwo::Operator(_) => todo!(), // Can't be operator + TempEvaluableTokensTwo::Operator(_) => return Err(create_custom_error( + "Cannot perform a method on an operator".to_string(), + t2_span + )), TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push( - (TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { - location: e.location.clone(), - token: EvaluableTokens::FunctionCall( - b!(EvaluableToken { - location: n.location().clone(), - token: EvaluableTokens::DynamicAccess(b!(e), n) - }), - a, - ), - }), t1_span) // TODO: Review if using t1 instead of t2 is correct + ( + TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + location: e.location.clone(), + token: EvaluableTokens::FunctionCall( + b!(EvaluableToken { + location: n.location().clone(), + token: EvaluableTokens::DynamicAccess(b!(e), n) + }), + a, + ), + }), + t1_span, + ), // TODO: Review if using t1 instead of t2 is correct ), } } TempEvaluableTokensOne::StaticFunctionCall(n, a) => { let Some((token, t2_span)) = new_evaluables.pop() else { - todo!() // Must have previous + return Err(create_custom_error( + "Must have something to perform a static function call on".to_string(), + t1_span + )); }; match token { - TempEvaluableTokensTwo::Operator(_) => todo!(), // Can't be operator + TempEvaluableTokensTwo::Operator(_) => return Err(create_custom_error( + "Cannot perform a static function call on an operator".to_string(), + t2_span + )), TempEvaluableTokensTwo::EvaluableToken(e) => new_evaluables.push( - (TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { - location: e.location.clone(), - token: EvaluableTokens::FunctionCall( - b!(EvaluableToken { - location: n.location().clone(), - token: EvaluableTokens::StaticAccess(b!(e), n) - }), - a, - ), - }), t1_span) // TODO: Review if using t1 instead of t2 is correct + ( + TempEvaluableTokensTwo::EvaluableToken(EvaluableToken { + location: e.location.clone(), + token: EvaluableTokens::FunctionCall( + b!(EvaluableToken { + location: n.location().clone(), + token: EvaluableTokens::StaticAccess(b!(e), n) + }), + a, + ), + }), + t1_span, + ), ), } } @@ -465,7 +500,7 @@ pub fn parse_evaluable<'a, 'b>( if section.len() < 2 { return Err(create_custom_error( "Expected evaluable after prefix operator".to_string(), - *span + *span, )); } @@ -503,7 +538,7 @@ pub fn parse_evaluable<'a, 'b>( (_, (TempEvaluableTokensTwo::EvaluableToken(e), span)) => { return Err(create_custom_error( "Expected infix operator between previous and this evaluable".to_string(), - *span + *span, )); } } @@ -511,10 +546,11 @@ pub fn parse_evaluable<'a, 'b>( None }; - if enumerated_slice.is_empty() { // Trailing operator + if enumerated_slice.is_empty() { + // Trailing operator return Err(create_custom_error( "Trailing operator".to_string(), - operator.unwrap().1 + operator.unwrap().1, )); } diff --git a/src/root/parser/parse_function/parse_line.rs b/src/root/parser/parse_function/parse_line.rs index 77771b9..3d0728f 100644 --- a/src/root/parser/parse_function/parse_line.rs +++ b/src/root/parser/parse_function/parse_line.rs @@ -1,4 +1,6 @@ -use crate::root::errors::parser_errors::{create_custom_error, create_custom_error_tree, to_error_tree}; +use crate::root::errors::parser_errors::{ + create_custom_error, create_custom_error_tree, to_error_tree, +}; use crate::root::parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::parser::parse_function::parse_break::{test_parse_break, BreakToken}; use crate::root::parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken};