1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-09 08:50:29 +00:00

Nicer lexer error for "!"

This commit is contained in:
Jonathan Coates 2024-05-28 20:16:32 +01:00
parent 209b1ddbf9
commit 5af3e15dd5
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 31 additions and 0 deletions

View File

@ -284,6 +284,23 @@ function errors.wrong_ne(start_pos, end_pos)
}
end
--[[- `!` was used instead of `not`.
@tparam number start_pos The start position of the token.
@tparam number end_pos The end position of the token.
@return The resulting parse error.
]]
function errors.wrong_not(start_pos, end_pos)
expect(1, start_pos, "number")
expect(2, end_pos, "number")
return {
"Unexpected character.",
annotate(start_pos, end_pos),
"Tip: Replace this with " .. code("not") .. " to negate a boolean.",
}
end
--[[- An unexpected character was used.
@tparam number pos The position of this character.

View File

@ -327,6 +327,9 @@ local function lex_token(context, str, pos)
elseif contents == "!=" or contents == "<>" then
context.report(errors.wrong_ne, pos, end_pos)
return tokens.NE, end_pos
elseif contents == "!" then
context.report(errors.wrong_not, pos, end_pos)
return tokens.NOT, end_pos
end
end

View File

@ -265,6 +265,7 @@ if a != b then end
if a ~= b then end
if a && b then end
if a || b then end
if ! a then end
```
```txt
@ -307,6 +308,16 @@ Tip: Replace this with or to check if either value is true.
4:9-4:9 IDENT b
4:11-4:14 THEN then
4:16-4:18 END end
5:1-5:2 IF if
Unexpected character.
|
5 | if ! a then end
| ^
Tip: Replace this with not to negate a boolean.
5:4-5:4 NOT !
5:6-5:6 IDENT a
5:8-5:11 THEN then
5:13-5:15 END end
```
For entirely unknown glyphs we should just give up and return an `ERROR` token.