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 | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 17 | #include "thrift.tab.hh" |
| 18 | |
| 19 | %} |
| 20 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Provides the yylineno global, useful for debugging output |
| 23 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | %option lex-compat |
| 25 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Helper definitions, comments, constants, and whatnot |
| 28 | */ |
| 29 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 30 | intconstant ([+-]?[0-9]+) |
| 31 | dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?) |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 32 | identifier ([a-zA-Z_][\.a-zA-Z_0-9]*) |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 33 | whitespace ([ \t\r\n]*) |
| 34 | multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/") |
| 35 | comment ("//"[^\n]*) |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 36 | unixcomment ("#"[^\n]*) |
Mark Slee | ae2bc3c | 2006-11-08 23:44:59 +0000 | [diff] [blame] | 37 | symbol ([:;\,\{\}\(\)\=<>\[\]]) |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 38 | dliteral ("\""[^"]*"\"") |
| 39 | sliteral ("'"[^']*"'") |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 40 | |
| 41 | %% |
| 42 | |
| 43 | {whitespace} { /* do nothing */ } |
| 44 | {multicomm} { /* do nothing */ } |
| 45 | {comment} { /* do nothing */ } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 46 | {unixcomment} { /* do nothing */ } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 48 | {symbol} { return yytext[0]; } |
| 49 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 50 | "namespace" { return tok_namespace; } |
| 51 | "cpp_namespace" { return tok_cpp_namespace; } |
| 52 | "cpp_include" { return tok_cpp_include; } |
| 53 | "cpp_type" { return tok_cpp_type; } |
| 54 | "java_package" { return tok_java_package; } |
| 55 | "include" { return tok_include; } |
| 56 | |
| 57 | "void" { return tok_void; } |
| 58 | "bool" { return tok_bool; } |
| 59 | "byte" { return tok_byte; } |
| 60 | "i16" { return tok_i16; } |
| 61 | "i32" { return tok_i32; } |
| 62 | "i64" { return tok_i64; } |
| 63 | "double" { return tok_double; } |
| 64 | "string" { return tok_string; } |
| 65 | "map" { return tok_map; } |
| 66 | "list" { return tok_list; } |
| 67 | "set" { return tok_set; } |
| 68 | "async" { return tok_async; } |
| 69 | "typedef" { return tok_typedef; } |
| 70 | "struct" { return tok_struct; } |
| 71 | "exception" { return tok_xception; } |
| 72 | "extends" { return tok_extends; } |
| 73 | "throws" { return tok_throws; } |
| 74 | "service" { return tok_service; } |
| 75 | "enum" { return tok_enum; } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 76 | "const" { return tok_const; } |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 77 | |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 78 | {intconstant} { |
| 79 | yylval.iconst = atoi(yytext); |
| 80 | return tok_int_constant; |
| 81 | } |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 82 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 83 | {dubconstant} { |
| 84 | yylval.dconst = atof(yytext); |
| 85 | return tok_dub_constant; |
| 86 | } |
| 87 | |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 88 | {identifier} { |
| 89 | yylval.id = strdup(yytext); |
| 90 | return tok_identifier; |
| 91 | } |
| 92 | |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 93 | {dliteral} { |
| 94 | yylval.id = strdup(yytext+1); |
| 95 | yylval.id[strlen(yylval.id)-1] = '\0'; |
| 96 | return tok_literal; |
| 97 | } |
| 98 | |
| 99 | {sliteral} { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 100 | yylval.id = strdup(yytext+1); |
| 101 | yylval.id[strlen(yylval.id)-1] = '\0'; |
| 102 | return tok_literal; |
| 103 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 104 | |
| 105 | %% |