1 /*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a simple flex scanner for TableGen files. This is pretty
11 // straight-forward, except for the magic to handle file inclusion.
13 //===----------------------------------------------------------------------===*/
18 %option never-interactive
22 %option outfile="Lexer.cpp"
31 typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
32 #include "FileParser.h"
38 // Global variable recording the location of the include directory
39 std::string IncludeDirectory;
41 /// ParseInt - This has to handle the special case of binary numbers 0b0101
43 static int ParseInt(const char *Str) {
44 if (Str[0] == '0' && Str[1] == 'b')
45 return strtol(Str+2, 0, 2);
46 return strtol(Str, 0, 0);
49 static int CommentDepth = 0;
55 YY_BUFFER_STATE Buffer;
57 IncludeRec(const std::string &FN, FILE *F)
58 : Filename(FN), File(F), LineNo(0){
62 static std::vector<IncludeRec> IncludeStack;
65 if (IncludeStack.empty())
66 return std::cerr << "At end of input: ";
68 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
69 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
70 << IncludeStack[i].LineNo << ":\n";
71 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
72 << Filelineno << ": ";
75 /// ParseFile - this function begins the parsing of the specified tablegen file.
77 void ParseFile(const std::string &Filename, const std::string & IncludeDir) {
79 if (Filename != "-") {
80 F = fopen(Filename.c_str(), "r");
83 std::cerr << "Could not open input file '" + Filename + "'!\n";
86 IncludeStack.push_back(IncludeRec(Filename, F));
88 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
91 // Record the location of the include directory so that the lexer can find
93 IncludeDirectory = IncludeDir;
101 /// HandleInclude - This function is called when an include directive is
102 /// encountered in the input stream...
104 static void HandleInclude(const char *Buffer) {
105 unsigned Length = yyleng;
106 assert(Buffer[Length-1] == '"');
107 Buffer += strlen("include ");
108 Length -= strlen("include ");
109 while (*Buffer != '"') {
113 assert(Length >= 2 && "Double quotes not found?");
114 std::string Filename(Buffer+1, Buffer+Length-1);
115 //std::cerr << "Filename = '" << Filename << "'\n";
117 // Save the line number and lex buffer of the includer...
118 IncludeStack.back().LineNo = Filelineno;
119 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
121 // Open the new input file...
122 yyin = fopen(Filename.c_str(), "r");
124 // If we couldn't find the file in the current directory, look for it in
125 // the include directories.
127 // NOTE: Right now, there is only one directory. We need to eventually add
129 std::string NextFilename = IncludeDirectory + "/" + Filename;
130 yyin = fopen(NextFilename.c_str(), "r");
132 err() << "Could not find include file '" << Filename << "'!\n";
135 Filename = NextFilename;
138 // Add the file to our include stack...
139 IncludeStack.push_back(IncludeRec(Filename, yyin));
140 Filelineno = 1; // Reset line numbering...
141 //yyrestart(yyin); // Start lexing the new file...
143 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
146 /// yywrap - This is called when the lexer runs out of input in one of the
147 /// files. Switch back to an includer if an includee has run out of input.
151 if (IncludeStack.back().File != stdin)
152 fclose(IncludeStack.back().File);
153 IncludeStack.pop_back();
154 if (IncludeStack.empty()) return 1; // Top-level file is done.
156 // Otherwise, we need to switch back to a file which included the current one.
157 Filelineno = IncludeStack.back().LineNo; // Restore current line number
158 yy_switch_to_buffer(IncludeStack.back().Buffer);
162 } // End llvm namespace
164 using namespace llvm;
170 Identifier [a-zA-Z_][0-9a-zA-Z_]*
171 Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
172 CodeFragment \[\{([^}]+|\}[^\]])*\}\]
174 IncludeStr include[ \t\n]+\"[^"]*\"
178 {Comment} { /* Ignore comments */ }
180 {IncludeStr} { HandleInclude(yytext); }
181 {CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
182 return CODEFRAGMENT; }
186 bits { return BITS; }
187 string { return STRING; }
188 list { return LIST; }
189 code { return CODE; }
192 class { return CLASS; }
194 field { return FIELD; }
198 !sra { return SRATOK; }
199 !srl { return SRLTOK; }
200 !shl { return SHLTOK; }
203 {Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
205 ${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
208 {StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
211 {Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
213 [ \t\n\r]+ { /* Ignore whitespace */ }
216 "/*" { BEGIN(comment); CommentDepth++; }
217 <comment>[^*/]* {} /* eat anything that's not a '*' or '/' */
218 <comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */
219 <comment>"/*" { ++CommentDepth; }
220 <comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */
221 <comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
222 <comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
224 . { return Filetext[0]; }