1 /*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
3 // This file defines a simple flex scanner for TableGen files. This is pretty
4 // straight-forward, except for the magic to handle file inclusion.
6 //===----------------------------------------------------------------------===*/
11 %option never-interactive
15 %option outfile="Lexer.cpp"
24 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
25 #include "FileParser.h"
27 // ParseInt - This has to handle the special case of binary numbers 0b0101
28 static int ParseInt(const char *Str) {
29 if (Str[0] == '0' && Str[1] == 'b')
30 return strtol(Str+2, 0, 2);
31 return strtol(Str, 0, 0);
34 static int CommentDepth = 0;
40 YY_BUFFER_STATE Buffer;
42 IncludeRec(const std::string &FN, FILE *F)
43 : Filename(FN), File(F), LineNo(0){
47 static std::vector<IncludeRec> IncludeStack;
51 if (IncludeStack.empty())
52 return std::cerr << "At end of input: ";
54 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
55 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
56 << IncludeStack[i].LineNo << ":\n";
57 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
58 << Filelineno << ": ";
64 void ParseFile(const std::string &Filename) {
66 if (Filename != "-") {
67 F = fopen(Filename.c_str(), "r");
70 std::cerr << "Could not open input file '" + Filename + "'!\n";
73 IncludeStack.push_back(IncludeRec(Filename, F));
75 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
84 // HandleInclude - This function is called when an include directive is
85 // encountered in the input stream...
86 static void HandleInclude(const char *Buffer) {
87 unsigned Length = yyleng;
88 assert(Buffer[Length-1] == '"');
89 Buffer += strlen("include ");
90 Length -= strlen("include ");
91 while (*Buffer != '"') {
95 assert(Length >= 2 && "Double quotes not found?");
96 std::string Filename(Buffer+1, Buffer+Length-1);
97 //std::cerr << "Filename = '" << Filename << "'\n";
99 // Save the line number and lex buffer of the includer...
100 IncludeStack.back().LineNo = Filelineno;
101 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
103 // Open the new input file...
104 yyin = fopen(Filename.c_str(), "r");
106 err() << "Could not find include file '" << Filename << "'!\n";
110 // Add the file to our include stack...
111 IncludeStack.push_back(IncludeRec(Filename, yyin));
112 Filelineno = 1; // Reset line numbering...
113 //yyrestart(yyin); // Start lexing the new file...
115 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
119 // yywrap - This is called when the lexer runs out of input in one of the files.
120 // Switch back to an includer if an includee has run out of input.
124 if (IncludeStack.back().File != stdin)
125 fclose(IncludeStack.back().File);
126 IncludeStack.pop_back();
127 if (IncludeStack.empty()) return 1; // Top-level file is done.
129 // Otherwise, we need to switch back to a file which included the current one.
130 Filelineno = IncludeStack.back().LineNo; // Restore current line number
131 yy_switch_to_buffer(IncludeStack.back().Buffer);
139 Identifier [a-zA-Z_][0-9a-zA-Z_]*
140 Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
141 CodeFragment \[\{([^}]+|\}[^\]])*\}\]
143 IncludeStr include[ \t\n]+\"[^"]*\"
147 {Comment} { /* Ignore comments */ }
149 {IncludeStr} { HandleInclude(yytext); }
150 {CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
151 return CODEFRAGMENT; }
155 bits { return BITS; }
156 string { return STRING; }
157 list { return LIST; }
158 code { return CODE; }
161 class { return CLASS; }
163 field { return FIELD; }
167 {Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
169 ${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
172 {StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
175 {Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
177 [ \t\n]+ { /* Ignore whitespace */ }
180 "/*" { BEGIN(comment); CommentDepth++; }
181 <comment>[^*/]* /* eat anything that's not a '*' or '/' */
182 <comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
183 <comment>"/*" { ++CommentDepth; }
184 <comment>"/"+[^*]* /* eat up /'s not followed by *'s */
185 <comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
186 <comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
188 . { return Filetext[0]; }