blob: 572c36dccab9f0bfaf06b8086db2e9232aa07ab6 [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"
Chris Lattner099e1982009-06-21 03:36:54 +000015#include "llvm/Support/SourceMgr.h"
Chris Lattnera8058742007-11-18 02:57:27 +000016#include "llvm/Support/MemoryBuffer.h"
Chuck Rose III8b0ec642007-11-21 19:36:25 +000017#include "llvm/Config/config.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 Lattnera8058742007-11-18 02:57:27 +000038
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000039/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4601652007-11-22 20:49:04 +000040/// location. This is defined to always return tgtok::Error.
Benjamin Kramerd1e17032010-09-27 17:42:11 +000041tgtok::TokKind TGLexer::ReturnError(const char *Loc, const Twine &Msg) {
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000042 PrintError(Loc, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000043 return tgtok::Error;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000044}
Chris Lattnera8058742007-11-18 02:57:27 +000045
Chris Lattnera8058742007-11-18 02:57:27 +000046
Benjamin Kramerd1e17032010-09-27 17:42:11 +000047void TGLexer::PrintError(const char *Loc, const Twine &Msg) const {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000048 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
Chris Lattner1c8ae592009-03-13 16:01:53 +000049}
50
Benjamin Kramerd1e17032010-09-27 17:42:11 +000051void TGLexer::PrintError(SMLoc Loc, const Twine &Msg) const {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000052 SrcMgr.PrintMessage(Loc, Msg, "error");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000053}
54
Chris Lattner1c8ae592009-03-13 16:01:53 +000055
Chris Lattnera8058742007-11-18 02:57:27 +000056int TGLexer::getNextChar() {
57 char CurChar = *CurPtr++;
58 switch (CurChar) {
59 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000060 return (unsigned char)CurChar;
Chris Lattneraa739d22009-03-13 07:05:43 +000061 case 0: {
Chris Lattnera8058742007-11-18 02:57:27 +000062 // A nul character in the stream is either the end of the current buffer or
63 // a random nul in the file. Disambiguate that here.
64 if (CurPtr-1 != CurBuf->getBufferEnd())
65 return 0; // Just whitespace.
66
67 // If this is the end of an included file, pop the parent file off the
68 // include stack.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000069 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
70 if (ParentIncludeLoc != SMLoc()) {
Chris Lattneraa739d22009-03-13 07:05:43 +000071 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
72 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattner1c8ae592009-03-13 16:01:53 +000073 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnera8058742007-11-18 02:57:27 +000074 return getNextChar();
75 }
76
77 // Otherwise, return end of file.
78 --CurPtr; // Another call to lex will return EOF again.
79 return EOF;
Chris Lattneraa739d22009-03-13 07:05:43 +000080 }
Chris Lattnera8058742007-11-18 02:57:27 +000081 case '\n':
82 case '\r':
83 // Handle the newline character by ignoring it and incrementing the line
84 // count. However, be careful about 'dos style' files with \n\r in them.
85 // Only treat a \n\r or \r\n as a single line.
86 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
87 *CurPtr != CurChar)
Chris Lattnerc1819182007-11-18 05:48:46 +000088 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +000089 return '\n';
90 }
91}
92
Chris Lattnerf4601652007-11-22 20:49:04 +000093tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +000094 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +000095 // This always consumes at least one character.
96 int CurChar = getNextChar();
97
98 switch (CurChar) {
99 default:
Chris Lattnerc2b08752010-10-05 22:59:29 +0000100 // Handle letters: [a-zA-Z_#]
David Greene065f2592009-05-05 16:28:25 +0000101 if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
Chris Lattnera8058742007-11-18 02:57:27 +0000102 return LexIdentifier();
103
Chris Lattnerf4601652007-11-22 20:49:04 +0000104 // Unknown character, emit an error.
105 return ReturnError(TokStart, "Unexpected character");
106 case EOF: return tgtok::Eof;
107 case ':': return tgtok::colon;
108 case ';': return tgtok::semi;
109 case '.': return tgtok::period;
110 case ',': return tgtok::comma;
111 case '<': return tgtok::less;
112 case '>': return tgtok::greater;
113 case ']': return tgtok::r_square;
114 case '{': return tgtok::l_brace;
115 case '}': return tgtok::r_brace;
116 case '(': return tgtok::l_paren;
117 case ')': return tgtok::r_paren;
118 case '=': return tgtok::equal;
119 case '?': return tgtok::question;
120
Chris Lattnera8058742007-11-18 02:57:27 +0000121 case 0:
122 case ' ':
123 case '\t':
124 case '\n':
125 case '\r':
126 // Ignore whitespace.
127 return LexToken();
128 case '/':
129 // If this is the start of a // comment, skip until the end of the line or
130 // the end of the buffer.
131 if (*CurPtr == '/')
132 SkipBCPLComment();
133 else if (*CurPtr == '*') {
134 if (SkipCComment())
Chris Lattnerf4601652007-11-22 20:49:04 +0000135 return tgtok::Error;
136 } else // Otherwise, this is an error.
137 return ReturnError(TokStart, "Unexpected character");
Chris Lattnera8058742007-11-18 02:57:27 +0000138 return LexToken();
139 case '-': case '+':
140 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
141 case '7': case '8': case '9':
142 return LexNumber();
143 case '"': return LexString();
144 case '$': return LexVarName();
145 case '[': return LexBracket();
146 case '!': return LexExclaim();
147 }
148}
149
150/// LexString - Lex "[^"]*"
Chris Lattnerf4601652007-11-22 20:49:04 +0000151tgtok::TokKind TGLexer::LexString() {
Chris Lattnera8058742007-11-18 02:57:27 +0000152 const char *StrStart = CurPtr;
153
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000154 CurStrVal = "";
155
Chris Lattnera8058742007-11-18 02:57:27 +0000156 while (*CurPtr != '"') {
157 // If we hit the end of the buffer, report an error.
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000158 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
159 return ReturnError(StrStart, "End of file in string literal");
160
161 if (*CurPtr == '\n' || *CurPtr == '\r')
162 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnera8058742007-11-18 02:57:27 +0000163
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000164 if (*CurPtr != '\\') {
165 CurStrVal += *CurPtr++;
166 continue;
167 }
168
Chris Lattnera8058742007-11-18 02:57:27 +0000169 ++CurPtr;
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000170
171 switch (*CurPtr) {
172 case '\\': case '\'': case '"':
173 // These turn into their literal character.
174 CurStrVal += *CurPtr++;
175 break;
Chris Lattnere023bb62009-03-13 21:23:43 +0000176 case 't':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000177 CurStrVal += '\t';
Chris Lattnere023bb62009-03-13 21:23:43 +0000178 ++CurPtr;
179 break;
180 case 'n':
Chris Lattner7f3b28a2009-03-13 21:33:17 +0000181 CurStrVal += '\n';
Chris Lattnere023bb62009-03-13 21:23:43 +0000182 ++CurPtr;
183 break;
184
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000185 case '\n':
186 case '\r':
187 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
188
189 // If we hit the end of the buffer, report an error.
190 case '\0':
191 if (CurPtr == CurBuf->getBufferEnd())
192 return ReturnError(StrStart, "End of file in string literal");
193 // FALL THROUGH
194 default:
195 return ReturnError(CurPtr, "invalid escape in string literal");
196 }
Chris Lattnera8058742007-11-18 02:57:27 +0000197 }
198
Chris Lattnera8058742007-11-18 02:57:27 +0000199 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000200 return tgtok::StrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000201}
202
Chris Lattnerf4601652007-11-22 20:49:04 +0000203tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnera8058742007-11-18 02:57:27 +0000204 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4601652007-11-22 20:49:04 +0000205 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnera8058742007-11-18 02:57:27 +0000206
207 // Otherwise, we're ok, consume the rest of the characters.
208 const char *VarNameStart = CurPtr++;
209
210 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
211 ++CurPtr;
212
Chris Lattnerf4601652007-11-22 20:49:04 +0000213 CurStrVal.assign(VarNameStart, CurPtr);
214 return tgtok::VarName;
Chris Lattnera8058742007-11-18 02:57:27 +0000215}
216
217
Chris Lattnerf4601652007-11-22 20:49:04 +0000218tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnerc2b08752010-10-05 22:59:29 +0000219 // The first letter is [a-zA-Z_#].
Chris Lattnerf4601652007-11-22 20:49:04 +0000220 const char *IdentStart = TokStart;
Chris Lattnera8058742007-11-18 02:57:27 +0000221
Chris Lattnerc2b08752010-10-05 22:59:29 +0000222 // Match the rest of the identifier regex: [0-9a-zA-Z_#]*
223 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_' ||
224 *CurPtr == '#')
225 ++CurPtr;
David Greene065f2592009-05-05 16:28:25 +0000226
Chris Lattnera8058742007-11-18 02:57:27 +0000227
228 // Check to see if this identifier is a keyword.
229 unsigned Len = CurPtr-IdentStart;
230
Chris Lattnerf4601652007-11-22 20:49:04 +0000231 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
232 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
233 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
234 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
235 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
236 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
237 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
Chris Lattnera8058742007-11-18 02:57:27 +0000238
Chris Lattnerf4601652007-11-22 20:49:04 +0000239 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
240 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
241 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
242 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
243 return tgtok::MultiClass;
244 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
245 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
246 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
Chris Lattnera8058742007-11-18 02:57:27 +0000247
248 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000249 if (LexInclude()) return tgtok::Error;
250 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000251 }
252
Chris Lattnerf4601652007-11-22 20:49:04 +0000253 CurStrVal.assign(IdentStart, CurPtr);
254 return tgtok::Id;
Chris Lattnera8058742007-11-18 02:57:27 +0000255}
256
257/// LexInclude - We just read the "include" token. Get the string token that
258/// comes next and enter the include.
259bool TGLexer::LexInclude() {
260 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000261 tgtok::TokKind Tok = LexToken();
262 if (Tok == tgtok::Error) return true;
263 if (Tok != tgtok::StrVal) {
264 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000265 return true;
266 }
267
268 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000269 std::string Filename = CurStrVal;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000270 std::string IncludedFile;
Chris Lattnera8058742007-11-18 02:57:27 +0000271
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000272
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000273 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
274 IncludedFile);
Chris Lattnerd926e042009-06-21 05:33:06 +0000275 if (CurBuffer == -1) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000276 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000277 return true;
278 }
279
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000280 Dependencies.push_back(IncludedFile);
Chris Lattnera8058742007-11-18 02:57:27 +0000281 // Save the line number and lex buffer of the includer.
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000282 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +0000283 CurPtr = CurBuf->getBufferStart();
284 return false;
285}
286
287void TGLexer::SkipBCPLComment() {
288 ++CurPtr; // skip the second slash.
289 while (1) {
290 switch (*CurPtr) {
291 case '\n':
292 case '\r':
293 return; // Newline is end of comment.
294 case 0:
295 // If this is the end of the buffer, end the comment.
296 if (CurPtr == CurBuf->getBufferEnd())
297 return;
298 break;
299 }
300 // Otherwise, skip the character.
301 ++CurPtr;
302 }
303}
304
305/// SkipCComment - This skips C-style /**/ comments. The only difference from C
306/// is that we allow nesting.
307bool TGLexer::SkipCComment() {
308 ++CurPtr; // skip the star.
309 unsigned CommentDepth = 1;
310
311 while (1) {
312 int CurChar = getNextChar();
313 switch (CurChar) {
314 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000315 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000316 return true;
317 case '*':
318 // End of the comment?
319 if (CurPtr[0] != '/') break;
320
321 ++CurPtr; // End the */.
322 if (--CommentDepth == 0)
323 return false;
324 break;
325 case '/':
326 // Start of a nested comment?
327 if (CurPtr[0] != '*') break;
328 ++CurPtr;
329 ++CommentDepth;
330 break;
331 }
332 }
333}
334
335/// LexNumber - Lex:
336/// [-+]?[0-9]+
337/// 0x[0-9a-fA-F]+
338/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000339tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000340 if (CurPtr[-1] == '0') {
341 if (CurPtr[0] == 'x') {
342 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000343 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000344 while (isxdigit(CurPtr[0]))
345 ++CurPtr;
346
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000347 // Requires at least one hex digit.
348 if (CurPtr == NumStart)
Chris Lattner4226bb02009-06-21 19:22:49 +0000349 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000350
Dan Gohman63f97202008-10-17 01:33:43 +0000351 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000352 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000353 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000354 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000355 if (errno == ERANGE) {
356 errno = 0;
357 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
358 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000359 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000360 if (errno == ERANGE)
Chris Lattner4226bb02009-06-21 19:22:49 +0000361 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohman63f97202008-10-17 01:33:43 +0000362 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000363 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000364 } else if (CurPtr[0] == 'b') {
365 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000366 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000367 while (CurPtr[0] == '0' || CurPtr[0] == '1')
368 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000369
370 // Requires at least one binary digit.
371 if (CurPtr == NumStart)
372 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000373 CurIntVal = strtoll(NumStart, 0, 2);
374 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000375 }
376 }
377
378 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000379 if (!isdigit(CurPtr[0])) {
380 if (CurPtr[-1] == '-')
381 return tgtok::minus;
382 else if (CurPtr[-1] == '+')
383 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000384 }
385
386 while (isdigit(CurPtr[0]))
387 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000388 CurIntVal = strtoll(TokStart, 0, 10);
389 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000390}
391
392/// LexBracket - We just read '['. If this is a code block, return it,
393/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000394tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000395 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000396 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000397 ++CurPtr;
398 const char *CodeStart = CurPtr;
399 while (1) {
400 int Char = getNextChar();
401 if (Char == EOF) break;
402
403 if (Char != '}') continue;
404
405 Char = getNextChar();
406 if (Char == EOF) break;
407 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000408 CurStrVal.assign(CodeStart, CurPtr-2);
409 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000410 }
411 }
412
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000413 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000414}
415
416/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000417tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000418 if (!isalpha(*CurPtr))
Bill Wendlingdd2b6cb2010-12-08 13:03:15 +0000419 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000420
421 const char *Start = CurPtr++;
422 while (isalpha(*CurPtr))
423 ++CurPtr;
424
425 // Check to see which operator this is.
Bill Wendlingcd466f52010-12-08 20:02:49 +0000426 tgtok::TokKind Kind =
427 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
428 .Case("eq", tgtok::XEq)
429 .Case("if", tgtok::XIf)
David Greene1434f662011-01-07 17:05:37 +0000430 .Case("head", tgtok::XHead)
431 .Case("tail", tgtok::XTail)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000432 .Case("con", tgtok::XConcat)
433 .Case("shl", tgtok::XSHL)
434 .Case("sra", tgtok::XSRA)
435 .Case("srl", tgtok::XSRL)
436 .Case("cast", tgtok::XCast)
David Greene1434f662011-01-07 17:05:37 +0000437 .Case("empty", tgtok::XEmpty)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000438 .Case("subst", tgtok::XSubst)
439 .Case("foreach", tgtok::XForEach)
440 .Case("strconcat", tgtok::XStrConcat)
441 .Default(tgtok::Error);
David Greened418c1b2009-05-14 20:54:48 +0000442
Bill Wendlingcd466f52010-12-08 20:02:49 +0000443 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000444}
445