Skip to content

Commit

Permalink
Rust FMT
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Lucas committed Apr 10, 2024
1 parent 11a4f7c commit d8cec8f
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 131 deletions.
10 changes: 5 additions & 5 deletions src/root/nom_parser/parse_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 17 additions & 12 deletions src/root/nom_parser/parse_function/parse_assigner.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<Span, AssignmentOperatorToken> {
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,
}))
}
Ok((
ns,
AssignmentOperatorToken {
location: Location::from_span(s),
assignment_operator: a,
},
))
}
113 changes: 63 additions & 50 deletions src/root/nom_parser/parse_function/parse_evaluable.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)]
Expand All @@ -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<Span, EvaluableToken> {
Expand Down Expand Up @@ -62,14 +67,17 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
let (ns, evaluable) = parse_evaluable(ns, false)?;
evaluables.push(TempEvaluableTokens::EvaluableToken(evaluable));
ns
}
else {
} else {
let (ns, token) = alt((
|x| parse_literal(x)
.map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Literal(t)))),
|x| {
parse_literal(x)
.map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Literal(t))))
},
|x| parse_operator(x).map(|(s, t)| (s, TempEvaluableTokens::Operator(t))),
|x| parse_full_name(x)
.map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Name(t))))
|x| {
parse_full_name(x)
.map(|(s, t)| (s, temp_from_token(s, EvaluableTokens::Name(t))))
},
))(ns)?;
evaluables.push(token);
ns
Expand All @@ -81,11 +89,15 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
enum TempOperation {
Infix(Box<TempOperation>, OperatorToken, Box<TempOperation>),
Prefix(OperatorToken, Box<TempOperation>),
Value(usize)
Value(usize),
}

fn parse_prefix(section: &[(usize, TempEvaluableTokens)]) -> ParseResult<&[(usize, TempEvaluableTokens)], TempOperation> {
let TempEvaluableTokens::Operator(operator) = &section[0].1 else { panic!() };
fn parse_prefix(
section: &[(usize, TempEvaluableTokens)],
) -> ParseResult<&[(usize, TempEvaluableTokens)], TempOperation> {
let TempEvaluableTokens::Operator(operator) = &section[0].1 else {
panic!()
};

if section.len() < 2 {
todo!()
Expand All @@ -95,15 +107,17 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
(p, TempEvaluableTokens::EvaluableToken(_)) => {
(&section[2..], TempOperation::Value(*p))
}
(_, TempEvaluableTokens::Operator(_)) => {
parse_prefix(&section[1..])?
}
(_, TempEvaluableTokens::Operator(_)) => parse_prefix(&section[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();
Expand Down Expand Up @@ -136,10 +150,10 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
}
(_, TempEvaluableTokens::Operator(_)) => {
let (new_slice, value) = match parse_prefix(enumerated_slice) {
Ok(r) => {
r
Ok(r) => r,
Err(_) => {
todo!()
}
Err(_) => { todo!() }
};
enumerated_slice = new_slice;
value
Expand All @@ -148,8 +162,7 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,

if base.is_none() {
base = Some(value);
}
else {
} else {
after.push(Some((operator.unwrap(), value)));
}
}
Expand All @@ -158,16 +171,17 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,

for priority in operator_priority {
for (pos, (op, _)) in after.iter().map(|x| x.as_ref().unwrap()).enumerate() {
if op.get_priority_t() != priority { continue; }
if op.get_priority_t() != priority {
continue;
}

if pos == 0 {
let (op, rhs) = after.remove(pos).unwrap();
base = Some(TempOperation::Infix(b!(base.unwrap()), op, b!(rhs)))
}
else {
} else {
let (op, rhs) = after.remove(pos).unwrap();
let (lop, base) = after[pos-1].take().unwrap();
after[pos-1] = Some((lop, TempOperation::Infix(b!(base), op, b!(rhs))));
let (lop, base) = after[pos - 1].take().unwrap();
after[pos - 1] = Some((lop, TempOperation::Infix(b!(base), op, b!(rhs))));
}

break;
Expand All @@ -178,12 +192,16 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,

let mut evaluables: Vec<_> = enumerated.into_iter().map(|(_, e)| Some(e)).collect();


fn recursively_convert_temp(base: TempOperation, evaluables: &mut Vec<Option<TempEvaluableTokens>>) -> EvaluableToken {
fn recursively_convert_temp(
base: TempOperation,
evaluables: &mut Vec<Option<TempEvaluableTokens>>,
) -> EvaluableToken {
fn not_operator(te: TempEvaluableTokens) -> EvaluableToken {
match te {
TempEvaluableTokens::EvaluableToken(e) => {e}
TempEvaluableTokens::Operator(_) => {panic!()}
TempEvaluableTokens::EvaluableToken(e) => e,
TempEvaluableTokens::Operator(_) => {
panic!()
}
}
}

Expand All @@ -195,25 +213,20 @@ pub fn parse_evaluable(s: Span, semicolon_terminated: bool) -> ParseResult<Span,
token: EvaluableTokens::InfixOperator(
b!(lhs),
op,
b!(recursively_convert_temp(*rhs, evaluables))
b!(recursively_convert_temp(*rhs, evaluables)),
),
}
}
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())

}
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)))
}
}
69 changes: 38 additions & 31 deletions src/root/nom_parser/parse_function/parse_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -38,25 +38,26 @@ pub fn parse_if(s: Span) -> ParseResult<Span, IfToken> {
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)?;

Expand All @@ -66,26 +67,32 @@ pub fn parse_if(s: Span) -> ParseResult<Span, IfToken> {
// ? 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,
},
));
}
2 changes: 1 addition & 1 deletion src/root/nom_parser/parse_function/parse_initialisation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
Loading

0 comments on commit d8cec8f

Please sign in to comment.