From cdcf460edf7bab6b04b5083b8b3d1b4060cac97b Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Wed, 18 Sep 2024 22:27:00 +0200 Subject: [PATCH] math: Nicer error for non-ascii-lowercase identifiers This gave a weird error when you did e.g. `math Foo / 6`: "Missing Operator" and only the "F" marked. Adding an operator here anywhere won't help, so calling this an "Unknown function" is closer to the truth. We also get nicer markings because we know the extent of the identifier. --- src/tinyexpr.rs | 6 ++++-- tests/checks/math.fish | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tinyexpr.rs b/src/tinyexpr.rs index 5d653f638..7c49d2f70 100644 --- a/src/tinyexpr.rs +++ b/src/tinyexpr.rs @@ -405,12 +405,14 @@ impl<'s> State<'s> { // Look for a function call. // But not when it's an "x" followed by whitespace // - that's the alternative multiplication operator. - if next.first()?.is_ascii_lowercase() + // We look for alphabetic here even tho all our function names are ASCII, + // in order to give a nicer error. + if next.first()?.is_alphabetic() && !(*next.first()? == 'x' && next.len() > 1 && next[1].is_whitespace()) { let ident_len = next .iter() - .position(|&c| !(c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_')) + .position(|&c| !(c.is_alphabetic() || c.is_ascii_digit() || c == '_')) .unwrap_or(next.len()); let ident = &next[..ident_len]; diff --git a/tests/checks/math.fish b/tests/checks/math.fish index 712160c5c..cb2c4bec3 100644 --- a/tests/checks/math.fish +++ b/tests/checks/math.fish @@ -127,6 +127,12 @@ not math 'blah()' # CHECKERR: 'blah()' # CHECKERR: ^~~^ +# There is also no "Blah" function. +not math 'Blah()' +# CHECKERR: math: Error: Unknown function +# CHECKERR: 'Blah()' +# CHECKERR: ^~~^ + math n + 4 # CHECKERR: math: Error: Unknown function # CHECKERR: 'n + 4'