Add carat diagnostics to tblgen lexer errors.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44226 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-11-19 07:38:58 +00:00
parent 2b1f1066ac
commit c8a9bbcbc7
2 changed files with 59 additions and 33 deletions

View File

@ -37,14 +37,20 @@ TGLexer::~TGLexer() {
delete CurBuf; delete CurBuf;
} }
/// ReturnError - Set the error to the specified string at the specified
/// location. This is defined to always return YYERROR.
int TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
PrintError(Loc, Msg);
return YYERROR;
}
std::ostream &TGLexer::err() { std::ostream &TGLexer::err() const {
PrintIncludeStack(*cerr.stream()); PrintIncludeStack(*cerr.stream());
return *cerr.stream(); return *cerr.stream();
} }
void TGLexer::PrintIncludeStack(std::ostream &OS) { void TGLexer::PrintIncludeStack(std::ostream &OS) const {
for (unsigned i = 0, e = IncludeStack.size(); i != e; ++i) for (unsigned i = 0, e = IncludeStack.size(); i != e; ++i)
OS << "Included from " << IncludeStack[i].Buffer->getBufferIdentifier() OS << "Included from " << IncludeStack[i].Buffer->getBufferIdentifier()
<< ":" << IncludeStack[i].LineNo << ":\n"; << ":" << IncludeStack[i].LineNo << ":\n";
@ -52,6 +58,30 @@ void TGLexer::PrintIncludeStack(std::ostream &OS) {
<< CurLineNo << ": "; << CurLineNo << ": ";
} }
/// PrintError - Print the error at the specified location.
void TGLexer::PrintError(const char *ErrorLoc, const std::string &Msg) const {
err() << Msg << "\n";
assert(ErrorLoc && "Location not specified!");
// Scan backward to find the start of the line.
const char *LineStart = ErrorLoc;
while (LineStart != CurBuf->getBufferStart() &&
LineStart[-1] != '\n' && LineStart[-1] != '\r')
--LineStart;
// Get the end of the line.
const char *LineEnd = ErrorLoc;
while (LineEnd != CurBuf->getBufferEnd() &&
LineEnd[0] != '\n' && LineEnd[0] != '\r')
++LineEnd;
// Print out the line.
cerr << std::string(LineStart, LineEnd) << "\n";
// Print out spaces before the carat.
const char *Pos = LineStart;
while (Pos != ErrorLoc)
cerr << (*Pos == '\t' ? '\t' : ' ');
cerr << "^\n";
}
int TGLexer::getNextChar() { int TGLexer::getNextChar() {
char CurChar = *CurPtr++; char CurChar = *CurPtr++;
switch (CurChar) { switch (CurChar) {
@ -139,13 +169,11 @@ int TGLexer::LexString() {
while (*CurPtr != '"') { while (*CurPtr != '"') {
// If we hit the end of the buffer, report an error. // If we hit the end of the buffer, report an error.
if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd()) { if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
TheError = "End of file in string literal"; return ReturnError(StrStart, "End of file in string literal");
return YYERROR;
} else if (*CurPtr == '\n' || *CurPtr == '\r') { if (*CurPtr == '\n' || *CurPtr == '\r')
TheError = "End of line in string literal"; return ReturnError(StrStart, "End of line in string literal");
return YYERROR;
}
++CurPtr; ++CurPtr;
} }
@ -210,10 +238,11 @@ int TGLexer::LexIdentifier() {
/// comes next and enter the include. /// comes next and enter the include.
bool TGLexer::LexInclude() { bool TGLexer::LexInclude() {
// The token after the include must be a string. // The token after the include must be a string.
const char *TokStart = CurPtr-7;
int Tok = LexToken(); int Tok = LexToken();
if (Tok == YYERROR) return true; if (Tok == YYERROR) return true;
if (Tok != STRVAL) { if (Tok != STRVAL) {
TheError = "Expected filename after include"; PrintError(TokStart, "Expected filename after include");
return true; return true;
} }
@ -231,7 +260,7 @@ bool TGLexer::LexInclude() {
} }
if (NewBuf == 0) { if (NewBuf == 0) {
TheError = "Could not find include file '" + Filename + "'"; PrintError(TokStart, "Could not find include file '" + Filename + "'");
return true; return true;
} }
@ -265,6 +294,7 @@ void TGLexer::SkipBCPLComment() {
/// SkipCComment - This skips C-style /**/ comments. The only difference from C /// SkipCComment - This skips C-style /**/ comments. The only difference from C
/// is that we allow nesting. /// is that we allow nesting.
bool TGLexer::SkipCComment() { bool TGLexer::SkipCComment() {
const char *CommentStart = CurPtr-1;
++CurPtr; // skip the star. ++CurPtr; // skip the star.
unsigned CommentDepth = 1; unsigned CommentDepth = 1;
@ -272,7 +302,7 @@ bool TGLexer::SkipCComment() {
int CurChar = getNextChar(); int CurChar = getNextChar();
switch (CurChar) { switch (CurChar) {
case EOF: case EOF:
TheError = "Unterminated comment!"; PrintError(CommentStart, "Unterminated comment!");
return true; return true;
case '*': case '*':
// End of the comment? // End of the comment?
@ -306,10 +336,10 @@ int TGLexer::LexNumber() {
while (isxdigit(CurPtr[0])) while (isxdigit(CurPtr[0]))
++CurPtr; ++CurPtr;
if (CurPtr == NumStart) { // Requires at least one hex digit.
TheError = "Invalid hexadecimal number"; if (CurPtr == NumStart)
return YYERROR; return ReturnError(CurPtr-2, "Invalid hexadecimal number");
}
Filelval.IntVal = strtoll(NumStart, 0, 16); Filelval.IntVal = strtoll(NumStart, 0, 16);
return INTVAL; return INTVAL;
} else if (CurPtr[0] == 'b') { } else if (CurPtr[0] == 'b') {
@ -318,10 +348,9 @@ int TGLexer::LexNumber() {
while (CurPtr[0] == '0' || CurPtr[0] == '1') while (CurPtr[0] == '0' || CurPtr[0] == '1')
++CurPtr; ++CurPtr;
if (CurPtr == NumStart) { // Requires at least one binary digit.
TheError = "Invalid binary number"; if (CurPtr == NumStart)
return YYERROR; return ReturnError(CurPtr-2, "Invalid binary number");
}
Filelval.IntVal = strtoll(NumStart, 0, 2); Filelval.IntVal = strtoll(NumStart, 0, 2);
return INTVAL; return INTVAL;
} }
@ -360,8 +389,7 @@ int TGLexer::LexBracket() {
} }
} }
TheError = "Invalid Code Block"; return ReturnError(CodeStart-2, "Unterminated Code Block");
return YYERROR;
} }
/// LexExclaim - Lex '!' and '![a-zA-Z]+'. /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
@ -382,8 +410,7 @@ int TGLexer::LexExclaim() {
if (Len == 3 && !memcmp(Start, "shl", 3)) return SHLTOK; if (Len == 3 && !memcmp(Start, "shl", 3)) return SHLTOK;
if (Len == 9 && !memcmp(Start, "strconcat", 9)) return STRCONCATTOK; if (Len == 9 && !memcmp(Start, "strconcat", 9)) return STRCONCATTOK;
TheError = "Unknown operator"; return ReturnError(Start-1, "Unknown operator");
return YYERROR;
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -431,9 +458,7 @@ void ParseFile(const std::string &Filename,
int Filelex() { int Filelex() {
assert(TheLexer && "No lexer setup yet!"); assert(TheLexer && "No lexer setup yet!");
int Tok = TheLexer->LexToken(); int Tok = TheLexer->LexToken();
if (Tok == YYERROR) { if (Tok == YYERROR)
err() << TheLexer->getError() << "\n";
exit(1); exit(1);
}
return Tok; return Tok;
} }

View File

@ -40,8 +40,6 @@ class TGLexer {
// IncludeDirectories - This is the list of directories we should search for // IncludeDirectories - This is the list of directories we should search for
// include files in. // include files in.
std::vector<std::string> IncludeDirectories; std::vector<std::string> IncludeDirectories;
std::string TheError;
public: public:
TGLexer(MemoryBuffer *StartBuf); TGLexer(MemoryBuffer *StartBuf);
~TGLexer(); ~TGLexer();
@ -52,11 +50,14 @@ public:
int LexToken(); int LexToken();
const std::string getError() const { return TheError; } void PrintError(const char *Loc, const std::string &Msg) const;
std::ostream &err() const;
void PrintIncludeStack(std::ostream &OS) const;
std::ostream &err();
void PrintIncludeStack(std::ostream &OS);
private: private:
int ReturnError(const char *Loc, const std::string &Msg);
int getNextChar(); int getNextChar();
void SkipBCPLComment(); void SkipBCPLComment();
bool SkipCComment(); bool SkipCComment();