blob: 0977b00e8c9a0752e4ba2f8d19dd9c7d905cdfda [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>
Chris Lattnera8058742007-11-18 02:57:27 +000025using namespace llvm;
26
Chris Lattner8070ea32009-06-21 03:41:50 +000027TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
Chris Lattneraa739d22009-03-13 07:05:43 +000028 CurBuffer = 0;
29 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +000030 CurPtr = CurBuf->getBufferStart();
Chris Lattner56a9fcf2007-11-19 07:43:52 +000031 TokStart = 0;
Chris Lattnera8058742007-11-18 02:57:27 +000032}
33
Chris Lattner1e3a8a42009-06-21 03:39:35 +000034SMLoc TGLexer::getLoc() const {
35 return SMLoc::getFromPointer(TokStart);
Chris Lattner1c8ae592009-03-13 16:01:53 +000036}
37
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000038/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4601652007-11-22 20:49:04 +000039/// location. This is defined to always return tgtok::Error.
Benjamin Kramerd1e17032010-09-27 17:42:11 +000040tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000041 PrintError(Loc, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000042 return tgtok::Error;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000043}
Chris Lattnera8058742007-11-18 02:57:27 +000044
Chris Lattnera8058742007-11-18 02:57:27 +000045int TGLexer::getNextChar() {
46 char CurChar = *CurPtr++;
47 switch (CurChar) {
48 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000049 return (unsigned char)CurChar;
Chris Lattneraa739d22009-03-13 07:05:43 +000050 case 0: {
Chris Lattnera8058742007-11-18 02:57:27 +000051 // A nul character in the stream is either the end of the current buffer or
52 // a random nul in the file. Disambiguate that here.
53 if (CurPtr-1 != CurBuf->getBufferEnd())
54 return 0; // Just whitespace.
55
56 // If this is the end of an included file, pop the parent file off the
57 // include stack.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000058 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
59 if (ParentIncludeLoc != SMLoc()) {
Chris Lattneraa739d22009-03-13 07:05:43 +000060 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
61 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattner1c8ae592009-03-13 16:01:53 +000062 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnera8058742007-11-18 02:57:27 +000063 return getNextChar();
64 }
65
66 // Otherwise, return end of file.
67 --CurPtr; // Another call to lex will return EOF again.
68 return EOF;
Chris Lattneraa739d22009-03-13 07:05:43 +000069 }
Chris Lattnera8058742007-11-18 02:57:27 +000070 case '\n':
71 case '\r':
72 // Handle the newline character by ignoring it and incrementing the line
73 // count. However, be careful about 'dos style' files with \n\r in them.
74 // Only treat a \n\r or \r\n as a single line.
75 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
76 *CurPtr != CurChar)
Chris Lattnerc1819182007-11-18 05:48:46 +000077 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +000078 return '\n';
79 }
80}
81
David Greenea761f922011-10-19 13:03:35 +000082int TGLexer::peekNextChar(int Index) {
83 return *(CurPtr + Index);
84}
85
Chris Lattnerf4601652007-11-22 20:49:04 +000086tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +000087 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +000088 // This always consumes at least one character.
89 int CurChar = getNextChar();
90
91 switch (CurChar) {
92 default:
David Greened3d1cad2011-10-19 13:04:43 +000093 // Handle letters: [a-zA-Z_]
94 if (isalpha(CurChar) || CurChar == '_')
Chris Lattnera8058742007-11-18 02:57:27 +000095 return LexIdentifier();
David Greened3d1cad2011-10-19 13:04:43 +000096
Chris Lattnerf4601652007-11-22 20:49:04 +000097 // Unknown character, emit an error.
98 return ReturnError(TokStart, "Unexpected character");
99 case EOF: return tgtok::Eof;
100 case ':': return tgtok::colon;
101 case ';': return tgtok::semi;
102 case '.': return tgtok::period;
103 case ',': return tgtok::comma;
104 case '<': return tgtok::less;
105 case '>': return tgtok::greater;
106 case ']': return tgtok::r_square;
107 case '{': return tgtok::l_brace;
108 case '}': return tgtok::r_brace;
109 case '(': return tgtok::l_paren;
110 case ')': return tgtok::r_paren;
111 case '=': return tgtok::equal;
112 case '?': return tgtok::question;
David Greened3d1cad2011-10-19 13:04:43 +0000113 case '#': return tgtok::paste;
Chris Lattnerf4601652007-11-22 20:49:04 +0000114
Chris Lattnera8058742007-11-18 02:57:27 +0000115 case 0:
116 case ' ':
117 case '\t':
118 case '\n':
119 case '\r':
120 // Ignore whitespace.
121 return LexToken();
122 case '/':
123 // If this is the start of a // comment, skip until the end of the line or
124 // the end of the buffer.
125 if (*CurPtr == '/')
126 SkipBCPLComment();
127 else if (*CurPtr == '*') {
128 if (SkipCComment())
Chris Lattnerf4601652007-11-22 20:49:04 +0000129 return tgtok::Error;
130 } else // Otherwise, this is an error.
131 return ReturnError(TokStart, "Unexpected character");
Chris Lattnera8058742007-11-18 02:57:27 +0000132 return LexToken();
133 case '-': case '+':
134 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
David Greene7efe9362011-10-19 13:03:39 +0000135 case '7': case '8': case '9': {
136 int NextChar = 0;
137 if (isdigit(CurChar)) {
138 // Allow identifiers to start with a number if it is followed by
139 // an identifier. This can happen with paste operations like
140 // foo#8i.
141 int i = 0;
142 do {
143 NextChar = peekNextChar(i++);
144 } while (isdigit(NextChar));
145
146 if (NextChar == 'x' || NextChar == 'b') {
147 // If this is [0-9]b[01] or [0-9]x[0-9A-fa-f] this is most
148 // likely a number.
149 int NextNextChar = peekNextChar(i);
150 switch (NextNextChar) {
151 default:
152 break;
153 case '0': case '1':
154 if (NextChar == 'b')
155 return LexNumber();
156 // Fallthrough
157 case '2': case '3': case '4': case '5':
158 case '6': case '7': case '8': case '9':
159 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
160 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
161 if (NextChar == 'x')
162 return LexNumber();
163 break;
164 }
165 }
166 }
167
168 if (isalpha(NextChar) || NextChar == '_')
169 return LexIdentifier();
170
Chris Lattnera8058742007-11-18 02:57:27 +0000171 return LexNumber();
David Greene7efe9362011-10-19 13:03:39 +0000172 }
Chris Lattnera8058742007-11-18 02:57:27 +0000173 case '"': return LexString();
174 case '$': return LexVarName();
175 case '[': return LexBracket();
176 case '!': return LexExclaim();
177 }
178}
179
180/// LexString - Lex "[^"]*"
Chris Lattnerf4601652007-11-22 20:49:04 +0000181tgtok::TokKind TGLexer::LexString() {
Chris Lattnera8058742007-11-18 02:57:27 +0000182 const char *StrStart = CurPtr;
183
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000184 CurStrVal = "";
185
Chris Lattnera8058742007-11-18 02:57:27 +0000186 while (*CurPtr != '"') {
187 // If we hit the end of the buffer, report an error.
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000188 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
189 return ReturnError(StrStart, "End of file in string literal");
190
191 if (*CurPtr == '\n' || *CurPtr == '\r')
192 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnera8058742007-11-18 02:57:27 +0000193
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000194 if (*CurPtr != '\\') {
195 CurStrVal += *CurPtr++;
196 continue;
197 }
198
Chris Lattnera8058742007-11-18 02:57:27 +0000199 ++CurPtr;
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000200
201 switch (*CurPtr) {
202 case '\\': case '\'': case '"':
203 // These turn into their literal character.
204 CurStrVal += *CurPtr++;
205 break;
Chris Lattnere023bb62009-03-13 21:23:43 +0000206 case 't':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000207 CurStrVal += '\t';
Chris Lattnere023bb62009-03-13 21:23:43 +0000208 ++CurPtr;
209 break;
210 case 'n':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000211 CurStrVal += '\n';
Chris Lattnere023bb62009-03-13 21:23:43 +0000212 ++CurPtr;
213 break;
214
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000215 case '\n':
216 case '\r':
217 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
218
219 // If we hit the end of the buffer, report an error.
220 case '\0':
221 if (CurPtr == CurBuf->getBufferEnd())
222 return ReturnError(StrStart, "End of file in string literal");
223 // FALL THROUGH
224 default:
225 return ReturnError(CurPtr, "invalid escape in string literal");
226 }
Chris Lattnera8058742007-11-18 02:57:27 +0000227 }
228
Chris Lattnera8058742007-11-18 02:57:27 +0000229 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000230 return tgtok::StrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000231}
232
Chris Lattnerf4601652007-11-22 20:49:04 +0000233tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnera8058742007-11-18 02:57:27 +0000234 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4601652007-11-22 20:49:04 +0000235 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnera8058742007-11-18 02:57:27 +0000236
237 // Otherwise, we're ok, consume the rest of the characters.
238 const char *VarNameStart = CurPtr++;
239
240 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
241 ++CurPtr;
242
Chris Lattnerf4601652007-11-22 20:49:04 +0000243 CurStrVal.assign(VarNameStart, CurPtr);
244 return tgtok::VarName;
Chris Lattnera8058742007-11-18 02:57:27 +0000245}
246
247
Chris Lattnerf4601652007-11-22 20:49:04 +0000248tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnerc2b08752010-10-05 22:59:29 +0000249 // The first letter is [a-zA-Z_#].
Chris Lattnerf4601652007-11-22 20:49:04 +0000250 const char *IdentStart = TokStart;
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000251
Chris Lattnerc2b08752010-10-05 22:59:29 +0000252 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
David Greened3d1cad2011-10-19 13:04:43 +0000253 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
Chris Lattnerc2b08752010-10-05 22:59:29 +0000254 ++CurPtr;
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000255
Chris Lattnera8058742007-11-18 02:57:27 +0000256 // Check to see if this identifier is a keyword.
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000257 StringRef Str(IdentStart, CurPtr-IdentStart);
258
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000259 if (Str == "include") {
Chris Lattnerf4601652007-11-22 20:49:04 +0000260 if (LexInclude()) return tgtok::Error;
261 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000262 }
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000263
Benjamin Krameree573182011-10-06 18:53:43 +0000264 tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
265 .Case("int", tgtok::Int)
266 .Case("bit", tgtok::Bit)
267 .Case("bits", tgtok::Bits)
268 .Case("string", tgtok::String)
269 .Case("list", tgtok::List)
270 .Case("code", tgtok::Code)
271 .Case("dag", tgtok::Dag)
272 .Case("class", tgtok::Class)
273 .Case("def", tgtok::Def)
Benjamin Krameree573182011-10-06 18:53:43 +0000274 .Case("defm", tgtok::Defm)
275 .Case("multiclass", tgtok::MultiClass)
276 .Case("field", tgtok::Field)
277 .Case("let", tgtok::Let)
278 .Case("in", tgtok::In)
279 .Default(tgtok::Id);
280
281 if (Kind == tgtok::Id)
282 CurStrVal.assign(Str.begin(), Str.end());
283 return Kind;
Chris Lattnera8058742007-11-18 02:57:27 +0000284}
285
286/// LexInclude - We just read the "include" token. Get the string token that
287/// comes next and enter the include.
288bool TGLexer::LexInclude() {
289 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000290 tgtok::TokKind Tok = LexToken();
291 if (Tok == tgtok::Error) return true;
292 if (Tok != tgtok::StrVal) {
293 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000294 return true;
295 }
296
297 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000298 std::string Filename = CurStrVal;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000299 std::string IncludedFile;
Chris Lattnera8058742007-11-18 02:57:27 +0000300
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000301
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000302 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
303 IncludedFile);
Chris Lattnerd926e042009-06-21 05:33:06 +0000304 if (CurBuffer == -1) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000305 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000306 return true;
307 }
308
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000309 Dependencies.push_back(IncludedFile);
Chris Lattnera8058742007-11-18 02:57:27 +0000310 // Save the line number and lex buffer of the includer.
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000311 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +0000312 CurPtr = CurBuf->getBufferStart();
313 return false;
314}
315
316void TGLexer::SkipBCPLComment() {
317 ++CurPtr; // skip the second slash.
318 while (1) {
319 switch (*CurPtr) {
320 case '\n':
321 case '\r':
322 return; // Newline is end of comment.
323 case 0:
324 // If this is the end of the buffer, end the comment.
325 if (CurPtr == CurBuf->getBufferEnd())
326 return;
327 break;
328 }
329 // Otherwise, skip the character.
330 ++CurPtr;
331 }
332}
333
334/// SkipCComment - This skips C-style /**/ comments. The only difference from C
335/// is that we allow nesting.
336bool TGLexer::SkipCComment() {
337 ++CurPtr; // skip the star.
338 unsigned CommentDepth = 1;
339
340 while (1) {
341 int CurChar = getNextChar();
342 switch (CurChar) {
343 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000344 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000345 return true;
346 case '*':
347 // End of the comment?
348 if (CurPtr[0] != '/') break;
349
350 ++CurPtr; // End the */.
351 if (--CommentDepth == 0)
352 return false;
353 break;
354 case '/':
355 // Start of a nested comment?
356 if (CurPtr[0] != '*') break;
357 ++CurPtr;
358 ++CommentDepth;
359 break;
360 }
361 }
362}
363
364/// LexNumber - Lex:
365/// [-+]?[0-9]+
366/// 0x[0-9a-fA-F]+
367/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000368tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000369 if (CurPtr[-1] == '0') {
370 if (CurPtr[0] == 'x') {
371 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000372 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000373 while (isxdigit(CurPtr[0]))
374 ++CurPtr;
375
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000376 // Requires at least one hex digit.
377 if (CurPtr == NumStart)
Chris Lattner4226bb02009-06-21 19:22:49 +0000378 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000379
Dan Gohman63f97202008-10-17 01:33:43 +0000380 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000381 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000382 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000383 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000384 if (errno == ERANGE) {
385 errno = 0;
386 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
387 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000388 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000389 if (errno == ERANGE)
Chris Lattner4226bb02009-06-21 19:22:49 +0000390 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohman63f97202008-10-17 01:33:43 +0000391 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000392 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000393 } else if (CurPtr[0] == 'b') {
394 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000395 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000396 while (CurPtr[0] == '0' || CurPtr[0] == '1')
397 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000398
399 // Requires at least one binary digit.
400 if (CurPtr == NumStart)
401 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000402 CurIntVal = strtoll(NumStart, 0, 2);
403 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000404 }
405 }
406
407 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000408 if (!isdigit(CurPtr[0])) {
409 if (CurPtr[-1] == '-')
410 return tgtok::minus;
411 else if (CurPtr[-1] == '+')
412 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000413 }
414
415 while (isdigit(CurPtr[0]))
416 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000417 CurIntVal = strtoll(TokStart, 0, 10);
418 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000419}
420
421/// LexBracket - We just read '['. If this is a code block, return it,
422/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000423tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000424 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000425 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000426 ++CurPtr;
427 const char *CodeStart = CurPtr;
428 while (1) {
429 int Char = getNextChar();
430 if (Char == EOF) break;
431
432 if (Char != '}') continue;
433
434 Char = getNextChar();
435 if (Char == EOF) break;
436 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000437 CurStrVal.assign(CodeStart, CurPtr-2);
438 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000439 }
440 }
441
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000442 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000443}
444
445/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000446tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000447 if (!isalpha(*CurPtr))
Bill Wendlingdd2b6cb2010-12-08 13:03:15 +0000448 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000449
450 const char *Start = CurPtr++;
451 while (isalpha(*CurPtr))
452 ++CurPtr;
453
454 // Check to see which operator this is.
Bill Wendlingcd466f52010-12-08 20:02:49 +0000455 tgtok::TokKind Kind =
456 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
457 .Case("eq", tgtok::XEq)
458 .Case("if", tgtok::XIf)
David Greene1434f662011-01-07 17:05:37 +0000459 .Case("head", tgtok::XHead)
460 .Case("tail", tgtok::XTail)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000461 .Case("con", tgtok::XConcat)
462 .Case("shl", tgtok::XSHL)
463 .Case("sra", tgtok::XSRA)
464 .Case("srl", tgtok::XSRL)
465 .Case("cast", tgtok::XCast)
David Greene1434f662011-01-07 17:05:37 +0000466 .Case("empty", tgtok::XEmpty)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000467 .Case("subst", tgtok::XSubst)
468 .Case("foreach", tgtok::XForEach)
469 .Case("strconcat", tgtok::XStrConcat)
470 .Default(tgtok::Error);
David Greened418c1b2009-05-14 20:54:48 +0000471
Bill Wendlingcd466f52010-12-08 20:02:49 +0000472 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000473}
474