Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 1 | /*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===*/ |
| 14 | |
| 15 | %option prefix="File" |
| 16 | %option yylineno |
| 17 | %option nostdinit |
| 18 | %option never-interactive |
| 19 | %option batch |
| 20 | %option nodefault |
| 21 | %option 8bit |
| 22 | %option outfile="Lexer.cpp" |
| 23 | %option ecs |
| 24 | %option noreject |
| 25 | %option noyymore |
| 26 | |
| 27 | %x comment |
| 28 | |
| 29 | %{ |
| 30 | #include "Record.h" |
| 31 | typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy; |
| 32 | #include "FileParser.h" |
| 33 | |
| 34 | int Fileparse(); |
| 35 | |
| 36 | namespace llvm { |
| 37 | |
| 38 | // Global variable recording the location of the include directory |
Chris Lattner | c1d6e4e | 2006-03-03 01:47:37 +0000 | [diff] [blame] | 39 | std::vector<std::string> IncludeDirectories; |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 40 | |
| 41 | /// ParseInt - This has to handle the special case of binary numbers 0b0101 |
| 42 | /// |
| 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); |
| 47 | } |
| 48 | |
| 49 | static int CommentDepth = 0; |
| 50 | |
| 51 | struct IncludeRec { |
| 52 | std::string Filename; |
| 53 | FILE *File; |
| 54 | unsigned LineNo; |
| 55 | YY_BUFFER_STATE Buffer; |
| 56 | |
| 57 | IncludeRec(const std::string &FN, FILE *F) |
| 58 | : Filename(FN), File(F), LineNo(0){ |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | static std::vector<IncludeRec> IncludeStack; |
| 63 | |
| 64 | std::ostream &err() { |
| 65 | if (IncludeStack.empty()) |
| 66 | return std::cerr << "At end of input: "; |
| 67 | |
| 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 << ": "; |
| 73 | } |
| 74 | |
| 75 | /// ParseFile - this function begins the parsing of the specified tablegen file. |
| 76 | /// |
Chris Lattner | c1d6e4e | 2006-03-03 01:47:37 +0000 | [diff] [blame] | 77 | void ParseFile(const std::string &Filename, |
| 78 | const std::vector<std::string> &IncludeDirs) { |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +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"; |
| 85 | exit (1); |
| 86 | } |
| 87 | IncludeStack.push_back(IncludeRec(Filename, F)); |
| 88 | } else { |
| 89 | IncludeStack.push_back(IncludeRec("<stdin>", stdin)); |
| 90 | } |
| 91 | |
| 92 | // Record the location of the include directory so that the lexer can find |
| 93 | // it later. |
Chris Lattner | c1d6e4e | 2006-03-03 01:47:37 +0000 | [diff] [blame] | 94 | IncludeDirectories = IncludeDirs; |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 95 | |
| 96 | Filein = F; |
| 97 | Filelineno = 1; |
| 98 | Fileparse(); |
| 99 | Filein = stdin; |
| 100 | } |
| 101 | |
| 102 | /// HandleInclude - This function is called when an include directive is |
| 103 | /// encountered in the input stream... |
| 104 | /// |
| 105 | static void HandleInclude(const char *Buffer) { |
| 106 | unsigned Length = yyleng; |
| 107 | assert(Buffer[Length-1] == '"'); |
| 108 | Buffer += strlen("include "); |
| 109 | Length -= strlen("include "); |
| 110 | while (*Buffer != '"') { |
| 111 | ++Buffer; |
| 112 | --Length; |
| 113 | } |
| 114 | assert(Length >= 2 && "Double quotes not found?"); |
| 115 | std::string Filename(Buffer+1, Buffer+Length-1); |
| 116 | //std::cerr << "Filename = '" << Filename << "'\n"; |
| 117 | |
| 118 | // Save the line number and lex buffer of the includer... |
| 119 | IncludeStack.back().LineNo = Filelineno; |
| 120 | IncludeStack.back().Buffer = YY_CURRENT_BUFFER; |
| 121 | |
| 122 | // Open the new input file... |
| 123 | yyin = fopen(Filename.c_str(), "r"); |
| 124 | if (yyin == 0) { |
| 125 | // If we couldn't find the file in the current directory, look for it in |
| 126 | // the include directories. |
| 127 | // |
Chris Lattner | c1d6e4e | 2006-03-03 01:47:37 +0000 | [diff] [blame] | 128 | std::string NextFilename; |
| 129 | for (unsigned i = 0, e = IncludeDirectories.size(); i != e; ++i) { |
| 130 | NextFilename = IncludeDirectories[i] + "/" + Filename; |
Chris Lattner | 56879b2 | 2006-03-03 19:34:28 +0000 | [diff] [blame] | 131 | if ((yyin = fopen(NextFilename.c_str(), "r"))) |
Chris Lattner | c1d6e4e | 2006-03-03 01:47:37 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 135 | if (yyin == 0) { |
| 136 | err() << "Could not find include file '" << Filename << "'!\n"; |
| 137 | exit(1); |
| 138 | } |
| 139 | Filename = NextFilename; |
| 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 | /// yywrap - This is called when the lexer runs out of input in one of the |
| 151 | /// files. Switch back to an includer if an includee has run out of input. |
| 152 | /// |
| 153 | extern "C" |
| 154 | int yywrap(void) { |
| 155 | if (IncludeStack.back().File != stdin) |
| 156 | fclose(IncludeStack.back().File); |
| 157 | IncludeStack.pop_back(); |
| 158 | if (IncludeStack.empty()) return 1; // Top-level file is done. |
| 159 | |
| 160 | // Otherwise, we need to switch back to a file which included the current one. |
| 161 | Filelineno = IncludeStack.back().LineNo; // Restore current line number |
| 162 | yy_switch_to_buffer(IncludeStack.back().Buffer); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | } // End llvm namespace |
| 167 | |
| 168 | using namespace llvm; |
| 169 | |
| 170 | %} |
| 171 | |
| 172 | Comment \/\/.* |
| 173 | |
| 174 | Identifier [a-zA-Z_][0-9a-zA-Z_]* |
| 175 | Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+ |
| 176 | CodeFragment \[\{([^}]+|\}[^\]])*\}\] |
| 177 | StringVal \"[^"]*\" |
| 178 | IncludeStr include[ \t\n]+\"[^"]*\" |
| 179 | |
| 180 | %% |
| 181 | |
| 182 | {Comment} { /* Ignore comments */ } |
| 183 | |
| 184 | {IncludeStr} { HandleInclude(yytext); } |
| 185 | {CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2); |
| 186 | return CODEFRAGMENT; } |
| 187 | |
| 188 | int { return INT; } |
| 189 | bit { return BIT; } |
| 190 | bits { return BITS; } |
| 191 | string { return STRING; } |
| 192 | list { return LIST; } |
| 193 | code { return CODE; } |
| 194 | dag { return DAG; } |
| 195 | |
| 196 | class { return CLASS; } |
| 197 | def { return DEF; } |
Chris Lattner | 2762738 | 2006-09-01 21:14:42 +0000 | [diff] [blame] | 198 | defm { return DEFM; } |
| 199 | multiclass { return MULTICLASS; } |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 200 | field { return FIELD; } |
| 201 | let { return LET; } |
| 202 | in { return IN; } |
| 203 | |
| 204 | !sra { return SRATOK; } |
| 205 | !srl { return SRLTOK; } |
| 206 | !shl { return SHLTOK; } |
Chris Lattner | b831691 | 2006-03-31 21:54:11 +0000 | [diff] [blame] | 207 | !strconcat { return STRCONCATTOK; } |
Chris Lattner | e8242b1 | 2006-02-14 05:13:13 +0000 | [diff] [blame] | 208 | |
| 209 | |
| 210 | {Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng); |
| 211 | return ID; } |
| 212 | ${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng); |
| 213 | return VARNAME; } |
| 214 | |
| 215 | {StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1); |
| 216 | return STRVAL; } |
| 217 | |
| 218 | {Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; } |
| 219 | |
| 220 | [ \t\n\r]+ { /* Ignore whitespace */ } |
| 221 | |
| 222 | |
| 223 | "/*" { BEGIN(comment); CommentDepth++; } |
| 224 | <comment>[^*/]* {} /* eat anything that's not a '*' or '/' */ |
| 225 | <comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */ |
| 226 | <comment>"/*" { ++CommentDepth; } |
| 227 | <comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */ |
| 228 | <comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } } |
| 229 | <comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); } |
| 230 | |
| 231 | . { return Filetext[0]; } |
| 232 | |
| 233 | %% |
| 234 | |