blob: a941c9fb069192d705260f6a14da9e0c16c1be08 [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
39std::string IncludeDirectory;
40
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///
John Criswell96b4bed2003-08-27 13:41:57 +000077void ParseFile(const std::string &Filename, const std::string & IncludeDir) {
Chris Lattnere623fe32003-07-30 19:55:10 +000078 FILE *F = stdin;
79 if (Filename != "-") {
80 F = fopen(Filename.c_str(), "r");
81
82 if (F == 0) {
83 std::cerr << "Could not open input file '" + Filename + "'!\n";
John Criswell2d930032003-09-09 14:37:48 +000084 exit (1);
Chris Lattnere623fe32003-07-30 19:55:10 +000085 }
Chris Lattner7dff0532003-07-30 20:56:47 +000086 IncludeStack.push_back(IncludeRec(Filename, F));
87 } else {
88 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000089 }
90
John Criswell96b4bed2003-08-27 13:41:57 +000091 // Record the location of the include directory so that the lexer can find
92 // it later.
John Criswell96b4bed2003-08-27 13:41:57 +000093 IncludeDirectory = IncludeDir;
94
Chris Lattnere623fe32003-07-30 19:55:10 +000095 Filein = F;
96 Filelineno = 1;
97 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +000098 Filein = stdin;
99}
100
Misha Brukman1700f772004-02-12 00:00:46 +0000101/// HandleInclude - This function is called when an include directive is
102/// encountered in the input stream...
103///
Chris Lattner7dff0532003-07-30 20:56:47 +0000104static void HandleInclude(const char *Buffer) {
105 unsigned Length = yyleng;
106 assert(Buffer[Length-1] == '"');
107 Buffer += strlen("include ");
108 Length -= strlen("include ");
109 while (*Buffer != '"') {
110 ++Buffer;
111 --Length;
112 }
113 assert(Length >= 2 && "Double quotes not found?");
114 std::string Filename(Buffer+1, Buffer+Length-1);
115 //std::cerr << "Filename = '" << Filename << "'\n";
116
117 // Save the line number and lex buffer of the includer...
118 IncludeStack.back().LineNo = Filelineno;
119 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
120
121 // Open the new input file...
122 yyin = fopen(Filename.c_str(), "r");
123 if (yyin == 0) {
John Criswell96b4bed2003-08-27 13:41:57 +0000124 // If we couldn't find the file in the current directory, look for it in
125 // the include directories.
126 //
Misha Brukman1700f772004-02-12 00:00:46 +0000127 // NOTE: Right now, there is only one directory. We need to eventually add
128 // support for more.
John Criswell96b4bed2003-08-27 13:41:57 +0000129 Filename = IncludeDirectory + "/" + Filename;
130 yyin = fopen(Filename.c_str(), "r");
131 if (yyin == 0) {
132 err() << "Could not find include file '" << Filename << "'!\n";
133 abort();
134 }
Chris Lattner7dff0532003-07-30 20:56:47 +0000135 }
136
137 // Add the file to our include stack...
138 IncludeStack.push_back(IncludeRec(Filename, yyin));
139 Filelineno = 1; // Reset line numbering...
140 //yyrestart(yyin); // Start lexing the new file...
141
142 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
143}
144
Misha Brukman1700f772004-02-12 00:00:46 +0000145/// yywrap - This is called when the lexer runs out of input in one of the
146/// files. Switch back to an includer if an includee has run out of input.
147///
Chris Lattner7dff0532003-07-30 20:56:47 +0000148extern "C"
149int yywrap() {
150 if (IncludeStack.back().File != stdin)
151 fclose(IncludeStack.back().File);
152 IncludeStack.pop_back();
153 if (IncludeStack.empty()) return 1; // Top-level file is done.
154
155 // Otherwise, we need to switch back to a file which included the current one.
156 Filelineno = IncludeStack.back().LineNo; // Restore current line number
157 yy_switch_to_buffer(IncludeStack.back().Buffer);
158 return 0;
159}
160
Brian Gaeked0fde302003-11-11 22:41:34 +0000161} // End llvm namespace
162
163using namespace llvm;
164
Chris Lattnere62c1182002-12-02 01:23:04 +0000165%}
166
Chris Lattnere3a1d052003-07-30 22:15:58 +0000167Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000168
Chris Lattnere3a1d052003-07-30 22:15:58 +0000169Identifier [a-zA-Z_][0-9a-zA-Z_]*
170Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
171CodeFragment \[\{([^}]+|\}[^\]])*\}\]
172StringVal \"[^"]*\"
173IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000174
175%%
176
177{Comment} { /* Ignore comments */ }
178
Chris Lattner7dff0532003-07-30 20:56:47 +0000179{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000180{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
181 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000182
Chris Lattnere62c1182002-12-02 01:23:04 +0000183int { return INT; }
184bit { return BIT; }
185bits { return BITS; }
186string { return STRING; }
187list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000188code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000189dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000190
191class { return CLASS; }
192def { return DEF; }
193field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000194let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000195in { return IN; }
196
197{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
198 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000199${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
200 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000201
202{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
203 return STRVAL; }
204
205{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
206
207[ \t\n]+ { /* Ignore whitespace */ }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000208
209
210"/*" { BEGIN(comment); CommentDepth++; }
211<comment>[^*/]* /* eat anything that's not a '*' or '/' */
212<comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
213<comment>"/*" { ++CommentDepth; }
214<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
215<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
Chris Lattner7dff0532003-07-30 20:56:47 +0000216<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
217
218. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000219
Chris Lattnere62c1182002-12-02 01:23:04 +0000220%%