blob: c9c674c8898d82e5f7abb6e21dda6b947a1f7898 [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
26#include "llvm/Config/config.h"
27
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)
Benjamin Krameree573182011-10-06 18:53:43 +0000277 .Case("defm", tgtok::Defm)
278 .Case("multiclass", tgtok::MultiClass)
279 .Case("field", tgtok::Field)
280 .Case("let", tgtok::Let)
281 .Case("in", tgtok::In)
282 .Default(tgtok::Id);
283
284 if (Kind == tgtok::Id)
285 CurStrVal.assign(Str.begin(), Str.end());
286 return Kind;
Chris Lattnera8058742007-11-18 02:57:27 +0000287}
288
289/// LexInclude - We just read the "include" token. Get the string token that
290/// comes next and enter the include.
291bool TGLexer::LexInclude() {
292 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000293 tgtok::TokKind Tok = LexToken();
294 if (Tok == tgtok::Error) return true;
295 if (Tok != tgtok::StrVal) {
296 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000297 return true;
298 }
299
300 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000301 std::string Filename = CurStrVal;
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000302 std::string IncludedFile;
Chris Lattnera8058742007-11-18 02:57:27 +0000303
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000304
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000305 CurBuffer = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr),
306 IncludedFile);
Chris Lattnerd926e042009-06-21 05:33:06 +0000307 if (CurBuffer == -1) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000308 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000309 return true;
310 }
311
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000312 Dependencies.push_back(IncludedFile);
Chris Lattnera8058742007-11-18 02:57:27 +0000313 // Save the line number and lex buffer of the includer.
Chris Lattner7ee5d5f2009-06-21 05:06:04 +0000314 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +0000315 CurPtr = CurBuf->getBufferStart();
316 return false;
317}
318
319void TGLexer::SkipBCPLComment() {
320 ++CurPtr; // skip the second slash.
321 while (1) {
322 switch (*CurPtr) {
323 case '\n':
324 case '\r':
325 return; // Newline is end of comment.
326 case 0:
327 // If this is the end of the buffer, end the comment.
328 if (CurPtr == CurBuf->getBufferEnd())
329 return;
330 break;
331 }
332 // Otherwise, skip the character.
333 ++CurPtr;
334 }
335}
336
337/// SkipCComment - This skips C-style /**/ comments. The only difference from C
338/// is that we allow nesting.
339bool TGLexer::SkipCComment() {
340 ++CurPtr; // skip the star.
341 unsigned CommentDepth = 1;
342
343 while (1) {
344 int CurChar = getNextChar();
345 switch (CurChar) {
346 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000347 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000348 return true;
349 case '*':
350 // End of the comment?
351 if (CurPtr[0] != '/') break;
352
353 ++CurPtr; // End the */.
354 if (--CommentDepth == 0)
355 return false;
356 break;
357 case '/':
358 // Start of a nested comment?
359 if (CurPtr[0] != '*') break;
360 ++CurPtr;
361 ++CommentDepth;
362 break;
363 }
364 }
365}
366
367/// LexNumber - Lex:
368/// [-+]?[0-9]+
369/// 0x[0-9a-fA-F]+
370/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000371tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000372 if (CurPtr[-1] == '0') {
373 if (CurPtr[0] == 'x') {
374 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000375 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000376 while (isxdigit(CurPtr[0]))
377 ++CurPtr;
378
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000379 // Requires at least one hex digit.
380 if (CurPtr == NumStart)
Chris Lattner4226bb02009-06-21 19:22:49 +0000381 return ReturnError(TokStart, "Invalid hexadecimal number");
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000382
Dan Gohman63f97202008-10-17 01:33:43 +0000383 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000384 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000385 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000386 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000387 if (errno == ERANGE) {
388 errno = 0;
389 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
390 if (errno == EINVAL)
Chris Lattner4226bb02009-06-21 19:22:49 +0000391 return ReturnError(TokStart, "Invalid hexadecimal number");
Dan Gohman63f97202008-10-17 01:33:43 +0000392 if (errno == ERANGE)
Chris Lattner4226bb02009-06-21 19:22:49 +0000393 return ReturnError(TokStart, "Hexadecimal number out of range");
Dan Gohman63f97202008-10-17 01:33:43 +0000394 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000395 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000396 } else if (CurPtr[0] == 'b') {
397 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000398 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000399 while (CurPtr[0] == '0' || CurPtr[0] == '1')
400 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000401
402 // Requires at least one binary digit.
403 if (CurPtr == NumStart)
404 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000405 CurIntVal = strtoll(NumStart, 0, 2);
406 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000407 }
408 }
409
410 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000411 if (!isdigit(CurPtr[0])) {
412 if (CurPtr[-1] == '-')
413 return tgtok::minus;
414 else if (CurPtr[-1] == '+')
415 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000416 }
417
418 while (isdigit(CurPtr[0]))
419 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000420 CurIntVal = strtoll(TokStart, 0, 10);
421 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000422}
423
424/// LexBracket - We just read '['. If this is a code block, return it,
425/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000426tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000427 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000428 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000429 ++CurPtr;
430 const char *CodeStart = CurPtr;
431 while (1) {
432 int Char = getNextChar();
433 if (Char == EOF) break;
434
435 if (Char != '}') continue;
436
437 Char = getNextChar();
438 if (Char == EOF) break;
439 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000440 CurStrVal.assign(CodeStart, CurPtr-2);
441 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000442 }
443 }
444
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000445 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000446}
447
448/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000449tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000450 if (!isalpha(*CurPtr))
Bill Wendlingdd2b6cb2010-12-08 13:03:15 +0000451 return ReturnError(CurPtr - 1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000452
453 const char *Start = CurPtr++;
454 while (isalpha(*CurPtr))
455 ++CurPtr;
456
457 // Check to see which operator this is.
Bill Wendlingcd466f52010-12-08 20:02:49 +0000458 tgtok::TokKind Kind =
459 StringSwitch<tgtok::TokKind>(StringRef(Start, CurPtr - Start))
460 .Case("eq", tgtok::XEq)
461 .Case("if", tgtok::XIf)
David Greene1434f662011-01-07 17:05:37 +0000462 .Case("head", tgtok::XHead)
463 .Case("tail", tgtok::XTail)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000464 .Case("con", tgtok::XConcat)
465 .Case("shl", tgtok::XSHL)
466 .Case("sra", tgtok::XSRA)
467 .Case("srl", tgtok::XSRL)
468 .Case("cast", tgtok::XCast)
David Greene1434f662011-01-07 17:05:37 +0000469 .Case("empty", tgtok::XEmpty)
Bill Wendlingcd466f52010-12-08 20:02:49 +0000470 .Case("subst", tgtok::XSubst)
471 .Case("foreach", tgtok::XForEach)
472 .Case("strconcat", tgtok::XStrConcat)
473 .Default(tgtok::Error);
David Greened418c1b2009-05-14 20:54:48 +0000474
Bill Wendlingcd466f52010-12-08 20:02:49 +0000475 return Kind != tgtok::Error ? Kind : ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000476}
477