Renamed token and pretoken

This commit is contained in:
2023-06-16 12:58:06 -07:00
parent 081dc1b7ab
commit 1495dcd561
17 changed files with 280 additions and 281 deletions

View File

@ -1,23 +1,23 @@
use std::collections::VecDeque;
use super::super::{
PreToken,
Token,
LineLocation
};
pub fn find_subs(
mut g: VecDeque<PreToken>,
mut g: VecDeque<Token>,
) -> (
Vec<(LineLocation, String)>,
VecDeque<PreToken>
VecDeque<Token>
) {
// Array of replacements
let mut r: Vec<(LineLocation, String)> = Vec::with_capacity(8);
// New token array, with updated locations
let mut n: VecDeque<PreToken> = VecDeque::with_capacity(g.len());
let mut n: VecDeque<Token> = VecDeque::with_capacity(g.len());
let mut offset: usize = 0;
@ -26,7 +26,7 @@ pub fn find_subs(
let mut t = g.pop_back().unwrap();
let target: Option<&str> = match &mut t {
PreToken::PreOperator(_, s) => {
Token::PreOperator(_, s) => {
let target = match &s[..] {
"*" => {Some("×")},
"/" => {Some("÷")},
@ -41,7 +41,7 @@ pub fn find_subs(
target
},
PreToken::PreWord(_, s) => {
Token::PreWord(_, s) => {
let target = match &s[..] {
// Greek letters
"alpha" => {Some("α")},

View File

@ -1,7 +1,7 @@
use std::collections::VecDeque;
use super::super::{
PreToken,
Token,
LineLocation,
ParserError,
Operator
@ -9,7 +9,7 @@ use super::super::{
fn lookback_signs(
g: &mut VecDeque<PreToken>
g: &mut VecDeque<Token>
) -> Result<(), (LineLocation, ParserError)> {
// Convert `-` operators to `neg` operators
@ -17,12 +17,12 @@ fn lookback_signs(
let mut i: usize = 0;
while i < g.len() {
if i == 0 {
let a: PreToken = g.remove(i).unwrap();
let a: Token = g.remove(i).unwrap();
match &a {
PreToken::PreOperator(l,o)
Token::PreOperator(l,o)
=> {
if o == "-" {
g.insert(i, PreToken::PreOperator(*l, String::from("neg")));
g.insert(i, Token::PreOperator(*l, String::from("neg")));
} else if o == "+" {
continue; // We should not increment i if we remove a token
} else {g.insert(i, a);}
@ -31,11 +31,11 @@ fn lookback_signs(
};
} else {
let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap();
let a: Token = g.remove(i-1).unwrap();
let b: Token = g.remove(i-1).unwrap();
match (&a, &b) {
(PreToken::PreOperator(_, sa), PreToken::PreOperator(l,sb))
(Token::PreOperator(_, sa), Token::PreOperator(l,sb))
=> {
if {
let o = Operator::from_string(sa);
@ -47,7 +47,7 @@ fn lookback_signs(
)
} {
if sb == "-" {
g.insert(i-1, PreToken::PreOperator(*l, String::from("neg")));
g.insert(i-1, Token::PreOperator(*l, String::from("neg")));
g.insert(i-1, a);
} else if sb == "+" {
g.insert(i-1, a);
@ -67,11 +67,11 @@ fn lookback_signs(
// Delete consecutive `neg`s
let mut i: usize = 1;
while i < g.len() {
let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap();
let a: Token = g.remove(i-1).unwrap();
let b: Token = g.remove(i-1).unwrap();
match (&a, &b) {
(PreToken::PreOperator(_,sa), PreToken::PreOperator(_,sb))
(Token::PreOperator(_,sa), Token::PreOperator(_,sb))
=> {
if !((sa == "neg") && (sb == "neg")) {
g.insert(i-1, b);
@ -95,7 +95,7 @@ fn lookback_signs(
// Inserts implicit operators
fn lookback(
g: &mut VecDeque<PreToken>
g: &mut VecDeque<Token>
) -> Result<(), (LineLocation, ParserError)> {
lookback_signs(g)?;
@ -103,24 +103,24 @@ fn lookback(
let mut i: usize = 0;
while i < g.len() {
if i >= 1 {
let a: PreToken = g.remove(i-1).unwrap();
let b: PreToken = g.remove(i-1).unwrap();
let a: Token = g.remove(i-1).unwrap();
let b: Token = g.remove(i-1).unwrap();
match (&a, &b) {
// Insert ImplicitMultiply
(PreToken::PreGroup(_,_), PreToken::PreGroup(l ,_))
| (PreToken::PreGroup(_,_), PreToken::PreQuantity(l,_))
| (PreToken::PreQuantity(_,_), PreToken::PreGroup(l,_))
| (PreToken::PreGroup(_,_), PreToken::PreWord(l,_))
| (PreToken::PreWord(_,_), PreToken::PreGroup(l,_))
| (PreToken::PreQuantity(_,_), PreToken::PreWord(l,_))
| (PreToken::PreWord(_,_), PreToken::PreQuantity(l,_))
| (PreToken::PreWord(_,_), PreToken::PreWord(l,_))
(Token::PreGroup(_,_), Token::PreGroup(l ,_))
| (Token::PreGroup(_,_), Token::PreQuantity(l,_))
| (Token::PreQuantity(_,_), Token::PreGroup(l,_))
| (Token::PreGroup(_,_), Token::PreWord(l,_))
| (Token::PreWord(_,_), Token::PreGroup(l,_))
| (Token::PreQuantity(_,_), Token::PreWord(l,_))
| (Token::PreWord(_,_), Token::PreQuantity(l,_))
| (Token::PreWord(_,_), Token::PreWord(l,_))
=> {
let loc = LineLocation{pos: l.pos-1, len: 0};
g.insert(i-1, b);
g.insert(i-1, PreToken::PreOperator(
g.insert(i-1, Token::PreOperator(
loc,
String::from("i*")
));
@ -128,9 +128,9 @@ fn lookback(
},
// Insert implicit multiplications for right-unary operators
(PreToken::PreQuantity(_,_), PreToken::PreOperator(l,s))
| (PreToken::PreGroup(_,_), PreToken::PreOperator(l,s))
| (PreToken::PreWord(_,_), PreToken::PreOperator(l,s))
(Token::PreQuantity(_,_), Token::PreOperator(l,s))
| (Token::PreGroup(_,_), Token::PreOperator(l,s))
| (Token::PreWord(_,_), Token::PreOperator(l,s))
=> {
let o = Operator::from_string(s);
let loc = LineLocation{pos: l.pos-1, len: 0};
@ -139,7 +139,7 @@ fn lookback(
if o.is_some() {
let o = o.unwrap();
if (!o.is_binary()) && (!o.is_left_associative()) {
g.insert(i-1, PreToken::PreOperator(
g.insert(i-1, Token::PreOperator(
loc,
String::from("i*")
));
@ -149,9 +149,9 @@ fn lookback(
},
// Insert implicit multiplications for left-unary operators.
(PreToken::PreOperator(_,s), PreToken::PreQuantity(l,_))
| (PreToken::PreOperator(_,s), PreToken::PreGroup(l,_))
| (PreToken::PreOperator(_,s), PreToken::PreWord(l,_))
(Token::PreOperator(_,s), Token::PreQuantity(l,_))
| (Token::PreOperator(_,s), Token::PreGroup(l,_))
| (Token::PreOperator(_,s), Token::PreWord(l,_))
=> {
let o = Operator::from_string(s);
let loc = LineLocation{pos: l.pos-1, len: 0};
@ -160,7 +160,7 @@ fn lookback(
if o.is_some() {
let o = o.unwrap();
if (!o.is_binary()) && o.is_left_associative() {
g.insert(i-1, PreToken::PreOperator(
g.insert(i-1, Token::PreOperator(
loc,
String::from("i*")
));
@ -170,7 +170,7 @@ fn lookback(
},
// The following are syntax errors
(PreToken::PreQuantity(la,_), PreToken::PreQuantity(lb,_))
(Token::PreQuantity(la,_), Token::PreQuantity(lb,_))
=> {
return Err((
LineLocation{pos: la.pos, len: lb.pos - la.pos + lb.len},
@ -189,13 +189,13 @@ fn lookback(
pub fn groupify(
mut g: VecDeque<PreToken>
mut g: VecDeque<Token>
) -> Result<
PreToken,
Token,
(LineLocation, ParserError)
> {
// Vector of grouping levels
let mut levels: Vec<(LineLocation, VecDeque<PreToken>)> = Vec::with_capacity(8);
let mut levels: Vec<(LineLocation, VecDeque<Token>)> = Vec::with_capacity(8);
levels.push((LineLocation{pos: 0, len: 0}, VecDeque::with_capacity(8)));
// Makes sure parenthesis are matched
@ -206,12 +206,12 @@ pub fn groupify(
let (l_now, v_now) = levels.last_mut().unwrap();
match t {
PreToken::PreGroupStart(l) => {
Token::PreGroupStart(l) => {
levels.push((l, VecDeque::with_capacity(8)));
i_level += 1;
},
PreToken::PreGroupEnd(l) => {
Token::PreGroupEnd(l) => {
let l = LineLocation {
pos: l_now.pos,
len: l.len + l.pos - l_now.pos
@ -226,7 +226,7 @@ pub fn groupify(
let (_, v_now) = levels.last_mut().unwrap();
lookback(&mut v)?;
v_now.push_back(PreToken::PreGroup(l, v));
v_now.push_back(Token::PreGroup(l, v));
},
_ => {
@ -252,12 +252,12 @@ pub fn groupify(
if v.len() == 0 { return Err((l, ParserError::EmptyGroup)) }
lookback(&mut v)?;
v_now.push_back(PreToken::PreGroup(l, v));
v_now.push_back(Token::PreGroup(l, v));
}
let (_, mut v) = levels.pop().unwrap();
lookback(&mut v)?;
return Ok(PreToken::PreGroup(LineLocation{pos:0, len:0}, v));
return Ok(Token::PreGroup(LineLocation{pos:0, len:0}, v));
}

View File

@ -1,24 +1,24 @@
use std::collections::VecDeque;
use super::super::{
PreToken,
Token,
LineLocation,
Operator
};
// Called whenever a token is finished.
#[inline(always)]
fn push_token(g: &mut VecDeque<PreToken>, t: Option<PreToken>, stop_i: usize) {
fn push_token(g: &mut VecDeque<Token>, t: Option<Token>, stop_i: usize) {
if t.is_none() { return }
let mut t = t.unwrap();
match t {
PreToken::PreGroupStart(ref mut l)
| PreToken::PreGroupEnd(ref mut l)
| PreToken::PreOperator(ref mut l, _)
| PreToken::PreQuantity(ref mut l, _)
| PreToken::PreWord(ref mut l, _)
Token::PreGroupStart(ref mut l)
| Token::PreGroupEnd(ref mut l)
| Token::PreOperator(ref mut l, _)
| Token::PreQuantity(ref mut l, _)
| Token::PreWord(ref mut l, _)
=> {
*l = LineLocation{
pos: l.pos,
@ -26,22 +26,22 @@ fn push_token(g: &mut VecDeque<PreToken>, t: Option<PreToken>, stop_i: usize) {
};
},
PreToken::PreGroup(_,_)
| PreToken::Container(_)
Token::PreGroup(_,_)
| Token::Container(_)
=> panic!()
};
// `2e` isn't exponential notation, it's 2*e.
// If a number ends in `e`, disconnect the `e` and make it a word.
if let PreToken::PreQuantity(l, s) = &t {
if let Token::PreQuantity(l, s) = &t {
let last = &s[s.len()-1..];
if last == "e" {
g.push_back(PreToken::PreQuantity(
g.push_back(Token::PreQuantity(
LineLocation { pos: l.pos, len: l.len-1 },
String::from(&s[0..s.len()-1])
));
g.push_back(PreToken::PreWord(
g.push_back(Token::PreWord(
LineLocation { pos: l.pos + l.len - 1, len: 1 },
String::from("e")
));
@ -51,9 +51,9 @@ fn push_token(g: &mut VecDeque<PreToken>, t: Option<PreToken>, stop_i: usize) {
}
// Some operators are written as words.
if let PreToken::PreWord(l, s) = &t {
if let Token::PreWord(l, s) = &t {
if Operator::from_string(s).is_some() {
t = PreToken::PreOperator(*l, s.clone());
t = Token::PreOperator(*l, s.clone());
}
}
@ -61,9 +61,9 @@ fn push_token(g: &mut VecDeque<PreToken>, t: Option<PreToken>, stop_i: usize) {
}
/// Turns a string into Tokens. First stage of parsing.
pub fn tokenize(input: &String) -> VecDeque<PreToken> {
let mut t: Option<PreToken> = None; // The current token we're reading
let mut g: VecDeque<PreToken> = VecDeque::with_capacity(32);
pub fn tokenize(input: &String) -> VecDeque<Token> {
let mut t: Option<Token> = None; // The current token we're reading
let mut g: VecDeque<Token> = VecDeque::with_capacity(32);
for (i, c) in input.chars().enumerate() {
@ -74,7 +74,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
match &mut t {
// If we're already building a number,
// append.
Some(PreToken::PreQuantity(_, val)) => {
Some(Token::PreQuantity(_, val)) => {
val.push(if c == ',' {'.'} else {c});
},
@ -82,7 +82,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// previous token and start one.
_ => {
push_token(&mut g, t, i);
t = Some(PreToken::PreQuantity(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::PreQuantity(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
},
@ -91,12 +91,12 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// Can be both a word or a number.
'e' => {
match &mut t {
Some(PreToken::PreWord(_, val)) => { val.push(c); },
Some(PreToken::PreQuantity(_, val)) => { val.push(c); },
Some(Token::PreWord(_, val)) => { val.push(c); },
Some(Token::PreQuantity(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(PreToken::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
}
@ -106,7 +106,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// or it can specify a negative exponent.
'-' | '+' => {
match &mut t {
Some(PreToken::PreQuantity(_, val)) => {
Some(Token::PreQuantity(_, val)) => {
if &val[val.len()-1..] == "e" {
// If the current number ends in an `e`,
// this negative specifies a negative exponent
@ -116,7 +116,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// Otherwise, end the number.
// We probably have a subtraction.
push_token(&mut g, t, i);
t = Some(PreToken::PreOperator(
t = Some(Token::PreOperator(
LineLocation{pos: i, len: 1},
String::from(c)
));
@ -126,7 +126,7 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// This may be a negative or a subtraction
_ => {
push_token(&mut g, t, i);
t = Some(PreToken::PreOperator(
t = Some(Token::PreOperator(
LineLocation{pos: i, len: 1},
String::from(c)
));
@ -139,10 +139,10 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
'^'|'!'|'%'|'='
=> {
match &mut t {
Some(PreToken::PreOperator(_, val)) => { val.push(c); },
Some(Token::PreOperator(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(PreToken::PreOperator(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::PreOperator(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
},
@ -150,11 +150,11 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// Group
'(' => {
push_token(&mut g, t, i);
t = Some(PreToken::PreGroupStart(LineLocation{pos: i, len: 0}));
t = Some(Token::PreGroupStart(LineLocation{pos: i, len: 0}));
},
')' => {
push_token(&mut g, t, i);
t = Some(PreToken::PreGroupEnd(LineLocation{pos: i, len: 0}));
t = Some(Token::PreGroupEnd(LineLocation{pos: i, len: 0}));
},
// Space. Basic seperator.
@ -166,11 +166,11 @@ pub fn tokenize(input: &String) -> VecDeque<PreToken> {
// Word
_ => {
match &mut t {
Some(PreToken::PreWord(_, val)) => { val.push(c); },
Some(Token::PreWord(_, val)) => { val.push(c); },
_ => {
push_token(&mut g, t, i);
t = Some(PreToken::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
t = Some(Token::PreWord(LineLocation{pos: i, len: 0}, String::from(c)));
}
};
}

View File

@ -2,25 +2,25 @@ use std::collections::VecDeque;
use crate::context::Context;
use super::super::{
PreToken,
Token,
ParserError,
LineLocation,
Token,
Expression,
Operator
};
fn treeify_binary(
i: usize,
g_inner: &mut VecDeque<PreToken>,
g_inner: &mut VecDeque<Token>,
context: &Context
) -> Result<bool, (LineLocation, ParserError)> {
let this: &PreToken = &g_inner[i];
let this: &Token = &g_inner[i];
if i == 0 {
// This binary operator is at the end of an expression.
let l = match this {
PreToken::PreOperator(l, _) => l,
Token::PreOperator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -32,7 +32,7 @@ fn treeify_binary(
&g_inner[i-1]
} else {
let l = match this {
PreToken::PreOperator(l, _) => l,
Token::PreOperator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -44,7 +44,7 @@ fn treeify_binary(
&g_inner[i+1]
} else {
let l = match this {
PreToken::PreOperator(l, _) => l,
Token::PreOperator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -55,7 +55,7 @@ fn treeify_binary(
if let PreToken::PreOperator(l, s) = left {
if let Token::PreOperator(l, s) = left {
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
let o = o.unwrap();
@ -74,7 +74,7 @@ fn treeify_binary(
}
}
if let PreToken::PreOperator(l, s) = right {
if let Token::PreOperator(l, s) = right {
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
let o = o.unwrap();
@ -96,7 +96,7 @@ fn treeify_binary(
// This operator
let this_op = {
let PreToken::PreOperator(l, s) = this else {panic!()};
let Token::PreOperator(l, s) = this else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -104,14 +104,14 @@ fn treeify_binary(
// The operators contesting our arguments
let left_op = if i > 1 {
let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let Token::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
} else { None };
let right_op = if i < g_inner.len()-2 {
let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let Token::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
@ -126,22 +126,22 @@ fn treeify_binary(
let left_pre = g_inner.remove(i-1).unwrap();
let this_pre = g_inner.remove(i-1).unwrap();
let right_pre = g_inner.remove(i-1).unwrap();
let left: Token; let right: Token;
if let PreToken::PreGroup(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_token(context)?;}
if let PreToken::PreGroup(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_token(context)?;}
let left: Expression; let right: Expression;
if let Token::PreGroup(_, _) = right_pre { right = treeify(right_pre, context)?; } else {right = right_pre.to_expression(context)?;}
if let Token::PreGroup(_, _) = left_pre { left = treeify(left_pre, context)?; } else {left = left_pre.to_expression(context)?;}
let o = {
let PreToken::PreOperator(_, s) = this_pre else {panic!()};
let Token::PreOperator(_, s) = this_pre else {panic!()};
let o = Operator::from_string(&s);
if o.is_none() { panic!() }
o.unwrap()
};
let mut new_token_args: VecDeque<Token> = VecDeque::with_capacity(2);
let mut new_token_args: VecDeque<Expression> = VecDeque::with_capacity(2);
new_token_args.push_back(left);
new_token_args.push_back(right);
g_inner.insert(i-1, PreToken::Container(o.into_token(new_token_args)));
g_inner.insert(i-1, Token::Container(o.into_expression(new_token_args)));
return Ok(true);
} else {
@ -151,20 +151,20 @@ fn treeify_binary(
fn treeify_unary(
i: usize,
g_inner: &mut VecDeque<PreToken>,
g_inner: &mut VecDeque<Token>,
left_associative: bool,
context: &Context
) -> Result<bool, (LineLocation, ParserError)> {
let this: &PreToken = &g_inner[i];
let next: &PreToken;
let this: &Token = &g_inner[i];
let next: &Token;
if left_associative {
next = {
if i > 0 {
&g_inner[i-1]
} else {
let l = match this {
PreToken::PreOperator(l, _) => l,
Token::PreOperator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -176,7 +176,7 @@ fn treeify_unary(
&g_inner[i+1]
} else {
let l = match this {
PreToken::PreOperator(l, _) => l,
Token::PreOperator(l, _) => l,
_ => panic!()
};
return Err((*l, ParserError::Syntax));
@ -186,7 +186,7 @@ fn treeify_unary(
// We need to check the element after unary operators too.
// Bad syntax like `3!3` won't be caught otherwise.
let prev: Option<&PreToken>;
let prev: Option<&Token>;
if left_associative {
prev = if i < g_inner.len()-1 { Some(&g_inner[i+1]) } else {None};
} else {
@ -194,7 +194,7 @@ fn treeify_unary(
}
if prev.is_some() {
if let PreToken::PreOperator(_,_) = prev.unwrap() {
if let Token::PreOperator(_,_) = prev.unwrap() {
} else {
return Err((
*this.get_line_location(),
@ -203,7 +203,7 @@ fn treeify_unary(
}
}
if let PreToken::PreOperator(l, _) = next {
if let Token::PreOperator(l, _) = next {
let tl = *this.get_line_location();
return Err((
LineLocation{pos: tl.pos, len: l.pos - tl.pos + l.len},
@ -214,7 +214,7 @@ fn treeify_unary(
// This operator
let this_op = {
let PreToken::PreOperator(l, s) = this else {panic!()};
let Token::PreOperator(l, s) = this else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -223,14 +223,14 @@ fn treeify_unary(
// The operator contesting our argument
let next_op = if left_associative {
if i > 1 {
let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let Token::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
} else { None }
} else {
if i < g_inner.len()-2 {
let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let Token::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap())
@ -239,28 +239,28 @@ fn treeify_unary(
if next_op.is_none() || this_op > next_op.unwrap() {
let this_pre = g_inner.remove(i).unwrap();
let next_pre: PreToken; let next: Token;
let next_pre: Token; let next: Expression;
if left_associative {
next_pre = g_inner.remove(i-1).unwrap();
} else {
next_pre = g_inner.remove(i).unwrap();
}
if let PreToken::PreGroup(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_token(context)? }
if let Token::PreGroup(_, _) = next_pre { next = treeify(next_pre, context)?; } else { next = next_pre.to_expression(context)? }
let o = {
let PreToken::PreOperator(_, s) = this_pre else {panic!()};
let Token::PreOperator(_, s) = this_pre else {panic!()};
let o = Operator::from_string(&s);
if o.is_none() { panic!() }
o.unwrap()
};
let mut new_token_args: VecDeque<Token> = VecDeque::with_capacity(3);
let mut new_token_args: VecDeque<Expression> = VecDeque::with_capacity(3);
new_token_args.push_back(next);
if left_associative {
g_inner.insert(i-1, PreToken::Container(o.into_token(new_token_args)));
g_inner.insert(i-1, Token::Container(o.into_expression(new_token_args)));
} else {
g_inner.insert(i, PreToken::Container(o.into_token(new_token_args)));
g_inner.insert(i, Token::Container(o.into_expression(new_token_args)));
}
return Ok(true);
@ -274,12 +274,12 @@ fn treeify_unary(
pub fn treeify(
mut g: PreToken,
mut g: Token,
context: &Context
) -> Result<Token, (LineLocation, ParserError)> {
) -> Result<Expression, (LineLocation, ParserError)> {
let g_inner: &mut VecDeque<PreToken> = match g {
PreToken::PreGroup(_, ref mut x) => x,
let g_inner: &mut VecDeque<Token> = match g {
Token::PreGroup(_, ref mut x) => x,
_ => panic!()
};
@ -300,7 +300,7 @@ pub fn treeify(
// Convert preoperators
// If not an operator, move on.
let this_op = match &g_inner[i] {
PreToken::PreOperator(l, s) => {
Token::PreOperator(l, s) => {
let o = Operator::from_string(&s);
if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap()
@ -342,13 +342,13 @@ pub fn treeify(
let g = g_inner.pop_front().unwrap();
return match g {
// Catch edge cases
PreToken::PreOperator(l, _) => {
Token::PreOperator(l, _) => {
Err((l, ParserError::Syntax))
},
PreToken::PreGroup(_,_) => {
Token::PreGroup(_,_) => {
treeify(g, context)
},
_ => { Ok(g.to_token(context)?) }
_ => { Ok(g.to_expression(context)?) }
};
}