From c1a33b2b511686bf6ef0b861e43cf3065e666186 Mon Sep 17 00:00:00 2001 From: Kasper Date: Wed, 1 Jul 2026 08:47:00 +0200 Subject: [PATCH] Add `deg`, `degrees` ambiguous unit --- CHANGELOG.md | 2 +- src/lexer.rs | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62b4c81..39f0141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Next - Add ambiguous unit resolution depending on context and user locale. -- Add ambiguous units like `pound` +- Add ambiguous units like `pound`, `deg`, `$` and `kr` - Add currency symbols like `€` and `kr` - Add `as` and `into` keywords for unit conversion - Fix some units not being simplified diff --git a/src/lexer.rs b/src/lexer.rs index e712b11..2ea4efe 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -22,7 +22,7 @@ fn is_word_char_str(input: &str) -> bool { "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" => true, "Ω" | "Ω" | "µ" | "μ" | "ł" | "$" | "€" | "£" | "₹" | "₪" | "¥" | "₩" | "₱" | "฿" | "₺" - | "₴" | "₫" | "đ" | "Đ" | "č" => true, + | "₴" | "₫" | "đ" | "Đ" | "č" | "°" => true, _ => false, } } @@ -643,9 +643,27 @@ fn lex_word(word: &str, lexer: &mut Lexer) -> Result<(), String> { "fps" => Token::unit(FeetPerSecond), "kn" | "kt" | "knot" | "knots" => Token::unit(Knot), - "k" | "kelvin" | "kelvins" => Token::unit(Kelvin), - "c" | "celsius" => Token::unit(Celsius), - "f" | "fahrenheit" | "fahrenheits" => Token::unit(Fahrenheit), + "°" | "deg" | "degs" | "degree" | "degrees" => match read_word(lexer).as_str() { + "k" | "kelvin" | "kelvins" => Token::unit(Kelvin), + "c" | "celsius" | "celcius" => Token::unit(Celsius), + "f" | "fahrenheit" | "fahrenheits" | "farenheit" | "farenheits" => { + Token::unit(Fahrenheit) + } + other => { + let token = match get_region().as_str() { + "BS" | "BZ" | "KY" | "PR" | "PW" | "US" => Fahrenheit, + _ => Celsius, + }; + lexer.tokens.push(Token::unit(token)); + lex_word_if_non_empty(other, lexer)?; + return Ok(()); + } + }, + "k" | "°k" | "kelvin" | "kelvins" => Token::unit(Kelvin), + "c" | "°c" | "celsius" | "celcius" => Token::unit(Celsius), + "f" | "°f" | "fahrenheit" | "fahrenheits" | "farenheit" | "farenheits" => { + Token::unit(Fahrenheit) + } "AU$" => Token::unit(AUD), "R$" => Token::unit(BRL), -- 2.51.2