blob: 3b60099bc103233fb16d77c66a652138a31c9f1d [file] [log] [blame]
Misha Brukmanf2f5b392004-02-12 00:03:08 +00001/*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
John Criswellaefb6662003-10-21 15:29:18 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +00009//
Chris Lattner13854f22003-08-04 20:08:28 +000010// This file defines a simple flex scanner for TableGen files. This is pretty
11// straight-forward, except for the magic to handle file inclusion.
Chris Lattnere62c1182002-12-02 01:23:04 +000012//
Misha Brukmanf2f5b392004-02-12 00:03:08 +000013//===----------------------------------------------------------------------===*/
Chris Lattnere62c1182002-12-02 01:23:04 +000014
15%option prefix="File"
16%option yylineno
17%option nostdinit
18%option never-interactive
19%option batch
Chris Lattnere62c1182002-12-02 01:23:04 +000020%option nodefault
21%option 8bit
22%option outfile="Lexer.cpp"
23%option ecs
24%option noreject
25%option noyymore
26
Chris Lattnerd33b8db2003-07-30 19:39:36 +000027%x comment
Chris Lattnere62c1182002-12-02 01:23:04 +000028
29%{
30#include "Record.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000031typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
Chris Lattnere62c1182002-12-02 01:23:04 +000032#include "FileParser.h"
33
Brian Gaeked0fde302003-11-11 22:41:34 +000034int Fileparse();
35
36namespace llvm {
37
John Criswell96b4bed2003-08-27 13:41:57 +000038// Global variable recording the location of the include directory
Chris Lattnerd9f5d902006-03-03 01:47:14 +000039std::vector<std::string> IncludeDirectories;
John Criswell96b4bed2003-08-27 13:41:57 +000040
Misha Brukman1700f772004-02-12 00:00:46 +000041/// ParseInt - This has to handle the special case of binary numbers 0b0101
42///
Chris Lattnere62c1182002-12-02 01:23:04 +000043static 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
Chris Lattnerd33b8db2003-07-30 19:39:36 +000049static int CommentDepth = 0;
50
Chris Lattner7dff0532003-07-30 20:56:47 +000051struct 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
62static std::vector<IncludeRec> IncludeStack;
63
Chris Lattner7dff0532003-07-30 20:56:47 +000064std::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)
Chris Lattnerbc1f0dc2003-08-03 18:12:59 +000069 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
Chris Lattner7dff0532003-07-30 20:56:47 +000070 << IncludeStack[i].LineNo << ":\n";
71 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
72 << Filelineno << ": ";
73}
74
Misha Brukman1700f772004-02-12 00:00:46 +000075/// ParseFile - this function begins the parsing of the specified tablegen file.
76///
Chris Lattnerd9f5d902006-03-03 01:47:14 +000077void ParseFile(const std::string &Filename,
78 const std::vector<std::string> &IncludeDirs) {
Chris Lattnere623fe32003-07-30 19:55:10 +000079 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 Criswell2d930032003-09-09 14:37:48 +000085 exit (1);
Chris Lattnere623fe32003-07-30 19:55:10 +000086 }
Chris Lattner7dff0532003-07-30 20:56:47 +000087 IncludeStack.push_back(IncludeRec(Filename, F));
88 } else {
89 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000090 }
91
John Criswell96b4bed2003-08-27 13:41:57 +000092 // Record the location of the include directory so that the lexer can find
93 // it later.
Chris Lattnerd9f5d902006-03-03 01:47:14 +000094 IncludeDirectories = IncludeDirs;
John Criswell96b4bed2003-08-27 13:41:57 +000095
Chris Lattnere623fe32003-07-30 19:55:10 +000096 Filein = F;
97 Filelineno = 1;
98 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +000099 Filein = stdin;
100}
101
Misha Brukman1700f772004-02-12 00:00:46 +0000102/// HandleInclude - This function is called when an include directive is
103/// encountered in the input stream...
104///
Chris Lattner7dff0532003-07-30 20:56:47 +0000105static 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) {
John Criswell96b4bed2003-08-27 13:41:57 +0000125 // If we couldn't find the file in the current directory, look for it in
126 // the include directories.
127 //
Chris Lattnerd9f5d902006-03-03 01:47:14 +0000128 std::string NextFilename;
129 for (unsigned i = 0, e = IncludeDirectories.size(); i != e; ++i) {
130 NextFilename = IncludeDirectories[i] + "/" + Filename;
Chris Lattner56879b22006-03-03 19:34:28 +0000131 if ((yyin = fopen(NextFilename.c_str(), "r")))
Chris Lattnerd9f5d902006-03-03 01:47:14 +0000132 break;
133 }
134
John Criswell96b4bed2003-08-27 13:41:57 +0000135 if (yyin == 0) {
136 err() << "Could not find include file '" << Filename << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000137 exit(1);
John Criswell96b4bed2003-08-27 13:41:57 +0000138 }
Chris Lattner83fe9172004-02-13 16:33:56 +0000139 Filename = NextFilename;
Chris Lattner7dff0532003-07-30 20:56:47 +0000140 }
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
Misha Brukman1700f772004-02-12 00:00:46 +0000150/// 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///
Chris Lattner7dff0532003-07-30 20:56:47 +0000153extern "C"
Chris Lattner45237092004-10-13 15:25:46 +0000154int yywrap(void) {
Chris Lattner7dff0532003-07-30 20:56:47 +0000155 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
Brian Gaeked0fde302003-11-11 22:41:34 +0000166} // End llvm namespace
167
168using namespace llvm;
169
Chris Lattnere62c1182002-12-02 01:23:04 +0000170%}
171
Chris Lattnere3a1d052003-07-30 22:15:58 +0000172Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000173
Chris Lattnere3a1d052003-07-30 22:15:58 +0000174Identifier [a-zA-Z_][0-9a-zA-Z_]*
175Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
176CodeFragment \[\{([^}]+|\}[^\]])*\}\]
177StringVal \"[^"]*\"
178IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000179
180%%
181
182{Comment} { /* Ignore comments */ }
183
Chris Lattner7dff0532003-07-30 20:56:47 +0000184{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000185{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
186 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000187
Chris Lattnere62c1182002-12-02 01:23:04 +0000188int { return INT; }
189bit { return BIT; }
190bits { return BITS; }
191string { return STRING; }
192list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000193code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000194dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000195
196class { return CLASS; }
197def { return DEF; }
198field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000199let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000200in { return IN; }
201
Chris Lattnerb9266f82005-04-19 01:11:03 +0000202!sra { return SRATOK; }
203!srl { return SRLTOK; }
204!shl { return SHLTOK; }
Chris Lattner711e5d92006-03-31 21:53:49 +0000205!strconcat { return STRCONCATTOK; }
Chris Lattnerb9266f82005-04-19 01:11:03 +0000206
207
Chris Lattnere62c1182002-12-02 01:23:04 +0000208{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
209 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000210${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
211 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000212
213{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
214 return STRVAL; }
215
216{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
217
Chris Lattner36c57572004-05-27 17:44:18 +0000218[ \t\n\r]+ { /* Ignore whitespace */ }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000219
220
221"/*" { BEGIN(comment); CommentDepth++; }
Chris Lattner99783322005-09-06 21:23:09 +0000222<comment>[^*/]* {} /* eat anything that's not a '*' or '/' */
223<comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000224<comment>"/*" { ++CommentDepth; }
Chris Lattner99783322005-09-06 21:23:09 +0000225<comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000226<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
Chris Lattnerf5761a52004-02-13 16:37:43 +0000227<comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
Chris Lattner7dff0532003-07-30 20:56:47 +0000228
229. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000230
Chris Lattnere62c1182002-12-02 01:23:04 +0000231%%
Chris Lattnere8242b12006-02-14 05:13:13 +0000232