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