From fa5050d5c73b5acee28999ec2ba44cf1173234b7 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Mon, 3 Oct 2011 21:09:04 -0700 Subject: [PATCH] Error reporting improvements. Don't print more than 3 lines of source file context with errors. (Any more than that is almost certainly not the Right Thing to do.) Make some parsing error messages more clear. --- parse.yy | 6 +++--- util.cpp | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/parse.yy b/parse.yy index d51420e5..0013dd9a 100644 --- a/parse.yy +++ b/parse.yy @@ -946,7 +946,7 @@ parameter_list else { std::vector alternates = MatchStrings(yytext, builtinTokens); std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + Error(@1, "Syntax error--token \"%s\" unexpected.%s", yytext, alts.c_str()); } $$ = NULL; } @@ -1050,7 +1050,7 @@ statement else { std::vector alternates = MatchStrings(yytext, builtinTokens); std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + Error(@1, "Syntax error--token \"%s\" unexpected.%s", yytext, alts.c_str()); } $$ = NULL; } @@ -1209,7 +1209,7 @@ translation_unit else { std::vector alternates = MatchStrings(yytext, builtinTokens); std::string alts = lGetAlternates(alternates); - Error(@1, "Syntax error--token \"%s\" unknown.%s", yytext, alts.c_str()); + Error(@1, "Syntax error--token \"%s\" unexpected.%s", yytext, alts.c_str()); } } ; diff --git a/util.cpp b/util.cpp index 599c2157..36cd83c0 100644 --- a/util.cpp +++ b/util.cpp @@ -111,7 +111,10 @@ lPrintFileLineContext(SourcePos p) { int c, curLine = 1; while ((c = fgetc(f)) != EOF) { - if (curLine >= p.first_line && curLine <= p.last_line) + // Don't print more than three lines of context. (More than that, + // and we're probably doing the wrong thing...) + if (curLine >= std::max(p.first_line, p.last_line-2) && + curLine <= p.last_line) fputc(c, stderr); if (c == '\n') ++curLine; @@ -313,6 +316,12 @@ FatalError(const char *file, int line, const char *message) { // http://en.wikipedia.org/wiki/Levenshtein_distance int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist) { + // Small hack: don't return 0 if the strings are the same; if we've + // gotten here, there's been a parsing error, and suggesting the same + // string isn't going to actually help things. + if (str1 == str2) + return maxDist; + int n1 = (int)str1.size(), n2 = (int)str2.size(); int nmax = std::max(n1, n2);