From 5af3e15dd58cf3053ff23936efb0dfc790c02ded Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Tue, 28 May 2024 20:16:32 +0100 Subject: [PATCH] Nicer lexer error for "!" --- .../modules/main/cc/internal/syntax/errors.lua | 17 +++++++++++++++++ .../modules/main/cc/internal/syntax/lexer.lua | 3 +++ .../modules/cc/internal/syntax/lexer_spec.md | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/errors.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/errors.lua index c425a26dc..ea5cf5d1d 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/errors.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/errors.lua @@ -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. diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/lexer.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/lexer.lua index 0b9d45a7e..9faf3b5a9 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/lexer.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/syntax/lexer.lua @@ -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 diff --git a/projects/core/src/test/resources/test-rom/spec/modules/cc/internal/syntax/lexer_spec.md b/projects/core/src/test/resources/test-rom/spec/modules/cc/internal/syntax/lexer_spec.md index a0049c367..c6e983566 100644 --- a/projects/core/src/test/resources/test-rom/spec/modules/cc/internal/syntax/lexer_spec.md +++ b/projects/core/src/test/resources/test-rom/spec/modules/cc/internal/syntax/lexer_spec.md @@ -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.