blob: 3262121ee8d8171e8b93dee86877997c4c3dc25f [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"
Chuck Rose III8b0ec642007-11-21 19:36:25 +000018#include "llvm/Config/config.h"
Bill Wendlingcd466f52010-12-08 20:02:49 +000019#include "llvm/ADT/StringSwitch.h"
20#include "llvm/ADT/Twine.h"
Chris Lattnera8058742007-11-18 02:57:27 +000021#include <cctype>
Duncan Sands4520dd22008-10-08 07:23:46 +000022#include <cstdio>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000023#include <cstdlib>
24#include <cstring>
Dan Gohman63f97202008-10-17 01:33:43 +000025#include <cerrno>
Chris Lattnera8058742007-11-18 02:57:27 +000026using namespace llvm;
27
Chris Lattner8070ea32009-06-21 03:41:50 +000028TGLexer::TGLexer(SourceMgr &SM) : SrcMgr(SM) {
Chris Lattneraa739d22009-03-13 07:05:43 +000029 CurBuffer = 0;
30 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +000031 CurPtr = CurBuf->getBufferStart();
Chris Lattner56a9fcf2007-11-19 07:43:52 +000032 TokStart = 0;
Chris Lattnera8058742007-11-18 02:57:27 +000033}
34
Chris Lattner1e3a8a42009-06-21 03:39:35 +000035SMLoc TGLexer::getLoc() const {
36 return SMLoc::getFromPointer(TokStart);
Chris Lattner1c8ae592009-03-13 16:01:53 +000037}
38
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 +000046int TGLexer::getNextChar() {
47 char CurChar = *CurPtr++;
48 switch (CurChar) {
49 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000050 return (unsigned char)CurChar;
Chris Lattneraa739d22009-03-13 07:05:43 +000051 case 0: {
Chris Lattnera8058742007-11-18 02:57:27 +000052 // A nul character in the stream is either the end of the current buffer or
53 // a random nul in the file. Disambiguate that here.
54 if (CurPtr-1 != CurBuf->getBufferEnd())
55 return 0; // Just whitespace.
56
57 // If this is the end of an included file, pop the parent file off the
58 // include stack.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000059 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
60 if (ParentIncludeLoc != SMLoc()) {
Chris Lattneraa739d22009-03-13 07:05:43 +000061 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
62 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattner1c8ae592009-03-13 16:01:53 +000063 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnera8058742007-11-18 02:57:27 +000064 return getNextChar();
65 }
66
67 // Otherwise, return end of file.
68 --CurPtr; // Another call to lex will return EOF again.
69 return EOF;
Chris Lattneraa739d22009-03-13 07:05:43 +000070 }
Chris Lattnera8058742007-11-18 02:57:27 +000071 case '\n':
72 case '\r':
73 // Handle the newline character by ignoring it and incrementing the line
74 // count. However, be careful about 'dos style' files with \n\r in them.
75 // Only treat a \n\r or \r\n as a single line.
76 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
77 *CurPtr != CurChar)
Chris Lattnerc1819182007-11-18 05:48:46 +000078 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +000079 return '\n';
80 }
81}
82
David Greenea761f922011-10-19 13:03:35 +000083int TGLexer::peekNextChar(int Index) {
84 return *(CurPtr + Index);
85}
86
Chris Lattnerf4601652007-11-22 20:49:04 +000087tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +000088 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +000089 // This always consumes at least one character.
90 int CurChar = getNextChar();
91
92 switch (CurChar) {
93 default:
Chris Lattnerc2b08752010-10-05 22:59:29 +000094 // Handle letters: [a-zA-Z_#]
David Greene065f2592009-05-05 16:28:25 +000095 if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
Chris Lattnera8058742007-11-18 02:57:27 +000096 return LexIdentifier();
97
Chris Lattnerf4601652007-11-22 20:49:04 +000098 // Unknown character, emit an error.
99 return ReturnError(TokStart, "Unexpected character");
100 case EOF: return tgtok::Eof;
101 case ':': return tgtok::colon;
102 case ';': return tgtok::semi;
103 case '.': return tgtok::period;
104 case ',': return tgtok::comma;
105 case '<': return tgtok::less;
106 case '>': return tgtok::greater;
107 case ']': return tgtok::r_square;
108 case '{': return tgtok::l_brace;
109 case '}': return tgtok::r_brace;
110 case '(': return tgtok::l_paren;
111 case ')': return tgtok::r_paren;
112 case '=': return tgtok::equal;
113 case '?': return tgtok::question;
114
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_#]*
253 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_' ||
254 *CurPtr == '#')
255 ++CurPtr;
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000256
Chris Lattnera8058742007-11-18 02:57:27 +0000257 // Check to see if this identifier is a keyword.
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000258 StringRef Str(IdentStart, CurPtr-IdentStart);
259
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000260 if (Str == "include") {
Chris Lattnerf4601652007-11-22 20:49:04 +0000261 if (LexInclude()) return tgtok::Error;
262 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000263 }
Benjamin Kramer37d42af2011-10-06 18:23:56 +0000264
Benjamin Krameree573182011-10-06 18:53:43 +0000265 tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
266 .Case("int", tgtok::Int)
267 .Case("bit", tgtok::Bit)
268 .Case("bits", tgtok::Bits)
269 .Case("string", tgtok::String)
270 .Case("list", tgtok::List)
271 .Case("code", tgtok::Code)
272 .Case("dag", tgtok::Dag)
273 .Case("class", tgtok::Class)
274 .Case("def", tgtok::Def)
Benjamin Krameree573182011-10-06 18:53:43 +0000275 .Case("defm", tgtok::Defm)
276 .Case("multiclass", tgtok::MultiClass)
277 .Case("field", tgtok::Field)
278 .Case("let", tgtok::Let)
279 .Case("in", tgtok::In)
280 .Default(tgtok::Id);
281
282 if (Kind == tgtok::Id)
283 CurStrVal.assign(Str.begin(), Str.end());
284 return Kind;
Chris Lattnera8058742007-11-18 02:57:27 +0000285}
286
287/// LexInclude - We just read the "include" token. Get the string token that
288/// comes next and enter the include.
289bool TGLexer::LexInclude() {
290 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000291 tgtok::TokKind Tok = LexToken();
292 if (Tok == tgtok::Error) return true;
293 if (Tok != tgtok::StrVal) {
294 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000295 return true;
296 }
297
298 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000299 std::string Filename = CurStrVal;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000300 std::string IncludedFile;
Chris Lattnera8058742007-11-18 02:57:27 +0000301
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000302
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000303 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
304 IncludedFile);
Chris Lattnerd926e042009-06-21 05:33:06 +0000305 if (CurBuffer == -1) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000306 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000307 return true;
308 }
309
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000310 Dependencies.push_back(IncludedFile);
Chris Lattnera8058742007-11-18 02:57:27 +0000311 // Save the line number and lex buffer of the includer.
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000312 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +0000313 CurPtr = CurBuf->getBufferStart();
314 return false;
315}
316
317void TGLexer::SkipBCPLComment() {
318 ++CurPtr; // skip the second slash.
319 while (1) {
320 switch (*CurPtr) {
321 case '\n':
322 case '\r':
323 return; // Newline is end of comment.
324 case 0:
325 // If this is the end of the buffer, end the comment.
326 if (CurPtr == CurBuf->getBufferEnd())
327 return;
328 break;
329 }
330 // Otherwise, skip the character.
331 ++CurPtr;
332 }
333}
334
335/// SkipCComment - This skips C-style /**/ comments. The only difference from C
336/// is that we allow nesting.
337bool TGLexer::SkipCComment() {
338 ++CurPtr; // skip the star.
339 unsigned CommentDepth = 1;
340
341 while (1) {
342 int CurChar = getNextChar();
343 switch (CurChar) {
344 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000345 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000346 return true;
347 case '*':
348 // End of the comment?
349 if (CurPtr[0] != '/') break;
350
351 ++CurPtr; // End the */.
352 if (--CommentDepth == 0)
353 return false;
354 break;
355 case '/':
356 // Start of a nested comment?
357 if (CurPtr[0] != '*') break;
358 ++CurPtr;
359 ++CommentDepth;
360 break;
361 }
362 }
363}
364
365/// LexNumber - Lex:
366/// [-+]?[0-9]+
367/// 0x[0-9a-fA-F]+
368/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000369tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000370 if (CurPtr[-1] == '0') {
371 if (CurPtr[0] == 'x') {
372 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000373 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000374 while (isxdigit(CurPtr[0]))
375 ++CurPtr;
376
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000377 // Requires at least one hex digit.
378 if (CurPtr == NumStart)
Chris Lattner4226bb02009-06-21 19:22:49 +0000379 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000380
Dan Gohman63f97202008-10-17 01:33:43 +0000381 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000382 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000383 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000384 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000385 if (errno == ERANGE) {
386 errno = 0;
387 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
388 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000389 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000390 if (errno == ERANGE)
Chris Lattner4226bb02009-06-21 19:22:49 +0000391 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohman63f97202008-10-17 01:33:43 +0000392 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000393 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000394 } else if (CurPtr[0] == 'b') {
395 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000396 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000397 while (CurPtr[0] == '0' || CurPtr[0] == '1')
398 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000399
400 // Requires at least one binary digit.
401 if (CurPtr == NumStart)
402 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000403 CurIntVal = strtoll(NumStart, 0, 2);
404 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000405 }
406 }
407
408 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000409 if (!isdigit(CurPtr[0])) {
410 if (CurPtr[-1] == '-')
411 return tgtok::minus;
412 else if (CurPtr[-1] == '+')
413 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000414 }
415
416 while (isdigit(CurPtr[0]))
417 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000418 CurIntVal = strtoll(TokStart, 0, 10);
419 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000420}
421
422/// LexBracket - We just read '['. If this is a code block, return it,
423/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000424tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000425 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000426 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000427 ++CurPtr;
428 const char *CodeStart = CurPtr;
429 while (1) {
430 int Char = getNextChar();
431 if (Char == EOF) break;
432
433 if (Char != '}') continue;
434
435 Char = getNextChar();
436 if (Char == EOF) break;
437 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000438 CurStrVal.assign(CodeStart, CurPtr-2);
439 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000440 }
441 }
442
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000443 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000444}
445
446/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000447tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000448 if (!isalpha(*CurPtr))
Bill Wendlingdd2b6cb2010-12-08 13:03:15 +0000449 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000450
451 const char *Start = CurPtr++;
452 while (isalpha(*CurPtr))
453 ++CurPtr;
454
455 // Check to see which operator this is.
Bill Wendlingcd466f52010-12-08 20:02:49 +0000456 tgtok::TokKind Kind =
457 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
458 .Case("eq", tgtok::XEq)
459 .Case("if", tgtok::XIf)
David Greene1434f662011-01-07 17:05:37 +0000460 .Case("head", tgtok::XHead)
461 .Case("tail", tgtok::XTail)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000462 .Case("con", tgtok::XConcat)
463 .Case("shl", tgtok::XSHL)
464 .Case("sra", tgtok::XSRA)
465 .Case("srl", tgtok::XSRL)
466 .Case("cast", tgtok::XCast)
David Greene1434f662011-01-07 17:05:37 +0000467 .Case("empty", tgtok::XEmpty)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000468 .Case("subst", tgtok::XSubst)
469 .Case("foreach", tgtok::XForEach)
470 .Case("strconcat", tgtok::XStrConcat)
471 .Default(tgtok::Error);
David Greened418c1b2009-05-14 20:54:48 +0000472
Bill Wendlingcd466f52010-12-08 20:02:49 +0000473 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000474}
475