Skip to content

Commit

Permalink
Add an error message about known reserved words (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-swanson committed Sep 22, 2020
1 parent f9087d3 commit cbd2f97
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
22 changes: 21 additions & 1 deletion pol-core/bscript/compiler/file/ErrorListener.cpp
@@ -1,10 +1,19 @@
#include "ErrorListener.h"

#include <set>

#include "clib/maputil.h"
#include "compiler/Profile.h"
#include "compiler/Report.h"

namespace Pol::Bscript::Compiler
{
std::set<std::string, Clib::ci_cmp_pred> 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 )
{
Expand All @@ -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 } );
}

Expand Down
@@ -1 +1 @@
errors004-reserved-word-as-argument-name.src:3:13: error: extraneous input 'downto' expecting {'unused', ')', IDENTIFIER}
errors004-reserved-word-as-argument-name.src:3:13: error: Did not expect reserved word 'downto' in this context.
@@ -0,0 +1 @@
Non-identifier declared as a variable: 'is'
@@ -0,0 +1 @@
errors010-reserved-word-syntax-error.src:1:5: error: Did not expect reserved word 'is' in this context.
@@ -0,0 +1,2 @@
var is := 1;
print("the value is " + is);

0 comments on commit cbd2f97

Please sign in to comment.