From 8a7603bc6efb12d5a7a8adc5c74cef50728f5459 Mon Sep 17 00:00:00 2001 From: Kasper Date: Tue, 6 Jul 2021 07:07:37 +0200 Subject: [PATCH] Fix watt parsing --- src/lexer.rs | 56 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/lexer.rs b/src/lexer.rs index 8b281d6..f258347 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -55,16 +55,18 @@ pub fn read_word_plain(chars: &mut Peekable) -> String { /// Read next as a word, otherwise return empty string. /// Leading whitespace is ignored. A trailing digit may be included. pub fn read_word(first_c: &str, lexer: &mut Lexer) -> String { - // skip whitespace let chars = &mut lexer.chars; - while let Some(current_char) = chars.peek() { - if current_char.trim().is_empty() { - chars.next(); - } else { - break; + let mut word = first_c.trim().to_owned(); + if word == "" { + // skip whitespace + while let Some(current_char) = chars.peek() { + if current_char.trim().is_empty() { + chars.next(); + } else { + break; + } } } - let mut word = first_c.trim().to_owned(); while let Some(next_char) = chars.peek() { if is_alphabetic_extended_str(&next_char) { word += chars.next().unwrap(); @@ -178,9 +180,7 @@ pub fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> { "pi" => Token::Constant(Pi), "e" => Token::Constant(E), - - "plus" => Token::Operator(Plus), - + "mod" => Token::Operator(Modulo), "sqrt" => Token::FunctionIdentifier(Sqrt), @@ -469,45 +469,61 @@ pub fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> { "watt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(WattHour), - _ => Token::Unit(Watt) + other => { + lexer.tokens.push(Token::Unit(Watt)); + parse_token(word, lexer)?; + return Ok(()); + }, } } "kilowatt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(KilowattHour), - _ => Token::Unit(Kilowatt), + other => { + lexer.tokens.push(Token::Unit(Kilowatt)); + parse_token(word, lexer)?; + return Ok(()); + }, } } "megawatt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(MegawattHour), - _ => Token::Unit(Megawatt), + other => { + lexer.tokens.push(Token::Unit(Megawatt)); + parse_token(word, lexer)?; + return Ok(()); + }, } } "gigawatt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(GigawattHour), - _ => Token::Unit(Gigawatt), + other => { + lexer.tokens.push(Token::Unit(Gigawatt)); + parse_token(word, lexer)?; + return Ok(()); + }, } } "terawatt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(TerawattHour), other => { - lexer.tokens.push(Token::Unit(Watt)); - parse_token(other, lexer)?; + lexer.tokens.push(Token::Unit(Terawatt)); + parse_token(word, lexer)?; return Ok(()); - } + }, } } "petawatt" => { match read_word("", lexer).as_str() { "hr" | "hrs" | "hour" | "hours" => Token::Unit(PetawattHour), other => { - lexer.tokens.push(Token::Unit(Watt)); - parse_token(other, lexer)?; + lexer.tokens.push(Token::Unit(Petawatt)); + parse_token(word, lexer)?; return Ok(()); - } + }, } }