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/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Config/config.h" // for strtoull()/strtoll() define
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/TableGen/Error.h"
31 TGLexer::TGLexer(SourceMgr
&SM
) : SrcMgr(SM
) {
32 CurBuffer
= SrcMgr
.getMainFileID();
33 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
34 CurPtr
= CurBuf
.begin();
38 SMLoc
TGLexer::getLoc() const {
39 return SMLoc::getFromPointer(TokStart
);
42 /// ReturnError - Set the error to the specified string at the specified
43 /// location. This is defined to always return tgtok::Error.
44 tgtok::TokKind
TGLexer::ReturnError(const char *Loc
, const Twine
&Msg
) {
49 int TGLexer::getNextChar() {
50 char CurChar
= *CurPtr
++;
53 return (unsigned char)CurChar
;
55 // A nul character in the stream is either the end of the current buffer or
56 // a random nul in the file. Disambiguate that here.
57 if (CurPtr
-1 != CurBuf
.end())
58 return 0; // Just whitespace.
60 // If this is the end of an included file, pop the parent file off the
62 SMLoc ParentIncludeLoc
= SrcMgr
.getParentIncludeLoc(CurBuffer
);
63 if (ParentIncludeLoc
!= SMLoc()) {
64 CurBuffer
= SrcMgr
.FindBufferContainingLoc(ParentIncludeLoc
);
65 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
66 CurPtr
= ParentIncludeLoc
.getPointer();
70 // Otherwise, return end of file.
71 --CurPtr
; // Another call to lex will return EOF again.
76 // Handle the newline character by ignoring it and incrementing the line
77 // count. However, be careful about 'dos style' files with \n\r in them.
78 // Only treat a \n\r or \r\n as a single line.
79 if ((*CurPtr
== '\n' || (*CurPtr
== '\r')) &&
81 ++CurPtr
; // Eat the two char newline sequence.
86 int TGLexer::peekNextChar(int Index
) {
87 return *(CurPtr
+ Index
);
90 tgtok::TokKind
TGLexer::LexToken() {
92 // This always consumes at least one character.
93 int CurChar
= getNextChar();
97 // Handle letters: [a-zA-Z_]
98 if (isalpha(CurChar
) || CurChar
== '_')
99 return LexIdentifier();
101 // Unknown character, emit an error.
102 return ReturnError(TokStart
, "Unexpected character");
103 case EOF
: return tgtok::Eof
;
104 case ':': return tgtok::colon
;
105 case ';': return tgtok::semi
;
106 case '.': return tgtok::period
;
107 case ',': return tgtok::comma
;
108 case '<': return tgtok::less
;
109 case '>': return tgtok::greater
;
110 case ']': return tgtok::r_square
;
111 case '{': return tgtok::l_brace
;
112 case '}': return tgtok::r_brace
;
113 case '(': return tgtok::l_paren
;
114 case ')': return tgtok::r_paren
;
115 case '=': return tgtok::equal
;
116 case '?': return tgtok::question
;
117 case '#': return tgtok::paste
;
124 // Ignore whitespace.
127 // If this is the start of a // comment, skip until the end of the line or
128 // the end of the buffer.
131 else if (*CurPtr
== '*') {
134 } else // Otherwise, this is an error.
135 return ReturnError(TokStart
, "Unexpected character");
138 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
139 case '7': case '8': case '9': {
141 if (isdigit(CurChar
)) {
142 // Allow identifiers to start with a number if it is followed by
143 // an identifier. This can happen with paste operations like
147 NextChar
= peekNextChar(i
++);
148 } while (isdigit(NextChar
));
150 if (NextChar
== 'x' || NextChar
== 'b') {
151 // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
153 int NextNextChar
= peekNextChar(i
);
154 switch (NextNextChar
) {
161 case '2': case '3': case '4': case '5':
162 case '6': case '7': case '8': case '9':
163 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
164 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
172 if (isalpha(NextChar
) || NextChar
== '_')
173 return LexIdentifier();
177 case '"': return LexString();
178 case '$': return LexVarName();
179 case '[': return LexBracket();
180 case '!': return LexExclaim();
184 /// LexString - Lex "[^"]*"
185 tgtok::TokKind
TGLexer::LexString() {
186 const char *StrStart
= CurPtr
;
190 while (*CurPtr
!= '"') {
191 // If we hit the end of the buffer, report an error.
192 if (*CurPtr
== 0 && CurPtr
== CurBuf
.end())
193 return ReturnError(StrStart
, "End of file in string literal");
195 if (*CurPtr
== '\n' || *CurPtr
== '\r')
196 return ReturnError(StrStart
, "End of line in string literal");
198 if (*CurPtr
!= '\\') {
199 CurStrVal
+= *CurPtr
++;
206 case '\\': case '\'': case '"':
207 // These turn into their literal character.
208 CurStrVal
+= *CurPtr
++;
221 return ReturnError(CurPtr
, "escaped newlines not supported in tblgen");
223 // If we hit the end of the buffer, report an error.
225 if (CurPtr
== CurBuf
.end())
226 return ReturnError(StrStart
, "End of file in string literal");
229 return ReturnError(CurPtr
, "invalid escape in string literal");
234 return tgtok::StrVal
;
237 tgtok::TokKind
TGLexer::LexVarName() {
238 if (!isalpha(CurPtr
[0]) && CurPtr
[0] != '_')
239 return ReturnError(TokStart
, "Invalid variable name");
241 // Otherwise, we're ok, consume the rest of the characters.
242 const char *VarNameStart
= CurPtr
++;
244 while (isalpha(*CurPtr
) || isdigit(*CurPtr
) || *CurPtr
== '_')
247 CurStrVal
.assign(VarNameStart
, CurPtr
);
248 return tgtok::VarName
;
251 tgtok::TokKind
TGLexer::LexIdentifier() {
252 // The first letter is [a-zA-Z_#].
253 const char *IdentStart
= TokStart
;
255 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
256 while (isalpha(*CurPtr
) || isdigit(*CurPtr
) || *CurPtr
== '_')
259 // Check to see if this identifier is a keyword.
260 StringRef
Str(IdentStart
, CurPtr
-IdentStart
);
262 if (Str
== "include") {
263 if (LexInclude()) return tgtok::Error
;
267 tgtok::TokKind Kind
= StringSwitch
<tgtok::TokKind
>(Str
)
268 .Case("int", tgtok::Int
)
269 .Case("bit", tgtok::Bit
)
270 .Case("bits", tgtok::Bits
)
271 .Case("string", tgtok::String
)
272 .Case("list", tgtok::List
)
273 .Case("code", tgtok::Code
)
274 .Case("dag", tgtok::Dag
)
275 .Case("class", tgtok::Class
)
276 .Case("def", tgtok::Def
)
277 .Case("foreach", tgtok::Foreach
)
278 .Case("defm", tgtok::Defm
)
279 .Case("defset", tgtok::Defset
)
280 .Case("multiclass", tgtok::MultiClass
)
281 .Case("field", tgtok::Field
)
282 .Case("let", tgtok::Let
)
283 .Case("in", tgtok::In
)
286 if (Kind
== tgtok::Id
)
287 CurStrVal
.assign(Str
.begin(), Str
.end());
291 /// LexInclude - We just read the "include" token. Get the string token that
292 /// comes next and enter the include.
293 bool TGLexer::LexInclude() {
294 // The token after the include must be a string.
295 tgtok::TokKind Tok
= LexToken();
296 if (Tok
== tgtok::Error
) return true;
297 if (Tok
!= tgtok::StrVal
) {
298 PrintError(getLoc(), "Expected filename after include");
303 std::string Filename
= CurStrVal
;
304 std::string IncludedFile
;
306 CurBuffer
= SrcMgr
.AddIncludeFile(Filename
, SMLoc::getFromPointer(CurPtr
),
309 PrintError(getLoc(), "Could not find include file '" + Filename
+ "'");
313 DependenciesMapTy::const_iterator Found
= Dependencies
.find(IncludedFile
);
314 if (Found
!= Dependencies
.end()) {
316 "File '" + IncludedFile
+ "' has already been included.");
317 SrcMgr
.PrintMessage(Found
->second
, SourceMgr::DK_Note
,
318 "previously included here");
321 Dependencies
.insert(std::make_pair(IncludedFile
, getLoc()));
322 // Save the line number and lex buffer of the includer.
323 CurBuf
= SrcMgr
.getMemoryBuffer(CurBuffer
)->getBuffer();
324 CurPtr
= CurBuf
.begin();
328 void TGLexer::SkipBCPLComment() {
329 ++CurPtr
; // skip the second slash.
334 return; // Newline is end of comment.
336 // If this is the end of the buffer, end the comment.
337 if (CurPtr
== CurBuf
.end())
341 // Otherwise, skip the character.
346 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
347 /// is that we allow nesting.
348 bool TGLexer::SkipCComment() {
349 ++CurPtr
; // skip the star.
350 unsigned CommentDepth
= 1;
353 int CurChar
= getNextChar();
356 PrintError(TokStart
, "Unterminated comment!");
359 // End of the comment?
360 if (CurPtr
[0] != '/') break;
362 ++CurPtr
; // End the */.
363 if (--CommentDepth
== 0)
367 // Start of a nested comment?
368 if (CurPtr
[0] != '*') break;
380 tgtok::TokKind
TGLexer::LexNumber() {
381 if (CurPtr
[-1] == '0') {
382 if (CurPtr
[0] == 'x') {
384 const char *NumStart
= CurPtr
;
385 while (isxdigit(CurPtr
[0]))
388 // Requires at least one hex digit.
389 if (CurPtr
== NumStart
)
390 return ReturnError(TokStart
, "Invalid hexadecimal number");
393 CurIntVal
= strtoll(NumStart
, nullptr, 16);
395 return ReturnError(TokStart
, "Invalid hexadecimal number");
396 if (errno
== ERANGE
) {
398 CurIntVal
= (int64_t)strtoull(NumStart
, nullptr, 16);
400 return ReturnError(TokStart
, "Invalid hexadecimal number");
402 return ReturnError(TokStart
, "Hexadecimal number out of range");
404 return tgtok::IntVal
;
405 } else if (CurPtr
[0] == 'b') {
407 const char *NumStart
= CurPtr
;
408 while (CurPtr
[0] == '0' || CurPtr
[0] == '1')
411 // Requires at least one binary digit.
412 if (CurPtr
== NumStart
)
413 return ReturnError(CurPtr
-2, "Invalid binary number");
414 CurIntVal
= strtoll(NumStart
, nullptr, 2);
415 return tgtok::BinaryIntVal
;
419 // Check for a sign without a digit.
420 if (!isdigit(CurPtr
[0])) {
421 if (CurPtr
[-1] == '-')
423 else if (CurPtr
[-1] == '+')
427 while (isdigit(CurPtr
[0]))
429 CurIntVal
= strtoll(TokStart
, nullptr, 10);
430 return tgtok::IntVal
;
433 /// LexBracket - We just read '['. If this is a code block, return it,
434 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
435 tgtok::TokKind
TGLexer::LexBracket() {
436 if (CurPtr
[0] != '{')
437 return tgtok::l_square
;
439 const char *CodeStart
= CurPtr
;
441 int Char
= getNextChar();
442 if (Char
== EOF
) break;
444 if (Char
!= '}') continue;
446 Char
= getNextChar();
447 if (Char
== EOF
) break;
449 CurStrVal
.assign(CodeStart
, CurPtr
-2);
450 return tgtok::CodeFragment
;
454 return ReturnError(CodeStart
-2, "Unterminated Code Block");
457 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
458 tgtok::TokKind
TGLexer::LexExclaim() {
459 if (!isalpha(*CurPtr
))
460 return ReturnError(CurPtr
- 1, "Invalid \"!operator\"");
462 const char *Start
= CurPtr
++;
463 while (isalpha(*CurPtr
))
466 // Check to see which operator this is.
467 tgtok::TokKind Kind
=
468 StringSwitch
<tgtok::TokKind
>(StringRef(Start
, CurPtr
- Start
))
469 .Case("eq", tgtok::XEq
)
470 .Case("ne", tgtok::XNe
)
471 .Case("le", tgtok::XLe
)
472 .Case("lt", tgtok::XLt
)
473 .Case("ge", tgtok::XGe
)
474 .Case("gt", tgtok::XGt
)
475 .Case("if", tgtok::XIf
)
476 .Case("isa", tgtok::XIsA
)
477 .Case("head", tgtok::XHead
)
478 .Case("tail", tgtok::XTail
)
479 .Case("size", tgtok::XSize
)
480 .Case("con", tgtok::XConcat
)
481 .Case("dag", tgtok::XDag
)
482 .Case("add", tgtok::XADD
)
483 .Case("and", tgtok::XAND
)
484 .Case("or", tgtok::XOR
)
485 .Case("shl", tgtok::XSHL
)
486 .Case("sra", tgtok::XSRA
)
487 .Case("srl", tgtok::XSRL
)
488 .Case("cast", tgtok::XCast
)
489 .Case("empty", tgtok::XEmpty
)
490 .Case("subst", tgtok::XSubst
)
491 .Case("foldl", tgtok::XFoldl
)
492 .Case("foreach", tgtok::XForEach
)
493 .Case("listconcat", tgtok::XListConcat
)
494 .Case("strconcat", tgtok::XStrConcat
)
495 .Default(tgtok::Error
);
497 return Kind
!= tgtok::Error
? Kind
: ReturnError(Start
-1, "Unknown operator");