1 /*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
4 //===------------------------------------------------------------------------=*/
9 %option never-interactive
13 %option outfile="Lexer.cpp"
22 typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
23 #include "FileParser.h"
25 // ParseInt - This has to handle the special case of binary numbers 0b0101
26 static int ParseInt(const char *Str) {
27 if (Str[0] == '0' && Str[1] == 'b')
28 return strtol(Str+2, 0, 2);
29 return strtol(Str, 0, 0);
32 static int CommentDepth = 0;
38 YY_BUFFER_STATE Buffer;
40 IncludeRec(const std::string &FN, FILE *F)
41 : Filename(FN), File(F), LineNo(0){
45 static std::vector<IncludeRec> IncludeStack;
49 if (IncludeStack.empty())
50 return std::cerr << "At end of input: ";
52 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
53 std::cerr << "IncFrom " << IncludeStack[i].Filename << ":"
54 << IncludeStack[i].LineNo << ":\n";
55 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
56 << Filelineno << ": ";
62 void ParseFile(const std::string &Filename) {
64 if (Filename != "-") {
65 F = fopen(Filename.c_str(), "r");
68 std::cerr << "Could not open input file '" + Filename + "'!\n";
71 IncludeStack.push_back(IncludeRec(Filename, F));
73 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
82 // HandleInclude - This function is called when an include directive is
83 // encountered in the input stream...
84 static void HandleInclude(const char *Buffer) {
85 unsigned Length = yyleng;
86 assert(Buffer[Length-1] == '"');
87 Buffer += strlen("include ");
88 Length -= strlen("include ");
89 while (*Buffer != '"') {
93 assert(Length >= 2 && "Double quotes not found?");
94 std::string Filename(Buffer+1, Buffer+Length-1);
95 //std::cerr << "Filename = '" << Filename << "'\n";
97 // Save the line number and lex buffer of the includer...
98 IncludeStack.back().LineNo = Filelineno;
99 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
101 // Open the new input file...
102 yyin = fopen(Filename.c_str(), "r");
104 err() << "Could not find include file '" << Filename << "'!\n";
108 // Add the file to our include stack...
109 IncludeStack.push_back(IncludeRec(Filename, yyin));
110 Filelineno = 1; // Reset line numbering...
111 //yyrestart(yyin); // Start lexing the new file...
113 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
117 // yywrap - This is called when the lexer runs out of input in one of the files.
118 // Switch back to an includer if an includee has run out of input.
122 if (IncludeStack.back().File != stdin)
123 fclose(IncludeStack.back().File);
124 IncludeStack.pop_back();
125 if (IncludeStack.empty()) return 1; // Top-level file is done.
127 // Otherwise, we need to switch back to a file which included the current one.
128 Filelineno = IncludeStack.back().LineNo; // Restore current line number
129 yy_switch_to_buffer(IncludeStack.back().Buffer);
137 Identifier [a-zA-Z_][0-9a-zA-Z_]*
138 Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
140 IncludeStr include[ \t\n]+\"[^"]*\"
144 {Comment} { /* Ignore comments */ }
146 {IncludeStr} { HandleInclude(yytext); }
151 bits { return BITS; }
152 string { return STRING; }
153 list { return LIST; }
155 class { return CLASS; }
157 field { return FIELD; }
161 {Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
164 {StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
167 {Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
169 [ \t\n]+ { /* Ignore whitespace */ }
172 "/*" { BEGIN(comment); CommentDepth++; }
173 <comment>[^*/]* /* eat anything that's not a '*' or '/' */
174 <comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
175 <comment>"/*" { ++CommentDepth; }
176 <comment>"/"+[^*]* /* eat up /'s not followed by *'s */
177 <comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
178 <comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
180 . { return Filetext[0]; }