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/Twine.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Config/config.h"
26 TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
28 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
29 CurPtr = CurBuf->getBufferStart();
33 SMLoc TGLexer::getLoc() const {
34 return SMLoc::getFromPointer(TokStart);
38 /// ReturnError - Set the error to the specified string at the specified
39 /// location. This is defined to always return tgtok::Error.
40 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
46 void TGLexer::PrintError(const char *Loc, const Twine &Msg) const {
47 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
50 void TGLexer::PrintError(SMLoc Loc, const Twine &Msg) const {
51 SrcMgr.PrintMessage(Loc, Msg, "error");
55 int TGLexer::getNextChar() {
56 char CurChar = *CurPtr++;
59 return (unsigned char)CurChar;
61 // A nul character in the stream is either the end of the current buffer or
62 // a random nul in the file. Disambiguate that here.
63 if (CurPtr-1 != CurBuf->getBufferEnd())
64 return 0; // Just whitespace.
66 // If this is the end of an included file, pop the parent file off the
68 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
69 if (ParentIncludeLoc != SMLoc()) {
70 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
71 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
72 CurPtr = ParentIncludeLoc.getPointer();
76 // Otherwise, return end of file.
77 --CurPtr; // Another call to lex will return EOF again.
82 // Handle the newline character by ignoring it and incrementing the line
83 // count. However, be careful about 'dos style' files with \n\r in them.
84 // Only treat a \n\r or \r\n as a single line.
85 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
87 ++CurPtr; // Eat the two char newline sequence.
92 tgtok::TokKind TGLexer::LexToken() {
94 // This always consumes at least one character.
95 int CurChar = getNextChar();
99 // Handle letters: [a-zA-Z_#]
100 if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
101 return LexIdentifier();
103 // Unknown character, emit an error.
104 return ReturnError(TokStart, "Unexpected character");
105 case EOF: return tgtok::Eof;
106 case ':': return tgtok::colon;
107 case ';': return tgtok::semi;
108 case '.': return tgtok::period;
109 case ',': return tgtok::comma;
110 case '<': return tgtok::less;
111 case '>': return tgtok::greater;
112 case ']': return tgtok::r_square;
113 case '{': return tgtok::l_brace;
114 case '}': return tgtok::r_brace;
115 case '(': return tgtok::l_paren;
116 case ')': return tgtok::r_paren;
117 case '=': return tgtok::equal;
118 case '?': return tgtok::question;
125 // Ignore whitespace.
128 // If this is the start of a // comment, skip until the end of the line or
129 // the end of the buffer.
132 else if (*CurPtr == '*') {
135 } else // Otherwise, this is an error.
136 return ReturnError(TokStart, "Unexpected character");
139 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
140 case '7': case '8': case '9':
142 case '"': return LexString();
143 case '$': return LexVarName();
144 case '[': return LexBracket();
145 case '!': return LexExclaim();
149 /// LexString - Lex "[^"]*"
150 tgtok::TokKind TGLexer::LexString() {
151 const char *StrStart = CurPtr;
155 while (*CurPtr != '"') {
156 // If we hit the end of the buffer, report an error.
157 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
158 return ReturnError(StrStart, "End of file in string literal");
160 if (*CurPtr == '\n' || *CurPtr == '\r')
161 return ReturnError(StrStart, "End of line in string literal");
163 if (*CurPtr != '\\') {
164 CurStrVal += *CurPtr++;
171 case '\\': case '\'': case '"':
172 // These turn into their literal character.
173 CurStrVal += *CurPtr++;
186 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
188 // If we hit the end of the buffer, report an error.
190 if (CurPtr == CurBuf->getBufferEnd())
191 return ReturnError(StrStart, "End of file in string literal");
194 return ReturnError(CurPtr, "invalid escape in string literal");
199 return tgtok::StrVal;
202 tgtok::TokKind TGLexer::LexVarName() {
203 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
204 return ReturnError(TokStart, "Invalid variable name");
206 // Otherwise, we're ok, consume the rest of the characters.
207 const char *VarNameStart = CurPtr++;
209 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
212 CurStrVal.assign(VarNameStart, CurPtr);
213 return tgtok::VarName;
217 tgtok::TokKind TGLexer::LexIdentifier() {
218 // The first letter is [a-zA-Z_#].
219 const char *IdentStart = TokStart;
221 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
222 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_' ||
227 // Check to see if this identifier is a keyword.
228 unsigned Len = CurPtr-IdentStart;
230 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
231 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
232 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
233 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
234 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
235 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
236 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
238 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
239 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
240 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
241 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
242 return tgtok::MultiClass;
243 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
244 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
245 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
247 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
248 if (LexInclude()) return tgtok::Error;
252 CurStrVal.assign(IdentStart, CurPtr);
256 /// LexInclude - We just read the "include" token. Get the string token that
257 /// comes next and enter the include.
258 bool TGLexer::LexInclude() {
259 // The token after the include must be a string.
260 tgtok::TokKind Tok = LexToken();
261 if (Tok == tgtok::Error) return true;
262 if (Tok != tgtok::StrVal) {
263 PrintError(getLoc(), "Expected filename after include");
268 std::string Filename = CurStrVal;
271 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
272 if (CurBuffer == -1) {
273 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
277 // Save the line number and lex buffer of the includer.
278 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
279 CurPtr = CurBuf->getBufferStart();
283 void TGLexer::SkipBCPLComment() {
284 ++CurPtr; // skip the second slash.
289 return; // Newline is end of comment.
291 // If this is the end of the buffer, end the comment.
292 if (CurPtr == CurBuf->getBufferEnd())
296 // Otherwise, skip the character.
301 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
302 /// is that we allow nesting.
303 bool TGLexer::SkipCComment() {
304 ++CurPtr; // skip the star.
305 unsigned CommentDepth = 1;
308 int CurChar = getNextChar();
311 PrintError(TokStart, "Unterminated comment!");
314 // End of the comment?
315 if (CurPtr[0] != '/') break;
317 ++CurPtr; // End the */.
318 if (--CommentDepth == 0)
322 // Start of a nested comment?
323 if (CurPtr[0] != '*') break;
335 tgtok::TokKind TGLexer::LexNumber() {
336 if (CurPtr[-1] == '0') {
337 if (CurPtr[0] == 'x') {
339 const char *NumStart = CurPtr;
340 while (isxdigit(CurPtr[0]))
343 // Requires at least one hex digit.
344 if (CurPtr == NumStart)
345 return ReturnError(TokStart, "Invalid hexadecimal number");
348 CurIntVal = strtoll(NumStart, 0, 16);
350 return ReturnError(TokStart, "Invalid hexadecimal number");
351 if (errno == ERANGE) {
353 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
355 return ReturnError(TokStart, "Invalid hexadecimal number");
357 return ReturnError(TokStart, "Hexadecimal number out of range");
359 return tgtok::IntVal;
360 } else if (CurPtr[0] == 'b') {
362 const char *NumStart = CurPtr;
363 while (CurPtr[0] == '0' || CurPtr[0] == '1')
366 // Requires at least one binary digit.
367 if (CurPtr == NumStart)
368 return ReturnError(CurPtr-2, "Invalid binary number");
369 CurIntVal = strtoll(NumStart, 0, 2);
370 return tgtok::IntVal;
374 // Check for a sign without a digit.
375 if (!isdigit(CurPtr[0])) {
376 if (CurPtr[-1] == '-')
378 else if (CurPtr[-1] == '+')
382 while (isdigit(CurPtr[0]))
384 CurIntVal = strtoll(TokStart, 0, 10);
385 return tgtok::IntVal;
388 /// LexBracket - We just read '['. If this is a code block, return it,
389 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
390 tgtok::TokKind TGLexer::LexBracket() {
391 if (CurPtr[0] != '{')
392 return tgtok::l_square;
394 const char *CodeStart = CurPtr;
396 int Char = getNextChar();
397 if (Char == EOF) break;
399 if (Char != '}') continue;
401 Char = getNextChar();
402 if (Char == EOF) break;
404 CurStrVal.assign(CodeStart, CurPtr-2);
405 return tgtok::CodeFragment;
409 return ReturnError(CodeStart-2, "Unterminated Code Block");
412 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
413 tgtok::TokKind TGLexer::LexExclaim() {
414 if (!isalpha(*CurPtr))
415 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
417 const char *Start = CurPtr++;
418 while (isalpha(*CurPtr))
421 // Check to see which operator this is.
422 switch (CurPtr - Start) {
426 if (!memcmp(Start, "eq", 2)) return tgtok::XEq;
427 if (!memcmp(Start, "if", 2)) return tgtok::XIf;
430 if (!memcmp(Start, "car", 3)) return tgtok::XCar;
431 if (!memcmp(Start, "cdr", 3)) return tgtok::XCdr;
432 if (!memcmp(Start, "con", 3)) return tgtok::XConcat;
433 if (!memcmp(Start, "shl", 3)) return tgtok::XSHL;
434 if (!memcmp(Start, "sra", 3)) return tgtok::XSRA;
435 if (!memcmp(Start, "srl", 3)) return tgtok::XSRL;
438 if (!memcmp(Start, "cast", 4)) return tgtok::XCast;
439 if (!memcmp(Start, "null", 4)) return tgtok::XNull;
442 if (!memcmp(Start, "subst", 5)) return tgtok::XSubst;
445 if (!memcmp(Start, "foreach", 7)) return tgtok::XForEach;
448 if (!memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
452 return ReturnError(Start - 1, "Unknown operator");