blob: ff322e74fba2d3812227f46f6108e76568dafbe9 [file] [log] [blame]
Chris Lattnera8058742007-11-18 02:57:27 +00001//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-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 Lattnera8058742007-11-18 02:57:27 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Lexer for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6aaca042007-11-18 05:25:45 +000014#include "TGLexer.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Error.h"
Chris Lattner099e1982009-06-21 03:36:54 +000016#include "llvm/Support/SourceMgr.h"
Chris Lattnera8058742007-11-18 02:57:27 +000017#include "llvm/Support/MemoryBuffer.h"
Bill Wendlingcd466f52010-12-08 20:02:49 +000018#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Twine.h"
Chris Lattnera8058742007-11-18 02:57:27 +000020#include <cctype>
Duncan Sands4520dd22008-10-08 07:23:46 +000021#include <cstdio>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000022#include <cstdlib>
23#include <cstring>
Dan Gohman63f97202008-10-17 01:33:43 +000024#include <cerrno>
Dylan Noblesmith8cc300c2011-12-22 23:08:39 +000025
Dylan Noblesmithbfbc9fc2011-12-22 23:16:09 +000026#include "llvm/Config/config.h" // for strtoull()/strtoll() define
Dylan Noblesmith8cc300c2011-12-22 23:08:39 +000027
Chris Lattnera8058742007-11-18 02:57:27 +000028using namespace llvm;
29
Chris Lattner8070ea32009-06-21 03:41:50 +000030TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
Chris Lattneraa739d22009-03-13 07:05:43 +000031 CurBuffer = 0;
32 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +000033 CurPtr = CurBuf->getBufferStart();
Chris Lattner56a9fcf2007-11-19 07:43:52 +000034 TokStart = 0;
Chris Lattnera8058742007-11-18 02:57:27 +000035}
36
Chris Lattner1e3a8a42009-06-21 03:39:35 +000037SMLoc TGLexer::getLoc() const {
38 return SMLoc::getFromPointer(TokStart);
Chris Lattner1c8ae592009-03-13 16:01:53 +000039}
40
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000041/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4601652007-11-22 20:49:04 +000042/// location. This is defined to always return tgtok::Error.
Benjamin Kramerd1e17032010-09-27 17:42:11 +000043tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000044 PrintError(Loc, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000045 return tgtok::Error;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000046}
Chris Lattnera8058742007-11-18 02:57:27 +000047
Chris Lattnera8058742007-11-18 02:57:27 +000048int TGLexer::getNextChar() {
49 char CurChar = *CurPtr++;
50 switch (CurChar) {
51 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000052 return (unsigned char)CurChar;
Chris Lattneraa739d22009-03-13 07:05:43 +000053 case 0: {
Chris Lattnera8058742007-11-18 02:57:27 +000054 // A nul character in the stream is either the end of the current buffer or
55 // a random nul in the file. Disambiguate that here.
56 if (CurPtr-1 != CurBuf->getBufferEnd())
57 return 0; // Just whitespace.
58
59 // If this is the end of an included file, pop the parent file off the
60 // include stack.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000061 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
62 if (ParentIncludeLoc != SMLoc()) {
Chris Lattneraa739d22009-03-13 07:05:43 +000063 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
64 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattner1c8ae592009-03-13 16:01:53 +000065 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnera8058742007-11-18 02:57:27 +000066 return getNextChar();
67 }
68
69 // Otherwise, return end of file.
70 --CurPtr; // Another call to lex will return EOF again.
71 return EOF;
Chris Lattneraa739d22009-03-13 07:05:43 +000072 }
Chris Lattnera8058742007-11-18 02:57:27 +000073 case '\n':
74 case '\r':
75 // Handle the newline character by ignoring it and incrementing the line
76 // count. However, be careful about 'dos style' files with \n\r in them.
77 // Only treat a \n\r or \r\n as a single line.
78 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
79 *CurPtr != CurChar)
Chris Lattnerc1819182007-11-18 05:48:46 +000080 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +000081 return '\n';
82 }
83}
84
David Greenea761f922011-10-19 13:03:35 +000085int TGLexer::peekNextChar(int Index) {
86 return *(CurPtr + Index);
87}
88
Chris Lattnerf4601652007-11-22 20:49:04 +000089tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +000090 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +000091 // This always consumes at least one character.
92 int CurChar = getNextChar();
93
94 switch (CurChar) {
95 default:
David Greened3d1cad2011-10-19 13:04:43 +000096 // Handle letters: [a-zA-Z_]
97 if (isalpha(CurChar) || CurChar == '_')
Chris Lattnera8058742007-11-18 02:57:27 +000098 return LexIdentifier();
David Greened3d1cad2011-10-19 13:04:43 +000099
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 // Unknown character, emit an error.
101 return ReturnError(TokStart, "Unexpected character");
102 case EOF: return tgtok::Eof;
103 case ':': return tgtok::colon;
104 case ';': return tgtok::semi;
105 case '.': return tgtok::period;
106 case ',': return tgtok::comma;
107 case '<': return tgtok::less;
108 case '>': return tgtok::greater;
109 case ']': return tgtok::r_square;
110 case '{': return tgtok::l_brace;
111 case '}': return tgtok::r_brace;
112 case '(': return tgtok::l_paren;
113 case ')': return tgtok::r_paren;
114 case '=': return tgtok::equal;
115 case '?': return tgtok::question;
David Greened3d1cad2011-10-19 13:04:43 +0000116 case '#': return tgtok::paste;
Chris Lattnerf4601652007-11-22 20:49:04 +0000117
Chris Lattnera8058742007-11-18 02:57:27 +0000118 case 0:
119 case ' ':
120 case '\t':
121 case '\n':
122 case '\r':
123 // Ignore whitespace.
124 return LexToken();
125 case '/':
126 // If this is the start of a // comment, skip until the end of the line or
127 // the end of the buffer.
128 if (*CurPtr == '/')
129 SkipBCPLComment();
130 else if (*CurPtr == '*') {
131 if (SkipCComment())
Chris Lattnerf4601652007-11-22 20:49:04 +0000132 return tgtok::Error;
133 } else // Otherwise, this is an error.
134 return ReturnError(TokStart, "Unexpected character");
Chris Lattnera8058742007-11-18 02:57:27 +0000135 return LexToken();
136 case '-': case '+':
137 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
David Greene7efe9362011-10-19 13:03:39 +0000138 case '7': case '8': case '9': {
139 int NextChar = 0;
140 if (isdigit(CurChar)) {
141 // Allow identifiers to start with a number if it is followed by
142 // an identifier. This can happen with paste operations like
143 // foo#8i.
144 int i = 0;
145 do {
146 NextChar = peekNextChar(i++);
147 } while (isdigit(NextChar));
148
149 if (NextChar == 'x' || NextChar == 'b') {
150 // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
151 // likely a number.
152 int NextNextChar = peekNextChar(i);
153 switch (NextNextChar) {
154 default:
155 break;
156 case '0': case '1':
157 if (NextChar == 'b')
158 return LexNumber();
159 // Fallthrough
160 case '2': case '3': case '4': case '5':
161 case '6': case '7': case '8': case '9':
162 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
163 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
164 if (NextChar == 'x')
165 return LexNumber();
166 break;
167 }
168 }
169 }
170
171 if (isalpha(NextChar) || NextChar == '_')
172 return LexIdentifier();
173
Chris Lattnera8058742007-11-18 02:57:27 +0000174 return LexNumber();
David Greene7efe9362011-10-19 13:03:39 +0000175 }
Chris Lattnera8058742007-11-18 02:57:27 +0000176 case '"': return LexString();
177 case '$': return LexVarName();
178 case '[': return LexBracket();
179 case '!': return LexExclaim();
180 }
181}
182
183/// LexString - Lex "[^"]*"
Chris Lattnerf4601652007-11-22 20:49:04 +0000184tgtok::TokKind TGLexer::LexString() {
Chris Lattnera8058742007-11-18 02:57:27 +0000185 const char *StrStart = CurPtr;
186
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000187 CurStrVal = "";
188
Chris Lattnera8058742007-11-18 02:57:27 +0000189 while (*CurPtr != '"') {
190 // If we hit the end of the buffer, report an error.
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000191 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
192 return ReturnError(StrStart, "End of file in string literal");
193
194 if (*CurPtr == '\n' || *CurPtr == '\r')
195 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnera8058742007-11-18 02:57:27 +0000196
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000197 if (*CurPtr != '\\') {
198 CurStrVal += *CurPtr++;
199 continue;
200 }
201
Chris Lattnera8058742007-11-18 02:57:27 +0000202 ++CurPtr;
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000203
204 switch (*CurPtr) {
205 case '\\': case '\'': case '"':
206 // These turn into their literal character.
207 CurStrVal += *CurPtr++;
208 break;
Chris Lattnere023bb62009-03-13 21:23:43 +0000209 case 't':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000210 CurStrVal += '\t';
Chris Lattnere023bb62009-03-13 21:23:43 +0000211 ++CurPtr;
212 break;
213 case 'n':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000214 CurStrVal += '\n';
Chris Lattnere023bb62009-03-13 21:23:43 +0000215 ++CurPtr;
216 break;
217
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000218 case '\n':
219 case '\r':
220 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
221
222 // If we hit the end of the buffer, report an error.
223 case '\0':
224 if (CurPtr == CurBuf->getBufferEnd())
225 return ReturnError(StrStart, "End of file in string literal");
226 // FALL THROUGH
227 default:
228 return ReturnError(CurPtr, "invalid escape in string literal");
229 }
Chris Lattnera8058742007-11-18 02:57:27 +0000230 }
231
Chris Lattnera8058742007-11-18 02:57:27 +0000232 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000233 return tgtok::StrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000234}
235
Chris Lattnerf4601652007-11-22 20:49:04 +0000236tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnera8058742007-11-18 02:57:27 +0000237 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4601652007-11-22 20:49:04 +0000238 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnera8058742007-11-18 02:57:27 +0000239
240 // Otherwise, we're ok, consume the rest of the characters.
241 const char *VarNameStart = CurPtr++;
242
243 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
244 ++CurPtr;
245
Chris Lattnerf4601652007-11-22 20:49:04 +0000246 CurStrVal.assign(VarNameStart, CurPtr);
247 return tgtok::VarName;
Chris Lattnera8058742007-11-18 02:57:27 +0000248}
249
250
Chris Lattnerf4601652007-11-22 20:49:04 +0000251tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnerc2b08752010-10-05 22:59:29 +0000252 // The first letter is [a-zA-Z_#].
Chris Lattnerf4601652007-11-22 20:49:04 +0000253 const char *IdentStart = TokStart;
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000254
Chris Lattnerc2b08752010-10-05 22:59:29 +0000255 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
David Greened3d1cad2011-10-19 13:04:43 +0000256 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
Chris Lattnerc2b08752010-10-05 22:59:29 +0000257 ++CurPtr;
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000258
Chris Lattnera8058742007-11-18 02:57:27 +0000259 // Check to see if this identifier is a keyword.
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000260 StringRef Str(IdentStart, CurPtr-IdentStart);
261
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000262 if (Str == "include") {
Chris Lattnerf4601652007-11-22 20:49:04 +0000263 if (LexInclude()) return tgtok::Error;
264 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000265 }
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000266
Benjamin Krameree573182011-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 Greenecebb4ee2012-02-22 16:09:41 +0000277 .Case("foreach", tgtok::Foreach)
Benjamin Krameree573182011-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 Lattnera8058742007-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 Lattnerf4601652007-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 Lattnera8058742007-11-18 02:57:27 +0000298 return true;
299 }
300
301 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000302 std::string Filename = CurStrVal;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000303 std::string IncludedFile;
Chris Lattnera8058742007-11-18 02:57:27 +0000304
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000305
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000306 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
307 IncludedFile);
Chris Lattnerd926e042009-06-21 05:33:06 +0000308 if (CurBuffer == -1) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000309 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000310 return true;
311 }
312
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000313 Dependencies.push_back(IncludedFile);
Chris Lattnera8058742007-11-18 02:57:27 +0000314 // Save the line number and lex buffer of the includer.
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000315 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +0000316 CurPtr = CurBuf->getBufferStart();
317 return false;
318}
319
320void TGLexer::SkipBCPLComment() {
321 ++CurPtr; // skip the second slash.
322 while (1) {
323 switch (*CurPtr) {
324 case '\n':
325 case '\r':
326 return; // Newline is end of comment.
327 case 0:
328 // If this is the end of the buffer, end the comment.
329 if (CurPtr == CurBuf->getBufferEnd())
330 return;
331 break;
332 }
333 // Otherwise, skip the character.
334 ++CurPtr;
335 }
336}
337
338/// SkipCComment - This skips C-style /**/ comments. The only difference from C
339/// is that we allow nesting.
340bool TGLexer::SkipCComment() {
341 ++CurPtr; // skip the star.
342 unsigned CommentDepth = 1;
343
344 while (1) {
345 int CurChar = getNextChar();
346 switch (CurChar) {
347 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000348 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000349 return true;
350 case '*':
351 // End of the comment?
352 if (CurPtr[0] != '/') break;
353
354 ++CurPtr; // End the */.
355 if (--CommentDepth == 0)
356 return false;
357 break;
358 case '/':
359 // Start of a nested comment?
360 if (CurPtr[0] != '*') break;
361 ++CurPtr;
362 ++CommentDepth;
363 break;
364 }
365 }
366}
367
368/// LexNumber - Lex:
369/// [-+]?[0-9]+
370/// 0x[0-9a-fA-F]+
371/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000372tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000373 if (CurPtr[-1] == '0') {
374 if (CurPtr[0] == 'x') {
375 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000376 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000377 while (isxdigit(CurPtr[0]))
378 ++CurPtr;
379
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000380 // Requires at least one hex digit.
381 if (CurPtr == NumStart)
Chris Lattner4226bb02009-06-21 19:22:49 +0000382 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000383
Dan Gohman63f97202008-10-17 01:33:43 +0000384 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000385 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000386 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000387 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000388 if (errno == ERANGE) {
389 errno = 0;
390 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
391 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000392 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000393 if (errno == ERANGE)
Chris Lattner4226bb02009-06-21 19:22:49 +0000394 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohman63f97202008-10-17 01:33:43 +0000395 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000396 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000397 } else if (CurPtr[0] == 'b') {
398 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000399 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000400 while (CurPtr[0] == '0' || CurPtr[0] == '1')
401 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000402
403 // Requires at least one binary digit.
404 if (CurPtr == NumStart)
405 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000406 CurIntVal = strtoll(NumStart, 0, 2);
407 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000408 }
409 }
410
411 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000412 if (!isdigit(CurPtr[0])) {
413 if (CurPtr[-1] == '-')
414 return tgtok::minus;
415 else if (CurPtr[-1] == '+')
416 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000417 }
418
419 while (isdigit(CurPtr[0]))
420 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000421 CurIntVal = strtoll(TokStart, 0, 10);
422 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000423}
424
425/// LexBracket - We just read '['. If this is a code block, return it,
426/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000427tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000428 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000429 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000430 ++CurPtr;
431 const char *CodeStart = CurPtr;
432 while (1) {
433 int Char = getNextChar();
434 if (Char == EOF) break;
435
436 if (Char != '}') continue;
437
438 Char = getNextChar();
439 if (Char == EOF) break;
440 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000441 CurStrVal.assign(CodeStart, CurPtr-2);
442 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000443 }
444 }
445
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000446 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000447}
448
449/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000450tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000451 if (!isalpha(*CurPtr))
Bill Wendlingdd2b6cb2010-12-08 13:03:15 +0000452 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000453
454 const char *Start = CurPtr++;
455 while (isalpha(*CurPtr))
456 ++CurPtr;
457
458 // Check to see which operator this is.
Bill Wendlingcd466f52010-12-08 20:02:49 +0000459 tgtok::TokKind Kind =
460 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
461 .Case("eq", tgtok::XEq)
462 .Case("if", tgtok::XIf)
David Greene1434f662011-01-07 17:05:37 +0000463 .Case("head", tgtok::XHead)
464 .Case("tail", tgtok::XTail)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000465 .Case("con", tgtok::XConcat)
466 .Case("shl", tgtok::XSHL)
467 .Case("sra", tgtok::XSRA)
468 .Case("srl", tgtok::XSRL)
469 .Case("cast", tgtok::XCast)
David Greene1434f662011-01-07 17:05:37 +0000470 .Case("empty", tgtok::XEmpty)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000471 .Case("subst", tgtok::XSubst)
472 .Case("foreach", tgtok::XForEach)
473 .Case("strconcat", tgtok::XStrConcat)
474 .Default(tgtok::Error);
David Greened418c1b2009-05-14 20:54:48 +0000475
Bill Wendlingcd466f52010-12-08 20:02:49 +0000476 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000477}
478