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.
This commit is contained in:
Fabian Boehm 2024-09-18 22:27:00 +02:00
parent fc7be1c2a3
commit cdcf460edf
2 changed files with 10 additions and 2 deletions

View file

@ -405,12 +405,14 @@ impl<'s> State<'s> {
// Look for a function call. // Look for a function call.
// But not when it's an "x" followed by whitespace // But not when it's an "x" followed by whitespace
// - that's the alternative multiplication operator. // - 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()) && !(*next.first()? == 'x' && next.len() > 1 && next[1].is_whitespace())
{ {
let ident_len = next let ident_len = next
.iter() .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()); .unwrap_or(next.len());
let ident = &next[..ident_len]; let ident = &next[..ident_len];

View file

@ -127,6 +127,12 @@ not math 'blah()'
# CHECKERR: 'blah()' # CHECKERR: 'blah()'
# CHECKERR: ^~~^ # CHECKERR: ^~~^
# There is also no "Blah" function.
not math 'Blah()'
# CHECKERR: math: Error: Unknown function
# CHECKERR: 'Blah()'
# CHECKERR: ^~~^
math n + 4 math n + 4
# CHECKERR: math: Error: Unknown function # CHECKERR: math: Error: Unknown function
# CHECKERR: 'n + 4' # CHECKERR: 'n + 4'