From cbd2f97c5b13d7b7dbe2e62823447a8cecec77ee Mon Sep 17 00:00:00 2001 From: Eric Swanson Date: Tue, 22 Sep 2020 03:11:25 -0700 Subject: [PATCH] Add an error message about known reserved words (#322) --- .../bscript/compiler/file/ErrorListener.cpp | 22 ++++++++++++++++++- ...ors004-reserved-word-as-argument-name.err2 | 2 +- .../errors010-reserved-word-syntax-error.err1 | 1 + .../errors010-reserved-word-syntax-error.err2 | 1 + .../errors010-reserved-word-syntax-error.src | 2 ++ 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 testsuite/escript/errors/errors010-reserved-word-syntax-error.err1 create mode 100644 testsuite/escript/errors/errors010-reserved-word-syntax-error.err2 create mode 100644 testsuite/escript/errors/errors010-reserved-word-syntax-error.src diff --git a/pol-core/bscript/compiler/file/ErrorListener.cpp b/pol-core/bscript/compiler/file/ErrorListener.cpp index dad21d5cb8..5188d807b9 100644 --- a/pol-core/bscript/compiler/file/ErrorListener.cpp +++ b/pol-core/bscript/compiler/file/ErrorListener.cpp @@ -1,10 +1,19 @@ #include "ErrorListener.h" +#include + +#include "clib/maputil.h" #include "compiler/Profile.h" #include "compiler/Report.h" namespace Pol::Bscript::Compiler { +std::set easily_overlooked_reserved_words = { + "as", "byval", "double", "downto", "float", "hash", + "in", "integer", "is", "long", "out", "real", + "signed", "stack", "step", "string", "to", "unsigned" +}; + ErrorListener::ErrorListener( std::string pathname, Profile& profile ) : pathname( std::move( pathname ) ), profile( profile ) { @@ -19,10 +28,21 @@ void ErrorListener::propagate_errors_to( Report& report, const SourceFileIdentif } } -void ErrorListener::syntaxError( antlr4::Recognizer*, antlr4::Token* /*offendingSymbol*/, +void ErrorListener::syntaxError( antlr4::Recognizer*, antlr4::Token* offendingSymbol, size_t line, size_t charPositionInLine, const std::string& msg, std::exception_ptr ) { + if ( offendingSymbol ) + { + std::string symbol = offendingSymbol->getText(); + if ( easily_overlooked_reserved_words.count( symbol ) ) + { + fmt::Writer w; + w << "Did not expect reserved word '" << symbol << "' in this context.\n"; + error_messages.push_back( { w.str(), line, charPositionInLine + 1 } ); + } + } + error_messages.push_back( { msg, line, charPositionInLine+1 } ); } diff --git a/testsuite/escript/errors/errors004-reserved-word-as-argument-name.err2 b/testsuite/escript/errors/errors004-reserved-word-as-argument-name.err2 index 1a537c3ae4..0d9ce7d6a9 100644 --- a/testsuite/escript/errors/errors004-reserved-word-as-argument-name.err2 +++ b/testsuite/escript/errors/errors004-reserved-word-as-argument-name.err2 @@ -1 +1 @@ -errors004-reserved-word-as-argument-name.src:3:13: error: extraneous input 'downto' expecting {'unused', ')', IDENTIFIER} \ No newline at end of file +errors004-reserved-word-as-argument-name.src:3:13: error: Did not expect reserved word 'downto' in this context. \ No newline at end of file diff --git a/testsuite/escript/errors/errors010-reserved-word-syntax-error.err1 b/testsuite/escript/errors/errors010-reserved-word-syntax-error.err1 new file mode 100644 index 0000000000..53cd797bed --- /dev/null +++ b/testsuite/escript/errors/errors010-reserved-word-syntax-error.err1 @@ -0,0 +1 @@ +Non-identifier declared as a variable: 'is' \ No newline at end of file diff --git a/testsuite/escript/errors/errors010-reserved-word-syntax-error.err2 b/testsuite/escript/errors/errors010-reserved-word-syntax-error.err2 new file mode 100644 index 0000000000..545831c97c --- /dev/null +++ b/testsuite/escript/errors/errors010-reserved-word-syntax-error.err2 @@ -0,0 +1 @@ +errors010-reserved-word-syntax-error.src:1:5: error: Did not expect reserved word 'is' in this context. \ No newline at end of file diff --git a/testsuite/escript/errors/errors010-reserved-word-syntax-error.src b/testsuite/escript/errors/errors010-reserved-word-syntax-error.src new file mode 100644 index 0000000000..2d11630ded --- /dev/null +++ b/testsuite/escript/errors/errors010-reserved-word-syntax-error.src @@ -0,0 +1,2 @@ +var is := 1; +print("the value is " + is);