blob: f5ab4a612a35e24145aa1160b36065a9b281aefe [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
David Reissea2cba82009-03-30 21:35:00 +00002/*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 */
Mark Slee31985722006-05-24 21:45:31 +000020
21/**
22 * Thrift parser.
23 *
24 * This parser is used on a thrift definition file.
25 *
Mark Slee31985722006-05-24 21:45:31 +000026 */
27
David Reissf1454162008-06-30 20:45:47 +000028#define __STDC_LIMIT_MACROS
29#define __STDC_FORMAT_MACROS
Mark Slee31985722006-05-24 21:45:31 +000030#include <stdio.h>
Roger Meier12d70532011-12-14 23:35:28 +000031#ifndef _MSC_VER
David Reissf1454162008-06-30 20:45:47 +000032#include <inttypes.h>
Roger Meier12d70532011-12-14 23:35:28 +000033#else
34#include <stdint.h>
35#endif
David Reiss400a5432008-07-25 19:48:39 +000036#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000037#include "main.h"
38#include "globals.h"
39#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000040#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000041
Mark Sleef5377b32006-10-10 01:42:59 +000042/**
43 * This global variable is used for automatic numbering of field indices etc.
44 * when parsing the members of a struct. Field values are automatically
45 * assigned starting from -1 and working their way down.
46 */
Mark Slee9cb7c612006-09-01 22:17:45 +000047int y_field_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000048int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000049const int struct_is_struct = 0;
50const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000051
52%}
53
Mark Sleef5377b32006-10-10 01:42:59 +000054/**
55 * This structure is used by the parser to hold the data types associated with
56 * various parse nodes.
57 */
Mark Slee31985722006-05-24 21:45:31 +000058%union {
Mark Slee30152872006-11-28 01:24:07 +000059 char* id;
David Reissf1454162008-06-30 20:45:47 +000060 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000061 double dconst;
62 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000063 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000064 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000065 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000066 t_typedef* ttypedef;
67 t_enum* tenum;
68 t_enum_value* tenumv;
69 t_const* tconst;
70 t_const_value* tconstv;
71 t_struct* tstruct;
72 t_service* tservice;
73 t_function* tfunction;
74 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000075 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000076 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000077 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000078 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000079}
80
Mark Sleef5377b32006-10-10 01:42:59 +000081/**
82 * Strings identifier
83 */
Mark Slee31985722006-05-24 21:45:31 +000084%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000085%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000086%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000087%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000088
89/**
Mark Slee30152872006-11-28 01:24:07 +000090 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000091 */
Mark Slee31985722006-05-24 21:45:31 +000092%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000093%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000094
Mark Sleef5377b32006-10-10 01:42:59 +000095/**
David Reiss399442b2008-02-20 02:28:05 +000096 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +000097 */
Mark Sleef0712dc2006-10-25 19:03:57 +000098%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000099%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +0000100%token tok_cpp_namespace
101%token tok_cpp_include
102%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +0000103%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +0000104%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000105%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000106%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000107%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000108%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000109%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000110%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000111%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000112%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000113%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000114%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000115%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000116%token tok_csharp_namespace
Jake Farrell7ae13e12011-10-18 14:35:26 +0000117%token tok_delphi_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
120 * Base datatype keywords
121 */
122%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000123%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000124%token tok_byte
125%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000126%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000127%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000128%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000129%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000130%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000131%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000132%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000133
Mark Sleef5377b32006-10-10 01:42:59 +0000134/**
135 * Complex type keywords
136 */
Mark Slee31985722006-05-24 21:45:31 +0000137%token tok_map
138%token tok_list
139%token tok_set
140
Mark Sleef5377b32006-10-10 01:42:59 +0000141/**
142 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000143 */
David Reiss6985a422009-03-24 20:00:47 +0000144%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000145
Mark Sleef5377b32006-10-10 01:42:59 +0000146/**
147 * Thrift language keywords
148 */
Mark Slee31985722006-05-24 21:45:31 +0000149%token tok_typedef
150%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000151%token tok_xception
152%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000153%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000154%token tok_service
155%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000156%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000157%token tok_required
158%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000159%token tok_union
Mark Slee31985722006-05-24 21:45:31 +0000160
Mark Sleef5377b32006-10-10 01:42:59 +0000161/**
162 * Grammar nodes
163 */
164
Mark Slee31985722006-05-24 21:45:31 +0000165%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000166%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000167%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000168%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000169%type<ttype> MapType
170%type<ttype> SetType
171%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000172
David Reisscdffe262007-08-14 17:12:31 +0000173%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000174%type<ttype> TypeDefinition
175
Mark Slee31985722006-05-24 21:45:31 +0000176%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000177
David Reissa2309992008-12-10 01:52:48 +0000178%type<ttype> TypeAnnotations
179%type<ttype> TypeAnnotationList
180%type<tannot> TypeAnnotation
181
Mark Slee31985722006-05-24 21:45:31 +0000182%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000183%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000184%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000185%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000186%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000187%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000188
189%type<tenum> Enum
190%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000191%type<tenumv> EnumDef
192
Mark Slee6a47fed2007-02-07 02:40:59 +0000193%type<ttypedef> Senum
194%type<tbase> SenumDefList
195%type<id> SenumDef
196
Mark Slee30152872006-11-28 01:24:07 +0000197%type<tconst> Const
198%type<tconstv> ConstValue
199%type<tconstv> ConstList
200%type<tconstv> ConstListContents
201%type<tconstv> ConstMap
202%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000203
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000204%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000205%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000206%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000207%type<tservice> Service
208
209%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000210%type<ttype> FunctionType
211%type<tservice> FunctionList
212
Mark Slee36bfa2e2007-01-19 20:09:51 +0000213%type<tstruct> Throws
214%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000215%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000216%type<tbool> XsdAll
217%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000218%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000219%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000220%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000221
David Reisscbd4bac2007-08-14 17:12:33 +0000222%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000223
Mark Slee31985722006-05-24 21:45:31 +0000224%%
225
Mark Sleef5377b32006-10-10 01:42:59 +0000226/**
227 * Thrift Grammar Implementation.
228 *
229 * For the most part this source file works its way top down from what you
230 * might expect to find in a typical .thrift file, i.e. type definitions and
231 * namespaces up top followed by service definitions using those types.
232 */
Mark Slee31985722006-05-24 21:45:31 +0000233
234Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000235 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000236 {
237 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000238 /*
239 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000240 if ($1 != NULL) {
241 g_program->set_doc($1);
242 }
David Reisscbd4bac2007-08-14 17:12:33 +0000243 */
244 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000245 }
246
David Reisscbd4bac2007-08-14 17:12:33 +0000247CaptureDocText:
248 {
249 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000250 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000251 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000252 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000253 $$ = NULL;
254 }
255 }
256
257/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
258DestroyDocText:
259 {
260 if (g_parse_mode == PROGRAM) {
261 clear_doctext();
262 }
263 }
264
265/* We have to DestroyDocText here, otherwise it catches the doctext
266 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000267HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000268 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000269 {
270 pdebug("HeaderList -> HeaderList Header");
271 }
272|
273 {
274 pdebug("HeaderList -> ");
275 }
276
277Header:
278 Include
279 {
280 pdebug("Header -> Include");
281 }
David Reiss79eca142008-02-27 01:55:13 +0000282| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 {
David Reiss79eca142008-02-27 01:55:13 +0000284 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000285 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000286 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 }
288 }
David Reissfb790d72010-09-02 16:41:45 +0000289| tok_namespace '*' tok_identifier
290 {
291 pdebug("Header -> tok_namespace * tok_identifier");
292 if (g_parse_mode == PROGRAM) {
293 g_program->set_namespace("*", $3);
294 }
295 }
David Reiss9a08dc62008-02-27 01:55:17 +0000296/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000297| tok_cpp_namespace tok_identifier
298 {
David Reiss9a08dc62008-02-27 01:55:17 +0000299 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000300 pdebug("Header -> tok_cpp_namespace tok_identifier");
301 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000302 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000303 }
304 }
305| tok_cpp_include tok_literal
306 {
307 pdebug("Header -> tok_cpp_include tok_literal");
308 if (g_parse_mode == PROGRAM) {
309 g_program->add_cpp_include($2);
310 }
311 }
Mark Sleee888b372007-01-12 01:06:24 +0000312| tok_php_namespace tok_identifier
313 {
David Reiss554ea6f2009-02-17 20:28:37 +0000314 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000315 pdebug("Header -> tok_php_namespace tok_identifier");
316 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000317 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000318 }
319 }
David Reiss320e45c2008-03-27 21:41:54 +0000320/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000321| tok_py_module tok_identifier
322 {
David Reiss320e45c2008-03-27 21:41:54 +0000323 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000324 pdebug("Header -> tok_py_module tok_identifier");
325 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000326 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000327 }
328 }
David Reiss07ef3a92008-03-27 21:42:39 +0000329/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000330| tok_perl_package tok_identifier
331 {
David Reiss07ef3a92008-03-27 21:42:39 +0000332 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000333 pdebug("Header -> tok_perl_namespace tok_identifier");
334 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000335 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000336 }
337 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000338/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000339| tok_ruby_namespace tok_identifier
340 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000341 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000342 pdebug("Header -> tok_ruby_namespace tok_identifier");
343 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000344 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000345 }
346 }
David Reiss3b455012008-03-27 21:40:46 +0000347/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000348| tok_smalltalk_category tok_st_identifier
349 {
David Reiss3b455012008-03-27 21:40:46 +0000350 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000351 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
352 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000353 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000354 }
355 }
David Reiss3b455012008-03-27 21:40:46 +0000356/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000357| tok_smalltalk_prefix tok_identifier
358 {
David Reiss3b455012008-03-27 21:40:46 +0000359 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000360 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
361 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000362 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000363 }
364 }
David Reiss771f8c72008-02-27 01:55:25 +0000365/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000366| tok_java_package tok_identifier
367 {
David Reiss9f646152008-03-02 21:59:48 +0000368 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000369 pdebug("Header -> tok_java_package tok_identifier");
370 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000371 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000372 }
373 }
David Reiss54b602b2008-03-27 21:41:06 +0000374/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000375| tok_cocoa_prefix tok_identifier
376 {
David Reiss54b602b2008-03-27 21:41:06 +0000377 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000378 pdebug("Header -> tok_cocoa_prefix tok_identifier");
379 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000380 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000381 }
382 }
David Reiss92e10d82009-02-17 20:28:19 +0000383/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000384| tok_xsd_namespace tok_literal
385 {
David Reiss92e10d82009-02-17 20:28:19 +0000386 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000387 pdebug("Header -> tok_xsd_namespace tok_literal");
388 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000389 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000390 }
391 }
David Reiss9d65bf02008-03-27 21:41:37 +0000392/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000393| tok_csharp_namespace tok_identifier
394 {
David Reiss9d65bf02008-03-27 21:41:37 +0000395 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000396 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000397 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000398 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000399 }
400 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000401/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
402| tok_delphi_namespace tok_identifier
403 {
404 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
405 pdebug("Header -> tok_delphi_namespace tok_identifier");
406 if (g_parse_mode == PROGRAM) {
407 g_program->set_namespace("delphi", $2);
408 }
409 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000410
411Include:
412 tok_include tok_literal
413 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000414 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000415 if (g_parse_mode == INCLUDES) {
416 std::string path = include_file(std::string($2));
417 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000418 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000419 }
420 }
421 }
Mark Slee31985722006-05-24 21:45:31 +0000422
423DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000424 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000425 {
426 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000427 if ($2 != NULL && $3 != NULL) {
428 $3->set_doc($2);
429 }
Mark Slee31985722006-05-24 21:45:31 +0000430 }
431|
432 {
433 pdebug("DefinitionList -> ");
434 }
435
436Definition:
Mark Slee30152872006-11-28 01:24:07 +0000437 Const
438 {
439 pdebug("Definition -> Const");
440 if (g_parse_mode == PROGRAM) {
441 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000442 }
David Reisscdffe262007-08-14 17:12:31 +0000443 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000444 }
445| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000446 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000447 pdebug("Definition -> TypeDefinition");
448 if (g_parse_mode == PROGRAM) {
449 g_scope->add_type($1->get_name(), $1);
450 if (g_parent_scope != NULL) {
451 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
452 }
Roger Meierba406d32013-07-15 22:41:34 +0200453 if (! g_program->is_unique_typename($1)) {
454 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
455 exit(1);
456 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000457 }
David Reisscdffe262007-08-14 17:12:31 +0000458 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000459 }
Mark Slee31985722006-05-24 21:45:31 +0000460| Service
461 {
462 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000463 if (g_parse_mode == PROGRAM) {
464 g_scope->add_service($1->get_name(), $1);
465 if (g_parent_scope != NULL) {
466 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
467 }
468 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200469 if (! g_program->is_unique_typename($1)) {
470 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
471 exit(1);
472 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000473 }
David Reisscdffe262007-08-14 17:12:31 +0000474 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000475 }
476
Mark Sleef0712dc2006-10-25 19:03:57 +0000477TypeDefinition:
478 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000479 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000480 pdebug("TypeDefinition -> Typedef");
481 if (g_parse_mode == PROGRAM) {
482 g_program->add_typedef($1);
483 }
484 }
485| Enum
486 {
487 pdebug("TypeDefinition -> Enum");
488 if (g_parse_mode == PROGRAM) {
489 g_program->add_enum($1);
490 }
491 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000492| Senum
493 {
494 pdebug("TypeDefinition -> Senum");
495 if (g_parse_mode == PROGRAM) {
496 g_program->add_typedef($1);
497 }
498 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000499| Struct
500 {
501 pdebug("TypeDefinition -> Struct");
502 if (g_parse_mode == PROGRAM) {
503 g_program->add_struct($1);
504 }
505 }
506| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000507 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000508 pdebug("TypeDefinition -> Xception");
509 if (g_parse_mode == PROGRAM) {
510 g_program->add_xception($1);
511 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000512 }
Mark Slee31985722006-05-24 21:45:31 +0000513
514Typedef:
Roger Meier30877382012-09-17 21:18:05 +0000515 tok_typedef FieldType tok_identifier TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000516 {
David Reiss4dd78012010-03-09 05:19:08 +0000517 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200518 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000519 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000520 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000521 if ($4 != NULL) {
522 $$->annotations_ = $4->annotations_;
523 delete $4;
524 }
Mark Slee31985722006-05-24 21:45:31 +0000525 }
526
Mark Slee6a47fed2007-02-07 02:40:59 +0000527CommaOrSemicolonOptional:
528 ','
529 {}
530| ';'
531 {}
532|
533 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000534
Mark Slee31985722006-05-24 21:45:31 +0000535Enum:
Roger Meier30877382012-09-17 21:18:05 +0000536 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000537 {
538 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000539 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200540 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000541 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000542 if ($6 != NULL) {
543 $$->annotations_ = $6->annotations_;
544 delete $6;
545 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000546 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000547 // make constants for all the enum values
548 if (g_parse_mode == PROGRAM) {
549 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
550 std::vector<t_enum_value*>::const_iterator c_iter;
551 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
552 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
553 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
554 const_val->set_enum($$);
555 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
556 if (g_parent_scope != NULL) {
557 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
558 }
559 }
560 }
Mark Slee31985722006-05-24 21:45:31 +0000561 }
562
563EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000564 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000565 {
566 pdebug("EnumDefList -> EnumDefList EnumDef");
567 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000568 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000569 }
570|
571 {
572 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000573 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000574 }
575
576EnumDef:
Roger Meier30877382012-09-17 21:18:05 +0000577 CaptureDocText tok_identifier '=' tok_int_constant TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000578 {
Mark Slee30152872006-11-28 01:24:07 +0000579 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000580 if ($4 < 0) {
581 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000582 }
David Reissf1454162008-06-30 20:45:47 +0000583 if ($4 > INT_MAX) {
584 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
585 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200586 validate_simple_identifier( $2);
Ben Craig9f9cd102013-10-09 09:47:48 -0500587 $$ = new t_enum_value($2, $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000588 if ($1 != NULL) {
589 $$->set_doc($1);
590 }
Roger Meier30877382012-09-17 21:18:05 +0000591 if ($5 != NULL) {
592 $$->annotations_ = $5->annotations_;
593 delete $5;
594 }
Mark Slee31985722006-05-24 21:45:31 +0000595 }
596|
Roger Meier30877382012-09-17 21:18:05 +0000597 CaptureDocText tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000598 {
Mark Slee30152872006-11-28 01:24:07 +0000599 pdebug("EnumDef -> tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200600 validate_simple_identifier( $2);
ccheeverf53b5cf2007-02-05 20:33:11 +0000601 $$ = new t_enum_value($2);
602 if ($1 != NULL) {
603 $$->set_doc($1);
604 }
Roger Meier30877382012-09-17 21:18:05 +0000605 if ($3 != NULL) {
606 $$->annotations_ = $3->annotations_;
607 delete $3;
608 }
Mark Slee30152872006-11-28 01:24:07 +0000609 }
610
Mark Slee6a47fed2007-02-07 02:40:59 +0000611Senum:
Roger Meier30877382012-09-17 21:18:05 +0000612 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000613 {
614 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200615 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000616 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000617 if ($6 != NULL) {
618 $$->annotations_ = $6->annotations_;
619 delete $6;
620 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000621 }
622
623SenumDefList:
624 SenumDefList SenumDef
625 {
626 pdebug("SenumDefList -> SenumDefList SenumDef");
627 $$ = $1;
628 $$->add_string_enum_val($2);
629 }
630|
631 {
632 pdebug("SenumDefList -> ");
633 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
634 $$->set_string_enum(true);
635 }
636
637SenumDef:
638 tok_literal CommaOrSemicolonOptional
639 {
640 pdebug("SenumDef -> tok_literal");
641 $$ = $1;
642 }
643
Mark Slee30152872006-11-28 01:24:07 +0000644Const:
645 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
646 {
647 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000648 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200649 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000650 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000651 $$ = new t_const($2, $3, $5);
652 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000653
654 g_scope->add_constant($3, $$);
655 if (g_parent_scope != NULL) {
656 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
657 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000658 } else {
659 $$ = NULL;
660 }
Mark Slee30152872006-11-28 01:24:07 +0000661 }
662
663ConstValue:
664 tok_int_constant
665 {
666 pdebug("ConstValue => tok_int_constant");
667 $$ = new t_const_value();
668 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000669 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
David Reissf1454162008-06-30 20:45:47 +0000670 pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
671 }
Mark Slee30152872006-11-28 01:24:07 +0000672 }
673| tok_dub_constant
674 {
675 pdebug("ConstValue => tok_dub_constant");
676 $$ = new t_const_value();
677 $$->set_double($1);
678 }
679| tok_literal
680 {
681 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000682 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000683 }
Mark Slee67fc6342006-11-29 03:37:04 +0000684| tok_identifier
685 {
686 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000687 $$ = new t_const_value();
688 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000689 }
Mark Slee30152872006-11-28 01:24:07 +0000690| ConstList
691 {
692 pdebug("ConstValue => ConstList");
693 $$ = $1;
694 }
695| ConstMap
696 {
697 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000698 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000699 }
700
701ConstList:
702 '[' ConstListContents ']'
703 {
704 pdebug("ConstList => [ ConstListContents ]");
705 $$ = $2;
706 }
707
708ConstListContents:
709 ConstListContents ConstValue CommaOrSemicolonOptional
710 {
711 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
712 $$ = $1;
713 $$->add_list($2);
714 }
715|
716 {
717 pdebug("ConstListContents =>");
718 $$ = new t_const_value();
719 $$->set_list();
720 }
721
722ConstMap:
723 '{' ConstMapContents '}'
724 {
725 pdebug("ConstMap => { ConstMapContents }");
726 $$ = $2;
727 }
728
729ConstMapContents:
730 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
731 {
732 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
733 $$ = $1;
734 $$->add_map($2, $4);
735 }
736|
737 {
738 pdebug("ConstMapContents =>");
739 $$ = new t_const_value();
740 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000741 }
742
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000743StructHead:
744 tok_struct
745 {
746 $$ = struct_is_struct;
747 }
748| tok_union
749 {
750 $$ = struct_is_union;
751 }
752
Mark Slee31985722006-05-24 21:45:31 +0000753Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000754 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000755 {
756 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200757 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000758 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000759 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000760 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000761 $$->set_name($2);
762 if ($7 != NULL) {
763 $$->annotations_ = $7->annotations_;
764 delete $7;
765 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000766 }
Ben Craig9f9cd102013-10-09 09:47:48 -0500767
Mark Slee36bfa2e2007-01-19 20:09:51 +0000768XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000769 tok_xsd_all
770 {
771 $$ = true;
772 }
773|
774 {
775 $$ = false;
776 }
777
Mark Slee36bfa2e2007-01-19 20:09:51 +0000778XsdOptional:
779 tok_xsd_optional
780 {
781 $$ = true;
782 }
783|
784 {
785 $$ = false;
786 }
787
Mark Slee7df0e2a2007-02-06 21:03:18 +0000788XsdNillable:
789 tok_xsd_nillable
790 {
791 $$ = true;
792 }
793|
794 {
795 $$ = false;
796 }
797
Mark Slee21135c32007-02-05 21:52:08 +0000798XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000799 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000800 {
Mark Slee748d83f2007-02-07 01:20:08 +0000801 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000802 }
803|
804 {
805 $$ = NULL;
806 }
807
Mark Slee9cb7c612006-09-01 22:17:45 +0000808Xception:
Roger Meier30877382012-09-17 21:18:05 +0000809 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000810 {
811 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200812 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000813 $4->set_name($2);
814 $4->set_xception(true);
815 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000816 if ($6 != NULL) {
817 $$->annotations_ = $6->annotations_;
818 delete $6;
819 }
Mark Slee31985722006-05-24 21:45:31 +0000820 }
821
822Service:
Roger Meier30877382012-09-17 21:18:05 +0000823 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000824 {
825 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200826 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000827 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000828 $$->set_name($2);
829 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000830 if ($9 != NULL) {
831 $$->annotations_ = $9->annotations_;
832 delete $9;
833 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 }
835
Mark Slee78165722007-09-10 22:08:49 +0000836FlagArgs:
837 {
838 g_arglist = 1;
839 }
840
841UnflagArgs:
842 {
843 g_arglist = 0;
844 }
845
Mark Slee36bfa2e2007-01-19 20:09:51 +0000846Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000847 tok_extends tok_identifier
848 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000849 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 $$ = NULL;
851 if (g_parse_mode == PROGRAM) {
852 $$ = g_scope->get_service($2);
853 if ($$ == NULL) {
854 yyerror("Service \"%s\" has not been defined.", $2);
855 exit(1);
856 }
857 }
858 }
859|
860 {
861 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000862 }
863
864FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000865 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000866 {
867 pdebug("FunctionList -> FunctionList Function");
868 $$ = $1;
869 $1->add_function($2);
870 }
871|
872 {
873 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000874 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000875 }
876
877Function:
Roger Meier30877382012-09-17 21:18:05 +0000878 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000879 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200880 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000881 $6->set_name(std::string($4) + "_args");
882 $$ = new t_function($3, $4, $6, $8, $2);
883 if ($1 != NULL) {
884 $$->set_doc($1);
885 }
Roger Meier30877382012-09-17 21:18:05 +0000886 if ($9 != NULL) {
887 $$->annotations_ = $9->annotations_;
888 delete $9;
889 }
Mark Slee31985722006-05-24 21:45:31 +0000890 }
891
David Reiss6985a422009-03-24 20:00:47 +0000892Oneway:
893 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000894 {
Mark Slee52f643d2006-08-09 00:03:43 +0000895 $$ = true;
896 }
897|
898 {
899 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000900 }
901
Mark Slee36bfa2e2007-01-19 20:09:51 +0000902Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000903 tok_throws '(' FieldList ')'
904 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000905 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000906 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000907 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000908 yyerror("Throws clause may not contain non-exception types");
909 exit(1);
910 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000911 }
912|
913 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000914 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000915 }
916
Mark Slee31985722006-05-24 21:45:31 +0000917FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000918 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000919 {
920 pdebug("FieldList -> FieldList , Field");
921 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000922 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100923 yyerror("\"%d: %s\" - field identifier/name has already been used", $2->get_key(), $2->get_name().c_str());
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000924 exit(1);
925 }
Mark Slee31985722006-05-24 21:45:31 +0000926 }
927|
928 {
929 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000930 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000932 }
933
934Field:
David Reiss53c10e02010-03-05 07:51:51 +0000935 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000936 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000937 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000938 if ($2.auto_assigned) {
David Reissbb461362009-04-02 19:23:59 +0000939 pwarning(1, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $5);
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000940 if (g_strict >= 192) {
941 yyerror("Implicit field keys are deprecated and not allowed with -strict");
942 exit(1);
943 }
Mark Slee31985722006-05-24 21:45:31 +0000944 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200945 validate_simple_identifier($5);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000946 $$ = new t_field($4, $5, $2.value);
David Reiss8320a922007-08-14 19:59:26 +0000947 $$->set_req($3);
948 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000949 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000950 validate_field_value($$, $6);
951 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000952 }
David Reiss8320a922007-08-14 19:59:26 +0000953 $$->set_xsd_optional($7);
954 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000955 if ($1 != NULL) {
956 $$->set_doc($1);
957 }
David Reiss8320a922007-08-14 19:59:26 +0000958 if ($9 != NULL) {
959 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000960 }
David Reiss53c10e02010-03-05 07:51:51 +0000961 if ($10 != NULL) {
962 $$->annotations_ = $10->annotations_;
963 delete $10;
964 }
Mark Slee31985722006-05-24 21:45:31 +0000965 }
Mark Slee7ff32452007-02-01 05:26:18 +0000966
967FieldIdentifier:
968 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000969 {
Mark Slee7ff32452007-02-01 05:26:18 +0000970 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000971 if (g_allow_neg_field_keys) {
972 /*
973 * g_allow_neg_field_keys exists to allow users to add explicitly
974 * specified key values to old .thrift files without breaking
975 * protocol compatibility.
976 */
977 if ($1 != y_field_val) {
978 /*
979 * warn if the user-specified negative value isn't what
980 * thrift would have auto-assigned.
981 */
Jens Geyer77407392012-12-11 23:38:12 +0100982 pwarning(1, "Nonpositive field key (%"PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000983 "auto-assigned by thrift (%d).\n", $1, y_field_val);
984 }
985 /*
986 * Leave $1 as-is, and update y_field_val to be one less than $1.
987 * The FieldList parsing will catch any duplicate key values.
988 */
Ben Craig9f9cd102013-10-09 09:47:48 -0500989 y_field_val = $1 - 1;
990 $$.value = $1;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000991 $$.auto_assigned = false;
992 } else {
Ben Craig9f9cd102013-10-09 09:47:48 -0500993 pwarning(1, "Nonpositive value (%"PRIi64") not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000994 $1);
995 $$.value = y_field_val--;
996 $$.auto_assigned = true;
997 }
998 } else {
Ben Craig9f9cd102013-10-09 09:47:48 -0500999 $$.value = $1;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001000 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001001 }
Mark Slee7ff32452007-02-01 05:26:18 +00001002 }
1003|
1004 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001005 $$.value = y_field_val--;
1006 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +00001007 }
1008
David Reiss8320a922007-08-14 19:59:26 +00001009FieldRequiredness:
1010 tok_required
1011 {
David Reiss45603e92009-09-02 22:15:55 +00001012 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001013 }
1014| tok_optional
1015 {
Mark Slee78165722007-09-10 22:08:49 +00001016 if (g_arglist) {
1017 if (g_parse_mode == PROGRAM) {
1018 pwarning(1, "optional keyword is ignored in argument lists.\n");
1019 }
David Reiss204420f2008-01-11 20:59:03 +00001020 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001021 } else {
David Reiss204420f2008-01-11 20:59:03 +00001022 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001023 }
David Reiss8320a922007-08-14 19:59:26 +00001024 }
1025|
1026 {
David Reiss204420f2008-01-11 20:59:03 +00001027 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001028 }
1029
Mark Slee7ff32452007-02-01 05:26:18 +00001030FieldValue:
1031 '=' ConstValue
1032 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001033 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001034 $$ = $2;
1035 } else {
1036 $$ = NULL;
1037 }
1038 }
1039|
1040 {
1041 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001042 }
Mark Slee31985722006-05-24 21:45:31 +00001043
Mark Slee31985722006-05-24 21:45:31 +00001044FunctionType:
1045 FieldType
1046 {
Mark Sleee8540632006-05-30 09:24:40 +00001047 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001048 $$ = $1;
1049 }
1050| tok_void
1051 {
Mark Sleee8540632006-05-30 09:24:40 +00001052 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001053 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001054 }
1055
1056FieldType:
1057 tok_identifier
1058 {
Mark Sleee8540632006-05-30 09:24:40 +00001059 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001060 if (g_parse_mode == INCLUDES) {
1061 // Ignore identifiers in include mode
1062 $$ = NULL;
1063 } else {
1064 // Lookup the identifier in the current scope
1065 $$ = g_scope->get_type($1);
1066 if ($$ == NULL) {
1067 yyerror("Type \"%s\" has not been defined.", $1);
1068 exit(1);
1069 }
Mark Sleee8540632006-05-30 09:24:40 +00001070 }
Mark Slee31985722006-05-24 21:45:31 +00001071 }
1072| BaseType
1073 {
Mark Sleee8540632006-05-30 09:24:40 +00001074 pdebug("FieldType -> BaseType");
1075 $$ = $1;
1076 }
1077| ContainerType
1078 {
1079 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001080 $$ = $1;
1081 }
1082
David Reissc8e30052009-07-27 17:02:42 +00001083BaseType: SimpleBaseType TypeAnnotations
1084 {
1085 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1086 if ($2 != NULL) {
1087 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1088 $$->annotations_ = $2->annotations_;
1089 delete $2;
1090 } else {
1091 $$ = $1;
1092 }
1093 }
1094
1095SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001096 tok_string
1097 {
1098 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001099 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001100 }
Mark Slee8d725a22007-04-13 01:57:12 +00001101| tok_binary
1102 {
1103 pdebug("BaseType -> tok_binary");
1104 $$ = g_type_binary;
1105 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001106| tok_slist
1107 {
1108 pdebug("BaseType -> tok_slist");
1109 $$ = g_type_slist;
1110 }
Mark Slee78f58e22006-09-02 04:17:07 +00001111| tok_bool
1112 {
1113 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001114 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001115 }
Mark Slee31985722006-05-24 21:45:31 +00001116| tok_byte
1117 {
1118 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001119 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001120 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001121| tok_i16
1122 {
1123 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001124 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001125 }
Mark Slee31985722006-05-24 21:45:31 +00001126| tok_i32
1127 {
1128 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001129 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001130 }
Mark Slee31985722006-05-24 21:45:31 +00001131| tok_i64
1132 {
1133 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001134 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001135 }
Mark Sleec98d0502006-09-06 02:42:25 +00001136| tok_double
1137 {
1138 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001139 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001140 }
Mark Slee31985722006-05-24 21:45:31 +00001141
David Reissa2309992008-12-10 01:52:48 +00001142ContainerType: SimpleContainerType TypeAnnotations
1143 {
1144 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1145 $$ = $1;
1146 if ($2 != NULL) {
1147 $$->annotations_ = $2->annotations_;
1148 delete $2;
1149 }
1150 }
1151
1152SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001153 MapType
1154 {
David Reissa2309992008-12-10 01:52:48 +00001155 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001156 $$ = $1;
1157 }
1158| SetType
1159 {
David Reissa2309992008-12-10 01:52:48 +00001160 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001161 $$ = $1;
1162 }
1163| ListType
1164 {
David Reissa2309992008-12-10 01:52:48 +00001165 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001166 $$ = $1;
1167 }
1168
1169MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001170 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001171 {
1172 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001173 $$ = new t_map($4, $6);
1174 if ($2 != NULL) {
1175 ((t_container*)$$)->set_cpp_name(std::string($2));
1176 }
Mark Sleee8540632006-05-30 09:24:40 +00001177 }
1178
1179SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001180 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001181 {
1182 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001183 $$ = new t_set($4);
1184 if ($2 != NULL) {
1185 ((t_container*)$$)->set_cpp_name(std::string($2));
1186 }
Mark Sleee8540632006-05-30 09:24:40 +00001187 }
1188
1189ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001190 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001191 {
1192 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001193 $$ = new t_list($3);
1194 if ($5 != NULL) {
1195 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001196 }
1197 }
1198
Mark Slee36bfa2e2007-01-19 20:09:51 +00001199CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001200 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001201 {
Mark Sleeafc76542007-02-09 21:55:44 +00001202 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001203 }
1204|
1205 {
1206 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001207 }
1208
David Reissa2309992008-12-10 01:52:48 +00001209TypeAnnotations:
1210 '(' TypeAnnotationList ')'
1211 {
1212 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1213 $$ = $2;
1214 }
1215|
1216 {
1217 $$ = NULL;
1218 }
1219
1220TypeAnnotationList:
1221 TypeAnnotationList TypeAnnotation
1222 {
1223 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1224 $$ = $1;
1225 $$->annotations_[$2->key] = $2->val;
1226 delete $2;
1227 }
1228|
1229 {
1230 /* Just use a dummy structure to hold the annotations. */
1231 $$ = new t_struct(g_program);
1232 }
1233
1234TypeAnnotation:
1235 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1236 {
1237 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1238 $$ = new t_annotation;
1239 $$->key = $1;
1240 $$->val = $3;
1241 }
1242
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001243%%