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/Support/SourceMgr.h"
16
#include "llvm/Support/MemoryBuffer.h"
17
#include "llvm/Config/config.h"
25
TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
27
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
28
CurPtr = CurBuf->getBufferStart();
32
SMLoc TGLexer::getLoc() const {
33
return SMLoc::getFromPointer(TokStart);
37
/// ReturnError - Set the error to the specified string at the specified
38
/// location. This is defined to always return tgtok::Error.
39
tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
45
void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
46
SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
49
void TGLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
50
SrcMgr.PrintMessage(Loc, Msg, "error");
54
int TGLexer::getNextChar() {
55
char CurChar = *CurPtr++;
58
return (unsigned char)CurChar;
60
// A nul character in the stream is either the end of the current buffer or
61
// a random nul in the file. Disambiguate that here.
62
if (CurPtr-1 != CurBuf->getBufferEnd())
63
return 0; // Just whitespace.
65
// If this is the end of an included file, pop the parent file off the
67
SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
68
if (ParentIncludeLoc != SMLoc()) {
69
CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
70
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
71
CurPtr = ParentIncludeLoc.getPointer();
75
// Otherwise, return end of file.
76
--CurPtr; // Another call to lex will return EOF again.
81
// Handle the newline character by ignoring it and incrementing the line
82
// count. However, be careful about 'dos style' files with \n\r in them.
83
// Only treat a \n\r or \r\n as a single line.
84
if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
86
++CurPtr; // Eat the two char newline sequence.
91
tgtok::TokKind TGLexer::LexToken() {
93
// This always consumes at least one character.
94
int CurChar = getNextChar();
98
// Handle letters: [a-zA-Z_]
99
if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
100
return LexIdentifier();
102
// Unknown character, emit an error.
103
return ReturnError(TokStart, "Unexpected character");
104
case EOF: return tgtok::Eof;
105
case ':': return tgtok::colon;
106
case ';': return tgtok::semi;
107
case '.': return tgtok::period;
108
case ',': return tgtok::comma;
109
case '<': return tgtok::less;
110
case '>': return tgtok::greater;
111
case ']': return tgtok::r_square;
112
case '{': return tgtok::l_brace;
113
case '}': return tgtok::r_brace;
114
case '(': return tgtok::l_paren;
115
case ')': return tgtok::r_paren;
116
case '=': return tgtok::equal;
117
case '?': return tgtok::question;
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
case '"': return LexString();
142
case '$': return LexVarName();
143
case '[': return LexBracket();
144
case '!': return LexExclaim();
148
/// LexString - Lex "[^"]*"
149
tgtok::TokKind TGLexer::LexString() {
150
const char *StrStart = CurPtr;
154
while (*CurPtr != '"') {
155
// If we hit the end of the buffer, report an error.
156
if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
157
return ReturnError(StrStart, "End of file in string literal");
159
if (*CurPtr == '\n' || *CurPtr == '\r')
160
return ReturnError(StrStart, "End of line in string literal");
162
if (*CurPtr != '\\') {
163
CurStrVal += *CurPtr++;
170
case '\\': case '\'': case '"':
171
// These turn into their literal character.
172
CurStrVal += *CurPtr++;
185
return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
187
// If we hit the end of the buffer, report an error.
189
if (CurPtr == CurBuf->getBufferEnd())
190
return ReturnError(StrStart, "End of file in string literal");
193
return ReturnError(CurPtr, "invalid escape in string literal");
198
return tgtok::StrVal;
201
tgtok::TokKind TGLexer::LexVarName() {
202
if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
203
return ReturnError(TokStart, "Invalid variable name");
205
// Otherwise, we're ok, consume the rest of the characters.
206
const char *VarNameStart = CurPtr++;
208
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
211
CurStrVal.assign(VarNameStart, CurPtr);
212
return tgtok::VarName;
216
tgtok::TokKind TGLexer::LexIdentifier() {
217
// The first letter is [a-zA-Z_].
218
const char *IdentStart = TokStart;
220
// Match the rest of the identifier regex: [0-9a-zA-Z_]*
221
while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_'
223
// If this contains a '#', make sure it's value
224
if (*CurPtr == '#') {
225
if (strncmp(CurPtr, "#NAME#", 6) != 0) {
236
// Check to see if this identifier is a keyword.
237
unsigned Len = CurPtr-IdentStart;
239
if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
240
if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
241
if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
242
if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
243
if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
244
if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
245
if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
247
if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
248
if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
249
if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
250
if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
251
return tgtok::MultiClass;
252
if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
253
if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
254
if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
256
if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
257
if (LexInclude()) return tgtok::Error;
261
CurStrVal.assign(IdentStart, CurPtr);
265
/// LexInclude - We just read the "include" token. Get the string token that
266
/// comes next and enter the include.
267
bool TGLexer::LexInclude() {
268
// The token after the include must be a string.
269
tgtok::TokKind Tok = LexToken();
270
if (Tok == tgtok::Error) return true;
271
if (Tok != tgtok::StrVal) {
272
PrintError(getLoc(), "Expected filename after include");
277
std::string Filename = CurStrVal;
280
CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
281
if (CurBuffer == -1) {
282
PrintError(getLoc(), "Could not find include file '" + Filename + "'");
286
// Save the line number and lex buffer of the includer.
287
CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
288
CurPtr = CurBuf->getBufferStart();
292
void TGLexer::SkipBCPLComment() {
293
++CurPtr; // skip the second slash.
298
return; // Newline is end of comment.
300
// If this is the end of the buffer, end the comment.
301
if (CurPtr == CurBuf->getBufferEnd())
305
// Otherwise, skip the character.
310
/// SkipCComment - This skips C-style /**/ comments. The only difference from C
311
/// is that we allow nesting.
312
bool TGLexer::SkipCComment() {
313
++CurPtr; // skip the star.
314
unsigned CommentDepth = 1;
317
int CurChar = getNextChar();
320
PrintError(TokStart, "Unterminated comment!");
323
// End of the comment?
324
if (CurPtr[0] != '/') break;
326
++CurPtr; // End the */.
327
if (--CommentDepth == 0)
331
// Start of a nested comment?
332
if (CurPtr[0] != '*') break;
344
tgtok::TokKind TGLexer::LexNumber() {
345
if (CurPtr[-1] == '0') {
346
if (CurPtr[0] == 'x') {
348
const char *NumStart = CurPtr;
349
while (isxdigit(CurPtr[0]))
352
// Requires at least one hex digit.
353
if (CurPtr == NumStart)
354
return ReturnError(TokStart, "Invalid hexadecimal number");
357
CurIntVal = strtoll(NumStart, 0, 16);
359
return ReturnError(TokStart, "Invalid hexadecimal number");
360
if (errno == ERANGE) {
362
CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
364
return ReturnError(TokStart, "Invalid hexadecimal number");
366
return ReturnError(TokStart, "Hexadecimal number out of range");
368
return tgtok::IntVal;
369
} else if (CurPtr[0] == 'b') {
371
const char *NumStart = CurPtr;
372
while (CurPtr[0] == '0' || CurPtr[0] == '1')
375
// Requires at least one binary digit.
376
if (CurPtr == NumStart)
377
return ReturnError(CurPtr-2, "Invalid binary number");
378
CurIntVal = strtoll(NumStart, 0, 2);
379
return tgtok::IntVal;
383
// Check for a sign without a digit.
384
if (!isdigit(CurPtr[0])) {
385
if (CurPtr[-1] == '-')
387
else if (CurPtr[-1] == '+')
391
while (isdigit(CurPtr[0]))
393
CurIntVal = strtoll(TokStart, 0, 10);
394
return tgtok::IntVal;
397
/// LexBracket - We just read '['. If this is a code block, return it,
398
/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
399
tgtok::TokKind TGLexer::LexBracket() {
400
if (CurPtr[0] != '{')
401
return tgtok::l_square;
403
const char *CodeStart = CurPtr;
405
int Char = getNextChar();
406
if (Char == EOF) break;
408
if (Char != '}') continue;
410
Char = getNextChar();
411
if (Char == EOF) break;
413
CurStrVal.assign(CodeStart, CurPtr-2);
414
return tgtok::CodeFragment;
418
return ReturnError(CodeStart-2, "Unterminated Code Block");
421
/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
422
tgtok::TokKind TGLexer::LexExclaim() {
423
if (!isalpha(*CurPtr))
424
return ReturnError(CurPtr-1, "Invalid \"!operator\"");
426
const char *Start = CurPtr++;
427
while (isalpha(*CurPtr))
430
// Check to see which operator this is.
431
unsigned Len = CurPtr-Start;
433
if (Len == 3 && !memcmp(Start, "con", 3)) return tgtok::XConcat;
434
if (Len == 3 && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
435
if (Len == 3 && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
436
if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
437
if (Len == 2 && !memcmp(Start, "eq", 2)) return tgtok::XEq;
438
if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
439
if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
440
if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
441
if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
442
if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
443
if (Len == 3 && !memcmp(Start, "car", 3)) return tgtok::XCar;
444
if (Len == 3 && !memcmp(Start, "cdr", 3)) return tgtok::XCdr;
445
if (Len == 4 && !memcmp(Start, "null", 4)) return tgtok::XNull;
446
if (Len == 2 && !memcmp(Start, "if", 2)) return tgtok::XIf;
448
return ReturnError(Start-1, "Unknown operator");