blob: 653e9d10fbf0dc5163bb4d5a1bf789dd875cbf9a [file] [log] [blame]
Chris Lattnere8242b12006-02-14 05:13:13 +00001/*===-- 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%{
Chris Lattner5fa39df2006-11-05 23:28:58 +000030#include "llvm/Config/config.h"
Chris Lattnere8242b12006-02-14 05:13:13 +000031#include "Record.h"
32typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
33#include "FileParser.h"
34
35int Fileparse();
36
37namespace llvm {
38
39// Global variable recording the location of the include directory
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000040std::vector<std::string> IncludeDirectories;
Chris Lattnere8242b12006-02-14 05:13:13 +000041
42/// ParseInt - This has to handle the special case of binary numbers 0b0101
43///
44static int ParseInt(const char *Str) {
45 if (Str[0] == '0' && Str[1] == 'b')
Chris Lattnera4503de2006-09-18 22:28:27 +000046 return strtoll(Str+2, 0, 2);
47 return strtoll(Str, 0, 0);
Chris Lattnere8242b12006-02-14 05:13:13 +000048}
49
50static int CommentDepth = 0;
51
52struct IncludeRec {
53 std::string Filename;
54 FILE *File;
55 unsigned LineNo;
56 YY_BUFFER_STATE Buffer;
57
58 IncludeRec(const std::string &FN, FILE *F)
59 : Filename(FN), File(F), LineNo(0){
60 }
61};
62
63static std::vector<IncludeRec> IncludeStack;
64
65std::ostream &err() {
66 if (IncludeStack.empty())
67 return std::cerr << "At end of input: ";
68
69 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
70 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
71 << IncludeStack[i].LineNo << ":\n";
72 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
73 << Filelineno << ": ";
74}
75
76/// ParseFile - this function begins the parsing of the specified tablegen file.
77///
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000078void ParseFile(const std::string &Filename,
79 const std::vector<std::string> &IncludeDirs) {
Chris Lattnere8242b12006-02-14 05:13:13 +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 exit (1);
87 }
88 IncludeStack.push_back(IncludeRec(Filename, F));
89 } else {
90 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
91 }
92
93 // Record the location of the include directory so that the lexer can find
94 // it later.
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000095 IncludeDirectories = IncludeDirs;
Chris Lattnere8242b12006-02-14 05:13:13 +000096
97 Filein = F;
98 Filelineno = 1;
99 Fileparse();
100 Filein = stdin;
101}
102
103/// HandleInclude - This function is called when an include directive is
104/// encountered in the input stream...
105///
106static 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) {
126 // If we couldn't find the file in the current directory, look for it in
127 // the include directories.
128 //
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +0000129 std::string NextFilename;
130 for (unsigned i = 0, e = IncludeDirectories.size(); i != e; ++i) {
131 NextFilename = IncludeDirectories[i] + "/" + Filename;
Chris Lattner56879b22006-03-03 19:34:28 +0000132 if ((yyin = fopen(NextFilename.c_str(), "r")))
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +0000133 break;
134 }
135
Chris Lattnere8242b12006-02-14 05:13:13 +0000136 if (yyin == 0) {
137 err() << "Could not find include file '" << Filename << "'!\n";
138 exit(1);
139 }
140 Filename = NextFilename;
141 }
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/// yywrap - This is called when the lexer runs out of input in one of the
152/// files. Switch back to an includer if an includee has run out of input.
153///
154extern "C"
155int yywrap(void) {
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
167} // End llvm namespace
168
169using namespace llvm;
170
171%}
172
173Comment \/\/.*
174
175Identifier [a-zA-Z_][0-9a-zA-Z_]*
176Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
177CodeFragment \[\{([^}]+|\}[^\]])*\}\]
178StringVal \"[^"]*\"
179IncludeStr include[ \t\n]+\"[^"]*\"
180
181%%
182
183{Comment} { /* Ignore comments */ }
184
185{IncludeStr} { HandleInclude(yytext); }
186{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
187 return CODEFRAGMENT; }
188
189int { return INT; }
190bit { return BIT; }
191bits { return BITS; }
192string { return STRING; }
193list { return LIST; }
194code { return CODE; }
195dag { return DAG; }
196
197class { return CLASS; }
198def { return DEF; }
Chris Lattner27627382006-09-01 21:14:42 +0000199defm { return DEFM; }
200multiclass { return MULTICLASS; }
Chris Lattnere8242b12006-02-14 05:13:13 +0000201field { return FIELD; }
202let { return LET; }
203in { return IN; }
204
205!sra { return SRATOK; }
206!srl { return SRLTOK; }
207!shl { return SHLTOK; }
Chris Lattnerb8316912006-03-31 21:54:11 +0000208!strconcat { return STRCONCATTOK; }
Chris Lattnere8242b12006-02-14 05:13:13 +0000209
210
211{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
212 return ID; }
213${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
214 return VARNAME; }
215
216{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
217 return STRVAL; }
218
219{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
220
221[ \t\n\r]+ { /* Ignore whitespace */ }
222
223
224"/*" { BEGIN(comment); CommentDepth++; }
225<comment>[^*/]* {} /* eat anything that's not a '*' or '/' */
226<comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */
227<comment>"/*" { ++CommentDepth; }
228<comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */
229<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
230<comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
231
232. { return Filetext[0]; }
233
234%%
235