blob: f3cd32522aa751fc9e1c6fa733f2bd1b5c0e68fa [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001/*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
2//
Chris Lattner13854f22003-08-04 20:08:28 +00003// 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 Lattnere62c1182002-12-02 01:23:04 +00005//
Chris Lattner13854f22003-08-04 20:08:28 +00006//===----------------------------------------------------------------------===*/
Chris Lattnere62c1182002-12-02 01:23:04 +00007
8%option prefix="File"
9%option yylineno
10%option nostdinit
11%option never-interactive
12%option batch
Chris Lattnere62c1182002-12-02 01:23:04 +000013%option nodefault
14%option 8bit
15%option outfile="Lexer.cpp"
16%option ecs
17%option noreject
18%option noyymore
19
Chris Lattnerd33b8db2003-07-30 19:39:36 +000020%x comment
Chris Lattnere62c1182002-12-02 01:23:04 +000021
22%{
23#include "Record.h"
John Criswell96b4bed2003-08-27 13:41:57 +000024#include "Support/CommandLine.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000025typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
26#include "FileParser.h"
27
John Criswell96b4bed2003-08-27 13:41:57 +000028// Global variable recording the location of the include directory
29std::string IncludeDirectory;
30
Chris Lattnere62c1182002-12-02 01:23:04 +000031// ParseInt - This has to handle the special case of binary numbers 0b0101
32static 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 Lattnerd33b8db2003-07-30 19:39:36 +000038static int CommentDepth = 0;
39
Chris Lattner7dff0532003-07-30 20:56:47 +000040struct 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
51static std::vector<IncludeRec> IncludeStack;
52
53
54std::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 Lattnerbc1f0dc2003-08-03 18:12:59 +000059 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
Chris Lattner7dff0532003-07-30 20:56:47 +000060 << IncludeStack[i].LineNo << ":\n";
61 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
62 << Filelineno << ": ";
63}
64
65
Chris Lattnere623fe32003-07-30 19:55:10 +000066int Fileparse();
67
John Criswell96b4bed2003-08-27 13:41:57 +000068//
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//
79void ParseFile(const std::string &Filename, const std::string & IncludeDir) {
Chris Lattnere623fe32003-07-30 19:55:10 +000080 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 Lattner7dff0532003-07-30 20:56:47 +000088 IncludeStack.push_back(IncludeRec(Filename, F));
89 } else {
90 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000091 }
92
John Criswell96b4bed2003-08-27 13:41:57 +000093 //
94 // Record the location of the include directory so that the lexer can find
95 // it later.
96 //
97 IncludeDirectory = IncludeDir;
98
Chris Lattnere623fe32003-07-30 19:55:10 +000099 Filein = F;
100 Filelineno = 1;
101 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +0000102 Filein = stdin;
103}
104
Chris Lattner7dff0532003-07-30 20:56:47 +0000105// HandleInclude - This function is called when an include directive is
106// encountered in the input stream...
107static 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 Criswell96b4bed2003-08-27 13:41:57 +0000127 //
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 Lattner7dff0532003-07-30 20:56:47 +0000141 }
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//
155extern "C"
156int 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 Lattnere62c1182002-12-02 01:23:04 +0000168%}
169
Chris Lattnere3a1d052003-07-30 22:15:58 +0000170Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000171
Chris Lattnere3a1d052003-07-30 22:15:58 +0000172Identifier [a-zA-Z_][0-9a-zA-Z_]*
173Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
174CodeFragment \[\{([^}]+|\}[^\]])*\}\]
175StringVal \"[^"]*\"
176IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000177
178%%
179
180{Comment} { /* Ignore comments */ }
181
Chris Lattner7dff0532003-07-30 20:56:47 +0000182{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000183{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
184 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000185
Chris Lattnere62c1182002-12-02 01:23:04 +0000186int { return INT; }
187bit { return BIT; }
188bits { return BITS; }
189string { return STRING; }
190list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000191code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000192dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000193
194class { return CLASS; }
195def { return DEF; }
196field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000197let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000198in { return IN; }
199
200{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
201 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000202${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
203 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000204
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 Lattnerd33b8db2003-07-30 19:39:36 +0000211
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 Lattner7dff0532003-07-30 20:56:47 +0000219<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
220
221. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000222
Chris Lattnere62c1182002-12-02 01:23:04 +0000223%%