Bladeren bron

Add support for currency characters

David Peter 2 jaren geleden
bovenliggende
commit
4a87550b56
3 gewijzigde bestanden met toevoegingen van 31 en 8 verwijderingen
  1. 4 2
      examples/custom_dimensions.nbt
  2. 6 4
      examples/workhours.nbt
  3. 21 2
      numbat/src/tokenizer.rs

+ 4 - 2
examples/custom_dimensions.nbt

@@ -1,11 +1,13 @@
 dimension Money
+
+@aliases(EUR, €)
 unit euro: Money
 
 dimension Person
 unit person: Person
 
-let lunch_cost = 85 euro
+let lunch_cost = 85 
 
 let cost_per_person = lunch_cost / 4 person
 
-assert_eq(cost_per_person, 21.25 euro / person, 0.01 euro / person)
+assert_eq(cost_per_person, 21.25 € / person, 0.01 € / person)

+ 6 - 4
examples/workhours.nbt

@@ -1,5 +1,7 @@
 dimension Money
-unit EUR: Money
+
+@aliases(EUR, €)
+unit euro: Money
 
 dimension WorkHour
 
@@ -14,11 +16,11 @@ unit workyear = 200 workdays
 
 unit FTE = 1 workyear per year
 
-let rate = 1000 EUR / workday
-assert_eq(0.5 million EUR / rate, 500 workday)
+let rate = 1000  / workday
+assert_eq(0.5 million  / rate, 500 workday)
 
 assert_eq(3 FTE * 0.5 years -> workdays, 300 workday)
 
-let budget = 0.5 million EUR
+let budget = 0.5 million 
 let duration = 0.5 year
 assert_eq(budget / rate / duration -> FTE, 5 FTE)

+ 21 - 2
numbat/src/tokenizer.rs

@@ -83,8 +83,15 @@ fn is_exponent_char(c: char) -> bool {
     matches!(c, '¹' | '²' | '³' | '⁴' | '⁵')
 }
 
+fn is_currency_char(c: char) -> bool {
+    let c_u32 = c as u32;
+
+    // See https://en.wikipedia.org/wiki/Currency_Symbols_(Unicode_block)
+    (c_u32 >= 0x20A0 && c_u32 <= 0x20CF) || c == '£' || c == '¥' || c == '$' || c == '฿'
+}
+
 fn is_identifier_char(c: char) -> bool {
-    (c.is_alphanumeric() || c == '_') && !is_exponent_char(c)
+    (c.is_alphanumeric() || c == '_' || is_currency_char(c)) && !is_exponent_char(c)
 }
 
 impl Tokenizer {
@@ -392,5 +399,17 @@ fn tokenize_basic() {
         ])
     );
 
-    assert!(tokenize("$").is_err());
+    assert!(tokenize("…").is_err());
+}
+
+#[test]
+fn test_is_currency_char() {
+    assert!(is_currency_char('€'));
+    assert!(is_currency_char('$'));
+    assert!(is_currency_char('¥'));
+    assert!(is_currency_char('£'));
+    assert!(is_currency_char('฿'));
+    assert!(is_currency_char('₿'));
+
+    assert!(!is_currency_char('E'));
 }