1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Lexer for TableGen.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Twine.h"
28 TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
30 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
31 CurPtr = CurBuf->getBufferStart();
35 SMLoc TGLexer::getLoc() const {
36 return SMLoc::getFromPointer(TokStart);
39 /// ReturnError - Set the error to the specified string at the specified
40 /// location. This is defined to always return tgtok::Error.
41 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
46 int TGLexer::getNextChar() {
47 char CurChar = *CurPtr++;
50 return (unsigned char)CurChar;
52 // A nul character in the stream is either the end of the current buffer or
53 // a random nul in the file. Disambiguate that here.
54 if (CurPtr-1 != CurBuf->getBufferEnd())
55 return 0; // Just whitespace.
57 // If this is the end of an included file, pop the parent file off the
59 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
60 if (ParentIncludeLoc != SMLoc()) {
61 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
62 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
63 CurPtr = ParentIncludeLoc.getPointer();
67 // Otherwise, return end of file.
68 --CurPtr; // Another call to lex will return EOF again.
73 // Handle the newline character by ignoring it and incrementing the line
74 // count. However, be careful about 'dos style' files with \n\r in them.
75 // Only treat a \n\r or \r\n as a single line.
76 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
78 ++CurPtr; // Eat the two char newline sequence.
83 int TGLexer::peekNextChar(int Index) {
84 return *(CurPtr + Index);
87 tgtok::TokKind TGLexer::LexToken() {
89 // This always consumes at least one character.
90 int CurChar = getNextChar();
94 // Handle letters: [a-zA-Z_#]
95 if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
96 return LexIdentifier();
98 // Unknown character, emit an error.
99 return ReturnError(TokStart, "Unexpected character");
100 case EOF: return tgtok::Eof;
101 case ':': return tgtok::colon;
102 case ';': return tgtok::semi;
103 case '.': return tgtok::period;
104 case ',': return tgtok::comma;
105 case '<': return tgtok::less;
106 case '>': return tgtok::greater;
107 case ']': return tgtok::r_square;
108 case '{': return tgtok::l_brace;
109 case '}': return tgtok::r_brace;
110 case '(': return tgtok::l_paren;
111 case ')': return tgtok::r_paren;
112 case '=': return tgtok::equal;
113 case '?': return tgtok::question;
120 // Ignore whitespace.
123 // If this is the start of a // comment, skip until the end of the line or
124 // the end of the buffer.
127 else if (*CurPtr == '*') {
130 } else // Otherwise, this is an error.
131 return ReturnError(TokStart, "Unexpected character");
134 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
135 case '7': case '8': case '9':
137 case '"': return LexString();
138 case '$': return LexVarName();
139 case '[': return LexBracket();
140 case '!': return LexExclaim();
144 /// LexString - Lex "[^"]*"
145 tgtok::TokKind TGLexer::LexString() {
146 const char *StrStart = CurPtr;
150 while (*CurPtr != '"') {
151 // If we hit the end of the buffer, report an error.
152 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
153 return ReturnError(StrStart, "End of file in string literal");
155 if (*CurPtr == '\n' || *CurPtr == '\r')
156 return ReturnError(StrStart, "End of line in string literal");
158 if (*CurPtr != '\\') {
159 CurStrVal += *CurPtr++;
166 case '\\': case '\'': case '"':
167 // These turn into their literal character.
168 CurStrVal += *CurPtr++;
181 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
183 // If we hit the end of the buffer, report an error.
185 if (CurPtr == CurBuf->getBufferEnd())
186 return ReturnError(StrStart, "End of file in string literal");
189 return ReturnError(CurPtr, "invalid escape in string literal");
194 return tgtok::StrVal;
197 tgtok::TokKind TGLexer::LexVarName() {
198 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
199 return ReturnError(TokStart, "Invalid variable name");
201 // Otherwise, we're ok, consume the rest of the characters.
202 const char *VarNameStart = CurPtr++;
204 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
207 CurStrVal.assign(VarNameStart, CurPtr);
208 return tgtok::VarName;
212 tgtok::TokKind TGLexer::LexIdentifier() {
213 // The first letter is [a-zA-Z_#].
214 const char *IdentStart = TokStart;
216 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
217 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_' ||
221 // Check to see if this identifier is a keyword.
222 StringRef Str(IdentStart, CurPtr-IdentStart);
224 if (Str == "include") {
225 if (LexInclude()) return tgtok::Error;
229 tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
230 .Case("int", tgtok::Int)
231 .Case("bit", tgtok::Bit)
232 .Case("bits", tgtok::Bits)
233 .Case("string", tgtok::String)
234 .Case("list", tgtok::List)
235 .Case("code", tgtok::Code)
236 .Case("dag", tgtok::Dag)
237 .Case("class", tgtok::Class)
238 .Case("def", tgtok::Def)
239 .Case("defm", tgtok::Defm)
240 .Case("multiclass", tgtok::MultiClass)
241 .Case("field", tgtok::Field)
242 .Case("let", tgtok::Let)
243 .Case("in", tgtok::In)
246 if (Kind == tgtok::Id)
247 CurStrVal.assign(Str.begin(), Str.end());
251 /// LexInclude - We just read the "include" token. Get the string token that
252 /// comes next and enter the include.
253 bool TGLexer::LexInclude() {
254 // The token after the include must be a string.
255 tgtok::TokKind Tok = LexToken();
256 if (Tok == tgtok::Error) return true;
257 if (Tok != tgtok::StrVal) {
258 PrintError(getLoc(), "Expected filename after include");
263 std::string Filename = CurStrVal;
264 std::string IncludedFile;
267 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
269 if (CurBuffer == -1) {
270 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
274 Dependencies.push_back(IncludedFile);
275 // Save the line number and lex buffer of the includer.
276 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
277 CurPtr = CurBuf->getBufferStart();
281 void TGLexer::SkipBCPLComment() {
282 ++CurPtr; // skip the second slash.
287 return; // Newline is end of comment.
289 // If this is the end of the buffer, end the comment.
290 if (CurPtr == CurBuf->getBufferEnd())
294 // Otherwise, skip the character.
299 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
300 /// is that we allow nesting.
301 bool TGLexer::SkipCComment() {
302 ++CurPtr; // skip the star.
303 unsigned CommentDepth = 1;
306 int CurChar = getNextChar();
309 PrintError(TokStart, "Unterminated comment!");
312 // End of the comment?
313 if (CurPtr[0] != '/') break;
315 ++CurPtr; // End the */.
316 if (--CommentDepth == 0)
320 // Start of a nested comment?
321 if (CurPtr[0] != '*') break;
333 tgtok::TokKind TGLexer::LexNumber() {
334 if (CurPtr[-1] == '0') {
335 if (CurPtr[0] == 'x') {
337 const char *NumStart = CurPtr;
338 while (isxdigit(CurPtr[0]))
341 // Requires at least one hex digit.
342 if (CurPtr == NumStart)
343 return ReturnError(TokStart, "Invalid hexadecimal number");
346 CurIntVal = strtoll(NumStart, 0, 16);
348 return ReturnError(TokStart, "Invalid hexadecimal number");
349 if (errno == ERANGE) {
351 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
353 return ReturnError(TokStart, "Invalid hexadecimal number");
355 return ReturnError(TokStart, "Hexadecimal number out of range");
357 return tgtok::IntVal;
358 } else if (CurPtr[0] == 'b') {
360 const char *NumStart = CurPtr;
361 while (CurPtr[0] == '0' || CurPtr[0] == '1')
364 // Requires at least one binary digit.
365 if (CurPtr == NumStart)
366 return ReturnError(CurPtr-2, "Invalid binary number");
367 CurIntVal = strtoll(NumStart, 0, 2);
368 return tgtok::IntVal;
372 // Check for a sign without a digit.
373 if (!isdigit(CurPtr[0])) {
374 if (CurPtr[-1] == '-')
376 else if (CurPtr[-1] == '+')
380 while (isdigit(CurPtr[0]))
382 CurIntVal = strtoll(TokStart, 0, 10);
383 return tgtok::IntVal;
386 /// LexBracket - We just read '['. If this is a code block, return it,
387 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
388 tgtok::TokKind TGLexer::LexBracket() {
389 if (CurPtr[0] != '{')
390 return tgtok::l_square;
392 const char *CodeStart = CurPtr;
394 int Char = getNextChar();
395 if (Char == EOF) break;
397 if (Char != '}') continue;
399 Char = getNextChar();
400 if (Char == EOF) break;
402 CurStrVal.assign(CodeStart, CurPtr-2);
403 return tgtok::CodeFragment;
407 return ReturnError(CodeStart-2, "Unterminated Code Block");
410 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
411 tgtok::TokKind TGLexer::LexExclaim() {
412 if (!isalpha(*CurPtr))
413 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
415 const char *Start = CurPtr++;
416 while (isalpha(*CurPtr))
419 // Check to see which operator this is.
420 tgtok::TokKind Kind =
421 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
422 .Case("eq", tgtok::XEq)
423 .Case("if", tgtok::XIf)
424 .Case("head", tgtok::XHead)
425 .Case("tail", tgtok::XTail)
426 .Case("con", tgtok::XConcat)
427 .Case("shl", tgtok::XSHL)
428 .Case("sra", tgtok::XSRA)
429 .Case("srl", tgtok::XSRL)
430 .Case("cast", tgtok::XCast)
431 .Case("empty", tgtok::XEmpty)
432 .Case("subst", tgtok::XSubst)
433 .Case("foreach", tgtok::XForEach)
434 .Case("strconcat", tgtok::XStrConcat)
435 .Default(tgtok::Error);
437 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");