Improved tuple parsing

pull/2/head
Mark 2023-08-05 09:29:59 -07:00
parent c0b0402a7d
commit fbd984c36e
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
7 changed files with 116 additions and 51 deletions

View File

@ -11,6 +11,7 @@ pub enum DaisyError {
BadNumber,
BadVariable,
BadFunction,
BadTuple,
// Evaluation errors
BadMath,
@ -58,7 +59,12 @@ impl DaisyError {
return FormattedText::new(
"[e]Syntax Error:[n] Bad function name".to_string()
);
}
},
DaisyError::BadTuple => {
return FormattedText::new(
"[e]Syntax Error:[n] Bad tuple syntax".to_string()
);
},
DaisyError::BadMath => {

View File

@ -1,4 +1,3 @@
use std::collections::VecDeque;
use crate::parser::LineLocation;
use crate::quantity::Quantity;
use crate::parser::Operator;
@ -59,32 +58,6 @@ pub fn eval_operator(g: &Expression, context: &mut Context) -> Result<Option<Exp
return Ok(Some(r));
},
Operator::Tuple => {
if args.len() != 2 { panic!() };
let a = &args[0];
let b = &args[1];
let mut loc = *op_loc;
let mut vec: VecDeque<Expression> = VecDeque::new();
if let Expression::Tuple(l, v) = a {
loc += *l;
for i in v { vec.push_back(i.clone()) }
} else {
loc += a.get_linelocation();
vec.push_back(a.clone())
}
if let Expression::Tuple(l, v) = b {
loc += *l;
for i in v { vec.push_back(i.clone()) }
} else {
loc += b.get_linelocation();
vec.push_back(b.clone())
}
return Ok(Some(Expression::Tuple(loc, vec)))
}
Operator::Negative => {
if args.len() != 1 { panic!() };
let args = &args[0];

View File

@ -15,9 +15,7 @@ use super::Function;
pub enum Operator {
// When adding operators, don't forget to update help command text.
// It isn't automatically generated.
Tuple = 0,
ModuloLong, // Mod invoked with "mod"
ModuloLong = 0, // Mod invoked with "mod"
DivideLong, // Division invoked with "per"
UnitConvert,
Subtract,
@ -76,7 +74,6 @@ impl Operator {
}
return match s {
"," => {Some( Operator::Tuple )},
"+" => {Some( Operator::Add )},
"-" => {Some( Operator::Subtract )},
"neg" => {Some( Operator::Negative )},
@ -155,14 +152,6 @@ impl Operator {
pub fn print(&self, args: &VecDeque<Expression>) -> String {
match self {
Operator::Tuple => {
return format!(
"{}, {}",
self.add_parens_to_arg(&args[0]),
self.add_parens_to_arg(&args[1])
);
},
Operator::Negative => {
return format!("-{}", self.add_parens_to_arg(&args[0]));
},

View File

@ -209,6 +209,13 @@ pub fn groupify(
let mut levels: Vec<(LineLocation, VecDeque<Token>)> = Vec::with_capacity(8);
levels.push((LineLocation{pos: 0, len: last_linelocation.pos + last_linelocation.len}, VecDeque::with_capacity(8)));
// Group types
// if true, this is a tuple.
// if false, this is a group.
let mut is_tuple: Vec<bool> = Vec::with_capacity(8);
is_tuple.push(false);
// Makes sure parenthesis are matched
let mut i_level = 0;
@ -219,6 +226,7 @@ pub fn groupify(
match t {
Token::GroupStart(l) => {
levels.push((l, VecDeque::with_capacity(8)));
is_tuple.push(false);
i_level += 1;
},
@ -234,14 +242,23 @@ pub fn groupify(
let (_, v_now) = levels.last_mut().unwrap();
lookback(&mut v, context)?;
v_now.push_back(Token::Group(l, v));
let q = is_tuple.pop().unwrap();
if q {
v_now.push_back(Token::new_tuple(l, v)?);
} else {
v_now.push_back(Token::Group(l, v));
}
},
Token::TupleDelim(_) => {
*is_tuple.last_mut().unwrap() = true;
v_now.push_back(t);
},
_ => {
v_now.push_back(t);
}
}
}
/*
@ -260,12 +277,27 @@ pub fn groupify(
if v.len() == 0 { return Err((l, DaisyError::EmptyGroup)) }
lookback(&mut v, context)?;
v_now.push_back(Token::Group(l, v));
let q = is_tuple.pop().unwrap();
if q {
v_now.push_back(Token::new_tuple(l, v)?);
} else {
v_now.push_back(Token::Group(l, v));
}
}
let (_, mut v) = levels.pop().unwrap();
let (l, mut v) = levels.pop().unwrap();
let q = is_tuple.pop().unwrap();
if q {
return Err((l, DaisyError::BadTuple));
}
lookback(&mut v, context)?;
return Ok(Token::Group(LineLocation{pos:0, len:last_linelocation.pos + last_linelocation.len}, v));
return Ok(Token::Group(
LineLocation{pos:0, len:last_linelocation.pos + last_linelocation.len},
v
));
}

View File

@ -20,7 +20,8 @@ fn push_token(
let mut t = t.unwrap();
match t {
Token::GroupStart(ref mut l)
Token::TupleDelim(ref mut l)
| Token::GroupStart(ref mut l)
| Token::GroupEnd(ref mut l)
| Token::Operator(ref mut l, _)
| Token::Quantity(ref mut l, _)
@ -33,6 +34,7 @@ fn push_token(
},
Token::Group(_,_)
| Token::Tuple(_,_)
| Token::Container(_)
=> unreachable!()
};
@ -143,10 +145,7 @@ pub fn tokenize(input: &String, context: &Context) -> VecDeque<Token> {
',' => {
push_token(&mut g, t, i, context);
t = Some(Token::Operator(
LineLocation{pos: i, len: 1},
String::from(c)
));
t = Some(Token::TupleDelim(LineLocation{pos: i, len: 1}));
},
// Operator

View File

@ -124,6 +124,9 @@ fn treeify_binary(
if let Token::Group(l, _) = right_pre {
right = treeify(right_pre, context)?;
right.set_linelocation(&(right.get_linelocation() + l));
} else if let Token::Tuple(l, _) = right_pre {
right = treeify(right_pre, context)?;
right.set_linelocation(&(right.get_linelocation() + l));
} else {
right = right_pre.to_expression(context)?;
}
@ -131,6 +134,9 @@ fn treeify_binary(
if let Token::Group(l, _) = left_pre {
left = treeify(left_pre, context)?;
left.set_linelocation(&(left.get_linelocation() + l));
} else if let Token::Tuple(l, _) = left_pre {
left = treeify(left_pre, context)?;
left.set_linelocation(&(left.get_linelocation() + l));
} else {
left = left_pre.to_expression(context)?;
}
@ -251,6 +257,9 @@ fn treeify_unary(
if let Token::Group(l, _) = next_pre {
next = treeify(next_pre, context)?;
next.set_linelocation(&(next.get_linelocation() + l));
} else if let Token::Tuple(l, _) = next_pre {
next = treeify(next_pre, context)?;
next.set_linelocation(&(next.get_linelocation() + l));
} else {
next = next_pre.to_expression(context)?;
}
@ -286,8 +295,17 @@ pub fn treeify(
mut g: Token,
context: &Context
) -> Result<Expression, (LineLocation, DaisyError)> {
let (l, g_inner): (LineLocation, &mut VecDeque<Token>) = match g {
Token::Group(l, ref mut x) => (l, x),
Token::Tuple(l, parts) => {
let mut t: VecDeque<Expression> = VecDeque::new();
for p in parts {
t.push_back(treeify(p, context)?);
};
return Ok(Expression::Tuple(l, t));
},
_ => panic!()
};
@ -358,10 +376,12 @@ pub fn treeify(
Token::Operator(l, _) => {
Err((l, DaisyError::Syntax))
},
Token::Tuple(_, _) |
Token::Group(_,_) => {
treeify(g, context)
},
_ => { Ok(g.to_expression(context)?) }
};
}

View File

@ -17,9 +17,11 @@ pub enum Token {
Word(LineLocation, String),
Operator(LineLocation, String),
TupleDelim(LineLocation),
GroupStart(LineLocation),
GroupEnd(LineLocation),
Group(LineLocation, VecDeque<Token>),
Tuple(LineLocation, VecDeque<Token>),
// Never parsed from input, used to build a tree.
Container(Expression)
@ -32,9 +34,11 @@ impl Token {
Token::Quantity(l, _)
| Token::Word(l, _)
| Token::Operator(l, _)
| Token::TupleDelim(l)
| Token::GroupStart(l)
| Token::GroupEnd(l)
| Token::Group(l, _)
| Token::Tuple(l, _)
=> l.clone(),
Token::Container(_) => panic!("Containers do not have a linelocation.")
@ -47,9 +51,11 @@ impl Token {
Token::Quantity(l, _)
| Token::Word(l, _)
| Token::Operator(l, _)
| Token::TupleDelim(l)
| Token::GroupStart(l)
| Token::GroupEnd(l)
| Token::Group(l, _)
| Token::Tuple(l, _)
=> l,
Token::Container(_) => panic!("Containers do not have a linelocation.")
@ -93,9 +99,49 @@ impl Token {
Token::Operator(_,_)
| Token::GroupStart(_)
| Token::GroupEnd(_)
| Token::Group(_, _)
| Token::Group(_,_)
| Token::TupleDelim(_)
| Token::Tuple(_,_)
=> panic!("This token cannot be converted to an expression")
};
}
pub fn new_tuple(l: LineLocation, v: VecDeque<Token>) -> Result<Token, (LineLocation, DaisyError)> {
let mut parts: VecDeque<Token> = VecDeque::new();
let mut now: VecDeque<Token> = VecDeque::new();
let mut loc = LineLocation::new_zero();
for t in v {
match t {
Token::TupleDelim(_) => {
if now.len() == 0 {
return Err((l, DaisyError::BadTuple))
}
let g = Token::Group(loc, now);
parts.push_back(g);
loc = LineLocation::new_zero();
now = VecDeque::new();
},
_ => {
loc += t.get_linelocation();
now.push_back(t);
}
}
}
// Push last group
if now.len() == 0 {
return Err((l, DaisyError::BadTuple))
}
let g = Token::Group(loc, now);
parts.push_back(g);
return Ok(Token::Tuple(l, parts));
}
}