Directly support C style comments in tblgen, but allow them to actually nest
[oota-llvm.git] / utils / TableGen / FileLexer.l
1 /*===-- 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
19 %x comment
20
21 %{
22 #include "Record.h"
23 typedef 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
27 static 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
33 static int CommentDepth = 0;
34
35 %}
36
37 Comment     \/\/.*
38
39 Identifier  [a-zA-Z_][0-9a-zA-Z_]*
40 Integer     [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
41 StringVal   \"[^"]*\"
42
43 %%
44
45 {Comment}      { /* Ignore comments */ }
46
47 int            { return INT; }
48 bit            { return BIT; }
49 bits           { return BITS; }
50 string         { return STRING; }
51 list           { return LIST; }
52
53 class          { return CLASS; }
54 def            { return DEF; }
55 field          { return FIELD; }
56 set            { return SET; }
57 in             { return IN; }
58
59 {Identifier}   { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
60                  return ID; }
61
62 {StringVal}    { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
63                  return STRVAL; }
64
65 {Integer}      { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
66
67 [ \t\n]+       { /* Ignore whitespace */ }
68 .              { return Filetext[0]; }
69
70
71 "/*"                    { BEGIN(comment); CommentDepth++; }
72 <comment>[^*/]*         /* eat anything that's not a '*' or '/' */
73 <comment>"*"+[^*/]*     /* eat up '*'s not followed by '/'s */
74 <comment>"/*"           { ++CommentDepth; }
75 <comment>"/"+[^*]*      /* eat up /'s not followed by *'s */
76 <comment>"*"+"/"        { if (!--CommentDepth) { BEGIN(INITIAL); } }
77 <comment><<EOF>>        { fprintf(stderr, "Unterminated comment!\n"); abort(); }
78
79 %%