Skip to content

Commit

Permalink
Merge pull request #268 from frectonz/main
Browse files Browse the repository at this point in the history
Support ¥ symbol for JPY
  • Loading branch information
printfn authored Feb 23, 2024
2 parents 465413f + dc78e37 commit b042ecb
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Ident {
pub(crate) fn is_prefix_unit(&self) -> bool {
// when changing this also make sure to change number output formatting
// lexer identifier splitting
self.0 == "$" || self.0 == "\u{a3}"
["$", "\u{a3}", "\u{a5}"].contains(&&*self.0)
}

pub(crate) fn serialize(&self, write: &mut impl io::Write) -> FResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn is_valid_in_ident(ch: char, prev: Option<char>) -> bool {
'㏌', '㏏', '㏐', '㏓', '㏔', '㏕', '㏖', '㏗', '㏙', '㏛', '㏜', '㏝',
];
let only_valid_by_themselves = ['%', '‰', '‱', '′', '″', '’', '”', 'π'];
let split_on_subsequent_digit = ['$', '£'];
let split_on_subsequent_digit = ['$', '£', '¥'];
let always_invalid = ['λ'];
if always_invalid.contains(&ch) {
false
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ pub mod test_utils {
"HKD" => 8.0,
"AUD" => 1.3,
"PLN" => 0.2,
"JPY" => 149.9,
_ => panic!("unknown currency {currency}"),
})
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/num/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ impl FormattedValue {
kind: SpanKind::Ident,
});
}
if self.unit_str == "$" || self.unit_str == "\u{a3}" && !attrs.plain_number {
if ["$", "\u{a3}", "\u{a5}"].contains(&self.unit_str.as_str()) && !attrs.plain_number {
spans.push(Span {
string: self.unit_str,
kind: SpanKind::Ident,
Expand Down
2 changes: 1 addition & 1 deletion core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn parse_apply_cont<'a>(input: &'a [Token], lhs: &Expr) -> ParseResult<'a> {
}
Expr::Apply(Box::new(lhs.clone()), Box::new(rhs))
}
// support e.g. '$5' or '£3'
// support e.g. '$5', '£3' or '¥10'
(Expr::Ident(i), Expr::Literal(Value::Num(_))) if i.is_prefix_unit() => {
Expr::Apply(Box::new(lhs.clone()), Box::new(rhs))
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/units/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ const CURRENCIES: &[UnitTuple] = &[
("$", "$", "USD", ""),
("euro", "euros", "EUR", ""),
("\u{20ac}", "\u{20ac}", "EUR", ""), // Euro symbol
("\u{a3}", "\u{a3}", "GBP", ""),
("\u{a3}", "\u{a3}", "GBP", ""), // £
("\u{a5}", "\u{a5}", "JPY", ""), // ¥
("AU$", "AU$", "AUD", ""),
("HK$", "HK$", "HKD", ""),
("NZ$", "NZ$", "NZD", ""),
Expand Down
5 changes: 5 additions & 0 deletions core/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5397,6 +5397,11 @@ fn gbp_symbol() {
test_eval("£5 + £3", "£8");
}

#[test]
fn jpy_symbol() {
test_eval("¥5 + ¥3", "¥8");
}

#[test]
fn two_statements() {
test_eval("2; 4", "4");
Expand Down

0 comments on commit b042ecb

Please sign in to comment.