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