From 1bbb195ebcb90ebb652f09dbbec028a3a9c4d898 Mon Sep 17 00:00:00 2001 From: Robert Lucas Date: Wed, 10 Apr 2024 09:58:07 +0100 Subject: [PATCH] Continued nom parser implementation --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 1 + .../parse_function/parse_evaluable.rs | 17 +++++++++++------ .../nom_parser/parse_function/parse_operator.rs | 4 +++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32b6e44..1074a9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "accessors-rs" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cf100db746f59122e97eb1123e2bb56ddca630ea7095ff8fbe43033d4903a64" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "anstream" version = "0.6.13" @@ -412,6 +423,7 @@ dependencies = [ name = "whython-7" version = "0.1.0" dependencies = [ + "accessors-rs", "b-box", "clap", "color-print", diff --git a/Cargo.toml b/Cargo.toml index 073f030..78f519c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ nom = "7.1.3" nom_locate = "4.2.0" nom-supreme = "0.8.0" substring = "1.4.5" +accessors-rs = "0.1.0" [profile.release] opt-level = 3 diff --git a/src/root/nom_parser/parse_function/parse_evaluable.rs b/src/root/nom_parser/parse_function/parse_evaluable.rs index b197123..6f0584a 100644 --- a/src/root/nom_parser/parse_function/parse_evaluable.rs +++ b/src/root/nom_parser/parse_function/parse_evaluable.rs @@ -192,7 +192,7 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult = evaluables.into_iter().map(|e| Some(e)).collect(); + let mut evaluables: Vec<_> = enumerated.into_iter().map(|(_, e)| Some(e)).collect(); fn recursively_convert_temp(base: TempOperation, evaluables: &mut Vec>) -> EvaluableToken { @@ -205,19 +205,24 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult { - let lhs = recursively_convert_temp(lhs.into(), evaluables); + let lhs = recursively_convert_temp(*lhs, evaluables); EvaluableToken { location: lhs.location.clone(), token: EvaluableTokens::InfixOperator( b!(lhs), op, - b!(recursively_convert_temp(rhs.into(), evaluables)) + b!(recursively_convert_temp(*rhs, evaluables)) ), } - } - TempOperation::Prefix(_, _) => { - + TempOperation::Prefix(op, operand) => { + EvaluableToken { + location: op.location().clone(), + token: EvaluableTokens::PrefixOperator( + op, + b!(recursively_convert_temp(*operand, evaluables)) + ), + } } TempOperation::Value(p) => { not_operator(evaluables[p].take().unwrap()) diff --git a/src/root/nom_parser/parse_function/parse_operator.rs b/src/root/nom_parser/parse_function/parse_operator.rs index 7462fbb..f71005c 100644 --- a/src/root/nom_parser/parse_function/parse_operator.rs +++ b/src/root/nom_parser/parse_function/parse_operator.rs @@ -1,3 +1,4 @@ +use accessors_rs::Accessors; use nom::Err::Error; use nom::error::{ErrorKind, ParseError}; use nom::Parser; @@ -33,8 +34,9 @@ pub fn get_priority(operator: &OperatorTokens) -> usize { panic!() } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Accessors)] pub struct OperatorToken { + #[accessors(get)] location: Location, operator: OperatorTokens, }