Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 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 | |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 19 | %x comment |
Chris Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 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 | |
Chris Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 33 | static int CommentDepth = 0; |
| 34 | |
Chris Lattner | e623fe3 | 2003-07-30 19:55:10 +0000 | [diff] [blame^] | 35 | int Fileparse(); |
| 36 | |
| 37 | void 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 Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 57 | %} |
| 58 | |
| 59 | Comment \/\/.* |
| 60 | |
| 61 | Identifier [a-zA-Z_][0-9a-zA-Z_]* |
| 62 | Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+ |
| 63 | StringVal \"[^"]*\" |
| 64 | |
| 65 | %% |
| 66 | |
| 67 | {Comment} { /* Ignore comments */ } |
| 68 | |
| 69 | int { return INT; } |
| 70 | bit { return BIT; } |
| 71 | bits { return BITS; } |
| 72 | string { return STRING; } |
| 73 | list { return LIST; } |
| 74 | |
| 75 | class { return CLASS; } |
| 76 | def { return DEF; } |
| 77 | field { return FIELD; } |
| 78 | set { return SET; } |
| 79 | in { 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 Lattner | d33b8db | 2003-07-30 19:39:36 +0000 | [diff] [blame] | 91 | |
| 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 Lattner | e62c118 | 2002-12-02 01:23:04 +0000 | [diff] [blame] | 101 | %% |