1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Lexer for TableGen.
12 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/Streams.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
20 #include "FileParser.h"
24 // FIXME: REMOVE THIS.
28 TGLexer::TGLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
29 CurPtr = CurBuf->getBufferStart();
33 while (!IncludeStack.empty()) {
34 delete IncludeStack.back().Buffer;
35 IncludeStack.pop_back();
41 std::ostream &TGLexer::err() {
42 PrintIncludeStack(*cerr.stream());
43 return *cerr.stream();
47 void TGLexer::PrintIncludeStack(std::ostream &OS) {
48 for (unsigned i = 0, e = IncludeStack.size(); i != e; ++i)
49 OS << "Included from " << IncludeStack[i].Buffer->getBufferIdentifier()
50 << ":" << IncludeStack[i].LineNo << ":\n";
51 OS << "Parsing " << CurBuf->getBufferIdentifier() << ":"
55 int TGLexer::getNextChar() {
56 char CurChar = *CurPtr++;
61 // A nul character in the stream is either the end of the current buffer or
62 // a random nul in the file. Disambiguate that here.
63 if (CurPtr-1 != CurBuf->getBufferEnd())
64 return 0; // Just whitespace.
66 // If this is the end of an included file, pop the parent file off the
68 if (!IncludeStack.empty()) {
70 CurBuf = IncludeStack.back().Buffer;
71 CurLineNo = IncludeStack.back().LineNo;
72 CurPtr = IncludeStack.back().CurPtr;
73 IncludeStack.pop_back();
77 // Otherwise, return end of file.
78 --CurPtr; // Another call to lex will return EOF again.
82 // Handle the newline character by ignoring it and incrementing the line
83 // count. However, be careful about 'dos style' files with \n\r in them.
84 // Only treat a \n\r or \r\n as a single line.
85 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
87 ++CurPtr; // Each the two char newline sequence.
94 int TGLexer::LexToken() {
95 // This always consumes at least one character.
96 int CurChar = getNextChar();
100 // Handle letters: [a-zA-Z_]
101 if (isalpha(CurChar) || CurChar == '_')
102 return LexIdentifier();
104 // Unknown character, return the char itself.
105 return (unsigned char)CurChar;
106 case EOF: return YYEOF;
112 // Ignore whitespace.
115 // If this is the start of a // comment, skip until the end of the line or
116 // the end of the buffer.
119 else if (*CurPtr == '*') {
122 } else // Otherwise, return this / as a token.
126 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
127 case '7': case '8': case '9':
129 case '"': return LexString();
130 case '$': return LexVarName();
131 case '[': return LexBracket();
132 case '!': return LexExclaim();
136 /// LexString - Lex "[^"]*"
137 int TGLexer::LexString() {
138 const char *StrStart = CurPtr;
140 while (*CurPtr != '"') {
141 // If we hit the end of the buffer, report an error.
142 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd()) {
143 TheError = "End of file in string literal";
145 } else if (*CurPtr == '\n' || *CurPtr == '\r') {
146 TheError = "End of line in string literal";
153 Filelval.StrVal = new std::string(StrStart, CurPtr);
158 int TGLexer::LexVarName() {
159 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
160 return '$'; // Invalid varname.
162 // Otherwise, we're ok, consume the rest of the characters.
163 const char *VarNameStart = CurPtr++;
165 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
168 Filelval.StrVal = new std::string(VarNameStart, CurPtr);
173 int TGLexer::LexIdentifier() {
174 // The first letter is [a-zA-Z_].
175 const char *IdentStart = CurPtr-1;
177 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
178 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
181 // Check to see if this identifier is a keyword.
182 unsigned Len = CurPtr-IdentStart;
184 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return INT;
185 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return BIT;
186 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return BITS;
187 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return STRING;
188 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return LIST;
189 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return CODE;
190 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return DAG;
192 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return CLASS;
193 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return DEF;
194 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return DEFM;
195 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10)) return MULTICLASS;
196 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return FIELD;
197 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return LET;
198 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return IN;
200 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
201 if (LexInclude()) return YYERROR;
205 Filelval.StrVal = new std::string(IdentStart, CurPtr);
209 /// LexInclude - We just read the "include" token. Get the string token that
210 /// comes next and enter the include.
211 bool TGLexer::LexInclude() {
212 // The token after the include must be a string.
213 int Tok = LexToken();
214 if (Tok == YYERROR) return true;
216 TheError = "Expected filename after include";
221 std::string Filename = *Filelval.StrVal;
222 delete Filelval.StrVal;
224 // Try to find the file.
225 MemoryBuffer *NewBuf = MemoryBuffer::getFile(&Filename[0], Filename.size());
227 // If the file didn't exist directly, see if it's in an include path.
228 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
229 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
230 NewBuf = MemoryBuffer::getFile(&IncFile[0], IncFile.size());
234 TheError = "Could not find include file '" + Filename + "'";
238 // Save the line number and lex buffer of the includer.
239 IncludeStack.push_back(IncludeRec(CurBuf, CurPtr, CurLineNo));
241 CurLineNo = 1; // Reset line numbering.
243 CurPtr = CurBuf->getBufferStart();
247 void TGLexer::SkipBCPLComment() {
248 ++CurPtr; // skip the second slash.
253 return; // Newline is end of comment.
255 // If this is the end of the buffer, end the comment.
256 if (CurPtr == CurBuf->getBufferEnd())
260 // Otherwise, skip the character.
265 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
266 /// is that we allow nesting.
267 bool TGLexer::SkipCComment() {
268 ++CurPtr; // skip the star.
269 unsigned CommentDepth = 1;
272 int CurChar = getNextChar();
275 TheError = "Unterminated comment!";
278 // End of the comment?
279 if (CurPtr[0] != '/') break;
281 ++CurPtr; // End the */.
282 if (--CommentDepth == 0)
286 // Start of a nested comment?
287 if (CurPtr[0] != '*') break;
299 int TGLexer::LexNumber() {
300 const char *NumStart = CurPtr-1;
302 if (CurPtr[-1] == '0') {
303 if (CurPtr[0] == 'x') {
306 while (isxdigit(CurPtr[0]))
309 if (CurPtr == NumStart) {
310 TheError = "Invalid hexadecimal number";
313 Filelval.IntVal = strtoll(NumStart, 0, 16);
315 } else if (CurPtr[0] == 'b') {
318 while (CurPtr[0] == '0' || CurPtr[0] == '1')
321 if (CurPtr == NumStart) {
322 TheError = "Invalid binary number";
325 Filelval.IntVal = strtoll(NumStart, 0, 2);
330 // Check for a sign without a digit.
331 if (CurPtr[-1] == '-' || CurPtr[-1] == '+') {
332 if (!isdigit(CurPtr[0]))
336 while (isdigit(CurPtr[0]))
338 Filelval.IntVal = strtoll(NumStart, 0, 10);
342 /// LexBracket - We just read '['. If this is a code block, return it,
343 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
344 int TGLexer::LexBracket() {
345 if (CurPtr[0] != '{')
348 const char *CodeStart = CurPtr;
350 int Char = getNextChar();
351 if (Char == EOF) break;
353 if (Char != '}') continue;
355 Char = getNextChar();
356 if (Char == EOF) break;
358 Filelval.StrVal = new std::string(CodeStart, CurPtr-2);
363 TheError = "Invalid Code Block";
367 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
368 int TGLexer::LexExclaim() {
369 if (!isalpha(*CurPtr))
372 const char *Start = CurPtr++;
373 while (isalpha(*CurPtr))
376 // Check to see which operator this is.
377 unsigned Len = CurPtr-Start;
379 if (Len == 3 && !memcmp(Start, "con", 3)) return CONCATTOK;
380 if (Len == 3 && !memcmp(Start, "sra", 3)) return SRATOK;
381 if (Len == 3 && !memcmp(Start, "srl", 3)) return SRLTOK;
382 if (Len == 3 && !memcmp(Start, "shl", 3)) return SHLTOK;
383 if (Len == 9 && !memcmp(Start, "strconcat", 9)) return STRCONCATTOK;
385 TheError = "Unknown operator";
389 //===----------------------------------------------------------------------===//
390 // Interfaces used by the Bison parser.
391 //===----------------------------------------------------------------------===//
394 static TGLexer *TheLexer;
398 std::ostream &err() {
399 return TheLexer->err();
402 /// ParseFile - this function begins the parsing of the specified tablegen
405 void ParseFile(const std::string &Filename,
406 const std::vector<std::string> &IncludeDirs) {
407 std::string ErrorStr;
408 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(&Filename[0], Filename.size(),
411 cerr << "Could not open input file '" + Filename + "': " << ErrorStr <<"\n";
415 assert(!TheLexer && "Lexer isn't reentrant yet!");
416 TheLexer = new TGLexer(F);
418 // Record the location of the include directory so that the lexer can find
420 TheLexer->setIncludeDirs(IncludeDirs);
428 } // End llvm namespace
432 assert(TheLexer && "No lexer setup yet!");
433 int Tok = TheLexer->LexToken();
434 if (Tok == YYERROR) {
435 err() << TheLexer->getError() << "\n";