blob: cc6825f5e07e1c29546cb2d5bc2517499656fb11 [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"
Bill Wendlingf5da1332006-12-07 22:21:48 +000031#include "llvm/Support/Streams.h"
Chris Lattnere8242b12006-02-14 05:13:13 +000032#include "Record.h"
33typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
34#include "FileParser.h"
35
36int Fileparse();
37
38namespace llvm {
39
40// Global variable recording the location of the include directory
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000041std::vector<std::string> IncludeDirectories;
Chris Lattnere8242b12006-02-14 05:13:13 +000042
43/// ParseInt - This has to handle the special case of binary numbers 0b0101
44///
45static int ParseInt(const char *Str) {
46 if (Str[0] == '0' && Str[1] == 'b')
Chris Lattnera4503de2006-09-18 22:28:27 +000047 return strtoll(Str+2, 0, 2);
48 return strtoll(Str, 0, 0);
Chris Lattnere8242b12006-02-14 05:13:13 +000049}
50
51static int CommentDepth = 0;
52
53struct IncludeRec {
54 std::string Filename;
55 FILE *File;
56 unsigned LineNo;
57 YY_BUFFER_STATE Buffer;
58
59 IncludeRec(const std::string &FN, FILE *F)
60 : Filename(FN), File(F), LineNo(0){
61 }
62};
63
64static std::vector<IncludeRec> IncludeStack;
65
66std::ostream &err() {
Bill Wendlingf5da1332006-12-07 22:21:48 +000067 if (IncludeStack.empty()) {
68 cerr << "At end of input: ";
69 return *cerr.stream();
70 }
Chris Lattnere8242b12006-02-14 05:13:13 +000071
72 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
Bill Wendlingf5da1332006-12-07 22:21:48 +000073 cerr << "Included from " << IncludeStack[i].Filename << ":"
74 << IncludeStack[i].LineNo << ":\n";
75 cerr << "Parsing " << IncludeStack.back().Filename << ":"
76 << Filelineno << ": ";
77 return *cerr.stream();
Chris Lattnere8242b12006-02-14 05:13:13 +000078}
79
80/// ParseFile - this function begins the parsing of the specified tablegen file.
81///
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000082void ParseFile(const std::string &Filename,
83 const std::vector<std::string> &IncludeDirs) {
Chris Lattnere8242b12006-02-14 05:13:13 +000084 FILE *F = stdin;
85 if (Filename != "-") {
86 F = fopen(Filename.c_str(), "r");
87
88 if (F == 0) {
Bill Wendlingf5da1332006-12-07 22:21:48 +000089 cerr << "Could not open input file '" + Filename + "'!\n";
Chris Lattnere8242b12006-02-14 05:13:13 +000090 exit (1);
91 }
92 IncludeStack.push_back(IncludeRec(Filename, F));
93 } else {
94 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
95 }
96
97 // Record the location of the include directory so that the lexer can find
98 // it later.
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +000099 IncludeDirectories = IncludeDirs;
Chris Lattnere8242b12006-02-14 05:13:13 +0000100
101 Filein = F;
102 Filelineno = 1;
103 Fileparse();
104 Filein = stdin;
105}
106
107/// HandleInclude - This function is called when an include directive is
108/// encountered in the input stream...
109///
110static void HandleInclude(const char *Buffer) {
111 unsigned Length = yyleng;
112 assert(Buffer[Length-1] == '"');
113 Buffer += strlen("include ");
114 Length -= strlen("include ");
115 while (*Buffer != '"') {
116 ++Buffer;
117 --Length;
118 }
119 assert(Length >= 2 && "Double quotes not found?");
120 std::string Filename(Buffer+1, Buffer+Length-1);
Bill Wendlingf5da1332006-12-07 22:21:48 +0000121 //cerr << "Filename = '" << Filename << "'\n";
Chris Lattnere8242b12006-02-14 05:13:13 +0000122
123 // Save the line number and lex buffer of the includer...
124 IncludeStack.back().LineNo = Filelineno;
125 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
126
127 // Open the new input file...
128 yyin = fopen(Filename.c_str(), "r");
129 if (yyin == 0) {
130 // If we couldn't find the file in the current directory, look for it in
131 // the include directories.
132 //
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +0000133 std::string NextFilename;
134 for (unsigned i = 0, e = IncludeDirectories.size(); i != e; ++i) {
135 NextFilename = IncludeDirectories[i] + "/" + Filename;
Chris Lattner56879b22006-03-03 19:34:28 +0000136 if ((yyin = fopen(NextFilename.c_str(), "r")))
Chris Lattnerc1d6e4e2006-03-03 01:47:37 +0000137 break;
138 }
139
Chris Lattnere8242b12006-02-14 05:13:13 +0000140 if (yyin == 0) {
141 err() << "Could not find include file '" << Filename << "'!\n";
142 exit(1);
143 }
144 Filename = NextFilename;
145 }
146
147 // Add the file to our include stack...
148 IncludeStack.push_back(IncludeRec(Filename, yyin));
149 Filelineno = 1; // Reset line numbering...
150 //yyrestart(yyin); // Start lexing the new file...
151
152 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
153}
154
155/// yywrap - This is called when the lexer runs out of input in one of the
156/// files. Switch back to an includer if an includee has run out of input.
157///
158extern "C"
159int yywrap(void) {
160 if (IncludeStack.back().File != stdin)
161 fclose(IncludeStack.back().File);
162 IncludeStack.pop_back();
163 if (IncludeStack.empty()) return 1; // Top-level file is done.
164
165 // Otherwise, we need to switch back to a file which included the current one.
166 Filelineno = IncludeStack.back().LineNo; // Restore current line number
167 yy_switch_to_buffer(IncludeStack.back().Buffer);
168 return 0;
169}
170
171} // End llvm namespace
172
173using namespace llvm;
174
175%}
176
177Comment \/\/.*
178
179Identifier [a-zA-Z_][0-9a-zA-Z_]*
180Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
181CodeFragment \[\{([^}]+|\}[^\]])*\}\]
182StringVal \"[^"]*\"
183IncludeStr include[ \t\n]+\"[^"]*\"
184
185%%
186
187{Comment} { /* Ignore comments */ }
188
189{IncludeStr} { HandleInclude(yytext); }
190{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
191 return CODEFRAGMENT; }
192
193int { return INT; }
194bit { return BIT; }
195bits { return BITS; }
196string { return STRING; }
197list { return LIST; }
198code { return CODE; }
199dag { return DAG; }
200
201class { return CLASS; }
202def { return DEF; }
Chris Lattner27627382006-09-01 21:14:42 +0000203defm { return DEFM; }
204multiclass { return MULTICLASS; }
Chris Lattnere8242b12006-02-14 05:13:13 +0000205field { return FIELD; }
206let { return LET; }
207in { return IN; }
208
209!sra { return SRATOK; }
210!srl { return SRLTOK; }
211!shl { return SHLTOK; }
Chris Lattnerb8316912006-03-31 21:54:11 +0000212!strconcat { return STRCONCATTOK; }
Chris Lattnere8242b12006-02-14 05:13:13 +0000213
214
215{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
216 return ID; }
217${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
218 return VARNAME; }
219
220{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
221 return STRVAL; }
222
223{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
224
225[ \t\n\r]+ { /* Ignore whitespace */ }
226
227
228"/*" { BEGIN(comment); CommentDepth++; }
229<comment>[^*/]* {} /* eat anything that's not a '*' or '/' */
230<comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */
231<comment>"/*" { ++CommentDepth; }
232<comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */
233<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
234<comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
235
236. { return Filetext[0]; }
237
238%%
239