blob: 0858a8c5e44b0d869eefeeb94b1a036801155c60 [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +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//
Chris Lattner13854f22003-08-04 20:08:28 +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"
31typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
32#include "FileParser.h"
33
John Criswell96b4bed2003-08-27 13:41:57 +000034// Global variable recording the location of the include directory
35std::string IncludeDirectory;
36
Chris Lattnere62c1182002-12-02 01:23:04 +000037// ParseInt - This has to handle the special case of binary numbers 0b0101
38static int ParseInt(const char *Str) {
39 if (Str[0] == '0' && Str[1] == 'b')
40 return strtol(Str+2, 0, 2);
41 return strtol(Str, 0, 0);
42}
43
Chris Lattnerd33b8db2003-07-30 19:39:36 +000044static int CommentDepth = 0;
45
Chris Lattner7dff0532003-07-30 20:56:47 +000046struct IncludeRec {
47 std::string Filename;
48 FILE *File;
49 unsigned LineNo;
50 YY_BUFFER_STATE Buffer;
51
52 IncludeRec(const std::string &FN, FILE *F)
53 : Filename(FN), File(F), LineNo(0){
54 }
55};
56
57static std::vector<IncludeRec> IncludeStack;
58
59
60std::ostream &err() {
61 if (IncludeStack.empty())
62 return std::cerr << "At end of input: ";
63
64 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
Chris Lattnerbc1f0dc2003-08-03 18:12:59 +000065 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
Chris Lattner7dff0532003-07-30 20:56:47 +000066 << IncludeStack[i].LineNo << ":\n";
67 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
68 << Filelineno << ": ";
69}
70
71
Chris Lattnere623fe32003-07-30 19:55:10 +000072int Fileparse();
73
John Criswell96b4bed2003-08-27 13:41:57 +000074//
75// Function: ParseFile()
76//
77// Description:
78// This function begins the parsing of the specified tablegen file.
79//
80// Inputs:
81// Filename - A string containing the name of the file to parse.
82// IncludeDir - A string containing the directory from which include
83// files can be found.
84//
85void ParseFile(const std::string &Filename, const std::string & IncludeDir) {
Chris Lattnere623fe32003-07-30 19:55:10 +000086 FILE *F = stdin;
87 if (Filename != "-") {
88 F = fopen(Filename.c_str(), "r");
89
90 if (F == 0) {
91 std::cerr << "Could not open input file '" + Filename + "'!\n";
John Criswell2d930032003-09-09 14:37:48 +000092 exit (1);
Chris Lattnere623fe32003-07-30 19:55:10 +000093 }
Chris Lattner7dff0532003-07-30 20:56:47 +000094 IncludeStack.push_back(IncludeRec(Filename, F));
95 } else {
96 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000097 }
98
John Criswell96b4bed2003-08-27 13:41:57 +000099 //
100 // Record the location of the include directory so that the lexer can find
101 // it later.
102 //
103 IncludeDirectory = IncludeDir;
104
Chris Lattnere623fe32003-07-30 19:55:10 +0000105 Filein = F;
106 Filelineno = 1;
107 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +0000108 Filein = stdin;
109}
110
Chris Lattner7dff0532003-07-30 20:56:47 +0000111// HandleInclude - This function is called when an include directive is
112// encountered in the input stream...
113static void HandleInclude(const char *Buffer) {
114 unsigned Length = yyleng;
115 assert(Buffer[Length-1] == '"');
116 Buffer += strlen("include ");
117 Length -= strlen("include ");
118 while (*Buffer != '"') {
119 ++Buffer;
120 --Length;
121 }
122 assert(Length >= 2 && "Double quotes not found?");
123 std::string Filename(Buffer+1, Buffer+Length-1);
124 //std::cerr << "Filename = '" << Filename << "'\n";
125
126 // Save the line number and lex buffer of the includer...
127 IncludeStack.back().LineNo = Filelineno;
128 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
129
130 // Open the new input file...
131 yyin = fopen(Filename.c_str(), "r");
132 if (yyin == 0) {
John Criswell96b4bed2003-08-27 13:41:57 +0000133 //
134 // If we couldn't find the file in the current directory, look for it in
135 // the include directories.
136 //
137 // NOTE:
138 // Right now, there is only one directory. We need to eventually add
139 // support for more.
140 //
141 Filename = IncludeDirectory + "/" + Filename;
142 yyin = fopen(Filename.c_str(), "r");
143 if (yyin == 0) {
144 err() << "Could not find include file '" << Filename << "'!\n";
145 abort();
146 }
Chris Lattner7dff0532003-07-30 20:56:47 +0000147 }
148
149 // Add the file to our include stack...
150 IncludeStack.push_back(IncludeRec(Filename, yyin));
151 Filelineno = 1; // Reset line numbering...
152 //yyrestart(yyin); // Start lexing the new file...
153
154 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
155}
156
157
158// yywrap - This is called when the lexer runs out of input in one of the files.
159// Switch back to an includer if an includee has run out of input.
160//
161extern "C"
162int yywrap() {
163 if (IncludeStack.back().File != stdin)
164 fclose(IncludeStack.back().File);
165 IncludeStack.pop_back();
166 if (IncludeStack.empty()) return 1; // Top-level file is done.
167
168 // Otherwise, we need to switch back to a file which included the current one.
169 Filelineno = IncludeStack.back().LineNo; // Restore current line number
170 yy_switch_to_buffer(IncludeStack.back().Buffer);
171 return 0;
172}
173
Chris Lattnere62c1182002-12-02 01:23:04 +0000174%}
175
Chris Lattnere3a1d052003-07-30 22:15:58 +0000176Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000177
Chris Lattnere3a1d052003-07-30 22:15:58 +0000178Identifier [a-zA-Z_][0-9a-zA-Z_]*
179Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
180CodeFragment \[\{([^}]+|\}[^\]])*\}\]
181StringVal \"[^"]*\"
182IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000183
184%%
185
186{Comment} { /* Ignore comments */ }
187
Chris Lattner7dff0532003-07-30 20:56:47 +0000188{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000189{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
190 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000191
Chris Lattnere62c1182002-12-02 01:23:04 +0000192int { return INT; }
193bit { return BIT; }
194bits { return BITS; }
195string { return STRING; }
196list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000197code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000198dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000199
200class { return CLASS; }
201def { return DEF; }
202field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000203let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000204in { return IN; }
205
206{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
207 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000208${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
209 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000210
211{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
212 return STRVAL; }
213
214{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
215
216[ \t\n]+ { /* Ignore whitespace */ }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000217
218
219"/*" { BEGIN(comment); CommentDepth++; }
220<comment>[^*/]* /* eat anything that's not a '*' or '/' */
221<comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
222<comment>"/*" { ++CommentDepth; }
223<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
224<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
Chris Lattner7dff0532003-07-30 20:56:47 +0000225<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
226
227. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000228
Chris Lattnere62c1182002-12-02 01:23:04 +0000229%%