blob: c61e9ded3f75628a21198c5d2ae620eb6c511493 [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001/*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
2//
3//
4//===------------------------------------------------------------------------=*/
5
6%option prefix="File"
7%option yylineno
8%option nostdinit
9%option never-interactive
10%option batch
11%option noyywrap
12%option nodefault
13%option 8bit
14%option outfile="Lexer.cpp"
15%option ecs
16%option noreject
17%option noyymore
18
Chris Lattnerd33b8db2003-07-30 19:39:36 +000019%x comment
Chris Lattnere62c1182002-12-02 01:23:04 +000020
21%{
22#include "Record.h"
23typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
24#include "FileParser.h"
25
26// ParseInt - This has to handle the special case of binary numbers 0b0101
27static int ParseInt(const char *Str) {
28 if (Str[0] == '0' && Str[1] == 'b')
29 return strtol(Str+2, 0, 2);
30 return strtol(Str, 0, 0);
31}
32
Chris Lattnerd33b8db2003-07-30 19:39:36 +000033static int CommentDepth = 0;
34
Chris Lattnere623fe32003-07-30 19:55:10 +000035int Fileparse();
36
37void ParseFile(const std::string &Filename) {
38 FILE *F = stdin;
39 if (Filename != "-") {
40 F = fopen(Filename.c_str(), "r");
41
42 if (F == 0) {
43 std::cerr << "Could not open input file '" + Filename + "'!\n";
44 abort();
45 }
46 }
47
48 Filein = F;
49 Filelineno = 1;
50 Fileparse();
51
52 if (F != stdin)
53 fclose(F);
54 Filein = stdin;
55}
56
Chris Lattnere62c1182002-12-02 01:23:04 +000057%}
58
59Comment \/\/.*
60
61Identifier [a-zA-Z_][0-9a-zA-Z_]*
62Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
63StringVal \"[^"]*\"
64
65%%
66
67{Comment} { /* Ignore comments */ }
68
69int { return INT; }
70bit { return BIT; }
71bits { return BITS; }
72string { return STRING; }
73list { return LIST; }
74
75class { return CLASS; }
76def { return DEF; }
77field { return FIELD; }
78set { return SET; }
79in { return IN; }
80
81{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
82 return ID; }
83
84{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
85 return STRVAL; }
86
87{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
88
89[ \t\n]+ { /* Ignore whitespace */ }
90. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +000091
92
93"/*" { BEGIN(comment); CommentDepth++; }
94<comment>[^*/]* /* eat anything that's not a '*' or '/' */
95<comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
96<comment>"/*" { ++CommentDepth; }
97<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
98<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
99<comment><<EOF>> { fprintf(stderr, "Unterminated comment!\n"); abort(); }
100
Chris Lattnere62c1182002-12-02 01:23:04 +0000101%%