Turned panics into errors

This commit is contained in:
Kasper 2020-01-15 17:45:53 +01:00
parent fda6550d1a
commit febd5366e8
3 changed files with 12 additions and 19 deletions

View File

@ -94,7 +94,7 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
}
},
Token::FunctionIdentifier(function) => {
let child_node = children.get(0).expect("Paren has no child[0]");
let child_node = children.get(0).ok_or("Paren has no child[0]")?;
let child_answer = evaluate_node(child_node)?;
match function {
Cbrt => {
@ -178,21 +178,21 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
}
}
Token::Unit(unit) => {
let child_node = children.get(0).expect("Unit has no child[0]");
let child_node = children.get(0).ok_or("Unit has no child[0]")?;
let child_answer = evaluate_node(child_node)?;
Ok(Number::new(child_answer.value, unit.clone()))
},
Token::Negative => {
let child_node = children.get(0).expect("Negative has no child[0]");
let child_node = children.get(0).ok_or("Negative has no child[0]")?;
let child_answer = evaluate_node(child_node)?;
Ok(Number::new(-child_answer.value, child_answer.unit))
},
Token::Paren => {
let child_node = children.get(0).expect("Paren has no child[0]");
let child_node = children.get(0).ok_or("Paren has no child[0]")?;
return evaluate_node(child_node)
},
Token::UnaryOperator(operator) => {
let child_node = children.get(0).expect(format!("Token {:?} has no child[0]", token).as_str());
let child_node = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?;
let child_answer = evaluate_node(child_node)?;
match operator {
Percent => {
@ -208,8 +208,8 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
}
},
Token::TextOperator(operator) => {
let left_child = children.get(0).expect(format!("Token {:?} has no child[0]", token).as_str());
let right_child = children.get(1).expect(format!("Token {:?} has no child[1]", token).as_str());
let left_child = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?;
let right_child = children.get(1).ok_or(format!("Token {:?} has no child[1]", token))?;
match operator {
To => {
@ -233,8 +233,8 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Number, String> {
}
},
Token::Operator(operator) => {
let left_child = children.get(0).expect(format!("Token {:?} has no child[0]", token).as_str());
let right_child = children.get(1).expect(format!("Token {:?} has no child[1]", token).as_str());
let left_child = children.get(0).ok_or(format!("Token {:?} has no child[0]", token))?;
let right_child = children.get(1).ok_or(format!("Token {:?} has no child[1]", token))?;
let left = evaluate_node(left_child)?;
let right = evaluate_node(right_child)?;
match operator {

View File

@ -90,15 +90,8 @@ pub fn lex(input: &str, allow_trailing_operators: bool, default_degree: Unit) ->
Some('2') | Some('3') => {
byte_index += '2'.len_utf8();
chars.next();
// match chars.peek().unwrap_or(&'-') {
// ...and if the 2 or 3 isn't part of a different number
// '.' | '0'..='9' => {
// is_multidimensional = false;
// },
// we allow everything else and let the parser throw errors
// for things like km2pi
// _ => {}
// }
// we dont validate what comes after because it will be caught
// by the parser anyway (for example 3m35)
},
_ => is_multidimensional = false,
}

View File

@ -267,7 +267,7 @@ fn parse_level_6(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), S
// level 7 precedence: numbers, parens
fn parse_level_7(tokens: &TokenVector, pos: usize) -> Result<(AstNode, usize), String> {
let token: &Token = tokens.get(pos).expect(&format!("Unexpected end of input at {}", pos));
let token: &Token = tokens.get(pos).ok_or(format!("Unexpected end of input at {}", pos))?;
match token {
&Token::Number(_number) => {
let node = AstNode::new(token.clone());