blob: 14a9e4b339d15d26619e5a3d28e3b0a5c7f42520 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
2 * Thrift scanner.
3 *
4 * Tokenizes a thrift definition file.
5 * @author Mark Slee <mcslee@facebook.com>
6 */
Mark Sleef5377b32006-10-10 01:42:59 +00007
Mark Slee31985722006-05-24 21:45:31 +00008%{
9
10#include "main.h"
11#include "parse/t_program.h"
12
Mark Sleef5377b32006-10-10 01:42:59 +000013/**
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 Slee31985722006-05-24 21:45:31 +000017#include "thrift.tab.hh"
18
19%}
20
Mark Sleef5377b32006-10-10 01:42:59 +000021/**
22 * Provides the yylineno global, useful for debugging output
23 */
Mark Slee31985722006-05-24 21:45:31 +000024%option lex-compat
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * Helper definitions, comments, constants, and whatnot
28 */
29
Mark Slee30152872006-11-28 01:24:07 +000030intconstant ([+-]?[0-9]+)
Mark Slee600cdb32006-11-29 22:06:42 +000031hexconstant ("0x"[0-9A-Fa-f]+)
Mark Slee30152872006-11-28 01:24:07 +000032dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Mark Slee9cb7c612006-09-01 22:17:45 +000033identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
Mark Slee31985722006-05-24 21:45:31 +000034whitespace ([ \t\r\n]*)
35multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
36comment ("//"[^\n]*)
Mark Sleec98d0502006-09-06 02:42:25 +000037unixcomment ("#"[^\n]*)
Mark Sleeae2bc3c2006-11-08 23:44:59 +000038symbol ([:;\,\{\}\(\)\=<>\[\]])
Mark Slee30152872006-11-28 01:24:07 +000039dliteral ("\""[^"]*"\"")
40sliteral ("'"[^']*"'")
Mark Slee31985722006-05-24 21:45:31 +000041
42%%
43
44{whitespace} { /* do nothing */ }
45{multicomm} { /* do nothing */ }
46{comment} { /* do nothing */ }
Mark Sleec98d0502006-09-06 02:42:25 +000047{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000048
Mark Slee9cb7c612006-09-01 22:17:45 +000049{symbol} { return yytext[0]; }
50
Mark Sleef0712dc2006-10-25 19:03:57 +000051"namespace" { return tok_namespace; }
52"cpp_namespace" { return tok_cpp_namespace; }
53"cpp_include" { return tok_cpp_include; }
54"cpp_type" { return tok_cpp_type; }
55"java_package" { return tok_java_package; }
56"include" { return tok_include; }
57
58"void" { return tok_void; }
59"bool" { return tok_bool; }
60"byte" { return tok_byte; }
61"i16" { return tok_i16; }
62"i32" { return tok_i32; }
63"i64" { return tok_i64; }
64"double" { return tok_double; }
65"string" { return tok_string; }
66"map" { return tok_map; }
67"list" { return tok_list; }
68"set" { return tok_set; }
69"async" { return tok_async; }
70"typedef" { return tok_typedef; }
71"struct" { return tok_struct; }
72"exception" { return tok_xception; }
73"extends" { return tok_extends; }
74"throws" { return tok_throws; }
75"service" { return tok_service; }
76"enum" { return tok_enum; }
Mark Slee30152872006-11-28 01:24:07 +000077"const" { return tok_const; }
Mark Slee52f643d2006-08-09 00:03:43 +000078
Mark Slee4f8da1d2006-10-12 02:47:27 +000079{intconstant} {
80 yylval.iconst = atoi(yytext);
81 return tok_int_constant;
82}
Mark Sleef5377b32006-10-10 01:42:59 +000083
Mark Slee600cdb32006-11-29 22:06:42 +000084{hexconstant} {
85 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +000086 return tok_int_constant;
87}
88
Mark Slee30152872006-11-28 01:24:07 +000089{dubconstant} {
90 yylval.dconst = atof(yytext);
91 return tok_dub_constant;
92}
93
Mark Slee4f8da1d2006-10-12 02:47:27 +000094{identifier} {
95 yylval.id = strdup(yytext);
96 return tok_identifier;
97}
98
Mark Slee30152872006-11-28 01:24:07 +000099{dliteral} {
100 yylval.id = strdup(yytext+1);
101 yylval.id[strlen(yylval.id)-1] = '\0';
102 return tok_literal;
103}
104
105{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000106 yylval.id = strdup(yytext+1);
107 yylval.id[strlen(yylval.id)-1] = '\0';
108 return tok_literal;
109}
Mark Slee31985722006-05-24 21:45:31 +0000110
111%%