From d261105a863e7eec365c3f84d5bbcbbf328e1e26 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Fri, 23 Sep 2011 15:51:23 -0700 Subject: [PATCH] Error/warning reporting improvements. - Don't suggest matches when given an empty string or a single, non-alpha character. - Also fixed the parser to be a bit less confusing when it encounters an unexpected EOF. --- parse.yy | 30 +++++++++++++++++++++--------- util.cpp | 4 ++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/parse.yy b/parse.yy index 2ee40e7e..69869989 100644 --- a/parse.yy +++ b/parse.yy @@ -928,9 +928,13 @@ parameter_list builtinTokens.push_back(*token); ++token; } - std::vector alternates = MatchStrings(yytext, builtinTokens); - std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + if (strlen(yytext) == 0) + Error(@1, "Syntax error--premature end of file."); + else { + std::vector alternates = MatchStrings(yytext, builtinTokens); + std::string alts = lGetAlternates(alternates); + Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + } $$ = NULL; } ; @@ -1027,9 +1031,13 @@ statement builtinTokens.push_back(*token); ++token; } - std::vector alternates = MatchStrings(yytext, builtinTokens); - std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + if (strlen(yytext) == 0) + Error(@1, "Syntax error--premature end of file."); + else { + std::vector alternates = MatchStrings(yytext, builtinTokens); + std::string alts = lGetAlternates(alternates); + Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + } $$ = NULL; } ; @@ -1177,9 +1185,13 @@ translation_unit builtinTokens.push_back(*token); ++token; } - std::vector alternates = MatchStrings(yytext, builtinTokens); - std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + if (strlen(yytext) == 0) + Error(@1, "Syntax error--premature end of file."); + else { + std::vector alternates = MatchStrings(yytext, builtinTokens); + std::string alts = lGetAlternates(alternates); + Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + } } ; diff --git a/util.cpp b/util.cpp index 24b730ca..599c2157 100644 --- a/util.cpp +++ b/util.cpp @@ -344,6 +344,10 @@ StringEditDistance(const std::string &str1, const std::string &str2, int maxDist std::vector MatchStrings(const std::string &str, const std::vector &options) { + if (str.size() == 0 || (str.size() == 1 && !isalpha(str[0]))) + // don't even try... + return std::vector(); + const int maxDelta = 2; std::vector matches[maxDelta+1];