Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Thrift scanner. |
| 3 | * |
| 4 | * Tokenizes a thrift definition file. |
| 5 | * @author Mark Slee <mcslee@facebook.com> |
| 6 | */ |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 7 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 8 | %{ |
| 9 | |
| 10 | #include "main.h" |
| 11 | #include "parse/t_program.h" |
| 12 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 13 | /** |
| 14 | * Must be included AFTER parse/t_program.h, but I can't remember why anymore |
| 15 | * because I wrote this a while ago. |
| 16 | */ |
Mark Slee | eb0d024 | 2007-01-25 07:58:55 +0000 | [diff] [blame] | 17 | #include "thrifty.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 18 | |
Mark Slee | f12865a | 2007-01-12 00:23:26 +0000 | [diff] [blame] | 19 | void thrift_reserved_keyword(char* keyword) { |
| 20 | yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword); |
| 21 | exit(1); |
| 22 | } |
| 23 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | %} |
| 25 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Provides the yylineno global, useful for debugging output |
| 28 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 29 | %option lex-compat |
| 30 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Helper definitions, comments, constants, and whatnot |
| 33 | */ |
| 34 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 35 | intconstant ([+-]?[0-9]+) |
Mark Slee | 600cdb3 | 2006-11-29 22:06:42 +0000 | [diff] [blame] | 36 | hexconstant ("0x"[0-9A-Fa-f]+) |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 37 | dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?) |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 38 | identifier ([a-zA-Z_][\.a-zA-Z_0-9]*) |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 39 | whitespace ([ \t\r\n]*) |
| 40 | multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/") |
| 41 | comment ("//"[^\n]*) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 42 | unixcomment ("#"[^\n]*) |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 43 | doctext ("["(("["[^\]\[]*"]")|[^\]\[])*"]") /* allows one level of nesting */ |
Mark Slee | ae2bc3c | 2006-11-08 23:44:59 +0000 | [diff] [blame] | 44 | symbol ([:;\,\{\}\(\)\=<>\[\]]) |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 45 | dliteral ("\""[^"]*"\"") |
| 46 | sliteral ("'"[^']*"'") |
| 47 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 48 | |
| 49 | %% |
| 50 | |
| 51 | {whitespace} { /* do nothing */ } |
| 52 | {multicomm} { /* do nothing */ } |
| 53 | {comment} { /* do nothing */ } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 54 | {unixcomment} { /* do nothing */ } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 55 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 56 | {symbol} { return yytext[0]; } |
| 57 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 58 | "namespace" { return tok_namespace; } |
| 59 | "cpp_namespace" { return tok_cpp_namespace; } |
| 60 | "cpp_include" { return tok_cpp_include; } |
| 61 | "cpp_type" { return tok_cpp_type; } |
| 62 | "java_package" { return tok_java_package; } |
Mark Slee | e888b37 | 2007-01-12 01:06:24 +0000 | [diff] [blame] | 63 | "php_namespace" { return tok_php_namespace; } |
Mark Slee | 782abbb | 2007-01-19 00:17:02 +0000 | [diff] [blame] | 64 | "xsd_all" { return tok_xsd_all; } |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 65 | "xsd_optional" { return tok_xsd_optional; } |
Mark Slee | 7df0e2a | 2007-02-06 21:03:18 +0000 | [diff] [blame] | 66 | "xsd_nillable" { return tok_xsd_nillable; } |
Mark Slee | 0d9199e | 2007-01-31 02:08:30 +0000 | [diff] [blame] | 67 | "xsd_namespace" { return tok_xsd_namespace; } |
Mark Slee | 21135c3 | 2007-02-05 21:52:08 +0000 | [diff] [blame] | 68 | "xsd_attrs" { return tok_xsd_attrs; } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 69 | "include" { return tok_include; } |
| 70 | |
| 71 | "void" { return tok_void; } |
| 72 | "bool" { return tok_bool; } |
| 73 | "byte" { return tok_byte; } |
| 74 | "i16" { return tok_i16; } |
| 75 | "i32" { return tok_i32; } |
| 76 | "i64" { return tok_i64; } |
| 77 | "double" { return tok_double; } |
| 78 | "string" { return tok_string; } |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 79 | "binary" { return tok_binary; } |
Mark Slee | b6200d8 | 2007-01-19 19:14:36 +0000 | [diff] [blame] | 80 | "slist" { return tok_slist; } |
Mark Slee | 6a47fed | 2007-02-07 02:40:59 +0000 | [diff] [blame] | 81 | "senum" { return tok_senum; } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 82 | "map" { return tok_map; } |
| 83 | "list" { return tok_list; } |
| 84 | "set" { return tok_set; } |
| 85 | "async" { return tok_async; } |
| 86 | "typedef" { return tok_typedef; } |
| 87 | "struct" { return tok_struct; } |
| 88 | "exception" { return tok_xception; } |
| 89 | "extends" { return tok_extends; } |
| 90 | "throws" { return tok_throws; } |
| 91 | "service" { return tok_service; } |
| 92 | "enum" { return tok_enum; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 93 | "const" { return tok_const; } |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 94 | |
Mark Slee | f12865a | 2007-01-12 00:23:26 +0000 | [diff] [blame] | 95 | "abstract" { thrift_reserved_keyword(yytext); } |
| 96 | "and" { thrift_reserved_keyword(yytext); } |
| 97 | "as" { thrift_reserved_keyword(yytext); } |
| 98 | "assert" { thrift_reserved_keyword(yytext); } |
| 99 | "break" { thrift_reserved_keyword(yytext); } |
| 100 | "case" { thrift_reserved_keyword(yytext); } |
| 101 | "class" { thrift_reserved_keyword(yytext); } |
| 102 | "continue" { thrift_reserved_keyword(yytext); } |
| 103 | "declare" { thrift_reserved_keyword(yytext); } |
| 104 | "def" { thrift_reserved_keyword(yytext); } |
| 105 | "default" { thrift_reserved_keyword(yytext); } |
| 106 | "del" { thrift_reserved_keyword(yytext); } |
| 107 | "delete" { thrift_reserved_keyword(yytext); } |
| 108 | "do" { thrift_reserved_keyword(yytext); } |
| 109 | "elif" { thrift_reserved_keyword(yytext); } |
| 110 | "else" { thrift_reserved_keyword(yytext); } |
| 111 | "elseif" { thrift_reserved_keyword(yytext); } |
| 112 | "except" { thrift_reserved_keyword(yytext); } |
| 113 | "exec" { thrift_reserved_keyword(yytext); } |
| 114 | "false" { thrift_reserved_keyword(yytext); } |
| 115 | "final" { thrift_reserved_keyword(yytext); } |
| 116 | "finally" { thrift_reserved_keyword(yytext); } |
| 117 | "float" { thrift_reserved_keyword(yytext); } |
| 118 | "for" { thrift_reserved_keyword(yytext); } |
| 119 | "foreach" { thrift_reserved_keyword(yytext); } |
| 120 | "function" { thrift_reserved_keyword(yytext); } |
| 121 | "global" { thrift_reserved_keyword(yytext); } |
| 122 | "goto" { thrift_reserved_keyword(yytext); } |
| 123 | "if" { thrift_reserved_keyword(yytext); } |
| 124 | "implements" { thrift_reserved_keyword(yytext); } |
| 125 | "import" { thrift_reserved_keyword(yytext); } |
| 126 | "in" { thrift_reserved_keyword(yytext); } |
| 127 | "inline" { thrift_reserved_keyword(yytext); } |
| 128 | "instanceof" { thrift_reserved_keyword(yytext); } |
| 129 | "interface" { thrift_reserved_keyword(yytext); } |
| 130 | "is" { thrift_reserved_keyword(yytext); } |
| 131 | "lambda" { thrift_reserved_keyword(yytext); } |
| 132 | "native" { thrift_reserved_keyword(yytext); } |
| 133 | "new" { thrift_reserved_keyword(yytext); } |
| 134 | "not" { thrift_reserved_keyword(yytext); } |
| 135 | "or" { thrift_reserved_keyword(yytext); } |
| 136 | "pass" { thrift_reserved_keyword(yytext); } |
| 137 | "public" { thrift_reserved_keyword(yytext); } |
| 138 | "print" { thrift_reserved_keyword(yytext); } |
| 139 | "private" { thrift_reserved_keyword(yytext); } |
| 140 | "protected" { thrift_reserved_keyword(yytext); } |
| 141 | "raise" { thrift_reserved_keyword(yytext); } |
| 142 | "return" { thrift_reserved_keyword(yytext); } |
| 143 | "sizeof" { thrift_reserved_keyword(yytext); } |
| 144 | "static" { thrift_reserved_keyword(yytext); } |
| 145 | "switch" { thrift_reserved_keyword(yytext); } |
| 146 | "synchronized" { thrift_reserved_keyword(yytext); } |
| 147 | "this" { thrift_reserved_keyword(yytext); } |
| 148 | "throw" { thrift_reserved_keyword(yytext); } |
| 149 | "transient" { thrift_reserved_keyword(yytext); } |
| 150 | "true" { thrift_reserved_keyword(yytext); } |
| 151 | "try" { thrift_reserved_keyword(yytext); } |
| 152 | "unsigned" { thrift_reserved_keyword(yytext); } |
| 153 | "var" { thrift_reserved_keyword(yytext); } |
| 154 | "virtual" { thrift_reserved_keyword(yytext); } |
| 155 | "volatile" { thrift_reserved_keyword(yytext); } |
| 156 | "while" { thrift_reserved_keyword(yytext); } |
| 157 | "with" { thrift_reserved_keyword(yytext); } |
| 158 | "union" { thrift_reserved_keyword(yytext); } |
| 159 | "yield" { thrift_reserved_keyword(yytext); } |
| 160 | |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 161 | {intconstant} { |
| 162 | yylval.iconst = atoi(yytext); |
| 163 | return tok_int_constant; |
| 164 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 165 | |
Mark Slee | 600cdb3 | 2006-11-29 22:06:42 +0000 | [diff] [blame] | 166 | {hexconstant} { |
| 167 | sscanf(yytext+2, "%x", &yylval.iconst); |
Mark Slee | 600cdb3 | 2006-11-29 22:06:42 +0000 | [diff] [blame] | 168 | return tok_int_constant; |
| 169 | } |
| 170 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 171 | {dubconstant} { |
| 172 | yylval.dconst = atof(yytext); |
| 173 | return tok_dub_constant; |
| 174 | } |
| 175 | |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 176 | {identifier} { |
| 177 | yylval.id = strdup(yytext); |
| 178 | return tok_identifier; |
| 179 | } |
| 180 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 181 | {dliteral} { |
| 182 | yylval.id = strdup(yytext+1); |
| 183 | yylval.id[strlen(yylval.id)-1] = '\0'; |
| 184 | return tok_literal; |
| 185 | } |
| 186 | |
| 187 | {sliteral} { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 188 | yylval.id = strdup(yytext+1); |
| 189 | yylval.id[strlen(yylval.id)-1] = '\0'; |
| 190 | return tok_literal; |
| 191 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 192 | |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 193 | {doctext} { |
| 194 | yylval.id = strdup(yytext + 1); |
| 195 | yylval.id[strlen(yylval.id) - 1] = '\0'; |
| 196 | return tok_doctext; |
| 197 | } |
| 198 | |
| 199 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 200 | %% |