Fix watt parsing

This commit is contained in:
Kasper 2021-07-06 07:07:37 +02:00
parent b435833b98
commit 8a7603bc6e

View File

@ -55,16 +55,18 @@ pub fn read_word_plain(chars: &mut Peekable<Graphemes>) -> String {
/// Read next as a word, otherwise return empty string. /// Read next as a word, otherwise return empty string.
/// Leading whitespace is ignored. A trailing digit may be included. /// Leading whitespace is ignored. A trailing digit may be included.
pub fn read_word(first_c: &str, lexer: &mut Lexer) -> String { pub fn read_word(first_c: &str, lexer: &mut Lexer) -> String {
// skip whitespace
let chars = &mut lexer.chars; let chars = &mut lexer.chars;
while let Some(current_char) = chars.peek() { let mut word = first_c.trim().to_owned();
if current_char.trim().is_empty() { if word == "" {
chars.next(); // skip whitespace
} else { while let Some(current_char) = chars.peek() {
break; 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() { while let Some(next_char) = chars.peek() {
if is_alphabetic_extended_str(&next_char) { if is_alphabetic_extended_str(&next_char) {
word += chars.next().unwrap(); word += chars.next().unwrap();
@ -179,8 +181,6 @@ pub fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
"pi" => Token::Constant(Pi), "pi" => Token::Constant(Pi),
"e" => Token::Constant(E), "e" => Token::Constant(E),
"plus" => Token::Operator(Plus),
"mod" => Token::Operator(Modulo), "mod" => Token::Operator(Modulo),
"sqrt" => Token::FunctionIdentifier(Sqrt), "sqrt" => Token::FunctionIdentifier(Sqrt),
@ -469,45 +469,61 @@ pub fn parse_word(word: &str, lexer: &mut Lexer) -> Result<(), String> {
"watt" => { "watt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(WattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(WattHour),
_ => Token::Unit(Watt) other => {
lexer.tokens.push(Token::Unit(Watt));
parse_token(word, lexer)?;
return Ok(());
},
} }
} }
"kilowatt" => { "kilowatt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(KilowattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(KilowattHour),
_ => Token::Unit(Kilowatt), other => {
lexer.tokens.push(Token::Unit(Kilowatt));
parse_token(word, lexer)?;
return Ok(());
},
} }
} }
"megawatt" => { "megawatt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(MegawattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(MegawattHour),
_ => Token::Unit(Megawatt), other => {
lexer.tokens.push(Token::Unit(Megawatt));
parse_token(word, lexer)?;
return Ok(());
},
} }
} }
"gigawatt" => { "gigawatt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(GigawattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(GigawattHour),
_ => Token::Unit(Gigawatt), other => {
lexer.tokens.push(Token::Unit(Gigawatt));
parse_token(word, lexer)?;
return Ok(());
},
} }
} }
"terawatt" => { "terawatt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(TerawattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(TerawattHour),
other => { other => {
lexer.tokens.push(Token::Unit(Watt)); lexer.tokens.push(Token::Unit(Terawatt));
parse_token(other, lexer)?; parse_token(word, lexer)?;
return Ok(()); return Ok(());
} },
} }
} }
"petawatt" => { "petawatt" => {
match read_word("", lexer).as_str() { match read_word("", lexer).as_str() {
"hr" | "hrs" | "hour" | "hours" => Token::Unit(PetawattHour), "hr" | "hrs" | "hour" | "hours" => Token::Unit(PetawattHour),
other => { other => {
lexer.tokens.push(Token::Unit(Watt)); lexer.tokens.push(Token::Unit(Petawatt));
parse_token(other, lexer)?; parse_token(word, lexer)?;
return Ok(()); return Ok(());
} },
} }
} }