blob: abda14f2d402b6a67961c874217c6fc42a8d2bb0 [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 Sleeeb0d0242007-01-25 07:58:55 +000017#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000018
Mark Sleef12865a2007-01-12 00:23:26 +000019void thrift_reserved_keyword(char* keyword) {
20 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
21 exit(1);
22}
23
Mark Slee31985722006-05-24 21:45:31 +000024%}
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * Provides the yylineno global, useful for debugging output
28 */
Mark Slee31985722006-05-24 21:45:31 +000029%option lex-compat
30
Mark Sleef5377b32006-10-10 01:42:59 +000031/**
32 * Helper definitions, comments, constants, and whatnot
33 */
34
Mark Slee30152872006-11-28 01:24:07 +000035intconstant ([+-]?[0-9]+)
Mark Slee600cdb32006-11-29 22:06:42 +000036hexconstant ("0x"[0-9A-Fa-f]+)
Mark Slee30152872006-11-28 01:24:07 +000037dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Mark Slee9cb7c612006-09-01 22:17:45 +000038identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
Mark Slee31985722006-05-24 21:45:31 +000039whitespace ([ \t\r\n]*)
40multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
41comment ("//"[^\n]*)
Mark Sleec98d0502006-09-06 02:42:25 +000042unixcomment ("#"[^\n]*)
ccheeverf53b5cf2007-02-05 20:33:11 +000043doctext ("["(("["[^\]\[]*"]")|[^\]\[])*"]") /* allows one level of nesting */
Mark Sleeae2bc3c2006-11-08 23:44:59 +000044symbol ([:;\,\{\}\(\)\=<>\[\]])
ccheeverf53b5cf2007-02-05 20:33:11 +000045dliteral ("\""[^"]*"\"")
46sliteral ("'"[^']*"'")
47
Mark Slee31985722006-05-24 21:45:31 +000048
49%%
50
51{whitespace} { /* do nothing */ }
52{multicomm} { /* do nothing */ }
53{comment} { /* do nothing */ }
Mark Sleec98d0502006-09-06 02:42:25 +000054{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000055
Mark Slee9cb7c612006-09-01 22:17:45 +000056{symbol} { return yytext[0]; }
57
Mark Sleef0712dc2006-10-25 19:03:57 +000058"namespace" { return tok_namespace; }
59"cpp_namespace" { return tok_cpp_namespace; }
60"cpp_include" { return tok_cpp_include; }
61"cpp_type" { return tok_cpp_type; }
62"java_package" { return tok_java_package; }
Mark Sleee888b372007-01-12 01:06:24 +000063"php_namespace" { return tok_php_namespace; }
Mark Slee782abbb2007-01-19 00:17:02 +000064"xsd_all" { return tok_xsd_all; }
Mark Slee36bfa2e2007-01-19 20:09:51 +000065"xsd_optional" { return tok_xsd_optional; }
Mark Slee7df0e2a2007-02-06 21:03:18 +000066"xsd_nillable" { return tok_xsd_nillable; }
Mark Slee0d9199e2007-01-31 02:08:30 +000067"xsd_namespace" { return tok_xsd_namespace; }
Mark Slee21135c32007-02-05 21:52:08 +000068"xsd_attrs" { return tok_xsd_attrs; }
Mark Sleef0712dc2006-10-25 19:03:57 +000069"include" { return tok_include; }
70
71"void" { return tok_void; }
72"bool" { return tok_bool; }
73"byte" { return tok_byte; }
74"i16" { return tok_i16; }
75"i32" { return tok_i32; }
76"i64" { return tok_i64; }
77"double" { return tok_double; }
78"string" { return tok_string; }
Mark Sleeb6200d82007-01-19 19:14:36 +000079"slist" { return tok_slist; }
Mark Slee6a47fed2007-02-07 02:40:59 +000080"senum" { return tok_senum; }
Mark Sleef0712dc2006-10-25 19:03:57 +000081"map" { return tok_map; }
82"list" { return tok_list; }
83"set" { return tok_set; }
84"async" { return tok_async; }
85"typedef" { return tok_typedef; }
86"struct" { return tok_struct; }
87"exception" { return tok_xception; }
88"extends" { return tok_extends; }
89"throws" { return tok_throws; }
90"service" { return tok_service; }
91"enum" { return tok_enum; }
Mark Slee30152872006-11-28 01:24:07 +000092"const" { return tok_const; }
Mark Slee52f643d2006-08-09 00:03:43 +000093
Mark Sleef12865a2007-01-12 00:23:26 +000094"abstract" { thrift_reserved_keyword(yytext); }
95"and" { thrift_reserved_keyword(yytext); }
96"as" { thrift_reserved_keyword(yytext); }
97"assert" { thrift_reserved_keyword(yytext); }
98"break" { thrift_reserved_keyword(yytext); }
99"case" { thrift_reserved_keyword(yytext); }
100"class" { thrift_reserved_keyword(yytext); }
101"continue" { thrift_reserved_keyword(yytext); }
102"declare" { thrift_reserved_keyword(yytext); }
103"def" { thrift_reserved_keyword(yytext); }
104"default" { thrift_reserved_keyword(yytext); }
105"del" { thrift_reserved_keyword(yytext); }
106"delete" { thrift_reserved_keyword(yytext); }
107"do" { thrift_reserved_keyword(yytext); }
108"elif" { thrift_reserved_keyword(yytext); }
109"else" { thrift_reserved_keyword(yytext); }
110"elseif" { thrift_reserved_keyword(yytext); }
111"except" { thrift_reserved_keyword(yytext); }
112"exec" { thrift_reserved_keyword(yytext); }
113"false" { thrift_reserved_keyword(yytext); }
114"final" { thrift_reserved_keyword(yytext); }
115"finally" { thrift_reserved_keyword(yytext); }
116"float" { thrift_reserved_keyword(yytext); }
117"for" { thrift_reserved_keyword(yytext); }
118"foreach" { thrift_reserved_keyword(yytext); }
119"function" { thrift_reserved_keyword(yytext); }
120"global" { thrift_reserved_keyword(yytext); }
121"goto" { thrift_reserved_keyword(yytext); }
122"if" { thrift_reserved_keyword(yytext); }
123"implements" { thrift_reserved_keyword(yytext); }
124"import" { thrift_reserved_keyword(yytext); }
125"in" { thrift_reserved_keyword(yytext); }
126"inline" { thrift_reserved_keyword(yytext); }
127"instanceof" { thrift_reserved_keyword(yytext); }
128"interface" { thrift_reserved_keyword(yytext); }
129"is" { thrift_reserved_keyword(yytext); }
130"lambda" { thrift_reserved_keyword(yytext); }
131"native" { thrift_reserved_keyword(yytext); }
132"new" { thrift_reserved_keyword(yytext); }
133"not" { thrift_reserved_keyword(yytext); }
134"or" { thrift_reserved_keyword(yytext); }
135"pass" { thrift_reserved_keyword(yytext); }
136"public" { thrift_reserved_keyword(yytext); }
137"print" { thrift_reserved_keyword(yytext); }
138"private" { thrift_reserved_keyword(yytext); }
139"protected" { thrift_reserved_keyword(yytext); }
140"raise" { thrift_reserved_keyword(yytext); }
141"return" { thrift_reserved_keyword(yytext); }
142"sizeof" { thrift_reserved_keyword(yytext); }
143"static" { thrift_reserved_keyword(yytext); }
144"switch" { thrift_reserved_keyword(yytext); }
145"synchronized" { thrift_reserved_keyword(yytext); }
146"this" { thrift_reserved_keyword(yytext); }
147"throw" { thrift_reserved_keyword(yytext); }
148"transient" { thrift_reserved_keyword(yytext); }
149"true" { thrift_reserved_keyword(yytext); }
150"try" { thrift_reserved_keyword(yytext); }
151"unsigned" { thrift_reserved_keyword(yytext); }
152"var" { thrift_reserved_keyword(yytext); }
153"virtual" { thrift_reserved_keyword(yytext); }
154"volatile" { thrift_reserved_keyword(yytext); }
155"while" { thrift_reserved_keyword(yytext); }
156"with" { thrift_reserved_keyword(yytext); }
157"union" { thrift_reserved_keyword(yytext); }
158"yield" { thrift_reserved_keyword(yytext); }
159
Mark Slee4f8da1d2006-10-12 02:47:27 +0000160{intconstant} {
161 yylval.iconst = atoi(yytext);
162 return tok_int_constant;
163}
Mark Sleef5377b32006-10-10 01:42:59 +0000164
Mark Slee600cdb32006-11-29 22:06:42 +0000165{hexconstant} {
166 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000167 return tok_int_constant;
168}
169
Mark Slee30152872006-11-28 01:24:07 +0000170{dubconstant} {
171 yylval.dconst = atof(yytext);
172 return tok_dub_constant;
173}
174
Mark Slee4f8da1d2006-10-12 02:47:27 +0000175{identifier} {
176 yylval.id = strdup(yytext);
177 return tok_identifier;
178}
179
Mark Slee30152872006-11-28 01:24:07 +0000180{dliteral} {
181 yylval.id = strdup(yytext+1);
182 yylval.id[strlen(yylval.id)-1] = '\0';
183 return tok_literal;
184}
185
186{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000187 yylval.id = strdup(yytext+1);
188 yylval.id[strlen(yylval.id)-1] = '\0';
189 return tok_literal;
190}
Mark Slee31985722006-05-24 21:45:31 +0000191
ccheeverf53b5cf2007-02-05 20:33:11 +0000192{doctext} {
193 yylval.id = strdup(yytext + 1);
194 yylval.id[strlen(yylval.id) - 1] = '\0';
195 return tok_doctext;
196}
197
198
Mark Slee31985722006-05-24 21:45:31 +0000199%%