blob: 803f786adaade267d996c5874a5606c5bc9d80e0 [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 Slee58dfb4f2007-07-06 02:45:25 +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; }
71"php_namespace" { return tok_php_namespace; }
72"ruby_namespace" { return tok_ruby_namespace; }
73"xsd_all" { return tok_xsd_all; }
74"xsd_optional" { return tok_xsd_optional; }
75"xsd_nillable" { return tok_xsd_nillable; }
76"xsd_namespace" { return tok_xsd_namespace; }
77"xsd_attrs" { return tok_xsd_attrs; }
78"include" { return tok_include; }
Mark Sleef0712dc2006-10-25 19:03:57 +000079
Mark Slee58dfb4f2007-07-06 02:45:25 +000080"void" { return tok_void; }
81"bool" { return tok_bool; }
82"byte" { return tok_byte; }
83"i16" { return tok_i16; }
84"i32" { return tok_i32; }
85"i64" { return tok_i64; }
86"double" { return tok_double; }
87"string" { return tok_string; }
88"binary" { return tok_binary; }
89"slist" { return tok_slist; }
90"senum" { return tok_senum; }
91"map" { return tok_map; }
92"list" { return tok_list; }
93"set" { return tok_set; }
94"async" { return tok_async; }
95"typedef" { return tok_typedef; }
96"struct" { return tok_struct; }
97"exception" { return tok_xception; }
98"extends" { return tok_extends; }
99"throws" { return tok_throws; }
100"service" { return tok_service; }
101"enum" { return tok_enum; }
102"const" { return tok_const; }
Mark Slee52f643d2006-08-09 00:03:43 +0000103
Mark Sleef12865a2007-01-12 00:23:26 +0000104"abstract" { thrift_reserved_keyword(yytext); }
105"and" { thrift_reserved_keyword(yytext); }
106"as" { thrift_reserved_keyword(yytext); }
107"assert" { thrift_reserved_keyword(yytext); }
108"break" { thrift_reserved_keyword(yytext); }
109"case" { thrift_reserved_keyword(yytext); }
110"class" { thrift_reserved_keyword(yytext); }
111"continue" { thrift_reserved_keyword(yytext); }
112"declare" { thrift_reserved_keyword(yytext); }
113"def" { thrift_reserved_keyword(yytext); }
114"default" { thrift_reserved_keyword(yytext); }
115"del" { thrift_reserved_keyword(yytext); }
116"delete" { thrift_reserved_keyword(yytext); }
117"do" { thrift_reserved_keyword(yytext); }
118"elif" { thrift_reserved_keyword(yytext); }
119"else" { thrift_reserved_keyword(yytext); }
120"elseif" { thrift_reserved_keyword(yytext); }
121"except" { thrift_reserved_keyword(yytext); }
122"exec" { thrift_reserved_keyword(yytext); }
123"false" { thrift_reserved_keyword(yytext); }
124"final" { thrift_reserved_keyword(yytext); }
125"finally" { thrift_reserved_keyword(yytext); }
126"float" { thrift_reserved_keyword(yytext); }
127"for" { thrift_reserved_keyword(yytext); }
128"foreach" { thrift_reserved_keyword(yytext); }
129"function" { thrift_reserved_keyword(yytext); }
130"global" { thrift_reserved_keyword(yytext); }
131"goto" { thrift_reserved_keyword(yytext); }
132"if" { thrift_reserved_keyword(yytext); }
133"implements" { thrift_reserved_keyword(yytext); }
134"import" { thrift_reserved_keyword(yytext); }
135"in" { thrift_reserved_keyword(yytext); }
136"inline" { thrift_reserved_keyword(yytext); }
137"instanceof" { thrift_reserved_keyword(yytext); }
138"interface" { thrift_reserved_keyword(yytext); }
139"is" { thrift_reserved_keyword(yytext); }
140"lambda" { thrift_reserved_keyword(yytext); }
141"native" { thrift_reserved_keyword(yytext); }
142"new" { thrift_reserved_keyword(yytext); }
143"not" { thrift_reserved_keyword(yytext); }
144"or" { thrift_reserved_keyword(yytext); }
145"pass" { thrift_reserved_keyword(yytext); }
146"public" { thrift_reserved_keyword(yytext); }
147"print" { thrift_reserved_keyword(yytext); }
148"private" { thrift_reserved_keyword(yytext); }
149"protected" { thrift_reserved_keyword(yytext); }
150"raise" { thrift_reserved_keyword(yytext); }
151"return" { thrift_reserved_keyword(yytext); }
152"sizeof" { thrift_reserved_keyword(yytext); }
153"static" { thrift_reserved_keyword(yytext); }
154"switch" { thrift_reserved_keyword(yytext); }
155"synchronized" { thrift_reserved_keyword(yytext); }
156"this" { thrift_reserved_keyword(yytext); }
157"throw" { thrift_reserved_keyword(yytext); }
158"transient" { thrift_reserved_keyword(yytext); }
159"true" { thrift_reserved_keyword(yytext); }
160"try" { thrift_reserved_keyword(yytext); }
161"unsigned" { thrift_reserved_keyword(yytext); }
162"var" { thrift_reserved_keyword(yytext); }
163"virtual" { thrift_reserved_keyword(yytext); }
164"volatile" { thrift_reserved_keyword(yytext); }
165"while" { thrift_reserved_keyword(yytext); }
166"with" { thrift_reserved_keyword(yytext); }
167"union" { thrift_reserved_keyword(yytext); }
168"yield" { thrift_reserved_keyword(yytext); }
169
Mark Slee4f8da1d2006-10-12 02:47:27 +0000170{intconstant} {
171 yylval.iconst = atoi(yytext);
172 return tok_int_constant;
173}
Mark Sleef5377b32006-10-10 01:42:59 +0000174
Mark Slee600cdb32006-11-29 22:06:42 +0000175{hexconstant} {
176 sscanf(yytext+2, "%x", &yylval.iconst);
Mark Slee600cdb32006-11-29 22:06:42 +0000177 return tok_int_constant;
178}
179
Mark Slee30152872006-11-28 01:24:07 +0000180{dubconstant} {
181 yylval.dconst = atof(yytext);
182 return tok_dub_constant;
183}
184
Mark Slee4f8da1d2006-10-12 02:47:27 +0000185{identifier} {
186 yylval.id = strdup(yytext);
187 return tok_identifier;
188}
189
Mark Slee30152872006-11-28 01:24:07 +0000190{dliteral} {
191 yylval.id = strdup(yytext+1);
192 yylval.id[strlen(yylval.id)-1] = '\0';
193 return tok_literal;
194}
195
196{sliteral} {
Mark Sleef0712dc2006-10-25 19:03:57 +0000197 yylval.id = strdup(yytext+1);
198 yylval.id[strlen(yylval.id)-1] = '\0';
199 return tok_literal;
200}
Mark Slee31985722006-05-24 21:45:31 +0000201
ccheeverf53b5cf2007-02-05 20:33:11 +0000202{doctext} {
203 yylval.id = strdup(yytext + 1);
204 yylval.id[strlen(yylval.id) - 1] = '\0';
205 return tok_doctext;
206}
207
208
Mark Slee31985722006-05-24 21:45:31 +0000209%%