Skip to content

Commit

Permalink
simplified handling of syntax errors, the old code made assumptions a…
Browse files Browse the repository at this point in the history
…bout how lemon was internally implemented, that were no longer true for later versions of Lemon
  • Loading branch information
EmielBruijntjes committed Dec 16, 2021
1 parent 8646378 commit f08878a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
13 changes: 1 addition & 12 deletions src/parser.lemon
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@

%syntax_error
{
// Build up a vector of expected tokens
// Mostly based on the following SO post, but we instead put it in a std::vector
// https://stackoverflow.com/questions/11705737/expected-token-using-lemon-parser-generator/13010096#13010096
std::vector<std::string> expectedTokens;

// we loop over all known tokens, if we exceed our defined tokens we exit
// our last defined token is TOKEN_STRING, so we halt when we go past it
for (int i = 0; i <= TOKEN_STRING; ++i) {
int a = yy_find_shift_action(yypParser, (YYCODETYPE)i);
if (a < YYNSTATE + YYNRULE) expectedTokens.emplace_back(yyTokenName[i]);
}
parent->syntaxError(expectedTokens);
parent->syntaxError();
}

%stack_overflow
Expand Down
19 changes: 2 additions & 17 deletions src/tokenprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,10 @@ class TokenProcessor
/**
* This will get called by lemon in case of a syntax error
*/
void syntaxError(const std::vector<std::string> &expectedTokens)
void syntaxError()
{
// remember that we're in syntax error state
_error.append("Syntax error");

// If we have any expected tokens append them to the error
if (expectedTokens.empty() == false)
{
_error.append(", expected one of the following tokens [");

for (auto iter = expectedTokens.begin(); iter != expectedTokens.end(); ++iter)
{
_error.append(*iter);

// for every token that is not our last token (end iter - 1) we append a space for readability
if (iter != expectedTokens.end() - 1) _error.push_back(' ');
}

_error.push_back(']');
}
}

/**
Expand Down

0 comments on commit f08878a

Please sign in to comment.