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