blob: bc03055eb23646f42575f186d66ae9fc5e7f4709 [file] [log] [blame]
Chris Lattnerda4ab672007-11-18 02:57:27 +00001//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda4ab672007-11-18 02:57:27 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Lexer for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner98c39512007-11-18 05:25:45 +000014#include "TGLexer.h"
Bill Wendling55bc7182010-12-08 20:02:49 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/ADT/Twine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Config/config.h" // for strtoull()/strtoll() define
Eugene Zelenko33d7b762016-08-23 17:14:32 +000018#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/SourceMgr.h"
21#include "llvm/TableGen/Error.h"
Chris Lattnerda4ab672007-11-18 02:57:27 +000022#include <cctype>
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include <cerrno>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000024#include <cstdint>
Duncan Sands26ff6f92008-10-08 07:23:46 +000025#include <cstdio>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000026#include <cstdlib>
27#include <cstring>
Dylan Noblesmith345b7432011-12-22 23:08:39 +000028
Chris Lattnerda4ab672007-11-18 02:57:27 +000029using namespace llvm;
30
Chris Lattnerfd255752009-06-21 03:41:50 +000031TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
Alp Tokera55b95b2014-07-06 10:33:31 +000032 CurBuffer = SrcMgr.getMainFileID();
Rafael Espindolaa3c65092014-07-06 14:24:03 +000033 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
34 CurPtr = CurBuf.begin();
Craig Topper011817a2014-04-09 04:50:04 +000035 TokStart = nullptr;
Chris Lattnerda4ab672007-11-18 02:57:27 +000036}
37
Chris Lattner526c8cb2009-06-21 03:39:35 +000038SMLoc TGLexer::getLoc() const {
39 return SMLoc::getFromPointer(TokStart);
Chris Lattner87710ca2009-03-13 16:01:53 +000040}
41
Chris Lattner1a262962007-11-19 07:38:58 +000042/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4127dd2007-11-22 20:49:04 +000043/// location. This is defined to always return tgtok::Error.
Benjamin Kramerc7583112010-09-27 17:42:11 +000044tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
Chris Lattner1a262962007-11-19 07:38:58 +000045 PrintError(Loc, Msg);
Chris Lattnerf4127dd2007-11-22 20:49:04 +000046 return tgtok::Error;
Chris Lattner1a262962007-11-19 07:38:58 +000047}
Chris Lattnerda4ab672007-11-18 02:57:27 +000048
Chris Lattnerda4ab672007-11-18 02:57:27 +000049int TGLexer::getNextChar() {
50 char CurChar = *CurPtr++;
51 switch (CurChar) {
52 default:
Chris Lattner60700282007-11-18 05:48:46 +000053 return (unsigned char)CurChar;
Chris Lattner8db9bc72009-03-13 07:05:43 +000054 case 0: {
Chris Lattnerda4ab672007-11-18 02:57:27 +000055 // 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.
Rafael Espindolaa3c65092014-07-06 14:24:03 +000057 if (CurPtr-1 != CurBuf.end())
Chris Lattnerda4ab672007-11-18 02:57:27 +000058 return 0; // Just whitespace.
59
60 // If this is the end of an included file, pop the parent file off the
61 // include stack.
Chris Lattner526c8cb2009-06-21 03:39:35 +000062 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
63 if (ParentIncludeLoc != SMLoc()) {
Chris Lattner8db9bc72009-03-13 07:05:43 +000064 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
Rafael Espindolaa3c65092014-07-06 14:24:03 +000065 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
Chris Lattner87710ca2009-03-13 16:01:53 +000066 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnerda4ab672007-11-18 02:57:27 +000067 return getNextChar();
68 }
69
70 // Otherwise, return end of file.
71 --CurPtr; // Another call to lex will return EOF again.
72 return EOF;
Chris Lattner8db9bc72009-03-13 07:05:43 +000073 }
Chris Lattnerda4ab672007-11-18 02:57:27 +000074 case '\n':
75 case '\r':
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')) &&
80 *CurPtr != CurChar)
Chris Lattner60700282007-11-18 05:48:46 +000081 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnerda4ab672007-11-18 02:57:27 +000082 return '\n';
83 }
84}
85
David Greene9ba42082011-10-19 13:03:35 +000086int TGLexer::peekNextChar(int Index) {
87 return *(CurPtr + Index);
88}
89
Chris Lattnerf4127dd2007-11-22 20:49:04 +000090tgtok::TokKind TGLexer::LexToken() {
Chris Lattner4205d252007-11-19 07:43:52 +000091 TokStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +000092 // This always consumes at least one character.
93 int CurChar = getNextChar();
94
95 switch (CurChar) {
96 default:
David Greene8e85b482011-10-19 13:04:43 +000097 // Handle letters: [a-zA-Z_]
98 if (isalpha(CurChar) || CurChar == '_')
Chris Lattnerda4ab672007-11-18 02:57:27 +000099 return LexIdentifier();
David Greene8e85b482011-10-19 13:04:43 +0000100
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000101 // 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;
David Greene8e85b482011-10-19 13:04:43 +0000117 case '#': return tgtok::paste;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118
Chris Lattnerda4ab672007-11-18 02:57:27 +0000119 case 0:
120 case ' ':
121 case '\t':
122 case '\n':
123 case '\r':
124 // Ignore whitespace.
125 return LexToken();
126 case '/':
127 // If this is the start of a // comment, skip until the end of the line or
128 // the end of the buffer.
129 if (*CurPtr == '/')
130 SkipBCPLComment();
131 else if (*CurPtr == '*') {
132 if (SkipCComment())
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000133 return tgtok::Error;
134 } else // Otherwise, this is an error.
135 return ReturnError(TokStart, "Unexpected character");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000136 return LexToken();
137 case '-': case '+':
138 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
David Greene5c9fa022011-10-19 13:03:39 +0000139 case '7': case '8': case '9': {
140 int NextChar = 0;
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
144 // foo#8i.
145 int i = 0;
146 do {
147 NextChar = peekNextChar(i++);
148 } while (isdigit(NextChar));
149
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
152 // likely a number.
153 int NextNextChar = peekNextChar(i);
154 switch (NextNextChar) {
155 default:
156 break;
157 case '0': case '1':
158 if (NextChar == 'b')
159 return LexNumber();
Justin Bognerb03fd122016-08-17 05:10:15 +0000160 LLVM_FALLTHROUGH;
David Greene5c9fa022011-10-19 13:03:39 +0000161 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':
165 if (NextChar == 'x')
166 return LexNumber();
167 break;
168 }
169 }
170 }
171
172 if (isalpha(NextChar) || NextChar == '_')
173 return LexIdentifier();
174
Chris Lattnerda4ab672007-11-18 02:57:27 +0000175 return LexNumber();
David Greene5c9fa022011-10-19 13:03:39 +0000176 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000177 case '"': return LexString();
178 case '$': return LexVarName();
179 case '[': return LexBracket();
180 case '!': return LexExclaim();
181 }
182}
183
184/// LexString - Lex "[^"]*"
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000185tgtok::TokKind TGLexer::LexString() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000186 const char *StrStart = CurPtr;
187
Chris Lattner1bd36742009-03-13 21:03:27 +0000188 CurStrVal = "";
189
Chris Lattnerda4ab672007-11-18 02:57:27 +0000190 while (*CurPtr != '"') {
191 // If we hit the end of the buffer, report an error.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000192 if (*CurPtr == 0 && CurPtr == CurBuf.end())
Chris Lattner1a262962007-11-19 07:38:58 +0000193 return ReturnError(StrStart, "End of file in string literal");
194
195 if (*CurPtr == '\n' || *CurPtr == '\r')
196 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000197
Chris Lattner1bd36742009-03-13 21:03:27 +0000198 if (*CurPtr != '\\') {
199 CurStrVal += *CurPtr++;
200 continue;
201 }
202
Chris Lattnerda4ab672007-11-18 02:57:27 +0000203 ++CurPtr;
Chris Lattner1bd36742009-03-13 21:03:27 +0000204
205 switch (*CurPtr) {
206 case '\\': case '\'': case '"':
207 // These turn into their literal character.
208 CurStrVal += *CurPtr++;
209 break;
Chris Lattnera614ef22009-03-13 21:23:43 +0000210 case 't':
Chris Lattner8bd06d82009-03-13 21:33:17 +0000211 CurStrVal += '\t';
Chris Lattnera614ef22009-03-13 21:23:43 +0000212 ++CurPtr;
213 break;
214 case 'n':
Chris Lattner8bd06d82009-03-13 21:33:17 +0000215 CurStrVal += '\n';
Chris Lattnera614ef22009-03-13 21:23:43 +0000216 ++CurPtr;
217 break;
218
Chris Lattner1bd36742009-03-13 21:03:27 +0000219 case '\n':
220 case '\r':
221 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
222
223 // If we hit the end of the buffer, report an error.
224 case '\0':
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000225 if (CurPtr == CurBuf.end())
Chris Lattner1bd36742009-03-13 21:03:27 +0000226 return ReturnError(StrStart, "End of file in string literal");
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000227 LLVM_FALLTHROUGH;
Chris Lattner1bd36742009-03-13 21:03:27 +0000228 default:
229 return ReturnError(CurPtr, "invalid escape in string literal");
230 }
Chris Lattnerda4ab672007-11-18 02:57:27 +0000231 }
232
Chris Lattnerda4ab672007-11-18 02:57:27 +0000233 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000234 return tgtok::StrVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000235}
236
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000237tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000238 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000239 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000240
241 // Otherwise, we're ok, consume the rest of the characters.
242 const char *VarNameStart = CurPtr++;
243
244 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
245 ++CurPtr;
246
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000247 CurStrVal.assign(VarNameStart, CurPtr);
248 return tgtok::VarName;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000249}
250
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000251tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnerb8ff8f02010-10-05 22:59:29 +0000252 // The first letter is [a-zA-Z_#].
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000253 const char *IdentStart = TokStart;
Benjamin Kramera54985e2011-10-06 18:23:56 +0000254
Chris Lattnerb8ff8f02010-10-05 22:59:29 +0000255 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
David Greene8e85b482011-10-19 13:04:43 +0000256 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
Chris Lattnerb8ff8f02010-10-05 22:59:29 +0000257 ++CurPtr;
Benjamin Kramera54985e2011-10-06 18:23:56 +0000258
Chris Lattnerda4ab672007-11-18 02:57:27 +0000259 // Check to see if this identifier is a keyword.
Benjamin Kramera54985e2011-10-06 18:23:56 +0000260 StringRef Str(IdentStart, CurPtr-IdentStart);
261
Benjamin Kramera54985e2011-10-06 18:23:56 +0000262 if (Str == "include") {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000263 if (LexInclude()) return tgtok::Error;
264 return Lex();
Chris Lattnerda4ab672007-11-18 02:57:27 +0000265 }
Benjamin Kramera54985e2011-10-06 18:23:56 +0000266
Benjamin Kramerf9389a32011-10-06 18:53:43 +0000267 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)
David Greenefb927af2012-02-22 16:09:41 +0000277 .Case("foreach", tgtok::Foreach)
Benjamin Kramerf9389a32011-10-06 18:53:43 +0000278 .Case("defm", tgtok::Defm)
279 .Case("multiclass", tgtok::MultiClass)
280 .Case("field", tgtok::Field)
281 .Case("let", tgtok::Let)
282 .Case("in", tgtok::In)
283 .Default(tgtok::Id);
284
285 if (Kind == tgtok::Id)
286 CurStrVal.assign(Str.begin(), Str.end());
287 return Kind;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000288}
289
290/// LexInclude - We just read the "include" token. Get the string token that
291/// comes next and enter the include.
292bool TGLexer::LexInclude() {
293 // The token after the include must be a string.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000294 tgtok::TokKind Tok = LexToken();
295 if (Tok == tgtok::Error) return true;
296 if (Tok != tgtok::StrVal) {
297 PrintError(getLoc(), "Expected filename after include");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000298 return true;
299 }
300
301 // Get the string.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000302 std::string Filename = CurStrVal;
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000303 std::string IncludedFile;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000304
Joerg Sonnenbergeraf5f23e2011-06-01 13:10:15 +0000305 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
306 IncludedFile);
Alp Tokera55b95b2014-07-06 10:33:31 +0000307 if (!CurBuffer) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000308 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000309 return true;
310 }
311
Sean Silva3b964242013-02-07 04:30:39 +0000312 DependenciesMapTy::const_iterator Found = Dependencies.find(IncludedFile);
313 if (Found != Dependencies.end()) {
314 PrintError(getLoc(),
315 "File '" + IncludedFile + "' has already been included.");
316 SrcMgr.PrintMessage(Found->second, SourceMgr::DK_Note,
317 "previously included here");
318 return true;
319 }
320 Dependencies.insert(std::make_pair(IncludedFile, getLoc()));
Chris Lattnerda4ab672007-11-18 02:57:27 +0000321 // Save the line number and lex buffer of the includer.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000322 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer();
323 CurPtr = CurBuf.begin();
Chris Lattnerda4ab672007-11-18 02:57:27 +0000324 return false;
325}
326
327void TGLexer::SkipBCPLComment() {
328 ++CurPtr; // skip the second slash.
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000329 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000330 switch (*CurPtr) {
331 case '\n':
332 case '\r':
333 return; // Newline is end of comment.
334 case 0:
335 // If this is the end of the buffer, end the comment.
Rafael Espindolaa3c65092014-07-06 14:24:03 +0000336 if (CurPtr == CurBuf.end())
Chris Lattnerda4ab672007-11-18 02:57:27 +0000337 return;
338 break;
339 }
340 // Otherwise, skip the character.
341 ++CurPtr;
342 }
343}
344
345/// SkipCComment - This skips C-style /**/ comments. The only difference from C
346/// is that we allow nesting.
347bool TGLexer::SkipCComment() {
348 ++CurPtr; // skip the star.
349 unsigned CommentDepth = 1;
350
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000351 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000352 int CurChar = getNextChar();
353 switch (CurChar) {
354 case EOF:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000355 PrintError(TokStart, "Unterminated comment!");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000356 return true;
357 case '*':
358 // End of the comment?
359 if (CurPtr[0] != '/') break;
360
361 ++CurPtr; // End the */.
362 if (--CommentDepth == 0)
363 return false;
364 break;
365 case '/':
366 // Start of a nested comment?
367 if (CurPtr[0] != '*') break;
368 ++CurPtr;
369 ++CommentDepth;
370 break;
371 }
372 }
373}
374
375/// LexNumber - Lex:
376/// [-+]?[0-9]+
377/// 0x[0-9a-fA-F]+
378/// 0b[01]+
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000379tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000380 if (CurPtr[-1] == '0') {
381 if (CurPtr[0] == 'x') {
382 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000383 const char *NumStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000384 while (isxdigit(CurPtr[0]))
385 ++CurPtr;
386
Chris Lattner1a262962007-11-19 07:38:58 +0000387 // Requires at least one hex digit.
388 if (CurPtr == NumStart)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000389 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattner1a262962007-11-19 07:38:58 +0000390
Dan Gohmanca0546f2008-10-17 01:33:43 +0000391 errno = 0;
Craig Topper011817a2014-04-09 04:50:04 +0000392 CurIntVal = strtoll(NumStart, nullptr, 16);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000393 if (errno == EINVAL)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000394 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000395 if (errno == ERANGE) {
396 errno = 0;
Craig Topper011817a2014-04-09 04:50:04 +0000397 CurIntVal = (int64_t)strtoull(NumStart, nullptr, 16);
Dan Gohmanca0546f2008-10-17 01:33:43 +0000398 if (errno == EINVAL)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000399 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000400 if (errno == ERANGE)
Chris Lattner7d4c0d5f2009-06-21 19:22:49 +0000401 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohmanca0546f2008-10-17 01:33:43 +0000402 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000403 return tgtok::IntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000404 } else if (CurPtr[0] == 'b') {
405 ++CurPtr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000406 const char *NumStart = CurPtr;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000407 while (CurPtr[0] == '0' || CurPtr[0] == '1')
408 ++CurPtr;
Chris Lattner1a262962007-11-19 07:38:58 +0000409
410 // Requires at least one binary digit.
411 if (CurPtr == NumStart)
412 return ReturnError(CurPtr-2, "Invalid binary number");
Craig Topper011817a2014-04-09 04:50:04 +0000413 CurIntVal = strtoll(NumStart, nullptr, 2);
Pete Cooper25977642014-08-07 05:47:00 +0000414 return tgtok::BinaryIntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000415 }
416 }
417
418 // Check for a sign without a digit.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000419 if (!isdigit(CurPtr[0])) {
420 if (CurPtr[-1] == '-')
421 return tgtok::minus;
422 else if (CurPtr[-1] == '+')
423 return tgtok::plus;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000424 }
425
426 while (isdigit(CurPtr[0]))
427 ++CurPtr;
Craig Topper011817a2014-04-09 04:50:04 +0000428 CurIntVal = strtoll(TokStart, nullptr, 10);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000429 return tgtok::IntVal;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000430}
431
432/// LexBracket - We just read '['. If this is a code block, return it,
433/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000434tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000435 if (CurPtr[0] != '{')
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000436 return tgtok::l_square;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000437 ++CurPtr;
438 const char *CodeStart = CurPtr;
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000439 while (true) {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000440 int Char = getNextChar();
441 if (Char == EOF) break;
442
443 if (Char != '}') continue;
444
445 Char = getNextChar();
446 if (Char == EOF) break;
447 if (Char == ']') {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000448 CurStrVal.assign(CodeStart, CurPtr-2);
449 return tgtok::CodeFragment;
Chris Lattnerda4ab672007-11-18 02:57:27 +0000450 }
451 }
452
Chris Lattner1a262962007-11-19 07:38:58 +0000453 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000454}
455
456/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000457tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnerda4ab672007-11-18 02:57:27 +0000458 if (!isalpha(*CurPtr))
Bill Wendling4182a162010-12-08 13:03:15 +0000459 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000460
461 const char *Start = CurPtr++;
462 while (isalpha(*CurPtr))
463 ++CurPtr;
464
465 // Check to see which operator this is.
Bill Wendling55bc7182010-12-08 20:02:49 +0000466 tgtok::TokKind Kind =
467 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
468 .Case("eq", tgtok::XEq)
469 .Case("if", tgtok::XIf)
David Greene2f7cf7f2011-01-07 17:05:37 +0000470 .Case("head", tgtok::XHead)
471 .Case("tail", tgtok::XTail)
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000472 .Case("size", tgtok::XSize)
Bill Wendling55bc7182010-12-08 20:02:49 +0000473 .Case("con", tgtok::XConcat)
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000474 .Case("add", tgtok::XADD)
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000475 .Case("and", tgtok::XAND)
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000476 .Case("or", tgtok::XOR)
Bill Wendling55bc7182010-12-08 20:02:49 +0000477 .Case("shl", tgtok::XSHL)
478 .Case("sra", tgtok::XSRA)
479 .Case("srl", tgtok::XSRL)
480 .Case("cast", tgtok::XCast)
David Greene2f7cf7f2011-01-07 17:05:37 +0000481 .Case("empty", tgtok::XEmpty)
Bill Wendling55bc7182010-12-08 20:02:49 +0000482 .Case("subst", tgtok::XSubst)
483 .Case("foreach", tgtok::XForEach)
Daniel Sanders314e80e2014-05-07 10:13:19 +0000484 .Case("listconcat", tgtok::XListConcat)
Bill Wendling55bc7182010-12-08 20:02:49 +0000485 .Case("strconcat", tgtok::XStrConcat)
486 .Default(tgtok::Error);
David Greene5d0c0512009-05-14 20:54:48 +0000487
Bill Wendling55bc7182010-12-08 20:02:49 +0000488 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnerda4ab672007-11-18 02:57:27 +0000489}