Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 1 | /*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===// |
| 2 | // |
Chris Lattner | 13854f2 | 2003-08-04 20:08:28 +0000 | [diff] [blame] | 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. |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 5 | // |
Chris Lattner | 13854f2 | 2003-08-04 20:08:28 +0000 | [diff] [blame] | 6 | //===----------------------------------------------------------------------===*/ |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 7 | |
| 8 | %option prefix="File" |
| 9 | %option yylineno |
| 10 | %option nostdinit |
| 11 | %option never-interactive |
| 12 | %option batch |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 13 | %option nodefault |
| 14 | %option 8bit |
| 15 | %option outfile="Lexer.cpp" |
| 16 | %option ecs |
| 17 | %option noreject |
| 18 | %option noyymore |
| 19 | |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 20 | %x comment |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 21 | |
| 22 | %{ |
| 23 | #include "Record.h" |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame^] | 24 | #include "Support/CommandLine.h" |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 25 | typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy; |
| 26 | #include "FileParser.h" |
| 27 | |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame^] | 28 | // Global variable recording the location of the include directory |
| 29 | std::string IncludeDirectory; |
| 30 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 31 | // ParseInt - This has to handle the special case of binary numbers 0b0101 |
| 32 | static int ParseInt(const char *Str) { |
| 33 | if (Str[0] == '0' && Str[1] == 'b') |
| 34 | return strtol(Str+2, 0, 2); |
| 35 | return strtol(Str, 0, 0); |
| 36 | } |
| 37 | |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 38 | static int CommentDepth = 0; |
| 39 | |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 40 | struct IncludeRec { |
| 41 | std::string Filename; |
| 42 | FILE *File; |
| 43 | unsigned LineNo; |
| 44 | YY_BUFFER_STATE Buffer; |
| 45 | |
| 46 | IncludeRec(const std::string &FN, FILE *F) |
| 47 | : Filename(FN), File(F), LineNo(0){ |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | static std::vector<IncludeRec> IncludeStack; |
| 52 | |
| 53 | |
| 54 | std::ostream &err() { |
| 55 | if (IncludeStack.empty()) |
| 56 | return std::cerr << "At end of input: "; |
| 57 | |
| 58 | for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i) |
Chris Lattner | bc1f0dc | 2003-08-03 18:12:59 +0000 | [diff] [blame] | 59 | std::cerr << "Included from " << IncludeStack[i].Filename << ":" |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 60 | << IncludeStack[i].LineNo << ":\n"; |
| 61 | return std::cerr << "Parsing " << IncludeStack.back().Filename << ":" |
| 62 | << Filelineno << ": "; |
| 63 | } |
| 64 | |
| 65 | |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame] | 66 | int Fileparse(); |
| 67 | |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame^] | 68 | // |
| 69 | // Function: ParseFile() |
| 70 | // |
| 71 | // Description: |
| 72 | // This function begins the parsing of the specified tablegen file. |
| 73 | // |
| 74 | // Inputs: |
| 75 | // Filename - A string containing the name of the file to parse. |
| 76 | // IncludeDir - A string containing the directory from which include |
| 77 | // files can be found. |
| 78 | // |
| 79 | void ParseFile(const std::string &Filename, const std::string & IncludeDir) { |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame] | 80 | FILE *F = stdin; |
| 81 | if (Filename != "-") { |
| 82 | F = fopen(Filename.c_str(), "r"); |
| 83 | |
| 84 | if (F == 0) { |
| 85 | std::cerr << "Could not open input file '" + Filename + "'!\n"; |
| 86 | abort(); |
| 87 | } |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 88 | IncludeStack.push_back(IncludeRec(Filename, F)); |
| 89 | } else { |
| 90 | IncludeStack.push_back(IncludeRec("<stdin>", stdin)); |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame] | 91 | } |
| 92 | |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame^] | 93 | // |
| 94 | // Record the location of the include directory so that the lexer can find |
| 95 | // it later. |
| 96 | // |
| 97 | IncludeDirectory = IncludeDir; |
| 98 | |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame] | 99 | Filein = F; |
| 100 | Filelineno = 1; |
| 101 | Fileparse(); |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame] | 102 | Filein = stdin; |
| 103 | } |
| 104 | |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 105 | // HandleInclude - This function is called when an include directive is |
| 106 | // encountered in the input stream... |
| 107 | static void HandleInclude(const char *Buffer) { |
| 108 | unsigned Length = yyleng; |
| 109 | assert(Buffer[Length-1] == '"'); |
| 110 | Buffer += strlen("include "); |
| 111 | Length -= strlen("include "); |
| 112 | while (*Buffer != '"') { |
| 113 | ++Buffer; |
| 114 | --Length; |
| 115 | } |
| 116 | assert(Length >= 2 && "Double quotes not found?"); |
| 117 | std::string Filename(Buffer+1, Buffer+Length-1); |
| 118 | //std::cerr << "Filename = '" << Filename << "'\n"; |
| 119 | |
| 120 | // Save the line number and lex buffer of the includer... |
| 121 | IncludeStack.back().LineNo = Filelineno; |
| 122 | IncludeStack.back().Buffer = YY_CURRENT_BUFFER; |
| 123 | |
| 124 | // Open the new input file... |
| 125 | yyin = fopen(Filename.c_str(), "r"); |
| 126 | if (yyin == 0) { |
John Criswell | 96b4bed | 2003-08-27 13:41:57 +0000 | [diff] [blame^] | 127 | // |
| 128 | // If we couldn't find the file in the current directory, look for it in |
| 129 | // the include directories. |
| 130 | // |
| 131 | // NOTE: |
| 132 | // Right now, there is only one directory. We need to eventually add |
| 133 | // support for more. |
| 134 | // |
| 135 | Filename = IncludeDirectory + "/" + Filename; |
| 136 | yyin = fopen(Filename.c_str(), "r"); |
| 137 | if (yyin == 0) { |
| 138 | err() << "Could not find include file '" << Filename << "'!\n"; |
| 139 | abort(); |
| 140 | } |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Add the file to our include stack... |
| 144 | IncludeStack.push_back(IncludeRec(Filename, yyin)); |
| 145 | Filelineno = 1; // Reset line numbering... |
| 146 | //yyrestart(yyin); // Start lexing the new file... |
| 147 | |
| 148 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | // yywrap - This is called when the lexer runs out of input in one of the files. |
| 153 | // Switch back to an includer if an includee has run out of input. |
| 154 | // |
| 155 | extern "C" |
| 156 | int yywrap() { |
| 157 | if (IncludeStack.back().File != stdin) |
| 158 | fclose(IncludeStack.back().File); |
| 159 | IncludeStack.pop_back(); |
| 160 | if (IncludeStack.empty()) return 1; // Top-level file is done. |
| 161 | |
| 162 | // Otherwise, we need to switch back to a file which included the current one. |
| 163 | Filelineno = IncludeStack.back().LineNo; // Restore current line number |
| 164 | yy_switch_to_buffer(IncludeStack.back().Buffer); |
| 165 | return 0; |
| 166 | } |
| 167 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 168 | %} |
| 169 | |
Chris Lattner | e3a1d05 | 2003-07-30 22:15:58 +0000 | [diff] [blame] | 170 | Comment \/\/.* |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 171 | |
Chris Lattner | e3a1d05 | 2003-07-30 22:15:58 +0000 | [diff] [blame] | 172 | Identifier [a-zA-Z_][0-9a-zA-Z_]* |
| 173 | Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+ |
| 174 | CodeFragment \[\{([^}]+|\}[^\]])*\}\] |
| 175 | StringVal \"[^"]*\" |
| 176 | IncludeStr include[ \t\n]+\"[^"]*\" |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 177 | |
| 178 | %% |
| 179 | |
| 180 | {Comment} { /* Ignore comments */ } |
| 181 | |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 182 | {IncludeStr} { HandleInclude(yytext); } |
Chris Lattner | e3a1d05 | 2003-07-30 22:15:58 +0000 | [diff] [blame] | 183 | {CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2); |
| 184 | return CODEFRAGMENT; } |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 185 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 186 | int { return INT; } |
| 187 | bit { return BIT; } |
| 188 | bits { return BITS; } |
| 189 | string { return STRING; } |
| 190 | list { return LIST; } |
Chris Lattner | f05760d | 2003-07-30 21:47:42 +0000 | [diff] [blame] | 191 | code { return CODE; } |
Chris Lattner | 40f7113 | 2003-08-04 04:50:57 +0000 | [diff] [blame] | 192 | dag { return DAG; } |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 193 | |
| 194 | class { return CLASS; } |
| 195 | def { return DEF; } |
| 196 | field { return FIELD; } |
Chris Lattner | 42aa89e | 2003-08-04 04:56:53 +0000 | [diff] [blame] | 197 | let { return LET; } |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 198 | in { return IN; } |
| 199 | |
| 200 | {Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng); |
| 201 | return ID; } |
Chris Lattner | fb9ea58 | 2003-08-10 22:04:25 +0000 | [diff] [blame] | 202 | ${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng); |
| 203 | return VARNAME; } |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 204 | |
| 205 | {StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1); |
| 206 | return STRVAL; } |
| 207 | |
| 208 | {Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; } |
| 209 | |
| 210 | [ \t\n]+ { /* Ignore whitespace */ } |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | "/*" { BEGIN(comment); CommentDepth++; } |
| 214 | <comment>[^*/]* /* eat anything that's not a '*' or '/' */ |
| 215 | <comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */ |
| 216 | <comment>"/*" { ++CommentDepth; } |
| 217 | <comment>"/"+[^*]* /* eat up /'s not followed by *'s */ |
| 218 | <comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } } |
Chris Lattner | 7dff053 | 2003-07-30 20:56:47 +0000 | [diff] [blame] | 219 | <comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); } |
| 220 | |
| 221 | . { return Filetext[0]; } |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 222 | |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 223 | %% |