blob: 8814332c4e7194ad6b8f6b13c1220a2226236096 [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>
Ben Craige9576752013-10-11 08:19:16 -050037#ifdef _MSC_VER
38#include "windows/config.h"
39#endif
Mark Slee31985722006-05-24 21:45:31 +000040#include "main.h"
41#include "globals.h"
42#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000043#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000044
Ben Craige9576752013-10-11 08:19:16 -050045#ifdef _MSC_VER
46//warning C4065: switch statement contains 'default' but no 'case' labels
47#pragma warning(disable:4065)
48#endif
49
Mark Sleef5377b32006-10-10 01:42:59 +000050/**
51 * This global variable is used for automatic numbering of field indices etc.
52 * when parsing the members of a struct. Field values are automatically
53 * assigned starting from -1 and working their way down.
54 */
Mark Slee9cb7c612006-09-01 22:17:45 +000055int y_field_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000056int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000057const int struct_is_struct = 0;
58const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000059
60%}
61
Mark Sleef5377b32006-10-10 01:42:59 +000062/**
63 * This structure is used by the parser to hold the data types associated with
64 * various parse nodes.
65 */
Mark Slee31985722006-05-24 21:45:31 +000066%union {
Mark Slee30152872006-11-28 01:24:07 +000067 char* id;
David Reissf1454162008-06-30 20:45:47 +000068 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000069 double dconst;
70 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000071 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000072 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000073 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000074 t_typedef* ttypedef;
75 t_enum* tenum;
76 t_enum_value* tenumv;
77 t_const* tconst;
78 t_const_value* tconstv;
79 t_struct* tstruct;
80 t_service* tservice;
81 t_function* tfunction;
82 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000083 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000084 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000085 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000086 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000087}
88
Mark Sleef5377b32006-10-10 01:42:59 +000089/**
90 * Strings identifier
91 */
Mark Slee31985722006-05-24 21:45:31 +000092%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000093%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000094%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000095%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000096
97/**
Mark Slee30152872006-11-28 01:24:07 +000098 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000099 */
Mark Slee31985722006-05-24 21:45:31 +0000100%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +0000101%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +0000102
Mark Sleef5377b32006-10-10 01:42:59 +0000103/**
David Reiss399442b2008-02-20 02:28:05 +0000104 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +0000105 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000106%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +0000107%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +0000108%token tok_cpp_namespace
109%token tok_cpp_include
110%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +0000111%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +0000112%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000113%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000114%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000115%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000116%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000117%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000118%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000119%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000120%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000121%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000122%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000123%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000124%token tok_csharp_namespace
Jake Farrell7ae13e12011-10-18 14:35:26 +0000125%token tok_delphi_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000126
Mark Sleef5377b32006-10-10 01:42:59 +0000127/**
128 * Base datatype keywords
129 */
130%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000131%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000132%token tok_byte
133%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000134%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000135%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000136%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000137%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000138%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000139%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000140%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000141
Mark Sleef5377b32006-10-10 01:42:59 +0000142/**
143 * Complex type keywords
144 */
Mark Slee31985722006-05-24 21:45:31 +0000145%token tok_map
146%token tok_list
147%token tok_set
148
Mark Sleef5377b32006-10-10 01:42:59 +0000149/**
150 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000151 */
David Reiss6985a422009-03-24 20:00:47 +0000152%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000153
Mark Sleef5377b32006-10-10 01:42:59 +0000154/**
155 * Thrift language keywords
156 */
Mark Slee31985722006-05-24 21:45:31 +0000157%token tok_typedef
158%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000159%token tok_xception
160%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000161%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000162%token tok_service
163%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000164%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000165%token tok_required
166%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000167%token tok_union
Mark Slee31985722006-05-24 21:45:31 +0000168
Mark Sleef5377b32006-10-10 01:42:59 +0000169/**
170 * Grammar nodes
171 */
172
Mark Slee31985722006-05-24 21:45:31 +0000173%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000174%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000175%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000176%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000177%type<ttype> MapType
178%type<ttype> SetType
179%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000180
David Reisscdffe262007-08-14 17:12:31 +0000181%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000182%type<ttype> TypeDefinition
183
Mark Slee31985722006-05-24 21:45:31 +0000184%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000185
David Reissa2309992008-12-10 01:52:48 +0000186%type<ttype> TypeAnnotations
187%type<ttype> TypeAnnotationList
188%type<tannot> TypeAnnotation
189
Mark Slee31985722006-05-24 21:45:31 +0000190%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000191%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000192%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000193%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000194%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000195%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000196
197%type<tenum> Enum
198%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000199%type<tenumv> EnumDef
200
Mark Slee6a47fed2007-02-07 02:40:59 +0000201%type<ttypedef> Senum
202%type<tbase> SenumDefList
203%type<id> SenumDef
204
Mark Slee30152872006-11-28 01:24:07 +0000205%type<tconst> Const
206%type<tconstv> ConstValue
207%type<tconstv> ConstList
208%type<tconstv> ConstListContents
209%type<tconstv> ConstMap
210%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000211
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000212%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000213%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000214%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000215%type<tservice> Service
216
217%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000218%type<ttype> FunctionType
219%type<tservice> FunctionList
220
Mark Slee36bfa2e2007-01-19 20:09:51 +0000221%type<tstruct> Throws
222%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000223%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000224%type<tbool> XsdAll
225%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000226%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000227%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000228%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000229
David Reisscbd4bac2007-08-14 17:12:33 +0000230%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000231
Mark Slee31985722006-05-24 21:45:31 +0000232%%
233
Mark Sleef5377b32006-10-10 01:42:59 +0000234/**
235 * Thrift Grammar Implementation.
236 *
237 * For the most part this source file works its way top down from what you
238 * might expect to find in a typical .thrift file, i.e. type definitions and
239 * namespaces up top followed by service definitions using those types.
240 */
Mark Slee31985722006-05-24 21:45:31 +0000241
242Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000243 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000244 {
245 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000246 /*
247 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000248 if ($1 != NULL) {
249 g_program->set_doc($1);
250 }
David Reisscbd4bac2007-08-14 17:12:33 +0000251 */
252 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000253 }
254
David Reisscbd4bac2007-08-14 17:12:33 +0000255CaptureDocText:
256 {
257 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000258 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000259 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000260 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000261 $$ = NULL;
262 }
263 }
264
265/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
266DestroyDocText:
267 {
268 if (g_parse_mode == PROGRAM) {
269 clear_doctext();
270 }
271 }
272
273/* We have to DestroyDocText here, otherwise it catches the doctext
274 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000275HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000276 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000277 {
278 pdebug("HeaderList -> HeaderList Header");
279 }
280|
281 {
282 pdebug("HeaderList -> ");
283 }
284
285Header:
286 Include
287 {
288 pdebug("Header -> Include");
289 }
David Reiss79eca142008-02-27 01:55:13 +0000290| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000291 {
David Reiss79eca142008-02-27 01:55:13 +0000292 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000293 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000294 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000295 }
296 }
David Reissfb790d72010-09-02 16:41:45 +0000297| tok_namespace '*' tok_identifier
298 {
299 pdebug("Header -> tok_namespace * tok_identifier");
300 if (g_parse_mode == PROGRAM) {
301 g_program->set_namespace("*", $3);
302 }
303 }
David Reiss9a08dc62008-02-27 01:55:17 +0000304/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000305| tok_cpp_namespace tok_identifier
306 {
David Reiss9a08dc62008-02-27 01:55:17 +0000307 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000308 pdebug("Header -> tok_cpp_namespace tok_identifier");
309 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000310 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000311 }
312 }
313| tok_cpp_include tok_literal
314 {
315 pdebug("Header -> tok_cpp_include tok_literal");
316 if (g_parse_mode == PROGRAM) {
317 g_program->add_cpp_include($2);
318 }
319 }
Mark Sleee888b372007-01-12 01:06:24 +0000320| tok_php_namespace tok_identifier
321 {
David Reiss554ea6f2009-02-17 20:28:37 +0000322 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000323 pdebug("Header -> tok_php_namespace tok_identifier");
324 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000325 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000326 }
327 }
David Reiss320e45c2008-03-27 21:41:54 +0000328/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000329| tok_py_module tok_identifier
330 {
David Reiss320e45c2008-03-27 21:41:54 +0000331 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000332 pdebug("Header -> tok_py_module tok_identifier");
333 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000334 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000335 }
336 }
David Reiss07ef3a92008-03-27 21:42:39 +0000337/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000338| tok_perl_package tok_identifier
339 {
David Reiss07ef3a92008-03-27 21:42:39 +0000340 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000341 pdebug("Header -> tok_perl_namespace tok_identifier");
342 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000343 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000344 }
345 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000346/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000347| tok_ruby_namespace tok_identifier
348 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000349 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000350 pdebug("Header -> tok_ruby_namespace tok_identifier");
351 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000352 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000353 }
354 }
David Reiss3b455012008-03-27 21:40:46 +0000355/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000356| tok_smalltalk_category tok_st_identifier
357 {
David Reiss3b455012008-03-27 21:40:46 +0000358 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000359 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
360 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000361 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000362 }
363 }
David Reiss3b455012008-03-27 21:40:46 +0000364/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000365| tok_smalltalk_prefix tok_identifier
366 {
David Reiss3b455012008-03-27 21:40:46 +0000367 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000368 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
369 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000370 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000371 }
372 }
David Reiss771f8c72008-02-27 01:55:25 +0000373/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000374| tok_java_package tok_identifier
375 {
David Reiss9f646152008-03-02 21:59:48 +0000376 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000377 pdebug("Header -> tok_java_package tok_identifier");
378 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000379 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000380 }
381 }
David Reiss54b602b2008-03-27 21:41:06 +0000382/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000383| tok_cocoa_prefix tok_identifier
384 {
David Reiss54b602b2008-03-27 21:41:06 +0000385 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000386 pdebug("Header -> tok_cocoa_prefix tok_identifier");
387 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000388 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000389 }
390 }
David Reiss92e10d82009-02-17 20:28:19 +0000391/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000392| tok_xsd_namespace tok_literal
393 {
David Reiss92e10d82009-02-17 20:28:19 +0000394 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000395 pdebug("Header -> tok_xsd_namespace tok_literal");
396 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000397 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000398 }
399 }
David Reiss9d65bf02008-03-27 21:41:37 +0000400/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000401| tok_csharp_namespace tok_identifier
402 {
David Reiss9d65bf02008-03-27 21:41:37 +0000403 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000404 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000405 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000406 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000407 }
408 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000409/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
410| tok_delphi_namespace tok_identifier
411 {
412 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
413 pdebug("Header -> tok_delphi_namespace tok_identifier");
414 if (g_parse_mode == PROGRAM) {
415 g_program->set_namespace("delphi", $2);
416 }
417 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000418
419Include:
420 tok_include tok_literal
421 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000422 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000423 if (g_parse_mode == INCLUDES) {
424 std::string path = include_file(std::string($2));
425 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000426 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000427 }
428 }
429 }
Mark Slee31985722006-05-24 21:45:31 +0000430
431DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000432 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000433 {
434 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000435 if ($2 != NULL && $3 != NULL) {
436 $3->set_doc($2);
437 }
Mark Slee31985722006-05-24 21:45:31 +0000438 }
439|
440 {
441 pdebug("DefinitionList -> ");
442 }
443
444Definition:
Mark Slee30152872006-11-28 01:24:07 +0000445 Const
446 {
447 pdebug("Definition -> Const");
448 if (g_parse_mode == PROGRAM) {
449 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000450 }
David Reisscdffe262007-08-14 17:12:31 +0000451 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000452 }
453| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000454 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000455 pdebug("Definition -> TypeDefinition");
456 if (g_parse_mode == PROGRAM) {
457 g_scope->add_type($1->get_name(), $1);
458 if (g_parent_scope != NULL) {
459 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
460 }
Roger Meierba406d32013-07-15 22:41:34 +0200461 if (! g_program->is_unique_typename($1)) {
462 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
463 exit(1);
464 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000465 }
David Reisscdffe262007-08-14 17:12:31 +0000466 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000467 }
Mark Slee31985722006-05-24 21:45:31 +0000468| Service
469 {
470 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000471 if (g_parse_mode == PROGRAM) {
472 g_scope->add_service($1->get_name(), $1);
473 if (g_parent_scope != NULL) {
474 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
475 }
476 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200477 if (! g_program->is_unique_typename($1)) {
478 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
479 exit(1);
480 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000481 }
David Reisscdffe262007-08-14 17:12:31 +0000482 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000483 }
484
Mark Sleef0712dc2006-10-25 19:03:57 +0000485TypeDefinition:
486 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000487 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000488 pdebug("TypeDefinition -> Typedef");
489 if (g_parse_mode == PROGRAM) {
490 g_program->add_typedef($1);
491 }
492 }
493| Enum
494 {
495 pdebug("TypeDefinition -> Enum");
496 if (g_parse_mode == PROGRAM) {
497 g_program->add_enum($1);
498 }
499 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000500| Senum
501 {
502 pdebug("TypeDefinition -> Senum");
503 if (g_parse_mode == PROGRAM) {
504 g_program->add_typedef($1);
505 }
506 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000507| Struct
508 {
509 pdebug("TypeDefinition -> Struct");
510 if (g_parse_mode == PROGRAM) {
511 g_program->add_struct($1);
512 }
513 }
514| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000515 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000516 pdebug("TypeDefinition -> Xception");
517 if (g_parse_mode == PROGRAM) {
518 g_program->add_xception($1);
519 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000520 }
Mark Slee31985722006-05-24 21:45:31 +0000521
522Typedef:
Roger Meier30877382012-09-17 21:18:05 +0000523 tok_typedef FieldType tok_identifier TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000524 {
David Reiss4dd78012010-03-09 05:19:08 +0000525 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200526 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000527 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000528 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000529 if ($4 != NULL) {
530 $$->annotations_ = $4->annotations_;
531 delete $4;
532 }
Mark Slee31985722006-05-24 21:45:31 +0000533 }
534
Mark Slee6a47fed2007-02-07 02:40:59 +0000535CommaOrSemicolonOptional:
536 ','
537 {}
538| ';'
539 {}
540|
541 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000542
Mark Slee31985722006-05-24 21:45:31 +0000543Enum:
Roger Meier30877382012-09-17 21:18:05 +0000544 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000545 {
546 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000547 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200548 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000549 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000550 if ($6 != NULL) {
551 $$->annotations_ = $6->annotations_;
552 delete $6;
553 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000554 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000555 // make constants for all the enum values
556 if (g_parse_mode == PROGRAM) {
557 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
558 std::vector<t_enum_value*>::const_iterator c_iter;
559 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
560 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
561 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
562 const_val->set_enum($$);
563 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
564 if (g_parent_scope != NULL) {
565 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
566 }
567 }
568 }
Mark Slee31985722006-05-24 21:45:31 +0000569 }
570
571EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000572 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000573 {
574 pdebug("EnumDefList -> EnumDefList EnumDef");
575 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000576 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000577 }
578|
579 {
580 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000581 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000582 }
583
584EnumDef:
Roger Meier30877382012-09-17 21:18:05 +0000585 CaptureDocText tok_identifier '=' tok_int_constant TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000586 {
Mark Slee30152872006-11-28 01:24:07 +0000587 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000588 if ($4 < 0) {
589 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000590 }
David Reissf1454162008-06-30 20:45:47 +0000591 if ($4 > INT_MAX) {
592 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
593 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200594 validate_simple_identifier( $2);
Ben Craige9576752013-10-11 08:19:16 -0500595 $$ = new t_enum_value($2, static_cast<int>($4));
ccheeverf53b5cf2007-02-05 20:33:11 +0000596 if ($1 != NULL) {
597 $$->set_doc($1);
598 }
Roger Meier30877382012-09-17 21:18:05 +0000599 if ($5 != NULL) {
600 $$->annotations_ = $5->annotations_;
601 delete $5;
602 }
Mark Slee31985722006-05-24 21:45:31 +0000603 }
604|
Roger Meier30877382012-09-17 21:18:05 +0000605 CaptureDocText tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000606 {
Mark Slee30152872006-11-28 01:24:07 +0000607 pdebug("EnumDef -> tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200608 validate_simple_identifier( $2);
ccheeverf53b5cf2007-02-05 20:33:11 +0000609 $$ = new t_enum_value($2);
610 if ($1 != NULL) {
611 $$->set_doc($1);
612 }
Roger Meier30877382012-09-17 21:18:05 +0000613 if ($3 != NULL) {
614 $$->annotations_ = $3->annotations_;
615 delete $3;
616 }
Mark Slee30152872006-11-28 01:24:07 +0000617 }
618
Mark Slee6a47fed2007-02-07 02:40:59 +0000619Senum:
Roger Meier30877382012-09-17 21:18:05 +0000620 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000621 {
622 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200623 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000624 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000625 if ($6 != NULL) {
626 $$->annotations_ = $6->annotations_;
627 delete $6;
628 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000629 }
630
631SenumDefList:
632 SenumDefList SenumDef
633 {
634 pdebug("SenumDefList -> SenumDefList SenumDef");
635 $$ = $1;
636 $$->add_string_enum_val($2);
637 }
638|
639 {
640 pdebug("SenumDefList -> ");
641 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
642 $$->set_string_enum(true);
643 }
644
645SenumDef:
646 tok_literal CommaOrSemicolonOptional
647 {
648 pdebug("SenumDef -> tok_literal");
649 $$ = $1;
650 }
651
Mark Slee30152872006-11-28 01:24:07 +0000652Const:
653 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
654 {
655 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000656 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200657 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000658 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000659 $$ = new t_const($2, $3, $5);
660 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000661
662 g_scope->add_constant($3, $$);
663 if (g_parent_scope != NULL) {
664 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
665 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000666 } else {
667 $$ = NULL;
668 }
Mark Slee30152872006-11-28 01:24:07 +0000669 }
670
671ConstValue:
672 tok_int_constant
673 {
674 pdebug("ConstValue => tok_int_constant");
675 $$ = new t_const_value();
676 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000677 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100678 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000679 }
Mark Slee30152872006-11-28 01:24:07 +0000680 }
681| tok_dub_constant
682 {
683 pdebug("ConstValue => tok_dub_constant");
684 $$ = new t_const_value();
685 $$->set_double($1);
686 }
687| tok_literal
688 {
689 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000690 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000691 }
Mark Slee67fc6342006-11-29 03:37:04 +0000692| tok_identifier
693 {
694 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000695 $$ = new t_const_value();
696 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000697 }
Mark Slee30152872006-11-28 01:24:07 +0000698| ConstList
699 {
700 pdebug("ConstValue => ConstList");
701 $$ = $1;
702 }
703| ConstMap
704 {
705 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000706 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000707 }
708
709ConstList:
710 '[' ConstListContents ']'
711 {
712 pdebug("ConstList => [ ConstListContents ]");
713 $$ = $2;
714 }
715
716ConstListContents:
717 ConstListContents ConstValue CommaOrSemicolonOptional
718 {
719 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
720 $$ = $1;
721 $$->add_list($2);
722 }
723|
724 {
725 pdebug("ConstListContents =>");
726 $$ = new t_const_value();
727 $$->set_list();
728 }
729
730ConstMap:
731 '{' ConstMapContents '}'
732 {
733 pdebug("ConstMap => { ConstMapContents }");
734 $$ = $2;
735 }
736
737ConstMapContents:
738 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
739 {
740 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
741 $$ = $1;
742 $$->add_map($2, $4);
743 }
744|
745 {
746 pdebug("ConstMapContents =>");
747 $$ = new t_const_value();
748 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000749 }
750
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000751StructHead:
752 tok_struct
753 {
754 $$ = struct_is_struct;
755 }
756| tok_union
757 {
758 $$ = struct_is_union;
759 }
760
Mark Slee31985722006-05-24 21:45:31 +0000761Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000762 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000763 {
764 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200765 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000766 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000767 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000768 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000769 $$->set_name($2);
770 if ($7 != NULL) {
771 $$->annotations_ = $7->annotations_;
772 delete $7;
773 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000774 }
Ben Craige9576752013-10-11 08:19:16 -0500775
Mark Slee36bfa2e2007-01-19 20:09:51 +0000776XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000777 tok_xsd_all
778 {
779 $$ = true;
780 }
781|
782 {
783 $$ = false;
784 }
785
Mark Slee36bfa2e2007-01-19 20:09:51 +0000786XsdOptional:
787 tok_xsd_optional
788 {
789 $$ = true;
790 }
791|
792 {
793 $$ = false;
794 }
795
Mark Slee7df0e2a2007-02-06 21:03:18 +0000796XsdNillable:
797 tok_xsd_nillable
798 {
799 $$ = true;
800 }
801|
802 {
803 $$ = false;
804 }
805
Mark Slee21135c32007-02-05 21:52:08 +0000806XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000807 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000808 {
Mark Slee748d83f2007-02-07 01:20:08 +0000809 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000810 }
811|
812 {
813 $$ = NULL;
814 }
815
Mark Slee9cb7c612006-09-01 22:17:45 +0000816Xception:
Roger Meier30877382012-09-17 21:18:05 +0000817 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000818 {
819 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200820 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000821 $4->set_name($2);
822 $4->set_xception(true);
823 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000824 if ($6 != NULL) {
825 $$->annotations_ = $6->annotations_;
826 delete $6;
827 }
Mark Slee31985722006-05-24 21:45:31 +0000828 }
829
830Service:
Roger Meier30877382012-09-17 21:18:05 +0000831 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000832 {
833 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200834 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000835 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000836 $$->set_name($2);
837 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000838 if ($9 != NULL) {
839 $$->annotations_ = $9->annotations_;
840 delete $9;
841 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000842 }
843
Mark Slee78165722007-09-10 22:08:49 +0000844FlagArgs:
845 {
846 g_arglist = 1;
847 }
848
849UnflagArgs:
850 {
851 g_arglist = 0;
852 }
853
Mark Slee36bfa2e2007-01-19 20:09:51 +0000854Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000855 tok_extends tok_identifier
856 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000857 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000858 $$ = NULL;
859 if (g_parse_mode == PROGRAM) {
860 $$ = g_scope->get_service($2);
861 if ($$ == NULL) {
862 yyerror("Service \"%s\" has not been defined.", $2);
863 exit(1);
864 }
865 }
866 }
867|
868 {
869 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000870 }
871
872FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000873 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000874 {
875 pdebug("FunctionList -> FunctionList Function");
876 $$ = $1;
877 $1->add_function($2);
878 }
879|
880 {
881 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000882 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000883 }
884
885Function:
Roger Meier30877382012-09-17 21:18:05 +0000886 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000887 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200888 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000889 $6->set_name(std::string($4) + "_args");
890 $$ = new t_function($3, $4, $6, $8, $2);
891 if ($1 != NULL) {
892 $$->set_doc($1);
893 }
Roger Meier30877382012-09-17 21:18:05 +0000894 if ($9 != NULL) {
895 $$->annotations_ = $9->annotations_;
896 delete $9;
897 }
Mark Slee31985722006-05-24 21:45:31 +0000898 }
899
David Reiss6985a422009-03-24 20:00:47 +0000900Oneway:
901 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000902 {
Mark Slee52f643d2006-08-09 00:03:43 +0000903 $$ = true;
904 }
905|
906 {
907 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000908 }
909
Mark Slee36bfa2e2007-01-19 20:09:51 +0000910Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000911 tok_throws '(' FieldList ')'
912 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000913 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000914 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000915 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000916 yyerror("Throws clause may not contain non-exception types");
917 exit(1);
918 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000919 }
920|
921 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000922 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000923 }
924
Mark Slee31985722006-05-24 21:45:31 +0000925FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000926 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000927 {
928 pdebug("FieldList -> FieldList , Field");
929 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000930 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100931 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 +0000932 exit(1);
933 }
Mark Slee31985722006-05-24 21:45:31 +0000934 }
935|
936 {
937 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000938 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000940 }
941
942Field:
David Reiss53c10e02010-03-05 07:51:51 +0000943 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000944 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000946 if ($2.auto_assigned) {
David Reissbb461362009-04-02 19:23:59 +0000947 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 +0000948 if (g_strict >= 192) {
949 yyerror("Implicit field keys are deprecated and not allowed with -strict");
950 exit(1);
951 }
Mark Slee31985722006-05-24 21:45:31 +0000952 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200953 validate_simple_identifier($5);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000954 $$ = new t_field($4, $5, $2.value);
David Reiss8320a922007-08-14 19:59:26 +0000955 $$->set_req($3);
956 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000957 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000958 validate_field_value($$, $6);
959 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000960 }
David Reiss8320a922007-08-14 19:59:26 +0000961 $$->set_xsd_optional($7);
962 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000963 if ($1 != NULL) {
964 $$->set_doc($1);
965 }
David Reiss8320a922007-08-14 19:59:26 +0000966 if ($9 != NULL) {
967 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000968 }
David Reiss53c10e02010-03-05 07:51:51 +0000969 if ($10 != NULL) {
970 $$->annotations_ = $10->annotations_;
971 delete $10;
972 }
Mark Slee31985722006-05-24 21:45:31 +0000973 }
Mark Slee7ff32452007-02-01 05:26:18 +0000974
975FieldIdentifier:
976 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000977 {
Mark Slee7ff32452007-02-01 05:26:18 +0000978 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000979 if (g_allow_neg_field_keys) {
980 /*
981 * g_allow_neg_field_keys exists to allow users to add explicitly
982 * specified key values to old .thrift files without breaking
983 * protocol compatibility.
984 */
985 if ($1 != y_field_val) {
986 /*
987 * warn if the user-specified negative value isn't what
988 * thrift would have auto-assigned.
989 */
Roger Meier5f2d34e2013-11-16 16:43:41 +0100990 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000991 "auto-assigned by thrift (%d).\n", $1, y_field_val);
992 }
993 /*
994 * Leave $1 as-is, and update y_field_val to be one less than $1.
995 * The FieldList parsing will catch any duplicate key values.
996 */
Ben Craige9576752013-10-11 08:19:16 -0500997 y_field_val = static_cast<int32_t>($1 - 1);
998 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000999 $$.auto_assigned = false;
1000 } else {
Ben Craige9576752013-10-11 08:19:16 -05001001 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001002 $1);
1003 $$.value = y_field_val--;
1004 $$.auto_assigned = true;
1005 }
1006 } else {
Ben Craige9576752013-10-11 08:19:16 -05001007 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001008 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001009 }
Mark Slee7ff32452007-02-01 05:26:18 +00001010 }
1011|
1012 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001013 $$.value = y_field_val--;
1014 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +00001015 }
1016
David Reiss8320a922007-08-14 19:59:26 +00001017FieldRequiredness:
1018 tok_required
1019 {
David Reiss45603e92009-09-02 22:15:55 +00001020 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001021 }
1022| tok_optional
1023 {
Mark Slee78165722007-09-10 22:08:49 +00001024 if (g_arglist) {
1025 if (g_parse_mode == PROGRAM) {
1026 pwarning(1, "optional keyword is ignored in argument lists.\n");
1027 }
David Reiss204420f2008-01-11 20:59:03 +00001028 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001029 } else {
David Reiss204420f2008-01-11 20:59:03 +00001030 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001031 }
David Reiss8320a922007-08-14 19:59:26 +00001032 }
1033|
1034 {
David Reiss204420f2008-01-11 20:59:03 +00001035 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001036 }
1037
Mark Slee7ff32452007-02-01 05:26:18 +00001038FieldValue:
1039 '=' ConstValue
1040 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001041 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001042 $$ = $2;
1043 } else {
1044 $$ = NULL;
1045 }
1046 }
1047|
1048 {
1049 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001050 }
Mark Slee31985722006-05-24 21:45:31 +00001051
Mark Slee31985722006-05-24 21:45:31 +00001052FunctionType:
1053 FieldType
1054 {
Mark Sleee8540632006-05-30 09:24:40 +00001055 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001056 $$ = $1;
1057 }
1058| tok_void
1059 {
Mark Sleee8540632006-05-30 09:24:40 +00001060 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001061 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001062 }
1063
1064FieldType:
1065 tok_identifier
1066 {
Mark Sleee8540632006-05-30 09:24:40 +00001067 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001068 if (g_parse_mode == INCLUDES) {
1069 // Ignore identifiers in include mode
1070 $$ = NULL;
1071 } else {
1072 // Lookup the identifier in the current scope
1073 $$ = g_scope->get_type($1);
1074 if ($$ == NULL) {
1075 yyerror("Type \"%s\" has not been defined.", $1);
1076 exit(1);
1077 }
Mark Sleee8540632006-05-30 09:24:40 +00001078 }
Mark Slee31985722006-05-24 21:45:31 +00001079 }
1080| BaseType
1081 {
Mark Sleee8540632006-05-30 09:24:40 +00001082 pdebug("FieldType -> BaseType");
1083 $$ = $1;
1084 }
1085| ContainerType
1086 {
1087 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001088 $$ = $1;
1089 }
1090
David Reissc8e30052009-07-27 17:02:42 +00001091BaseType: SimpleBaseType TypeAnnotations
1092 {
1093 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1094 if ($2 != NULL) {
1095 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1096 $$->annotations_ = $2->annotations_;
1097 delete $2;
1098 } else {
1099 $$ = $1;
1100 }
1101 }
1102
1103SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001104 tok_string
1105 {
1106 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001107 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001108 }
Mark Slee8d725a22007-04-13 01:57:12 +00001109| tok_binary
1110 {
1111 pdebug("BaseType -> tok_binary");
1112 $$ = g_type_binary;
1113 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001114| tok_slist
1115 {
1116 pdebug("BaseType -> tok_slist");
1117 $$ = g_type_slist;
1118 }
Mark Slee78f58e22006-09-02 04:17:07 +00001119| tok_bool
1120 {
1121 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001122 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001123 }
Mark Slee31985722006-05-24 21:45:31 +00001124| tok_byte
1125 {
1126 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001127 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001128 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001129| tok_i16
1130 {
1131 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001132 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001133 }
Mark Slee31985722006-05-24 21:45:31 +00001134| tok_i32
1135 {
1136 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001137 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001138 }
Mark Slee31985722006-05-24 21:45:31 +00001139| tok_i64
1140 {
1141 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001142 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001143 }
Mark Sleec98d0502006-09-06 02:42:25 +00001144| tok_double
1145 {
1146 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001147 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001148 }
Mark Slee31985722006-05-24 21:45:31 +00001149
David Reissa2309992008-12-10 01:52:48 +00001150ContainerType: SimpleContainerType TypeAnnotations
1151 {
1152 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1153 $$ = $1;
1154 if ($2 != NULL) {
1155 $$->annotations_ = $2->annotations_;
1156 delete $2;
1157 }
1158 }
1159
1160SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001161 MapType
1162 {
David Reissa2309992008-12-10 01:52:48 +00001163 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001164 $$ = $1;
1165 }
1166| SetType
1167 {
David Reissa2309992008-12-10 01:52:48 +00001168 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001169 $$ = $1;
1170 }
1171| ListType
1172 {
David Reissa2309992008-12-10 01:52:48 +00001173 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001174 $$ = $1;
1175 }
1176
1177MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001178 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001179 {
1180 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001181 $$ = new t_map($4, $6);
1182 if ($2 != NULL) {
1183 ((t_container*)$$)->set_cpp_name(std::string($2));
1184 }
Mark Sleee8540632006-05-30 09:24:40 +00001185 }
1186
1187SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001188 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001189 {
1190 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001191 $$ = new t_set($4);
1192 if ($2 != NULL) {
1193 ((t_container*)$$)->set_cpp_name(std::string($2));
1194 }
Mark Sleee8540632006-05-30 09:24:40 +00001195 }
1196
1197ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001198 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001199 {
1200 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001201 $$ = new t_list($3);
1202 if ($5 != NULL) {
1203 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001204 }
1205 }
1206
Mark Slee36bfa2e2007-01-19 20:09:51 +00001207CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001208 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001209 {
Mark Sleeafc76542007-02-09 21:55:44 +00001210 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001211 }
1212|
1213 {
1214 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001215 }
1216
David Reissa2309992008-12-10 01:52:48 +00001217TypeAnnotations:
1218 '(' TypeAnnotationList ')'
1219 {
1220 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1221 $$ = $2;
1222 }
1223|
1224 {
1225 $$ = NULL;
1226 }
1227
1228TypeAnnotationList:
1229 TypeAnnotationList TypeAnnotation
1230 {
1231 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1232 $$ = $1;
1233 $$->annotations_[$2->key] = $2->val;
1234 delete $2;
1235 }
1236|
1237 {
1238 /* Just use a dummy structure to hold the annotations. */
1239 $$ = new t_struct(g_program);
1240 }
1241
1242TypeAnnotation:
1243 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1244 {
1245 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1246 $$ = new t_annotation;
1247 $$->key = $1;
1248 $$->val = $3;
1249 }
1250
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001251%%