diff --git a/src/root/nom_parser/parse_function.rs b/src/root/nom_parser/parse_function.rs index b62f910..4b23de9 100644 --- a/src/root/nom_parser/parse_function.rs +++ b/src/root/nom_parser/parse_function.rs @@ -4,24 +4,24 @@ use nom::Parser; use nom_supreme::tag::complete::tag; use substring::Substring; -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_blocks::{braced_section, bracketed_section}; use crate::root::nom_parser::parse_function::parse_line::{parse_lines, LineTokens}; use crate::root::nom_parser::parse_name::{parse_full_name, parse_simple_name, NameToken}; use crate::root::nom_parser::parse_parameters::{parse_parameters, Parameters}; use crate::root::nom_parser::parse_toplevel::{TopLevelTokens, ToplevelTestFn}; +mod parse_assigner; +mod parse_assignment; mod parse_break; pub(crate) mod parse_evaluable; mod parse_if; mod parse_initialisation; mod parse_line; +mod parse_literal; +mod parse_operator; mod parse_return; mod parse_while; -mod parse_operator; -mod parse_literal; -mod parse_assigner; -mod parse_assignment; #[derive(Debug)] pub struct FunctionToken { diff --git a/src/root/nom_parser/parse_function/parse_assigner.rs b/src/root/nom_parser/parse_function/parse_assigner.rs index f545a93..3b7e13c 100644 --- a/src/root/nom_parser/parse_function/parse_assigner.rs +++ b/src/root/nom_parser/parse_function/parse_assigner.rs @@ -1,10 +1,10 @@ +use crate::root::nom_parser::parse::{Location, ParseResult, Span}; +use crate::root::nom_parser::parse_function::parse_operator::OperatorTokens; +use crate::root::nom_parser::parse_util::alt_many; use clap::builder::TypedValueParser; use nom::branch::alt; use nom::bytes::complete::tag; use nom::character::complete::multispace0; -use crate::root::nom_parser::parse::{Location, ParseResult, Span}; -use crate::root::nom_parser::parse_function::parse_operator::OperatorTokens; -use crate::root::nom_parser::parse_util::alt_many; use nom::Parser; #[derive(Debug)] @@ -21,18 +21,23 @@ enum AssignmentOperatorTokens { const ASSIGNERS: [(&str, AssignmentOperatorTokens); 2] = [ ("=", AssignmentOperatorTokens::Normal), - ("+=", AssignmentOperatorTokens::Combination(OperatorTokens::Add)) + ( + "+=", + AssignmentOperatorTokens::Combination(OperatorTokens::Add), + ), ]; pub fn parse_assigner(s: Span) -> ParseResult { let (s, _) = multispace0(s)?; - let (ns, a) = alt_many(ASSIGNERS.map(|(t, o)| { - move |x| tag(t)(x).map(|(s, _)| (s, o.clone())) - })).parse(s)?; + let (ns, a) = alt_many(ASSIGNERS.map(|(t, o)| move |x| tag(t)(x).map(|(s, _)| (s, o.clone())))) + .parse(s)?; - Ok((ns, AssignmentOperatorToken { - location: Location::from_span(s), - assignment_operator: a, - })) -} \ No newline at end of file + Ok(( + ns, + AssignmentOperatorToken { + location: Location::from_span(s), + assignment_operator: a, + }, + )) +} diff --git a/src/root/nom_parser/parse_function/parse_evaluable.rs b/src/root/nom_parser/parse_function/parse_evaluable.rs index 6e429aa..b3212cc 100644 --- a/src/root/nom_parser/parse_function/parse_evaluable.rs +++ b/src/root/nom_parser/parse_function/parse_evaluable.rs @@ -1,11 +1,13 @@ +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; +use crate::root::nom_parser::parse_blocks::braced_section; +use crate::root::nom_parser::parse_function::parse_literal::{ + parse_literal, LiteralToken, LiteralTokens, +}; +use crate::root::nom_parser::parse_function::parse_operator::{parse_operator, OperatorToken}; +use crate::root::nom_parser::parse_name::{parse_full_name, NameToken}; use b_box::b; use nom::branch::alt; use nom::character::complete::{char, multispace0}; -use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; -use crate::root::nom_parser::parse_blocks::braced_section; -use crate::root::nom_parser::parse_function::parse_literal::{LiteralToken, LiteralTokens, parse_literal}; -use crate::root::nom_parser::parse_function::parse_operator::{OperatorToken, parse_operator}; -use crate::root::nom_parser::parse_name::{NameToken, parse_full_name}; #[derive(Debug)] pub struct EvaluableToken { @@ -14,7 +16,10 @@ pub struct EvaluableToken { } pub fn temp_from_token(s: Span, token: EvaluableTokens) -> TempEvaluableTokens { - TempEvaluableTokens::EvaluableToken(EvaluableToken { location: Location::from_span(s), token }) + TempEvaluableTokens::EvaluableToken(EvaluableToken { + location: Location::from_span(s), + token, + }) } #[derive(Debug)] @@ -28,7 +33,7 @@ enum EvaluableTokens { #[derive(Debug)] enum TempEvaluableTokens { EvaluableToken(EvaluableToken), - Operator(OperatorToken) + Operator(OperatorToken), } pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult { @@ -62,14 +67,17 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult ParseResult, OperatorToken, Box), Prefix(OperatorToken, Box), - Value(usize) + Value(usize), } - fn parse_prefix(section: &[(usize, TempEvaluableTokens)]) -> ParseResult<&[(usize, TempEvaluableTokens)], TempOperation> { - let TempEvaluableTokens::Operator(operator) = §ion[0].1 else { panic!() }; + fn parse_prefix( + section: &[(usize, TempEvaluableTokens)], + ) -> ParseResult<&[(usize, TempEvaluableTokens)], TempOperation> { + let TempEvaluableTokens::Operator(operator) = §ion[0].1 else { + panic!() + }; if section.len() < 2 { todo!() @@ -95,15 +107,17 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult { (§ion[2..], TempOperation::Value(*p)) } - (_, TempEvaluableTokens::Operator(_)) => { - parse_prefix(§ion[1..])? - } + (_, TempEvaluableTokens::Operator(_)) => parse_prefix(§ion[1..])?, }; - Ok((remaining, TempOperation::Prefix(operator.clone(), Box::new(operand)))) + Ok(( + remaining, + TempOperation::Prefix(operator.clone(), Box::new(operand)), + )) } - let enumerated: Vec<(usize, TempEvaluableTokens)> = evaluables.into_iter().enumerate().collect(); + let enumerated: Vec<(usize, TempEvaluableTokens)> = + evaluables.into_iter().enumerate().collect(); let mut base = None; let mut after = Vec::new(); @@ -136,10 +150,10 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult { let (new_slice, value) = match parse_prefix(enumerated_slice) { - Ok(r) => { - r + Ok(r) => r, + Err(_) => { + todo!() } - Err(_) => { todo!() } }; enumerated_slice = new_slice; value @@ -148,8 +162,7 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult ParseResult ParseResult = enumerated.into_iter().map(|(_, e)| Some(e)).collect(); - - fn recursively_convert_temp(base: TempOperation, evaluables: &mut Vec>) -> EvaluableToken { + fn recursively_convert_temp( + base: TempOperation, + evaluables: &mut Vec>, + ) -> EvaluableToken { fn not_operator(te: TempEvaluableTokens) -> EvaluableToken { match te { - TempEvaluableTokens::EvaluableToken(e) => {e} - TempEvaluableTokens::Operator(_) => {panic!()} + TempEvaluableTokens::EvaluableToken(e) => e, + TempEvaluableTokens::Operator(_) => { + panic!() + } } } @@ -195,25 +213,20 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult { - EvaluableToken { - location: op.location().clone(), - token: EvaluableTokens::PrefixOperator( - op, - b!(recursively_convert_temp(*operand, evaluables)) - ), - } - } - TempOperation::Value(p) => { - not_operator(evaluables[p].take().unwrap()) - - } + 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()), } } Ok((s, recursively_convert_temp(base.unwrap(), &mut evaluables))) -} \ No newline at end of file +} diff --git a/src/root/nom_parser/parse_function/parse_if.rs b/src/root/nom_parser/parse_function/parse_if.rs index cc3f300..0cfbab9 100644 --- a/src/root/nom_parser/parse_function/parse_if.rs +++ b/src/root/nom_parser/parse_function/parse_if.rs @@ -2,10 +2,10 @@ use nom::character::complete::{multispace0, multispace1}; use nom::sequence::Tuple; use nom_supreme::tag::complete::tag; -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_blocks::bracketed_section; -use crate::root::nom_parser::parse_function::parse_evaluable::{EvaluableToken, parse_evaluable}; -use crate::root::nom_parser::parse_function::parse_line::{LineTestFn, LineTokens, parse_lines}; +use crate::root::nom_parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; +use crate::root::nom_parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; #[derive(Debug)] pub struct IfToken { @@ -38,25 +38,26 @@ pub fn parse_if(s: Span) -> ParseResult { loop { let (ns, _) = multispace0(s)?; - if ns.is_empty() { break; } + if ns.is_empty() { + break; + } let ns = if let Ok((ns, _)) = tag::<_, _, ErrorTree>("else")(ns) { ns - } - else { + } else { s = ns; break; }; - let (ns, condition) = if let Ok((ns, _)) = (multispace1::<_, ErrorTree>, tag("if")).parse(ns) { - let (ns, _) = multispace0(ns)?; - let (ns, content) = bracketed_section(ns)?; - let (_, condition) = parse_evaluable(content, false)?; - (ns, Some(condition)) - } - else { - (ns, None) - }; + let (ns, condition) = + if let Ok((ns, _)) = (multispace1::<_, ErrorTree>, tag("if")).parse(ns) { + let (ns, _) = multispace0(ns)?; + let (ns, content) = bracketed_section(ns)?; + let (_, condition) = parse_evaluable(content, false)?; + (ns, Some(condition)) + } else { + (ns, None) + }; let (ns, _) = multispace0(ns)?; @@ -66,26 +67,32 @@ pub fn parse_if(s: Span) -> ParseResult { // ? Handle else if if let Some(condition) = condition { elifs.push((condition, contents)); - } - else { // ? Handle else - return Ok((ns, IfToken { - location: Location::from_span(l), - if_condition, - if_contents, - elif_condition_contents: elifs, - else_contents: Some(contents), - })) + } else { + // ? Handle else + return Ok(( + ns, + IfToken { + location: Location::from_span(l), + if_condition, + if_contents, + elif_condition_contents: elifs, + else_contents: Some(contents), + }, + )); } s = ns; } // ? Ended without else - return Ok((s, IfToken { - location: Location::from_span(l), - if_condition, - if_contents, - elif_condition_contents: elifs, - else_contents: None, - })) + return Ok(( + s, + IfToken { + location: Location::from_span(l), + if_condition, + if_contents, + elif_condition_contents: elifs, + else_contents: None, + }, + )); } diff --git a/src/root/nom_parser/parse_function/parse_initialisation.rs b/src/root/nom_parser/parse_function/parse_initialisation.rs index ae04a52..fd73042 100644 --- a/src/root/nom_parser/parse_function/parse_initialisation.rs +++ b/src/root/nom_parser/parse_function/parse_initialisation.rs @@ -1,4 +1,4 @@ -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_function::parse_break::parse_break; use crate::root::nom_parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::nom_parser::parse_function::parse_line::{LineTestFn, LineTokens}; diff --git a/src/root/nom_parser/parse_function/parse_line.rs b/src/root/nom_parser/parse_function/parse_line.rs index 1c94732..823de4c 100644 --- a/src/root/nom_parser/parse_function/parse_line.rs +++ b/src/root/nom_parser/parse_function/parse_line.rs @@ -2,7 +2,7 @@ use crate::root::nom_parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::nom_parser::parse_function::parse_assignment::AssignmentToken; use crate::root::nom_parser::parse_function::parse_break::{test_parse_break, BreakToken}; use crate::root::nom_parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; -use crate::root::nom_parser::parse_function::parse_if::{IfToken, test_parse_if}; +use crate::root::nom_parser::parse_function::parse_if::{test_parse_if, IfToken}; use crate::root::nom_parser::parse_function::parse_initialisation::{ test_parse_initialisation, InitialisationToken, }; @@ -49,7 +49,7 @@ pub fn parse_line(s: Span) -> ParseResult { test_parse_return, test_parse_initialisation, test_parse_while, - test_parse_if + test_parse_if, )) .parse(s) { diff --git a/src/root/nom_parser/parse_function/parse_literal.rs b/src/root/nom_parser/parse_function/parse_literal.rs index 3f54cc3..6fe346d 100644 --- a/src/root/nom_parser/parse_function/parse_literal.rs +++ b/src/root/nom_parser/parse_function/parse_literal.rs @@ -1,18 +1,18 @@ +use crate::root::nom_parser::parse::{Location, ParseResult, Span}; use nom::branch::alt; use nom::bytes::complete::tag; use nom::character::complete::multispace0; -use crate::root::nom_parser::parse::{Location, ParseResult, Span}; #[derive(Debug)] pub struct LiteralToken { location: Location, - literal: LiteralTokens + literal: LiteralTokens, } #[derive(Debug)] pub enum LiteralTokens { Bool(bool), - Int(i64) + Int(i64), } pub fn parse_literal(s: Span) -> ParseResult { @@ -21,10 +21,13 @@ pub fn parse_literal(s: Span) -> ParseResult { let (ns, l) = alt(( |x| tag("true")(x).map(|(s, _)| (s, LiteralTokens::Bool(true))), |x| tag("false")(x).map(|(s, _)| (s, LiteralTokens::Bool(false))), - |x| nom::character::complete::i64(x).map(|(s, i)| (s, LiteralTokens::Int(i))) + |x| nom::character::complete::i64(x).map(|(s, i)| (s, LiteralTokens::Int(i))), ))(s)?; - let l = LiteralToken { location: Location::from_span(s), literal: l }; + let l = LiteralToken { + location: Location::from_span(s), + literal: l, + }; Ok((ns, l)) -} \ No newline at end of file +} diff --git a/src/root/nom_parser/parse_function/parse_operator.rs b/src/root/nom_parser/parse_function/parse_operator.rs index f71005c..17d7c99 100644 --- a/src/root/nom_parser/parse_function/parse_operator.rs +++ b/src/root/nom_parser/parse_function/parse_operator.rs @@ -1,20 +1,18 @@ +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use accessors_rs::Accessors; -use nom::Err::Error; use nom::error::{ErrorKind, ParseError}; +use nom::Err::Error; use nom::Parser; use nom_supreme::error::GenericErrorTree; use nom_supreme::tag::complete::tag; use nom_supreme::tag::TagError; -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; - const OPERATOR_MAPS: [(&str, OperatorTokens, bool); 3] = [ ("+", OperatorTokens::Add, false), ("-", OperatorTokens::Subtract, false), - ("!", OperatorTokens::Not, true) + ("!", OperatorTokens::Not, true), ]; - // TODO: Implement functionally pub fn is_prefix_op(operator: &OperatorTokens) -> bool { for (_, op, prefix) in &OPERATOR_MAPS { @@ -55,7 +53,7 @@ impl OperatorToken { pub enum OperatorTokens { Add, Subtract, - Not + Not, } impl OperatorTokens { @@ -71,13 +69,20 @@ impl OperatorTokens { pub fn parse_operator(s: Span) -> ParseResult { for (operator, token, _) in OPERATOR_MAPS { if let Ok((s, x)) = tag::<_, _, ErrorTree>(operator)(s) { - return Ok((s, OperatorToken { location: Location::from_span(x), operator: token })) + return Ok(( + s, + OperatorToken { + location: Location::from_span(x), + operator: token, + }, + )); } } - Err(Error( - GenericErrorTree::Alt( - OPERATOR_MAPS.iter().map(|(t, _, _)| GenericErrorTree::from_tag(s, *t)).collect() - ) - )) + Err(Error(GenericErrorTree::Alt( + OPERATOR_MAPS + .iter() + .map(|(t, _, _)| GenericErrorTree::from_tag(s, *t)) + .collect(), + ))) } diff --git a/src/root/nom_parser/parse_function/parse_while.rs b/src/root/nom_parser/parse_function/parse_while.rs index 4afc031..38f196b 100644 --- a/src/root/nom_parser/parse_function/parse_while.rs +++ b/src/root/nom_parser/parse_function/parse_while.rs @@ -3,7 +3,7 @@ use nom::sequence::Tuple; use nom::Parser; use nom_supreme::tag::complete::tag; -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_blocks::bracketed_section; use crate::root::nom_parser::parse_function::parse_evaluable::{parse_evaluable, EvaluableToken}; use crate::root::nom_parser::parse_function::parse_line::{parse_lines, LineTestFn, LineTokens}; diff --git a/src/root/nom_parser/parse_name.rs b/src/root/nom_parser/parse_name.rs index f91b30e..a432dba 100644 --- a/src/root/nom_parser/parse_name.rs +++ b/src/root/nom_parser/parse_name.rs @@ -1,4 +1,4 @@ -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_function::parse_evaluable::EvaluableToken; use nom::bytes::complete::take_till; use nom::Err::Error; @@ -19,7 +19,6 @@ pub struct NameToken { function_call: Option>, } - pub fn parse_full_name(s: Span) -> ParseResult { // TODO: Handle function calls diff --git a/src/root/nom_parser/parse_struct.rs b/src/root/nom_parser/parse_struct.rs index 079573f..9a99e08 100644 --- a/src/root/nom_parser/parse_struct.rs +++ b/src/root/nom_parser/parse_struct.rs @@ -1,4 +1,4 @@ -use crate::root::nom_parser::parse::{Location, ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, Location, ParseResult, Span}; use crate::root::nom_parser::parse_blocks::braced_section; use crate::root::nom_parser::parse_name::{parse_simple_name, NameToken}; use crate::root::nom_parser::parse_parameters::{parse_parameters, Parameters}; diff --git a/src/root/nom_parser/parse_util.rs b/src/root/nom_parser/parse_util.rs index ee79fd6..4d3aeec 100644 --- a/src/root/nom_parser/parse_util.rs +++ b/src/root/nom_parser/parse_util.rs @@ -1,4 +1,4 @@ -use crate::root::nom_parser::parse::{ParseResult, Span, ErrorTree}; +use crate::root::nom_parser::parse::{ErrorTree, ParseResult, Span}; use crate::root::nom_parser::parse_comments; use nom::bytes::complete::{take_till, take_while}; use nom::error::ParseError; @@ -53,18 +53,18 @@ pub fn take_till_whitespace(s: Span) -> ParseResult { // ? Source: https://stackoverflow.com/a/76759023/10619498 // TODO: Does this work with pub fn alt_many(mut parsers: Ps) -> impl Parser - where - P: Parser, - I: Clone, - for<'a> &'a mut Ps: IntoIterator, - E: ParseError, +where + P: Parser, + I: Clone, + for<'a> &'a mut Ps: IntoIterator, + E: ParseError, { move |input: I| { for parser in &mut parsers { - if let r@Ok(_) = parser.parse(input.clone()) { + if let r @ Ok(_) = parser.parse(input.clone()) { return r; } } nom::combinator::fail::(input) } -} \ No newline at end of file +}