blob: 3100c6a6caf835de069f7b761f0cb44d0387b4c5 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
Mark Sleee9ce01c2007-05-16 02:29:53 +00002 * 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 Slee31985722006-05-24 21:45:31 +000010 * Thrift scanner.
11 *
12 * Tokenizes a thrift definition file.
13 * @author Mark Slee <mcslee@facebook.com>
14 */
Mark Sleef5377b32006-10-10 01:42:59 +000015
Mark Slee31985722006-05-24 21:45:31 +000016%{
17
18#include "main.h"
19#include "parse/t_program.h"
20
Mark Sleef5377b32006-10-10 01:42:59 +000021/**
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 Sleeeb0d0242007-01-25 07:58:55 +000025#include "thrifty.h"
Mark Slee31985722006-05-24 21:45:31 +000026
Mark Sleef12865a2007-01-12 00:23:26 +000027void thrift_reserved_keyword(char* keyword) {
28 yyerror("Cannot use reserved language keyword: \"%s\"\n", keyword);
29 exit(1);
30}
31
Mark Slee31985722006-05-24 21:45:31 +000032%}
33
Mark Sleef5377b32006-10-10 01:42:59 +000034/**
35 * Provides the yylineno global, useful for debugging output
36 */
Mark Slee31985722006-05-24 21:45:31 +000037%option lex-compat
38
Mark Sleef5377b32006-10-10 01:42:59 +000039/**
40 * Helper definitions, comments, constants, and whatnot
41 */
42
Mark Slee30152872006-11-28 01:24:07 +000043intconstant ([+-]?[0-9]+)
Mark Slee600cdb32006-11-29 22:06:42 +000044hexconstant ("0x"[0-9A-Fa-f]+)
Mark Slee30152872006-11-28 01:24:07 +000045dubconstant ([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
Mark Slee9cb7c612006-09-01 22:17:45 +000046identifier ([a-zA-Z_][\.a-zA-Z_0-9]*)
Mark Slee31985722006-05-24 21:45:31 +000047whitespace ([ \t\r\n]*)
48multicomm ("/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
49comment ("//"[^\n]*)
Mark Sleec98d0502006-09-06 02:42:25 +000050unixcomment ("#"[^\n]*)
ccheeverf53b5cf2007-02-05 20:33:11 +000051doctext ("["(("["[^\]\[]*"]")|[^\]\[])*"]") /* allows one level of nesting */
Mark Sleeae2bc3c2006-11-08 23:44:59 +000052symbol ([:;\,\{\}\(\)\=<>\[\]])
ccheeverf53b5cf2007-02-05 20:33:11 +000053dliteral ("\""[^"]*"\"")
54sliteral ("'"[^']*"'")
55
Mark Slee31985722006-05-24 21:45:31 +000056
57%%
58
59{whitespace} { /* do nothing */ }
60{multicomm} { /* do nothing */ }
61{comment} { /* do nothing */ }
Mark Sleec98d0502006-09-06 02:42:25 +000062{unixcomment} { /* do nothing */ }
Mark Slee31985722006-05-24 21:45:31 +000063
Mark Slee9cb7c612006-09-01 22:17:45 +000064{symbol} { return yytext[0]; }
65
Mark Sleef0712dc2006-10-25 19:03:57 +000066"namespace" { return tok_namespace; }
67"cpp_namespace" { return tok_cpp_namespace; }
68"cpp_include" { return tok_cpp_include; }
69"cpp_type" { return tok_cpp_type; }
70"java_package" { return tok_java_package; }
Mark Sleee888b372007-01-12 01:06:24 +000071"php_namespace" { return tok_php_namespace; }
Mark Slee782abbb2007-01-19 00:17:02 +000072"xsd_all" { return tok_xsd_all; }
Mark Slee36bfa2e2007-01-19 20:09:51 +000073"xsd_optional" { return tok_xsd_optional; }
Mark Slee7df0e2a2007-02-06 21:03:18 +000074"xsd_nillable" { return tok_xsd_nillable; }
Mark Slee0d9199e2007-01-31 02:08:30 +000075"xsd_namespace" { return tok_xsd_namespace; }
Mark Slee21135c32007-02-05 21:52:08 +000076"xsd_attrs" { return tok_xsd_attrs; }
Mark Sleef0712dc2006-10-25 19:03:57 +000077"include" { return tok_include; }
78
79"void" { return tok_void; }
80"bool" { return tok_bool; }
81"byte" { return tok_byte; }
82"i16" { return tok_i16; }
83"i32" { return tok_i32; }
84"i64" { return tok_i64; }
85"double" { return tok_double; }
86"string" { return tok_string; }
Mark Slee8d725a22007-04-13 01:57:12 +000087"binary" { return tok_binary; }
Mark Sleeb6200d82007-01-19 19:14:36 +000088"slist" { return tok_slist; }
Mark Slee6a47fed2007-02-07 02:40:59 +000089"senum" { return tok_senum; }
Mark Sleef0712dc2006-10-25 19:03:57 +000090"map" { return tok_map; }
91"list" { return tok_list; }
92"set" { return tok_set; }
93"async" { return tok_async; }
94"typedef" { return tok_typedef; }
95"struct" { return tok_struct; }
96"exception" { return tok_xception; }
97"extends" { return tok_extends; }
98"throws" { return tok_throws; }
99"service" { return tok_service; }
100"enum" { return tok_enum; }
Mark Slee30152872006-11-28 01:24:07 +0000101"const" { return tok_const; }
Mark Slee52f643d2006-08-09 00:03:43 +0000102
Mark Sleef12865a2007-01-12 00:23:26 +0000103"abstract" { thrift_reserved_keyword(yytext); }
104"and" { thrift_reserved_keyword(yytext); }
105"as" { thrift_reserved_keyword(yytext); }
106"assert" { thrift_reserved_keyword(yytext); }
107"break" { thrift_reserved_keyword(yytext); }
108"case" { thrift_reserved_keyword(yytext); }
109"class" { thrift_reserved_keyword(yytext); }
110"continue" { thrift_reserved_keyword(yytext); }
111"declare" { thrift_reserved_keyword(yytext); }
112"def" { thrift_reserved_keyword(yytext); }
113"default" { thrift_reserved_keyword(yytext); }
114"del" { thrift_reserved_keyword(yytext); }
115"delete" { thrift_reserved_keyword(yytext); }
116"do" { thrift_reserved_keyword(yytext); }
117"elif" { thrift_reserved_keyword(yytext); }
118"else" { thrift_reserved_keyword(yytext); }
119"elseif" { thrift_reserved_keyword(yytext); }
120"except" { thrift_reserved_keyword(yytext); }
121"exec" { thrift_reserved_keyword(yytext); }
122"false" { thrift_reserved_keyword(yytext); }
123"final" { thrift_reserved_keyword(yytext); }
124"finally" { thrift_reserved_keyword(yytext); }
125"float" { thrift_reserved_keyword(yytext); }
126"for" { thrift_reserved_keyword(yytext); }
127"foreach" { thrift_reserved_keyword(yytext); }
128"function" { thrift_reserved_keyword(yytext); }
129"global" { thrift_reserved_keyword(yytext); }
130"goto" { thrift_reserved_keyword(yytext); }
131"if" { thrift_reserved_keyword(yytext); }
132"implements" { thrift_reserved_keyword(yytext); }
133"import" { thrift_reserved_keyword(yytext); }
134"in" { thrift_reserved_keyword(yytext); }
135"inline" { thrift_reserved_keyword(yytext); }
136"instanceof" { thrift_reserved_keyword(yytext); }
137"interface" { thrift_reserved_keyword(yytext); }
138"is" { thrift_reserved_keyword(yytext); }
139"lambda" { thrift_reserved_keyword(yytext); }
140"native" { thrift_reserved_keyword(yytext); }
141"new" { thrift_reserved_keyword(yytext); }
142"not" { thrift_reserved_keyword(yytext); }
143"or" { thrift_reserved_keyword(yytext); }
144"pass" { thrift_reserved_keyword(yytext); }
145"public" { thrift_reserved_keyword(yytext); }
146"print" { thrift_reserved_keyword(yytext); }
147"private" { thrift_reserved_keyword(yytext); }
148"protected" { thrift_reserved_keyword(yytext); }
149"raise" { thrift_reserved_keyword(yytext); }
150"return" { thrift_reserved_keyword(yytext); }
151"sizeof" { thrift_reserved_keyword(yytext); }
152"static" { thrift_reserved_keyword(yytext); }
153"switch" { thrift_reserved_keyword(yytext); }
154"synchronized" { thrift_reserved_keyword(yytext); }
155"this" { thrift_reserved_keyword(yytext); }
156"throw" { thrift_reserved_keyword(yytext); }
157"transient" { thrift_reserved_keyword(yytext); }
158"true" { thrift_reserved_keyword(yytext); }
159"try" { thrift_reserved_keyword(yytext); }
160"unsigned" { thrift_reserved_keyword(yytext); }
161"var" { thrift_reserved_keyword(yytext); }
162"virtual" { thrift_reserved_keyword(yytext); }
163"volatile" { thrift_reserved_keyword(yytext); }
164"while" { thrift_reserved_keyword(yytext); }
165"with" { thrift_reserved_keyword(yytext); }
166"union" { thrift_reserved_keyword(yytext); }
167"yield" { thrift_reserved_keyword(yytext); }
168
Mark Slee4f8da1d2006-10-12 02:47:27 +0000169{intconstant} {
170 yylval.iconst = atoi(yytext);
171 return tok_int_constant;
172}
Mark Sleef5377b32006-10-10 01:42:59 +0000173
Mark Slee600cdb32006-11-29 22:06:42 +0000174{hexconstant} {
175 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000176 return tok_int_constant;
177}
178
Mark Slee30152872006-11-28 01:24:07 +0000179{dubconstant} {
180 yylval.dconst = atof(yytext);
181 return tok_dub_constant;
182}
183
Mark Slee4f8da1d2006-10-12 02:47:27 +0000184{identifier} {
185 yylval.id = strdup(yytext);
186 return tok_identifier;
187}
188
Mark Slee30152872006-11-28 01:24:07 +0000189{dliteral} {
190 yylval.id = strdup(yytext+1);
191 yylval.id[strlen(yylval.id)-1] = '\0';
192 return tok_literal;
193}
194
195{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000196 yylval.id = strdup(yytext+1);
197 yylval.id[strlen(yylval.id)-1] = '\0';
198 return tok_literal;
199}
Mark Slee31985722006-05-24 21:45:31 +0000200
ccheeverf53b5cf2007-02-05 20:33:11 +0000201{doctext} {
202 yylval.id = strdup(yytext + 1);
203 yylval.id[strlen(yylval.id) - 1] = '\0';
204 return tok_doctext;
205}
206
207
Mark Slee31985722006-05-24 21:45:31 +0000208%%