blob: 98370a7fef5d55b860954f027e2902b38ac75e77 [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"
24typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
25#include "FileParser.h"
26
27// ParseInt - This has to handle the special case of binary numbers 0b0101
28static int ParseInt(const char *Str) {
29 if (Str[0] == '0' && Str[1] == 'b')
30 return strtol(Str+2, 0, 2);
31 return strtol(Str, 0, 0);
32}
33
Chris Lattnerd33b8db2003-07-30 19:39:36 +000034static int CommentDepth = 0;
35
Chris Lattner7dff0532003-07-30 20:56:47 +000036struct IncludeRec {
37 std::string Filename;
38 FILE *File;
39 unsigned LineNo;
40 YY_BUFFER_STATE Buffer;
41
42 IncludeRec(const std::string &FN, FILE *F)
43 : Filename(FN), File(F), LineNo(0){
44 }
45};
46
47static std::vector<IncludeRec> IncludeStack;
48
49
50std::ostream &err() {
51 if (IncludeStack.empty())
52 return std::cerr << "At end of input: ";
53
54 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
Chris Lattnerbc1f0dc2003-08-03 18:12:59 +000055 std::cerr << "Included from " << IncludeStack[i].Filename << ":"
Chris Lattner7dff0532003-07-30 20:56:47 +000056 << IncludeStack[i].LineNo << ":\n";
57 return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
58 << Filelineno << ": ";
59}
60
61
Chris Lattnere623fe32003-07-30 19:55:10 +000062int Fileparse();
63
64void ParseFile(const std::string &Filename) {
65 FILE *F = stdin;
66 if (Filename != "-") {
67 F = fopen(Filename.c_str(), "r");
68
69 if (F == 0) {
70 std::cerr << "Could not open input file '" + Filename + "'!\n";
71 abort();
72 }
Chris Lattner7dff0532003-07-30 20:56:47 +000073 IncludeStack.push_back(IncludeRec(Filename, F));
74 } else {
75 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000076 }
77
78 Filein = F;
79 Filelineno = 1;
80 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +000081 Filein = stdin;
82}
83
Chris Lattner7dff0532003-07-30 20:56:47 +000084// HandleInclude - This function is called when an include directive is
85// encountered in the input stream...
86static void HandleInclude(const char *Buffer) {
87 unsigned Length = yyleng;
88 assert(Buffer[Length-1] == '"');
89 Buffer += strlen("include ");
90 Length -= strlen("include ");
91 while (*Buffer != '"') {
92 ++Buffer;
93 --Length;
94 }
95 assert(Length >= 2 && "Double quotes not found?");
96 std::string Filename(Buffer+1, Buffer+Length-1);
97 //std::cerr << "Filename = '" << Filename << "'\n";
98
99 // Save the line number and lex buffer of the includer...
100 IncludeStack.back().LineNo = Filelineno;
101 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
102
103 // Open the new input file...
104 yyin = fopen(Filename.c_str(), "r");
105 if (yyin == 0) {
106 err() << "Could not find include file '" << Filename << "'!\n";
107 abort();
108 }
109
110 // Add the file to our include stack...
111 IncludeStack.push_back(IncludeRec(Filename, yyin));
112 Filelineno = 1; // Reset line numbering...
113 //yyrestart(yyin); // Start lexing the new file...
114
115 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
116}
117
118
119// yywrap - This is called when the lexer runs out of input in one of the files.
120// Switch back to an includer if an includee has run out of input.
121//
122extern "C"
123int yywrap() {
124 if (IncludeStack.back().File != stdin)
125 fclose(IncludeStack.back().File);
126 IncludeStack.pop_back();
127 if (IncludeStack.empty()) return 1; // Top-level file is done.
128
129 // Otherwise, we need to switch back to a file which included the current one.
130 Filelineno = IncludeStack.back().LineNo; // Restore current line number
131 yy_switch_to_buffer(IncludeStack.back().Buffer);
132 return 0;
133}
134
Chris Lattnere62c1182002-12-02 01:23:04 +0000135%}
136
Chris Lattnere3a1d052003-07-30 22:15:58 +0000137Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000138
Chris Lattnere3a1d052003-07-30 22:15:58 +0000139Identifier [a-zA-Z_][0-9a-zA-Z_]*
140Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
141CodeFragment \[\{([^}]+|\}[^\]])*\}\]
142StringVal \"[^"]*\"
143IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000144
145%%
146
147{Comment} { /* Ignore comments */ }
148
Chris Lattner7dff0532003-07-30 20:56:47 +0000149{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000150{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
151 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000152
Chris Lattnere62c1182002-12-02 01:23:04 +0000153int { return INT; }
154bit { return BIT; }
155bits { return BITS; }
156string { return STRING; }
157list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000158code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000159dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000160
161class { return CLASS; }
162def { return DEF; }
163field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000164let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000165in { return IN; }
166
167{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
168 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000169${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
170 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000171
172{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
173 return STRVAL; }
174
175{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
176
177[ \t\n]+ { /* Ignore whitespace */ }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000178
179
180"/*" { BEGIN(comment); CommentDepth++; }
181<comment>[^*/]* /* eat anything that's not a '*' or '/' */
182<comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
183<comment>"/*" { ++CommentDepth; }
184<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
185<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
Chris Lattner7dff0532003-07-30 20:56:47 +0000186<comment><<EOF>> { err() << "Unterminated comment!\n"; abort(); }
187
188. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000189
Chris Lattnere62c1182002-12-02 01:23:04 +0000190%%