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