blob: 7c4a90bbc0614b22bb1022704166597757753cd6 [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");
Jens Geyere8379b52014-01-25 00:59:45 +0100246 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status != ALREADY_PROCESSED))
247 {
248 g_program->set_doc(g_program_doctext_candidate);
249 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000250 }
David Reisscbd4bac2007-08-14 17:12:33 +0000251 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000252 }
253
David Reisscbd4bac2007-08-14 17:12:33 +0000254CaptureDocText:
255 {
256 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000257 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000258 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000259 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000260 $$ = NULL;
261 }
262 }
263
264/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
265DestroyDocText:
266 {
267 if (g_parse_mode == PROGRAM) {
268 clear_doctext();
269 }
270 }
271
272/* We have to DestroyDocText here, otherwise it catches the doctext
273 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000274HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000275 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000276 {
277 pdebug("HeaderList -> HeaderList Header");
278 }
279|
280 {
281 pdebug("HeaderList -> ");
282 }
283
284Header:
285 Include
286 {
287 pdebug("Header -> Include");
288 }
David Reiss79eca142008-02-27 01:55:13 +0000289| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000290 {
David Reiss79eca142008-02-27 01:55:13 +0000291 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100292 declare_valid_program_doctext();
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");
Jens Geyere8379b52014-01-25 00:59:45 +0100300 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000301 if (g_parse_mode == PROGRAM) {
302 g_program->set_namespace("*", $3);
303 }
304 }
David Reiss9a08dc62008-02-27 01:55:17 +0000305/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000306| tok_cpp_namespace tok_identifier
307 {
David Reiss9a08dc62008-02-27 01:55:17 +0000308 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000309 pdebug("Header -> tok_cpp_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100310 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000311 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000312 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000313 }
314 }
315| tok_cpp_include tok_literal
316 {
317 pdebug("Header -> tok_cpp_include tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100318 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000319 if (g_parse_mode == PROGRAM) {
320 g_program->add_cpp_include($2);
321 }
322 }
Mark Sleee888b372007-01-12 01:06:24 +0000323| tok_php_namespace tok_identifier
324 {
David Reiss554ea6f2009-02-17 20:28:37 +0000325 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000326 pdebug("Header -> tok_php_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100327 declare_valid_program_doctext();
Mark Sleee888b372007-01-12 01:06:24 +0000328 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000329 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000330 }
331 }
David Reiss320e45c2008-03-27 21:41:54 +0000332/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000333| tok_py_module tok_identifier
334 {
David Reiss320e45c2008-03-27 21:41:54 +0000335 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000336 pdebug("Header -> tok_py_module tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100337 declare_valid_program_doctext();
David Reissc6fc3292007-08-30 00:58:43 +0000338 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000339 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000340 }
341 }
David Reiss07ef3a92008-03-27 21:42:39 +0000342/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000343| tok_perl_package tok_identifier
344 {
David Reiss07ef3a92008-03-27 21:42:39 +0000345 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000346 pdebug("Header -> tok_perl_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100347 declare_valid_program_doctext();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000348 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000349 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000350 }
351 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000352/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000353| tok_ruby_namespace tok_identifier
354 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000355 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000356 pdebug("Header -> tok_ruby_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100357 declare_valid_program_doctext();
Mark Slee58dfb4f2007-07-06 02:45:25 +0000358 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000359 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000360 }
361 }
David Reiss3b455012008-03-27 21:40:46 +0000362/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000363| tok_smalltalk_category tok_st_identifier
364 {
David Reiss3b455012008-03-27 21:40:46 +0000365 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000366 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100367 declare_valid_program_doctext();
Mark Sleebd588222007-11-21 08:43:35 +0000368 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000369 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000370 }
371 }
David Reiss3b455012008-03-27 21:40:46 +0000372/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000373| tok_smalltalk_prefix tok_identifier
374 {
David Reiss3b455012008-03-27 21:40:46 +0000375 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000376 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100377 declare_valid_program_doctext();
David Reiss15457c92007-12-14 07:03:03 +0000378 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000379 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000380 }
381 }
David Reiss771f8c72008-02-27 01:55:25 +0000382/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000383| tok_java_package tok_identifier
384 {
David Reiss9f646152008-03-02 21:59:48 +0000385 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000386 pdebug("Header -> tok_java_package tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100387 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000388 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000389 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000390 }
391 }
David Reiss54b602b2008-03-27 21:41:06 +0000392/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000393| tok_cocoa_prefix tok_identifier
394 {
David Reiss54b602b2008-03-27 21:41:06 +0000395 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000396 pdebug("Header -> tok_cocoa_prefix tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100397 declare_valid_program_doctext();
Mark Slee7e9eea42007-09-10 21:00:23 +0000398 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000399 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000400 }
401 }
David Reiss92e10d82009-02-17 20:28:19 +0000402/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000403| tok_xsd_namespace tok_literal
404 {
David Reiss92e10d82009-02-17 20:28:19 +0000405 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000406 pdebug("Header -> tok_xsd_namespace tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100407 declare_valid_program_doctext();
Mark Slee0d9199e2007-01-31 02:08:30 +0000408 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000409 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000410 }
411 }
David Reiss9d65bf02008-03-27 21:41:37 +0000412/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000413| tok_csharp_namespace tok_identifier
414 {
David Reiss9d65bf02008-03-27 21:41:37 +0000415 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000416 pdebug("Header -> tok_csharp_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100417 declare_valid_program_doctext();
David Reiss7f42bcf2008-01-11 20:59:12 +0000418 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000419 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000420 }
421 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000422/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
423| tok_delphi_namespace tok_identifier
424 {
425 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
426 pdebug("Header -> tok_delphi_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100427 declare_valid_program_doctext();
Jake Farrell7ae13e12011-10-18 14:35:26 +0000428 if (g_parse_mode == PROGRAM) {
429 g_program->set_namespace("delphi", $2);
430 }
431 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000432
433Include:
434 tok_include tok_literal
435 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000436 pdebug("Include -> tok_include tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100437 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000438 if (g_parse_mode == INCLUDES) {
439 std::string path = include_file(std::string($2));
440 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000441 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000442 }
443 }
444 }
Mark Slee31985722006-05-24 21:45:31 +0000445
446DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000447 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000448 {
449 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000450 if ($2 != NULL && $3 != NULL) {
451 $3->set_doc($2);
452 }
Mark Slee31985722006-05-24 21:45:31 +0000453 }
454|
455 {
456 pdebug("DefinitionList -> ");
457 }
458
459Definition:
Mark Slee30152872006-11-28 01:24:07 +0000460 Const
461 {
462 pdebug("Definition -> Const");
463 if (g_parse_mode == PROGRAM) {
464 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000465 }
David Reisscdffe262007-08-14 17:12:31 +0000466 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000467 }
468| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000469 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000470 pdebug("Definition -> TypeDefinition");
471 if (g_parse_mode == PROGRAM) {
472 g_scope->add_type($1->get_name(), $1);
473 if (g_parent_scope != NULL) {
474 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
475 }
Roger Meierba406d32013-07-15 22:41:34 +0200476 if (! g_program->is_unique_typename($1)) {
477 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
478 exit(1);
479 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000480 }
David Reisscdffe262007-08-14 17:12:31 +0000481 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000482 }
Mark Slee31985722006-05-24 21:45:31 +0000483| Service
484 {
485 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000486 if (g_parse_mode == PROGRAM) {
487 g_scope->add_service($1->get_name(), $1);
488 if (g_parent_scope != NULL) {
489 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
490 }
491 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200492 if (! g_program->is_unique_typename($1)) {
493 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
494 exit(1);
495 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000496 }
David Reisscdffe262007-08-14 17:12:31 +0000497 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000498 }
499
Mark Sleef0712dc2006-10-25 19:03:57 +0000500TypeDefinition:
501 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000502 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000503 pdebug("TypeDefinition -> Typedef");
504 if (g_parse_mode == PROGRAM) {
505 g_program->add_typedef($1);
506 }
507 }
508| Enum
509 {
510 pdebug("TypeDefinition -> Enum");
511 if (g_parse_mode == PROGRAM) {
512 g_program->add_enum($1);
513 }
514 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000515| Senum
516 {
517 pdebug("TypeDefinition -> Senum");
518 if (g_parse_mode == PROGRAM) {
519 g_program->add_typedef($1);
520 }
521 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000522| Struct
523 {
524 pdebug("TypeDefinition -> Struct");
525 if (g_parse_mode == PROGRAM) {
526 g_program->add_struct($1);
527 }
528 }
529| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000530 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000531 pdebug("TypeDefinition -> Xception");
532 if (g_parse_mode == PROGRAM) {
533 g_program->add_xception($1);
534 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000535 }
Mark Slee31985722006-05-24 21:45:31 +0000536
537Typedef:
Roger Meier30877382012-09-17 21:18:05 +0000538 tok_typedef FieldType tok_identifier TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000539 {
David Reiss4dd78012010-03-09 05:19:08 +0000540 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200541 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000542 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000543 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000544 if ($4 != NULL) {
545 $$->annotations_ = $4->annotations_;
546 delete $4;
547 }
Mark Slee31985722006-05-24 21:45:31 +0000548 }
549
Mark Slee6a47fed2007-02-07 02:40:59 +0000550CommaOrSemicolonOptional:
551 ','
552 {}
553| ';'
554 {}
555|
556 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000557
Mark Slee31985722006-05-24 21:45:31 +0000558Enum:
Roger Meier30877382012-09-17 21:18:05 +0000559 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000560 {
561 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000562 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200563 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000564 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000565 if ($6 != NULL) {
566 $$->annotations_ = $6->annotations_;
567 delete $6;
568 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000569 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000570 // make constants for all the enum values
571 if (g_parse_mode == PROGRAM) {
572 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
573 std::vector<t_enum_value*>::const_iterator c_iter;
574 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
575 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
576 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
577 const_val->set_enum($$);
578 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
579 if (g_parent_scope != NULL) {
580 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
581 }
582 }
583 }
Mark Slee31985722006-05-24 21:45:31 +0000584 }
585
586EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000587 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000588 {
589 pdebug("EnumDefList -> EnumDefList EnumDef");
590 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000591 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000592 }
593|
594 {
595 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000596 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000597 }
598
599EnumDef:
Roger Meier30877382012-09-17 21:18:05 +0000600 CaptureDocText tok_identifier '=' tok_int_constant TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000601 {
Mark Slee30152872006-11-28 01:24:07 +0000602 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000603 if ($4 < 0) {
604 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000605 }
David Reissf1454162008-06-30 20:45:47 +0000606 if ($4 > INT_MAX) {
607 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
608 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200609 validate_simple_identifier( $2);
Ben Craige9576752013-10-11 08:19:16 -0500610 $$ = new t_enum_value($2, static_cast<int>($4));
ccheeverf53b5cf2007-02-05 20:33:11 +0000611 if ($1 != NULL) {
612 $$->set_doc($1);
613 }
Roger Meier30877382012-09-17 21:18:05 +0000614 if ($5 != NULL) {
615 $$->annotations_ = $5->annotations_;
616 delete $5;
617 }
Mark Slee31985722006-05-24 21:45:31 +0000618 }
619|
Roger Meier30877382012-09-17 21:18:05 +0000620 CaptureDocText tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000621 {
Mark Slee30152872006-11-28 01:24:07 +0000622 pdebug("EnumDef -> tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200623 validate_simple_identifier( $2);
ccheeverf53b5cf2007-02-05 20:33:11 +0000624 $$ = new t_enum_value($2);
625 if ($1 != NULL) {
626 $$->set_doc($1);
627 }
Roger Meier30877382012-09-17 21:18:05 +0000628 if ($3 != NULL) {
629 $$->annotations_ = $3->annotations_;
630 delete $3;
631 }
Mark Slee30152872006-11-28 01:24:07 +0000632 }
633
Mark Slee6a47fed2007-02-07 02:40:59 +0000634Senum:
Roger Meier30877382012-09-17 21:18:05 +0000635 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000636 {
637 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200638 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000639 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000640 if ($6 != NULL) {
641 $$->annotations_ = $6->annotations_;
642 delete $6;
643 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000644 }
645
646SenumDefList:
647 SenumDefList SenumDef
648 {
649 pdebug("SenumDefList -> SenumDefList SenumDef");
650 $$ = $1;
651 $$->add_string_enum_val($2);
652 }
653|
654 {
655 pdebug("SenumDefList -> ");
656 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
657 $$->set_string_enum(true);
658 }
659
660SenumDef:
661 tok_literal CommaOrSemicolonOptional
662 {
663 pdebug("SenumDef -> tok_literal");
664 $$ = $1;
665 }
666
Mark Slee30152872006-11-28 01:24:07 +0000667Const:
668 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
669 {
670 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000671 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200672 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000673 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000674 $$ = new t_const($2, $3, $5);
675 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000676
677 g_scope->add_constant($3, $$);
678 if (g_parent_scope != NULL) {
679 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
680 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000681 } else {
682 $$ = NULL;
683 }
Mark Slee30152872006-11-28 01:24:07 +0000684 }
685
686ConstValue:
687 tok_int_constant
688 {
689 pdebug("ConstValue => tok_int_constant");
690 $$ = new t_const_value();
691 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000692 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100693 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000694 }
Mark Slee30152872006-11-28 01:24:07 +0000695 }
696| tok_dub_constant
697 {
698 pdebug("ConstValue => tok_dub_constant");
699 $$ = new t_const_value();
700 $$->set_double($1);
701 }
702| tok_literal
703 {
704 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000705 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000706 }
Mark Slee67fc6342006-11-29 03:37:04 +0000707| tok_identifier
708 {
709 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000710 $$ = new t_const_value();
711 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000712 }
Mark Slee30152872006-11-28 01:24:07 +0000713| ConstList
714 {
715 pdebug("ConstValue => ConstList");
716 $$ = $1;
717 }
718| ConstMap
719 {
720 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000721 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000722 }
723
724ConstList:
725 '[' ConstListContents ']'
726 {
727 pdebug("ConstList => [ ConstListContents ]");
728 $$ = $2;
729 }
730
731ConstListContents:
732 ConstListContents ConstValue CommaOrSemicolonOptional
733 {
734 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
735 $$ = $1;
736 $$->add_list($2);
737 }
738|
739 {
740 pdebug("ConstListContents =>");
741 $$ = new t_const_value();
742 $$->set_list();
743 }
744
745ConstMap:
746 '{' ConstMapContents '}'
747 {
748 pdebug("ConstMap => { ConstMapContents }");
749 $$ = $2;
750 }
751
752ConstMapContents:
753 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
754 {
755 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
756 $$ = $1;
757 $$->add_map($2, $4);
758 }
759|
760 {
761 pdebug("ConstMapContents =>");
762 $$ = new t_const_value();
763 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000764 }
765
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000766StructHead:
767 tok_struct
768 {
769 $$ = struct_is_struct;
770 }
771| tok_union
772 {
773 $$ = struct_is_union;
774 }
775
Mark Slee31985722006-05-24 21:45:31 +0000776Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000777 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000778 {
779 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200780 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000781 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000782 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000783 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000784 $$->set_name($2);
785 if ($7 != NULL) {
786 $$->annotations_ = $7->annotations_;
787 delete $7;
788 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000789 }
Ben Craige9576752013-10-11 08:19:16 -0500790
Mark Slee36bfa2e2007-01-19 20:09:51 +0000791XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000792 tok_xsd_all
793 {
794 $$ = true;
795 }
796|
797 {
798 $$ = false;
799 }
800
Mark Slee36bfa2e2007-01-19 20:09:51 +0000801XsdOptional:
802 tok_xsd_optional
803 {
804 $$ = true;
805 }
806|
807 {
808 $$ = false;
809 }
810
Mark Slee7df0e2a2007-02-06 21:03:18 +0000811XsdNillable:
812 tok_xsd_nillable
813 {
814 $$ = true;
815 }
816|
817 {
818 $$ = false;
819 }
820
Mark Slee21135c32007-02-05 21:52:08 +0000821XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000822 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000823 {
Mark Slee748d83f2007-02-07 01:20:08 +0000824 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000825 }
826|
827 {
828 $$ = NULL;
829 }
830
Mark Slee9cb7c612006-09-01 22:17:45 +0000831Xception:
Roger Meier30877382012-09-17 21:18:05 +0000832 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000833 {
834 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200835 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000836 $4->set_name($2);
837 $4->set_xception(true);
838 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000839 if ($6 != NULL) {
840 $$->annotations_ = $6->annotations_;
841 delete $6;
842 }
Mark Slee31985722006-05-24 21:45:31 +0000843 }
844
845Service:
Roger Meier30877382012-09-17 21:18:05 +0000846 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000847 {
848 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200849 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000850 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000851 $$->set_name($2);
852 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000853 if ($9 != NULL) {
854 $$->annotations_ = $9->annotations_;
855 delete $9;
856 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000857 }
858
Mark Slee78165722007-09-10 22:08:49 +0000859FlagArgs:
860 {
861 g_arglist = 1;
862 }
863
864UnflagArgs:
865 {
866 g_arglist = 0;
867 }
868
Mark Slee36bfa2e2007-01-19 20:09:51 +0000869Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000870 tok_extends tok_identifier
871 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000872 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000873 $$ = NULL;
874 if (g_parse_mode == PROGRAM) {
875 $$ = g_scope->get_service($2);
876 if ($$ == NULL) {
877 yyerror("Service \"%s\" has not been defined.", $2);
878 exit(1);
879 }
880 }
881 }
882|
883 {
884 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000885 }
886
887FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000888 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000889 {
890 pdebug("FunctionList -> FunctionList Function");
891 $$ = $1;
892 $1->add_function($2);
893 }
894|
895 {
896 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000897 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000898 }
899
900Function:
Roger Meier30877382012-09-17 21:18:05 +0000901 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000902 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200903 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000904 $6->set_name(std::string($4) + "_args");
905 $$ = new t_function($3, $4, $6, $8, $2);
906 if ($1 != NULL) {
907 $$->set_doc($1);
908 }
Roger Meier30877382012-09-17 21:18:05 +0000909 if ($9 != NULL) {
910 $$->annotations_ = $9->annotations_;
911 delete $9;
912 }
Mark Slee31985722006-05-24 21:45:31 +0000913 }
914
David Reiss6985a422009-03-24 20:00:47 +0000915Oneway:
916 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000917 {
Mark Slee52f643d2006-08-09 00:03:43 +0000918 $$ = true;
919 }
920|
921 {
922 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000923 }
924
Mark Slee36bfa2e2007-01-19 20:09:51 +0000925Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000926 tok_throws '(' FieldList ')'
927 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000928 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000929 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000930 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000931 yyerror("Throws clause may not contain non-exception types");
932 exit(1);
933 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000934 }
935|
936 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000937 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000938 }
939
Mark Slee31985722006-05-24 21:45:31 +0000940FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000941 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000942 {
943 pdebug("FieldList -> FieldList , Field");
944 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000945 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100946 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 +0000947 exit(1);
948 }
Mark Slee31985722006-05-24 21:45:31 +0000949 }
950|
951 {
952 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000953 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000954 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000955 }
956
957Field:
David Reiss53c10e02010-03-05 07:51:51 +0000958 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000959 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000960 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000961 if ($2.auto_assigned) {
David Reissbb461362009-04-02 19:23:59 +0000962 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 +0000963 if (g_strict >= 192) {
964 yyerror("Implicit field keys are deprecated and not allowed with -strict");
965 exit(1);
966 }
Mark Slee31985722006-05-24 21:45:31 +0000967 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200968 validate_simple_identifier($5);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000969 $$ = new t_field($4, $5, $2.value);
David Reiss8320a922007-08-14 19:59:26 +0000970 $$->set_req($3);
971 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000972 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000973 validate_field_value($$, $6);
974 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000975 }
David Reiss8320a922007-08-14 19:59:26 +0000976 $$->set_xsd_optional($7);
977 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000978 if ($1 != NULL) {
979 $$->set_doc($1);
980 }
David Reiss8320a922007-08-14 19:59:26 +0000981 if ($9 != NULL) {
982 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000983 }
David Reiss53c10e02010-03-05 07:51:51 +0000984 if ($10 != NULL) {
985 $$->annotations_ = $10->annotations_;
986 delete $10;
987 }
Mark Slee31985722006-05-24 21:45:31 +0000988 }
Mark Slee7ff32452007-02-01 05:26:18 +0000989
990FieldIdentifier:
991 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000992 {
Mark Slee7ff32452007-02-01 05:26:18 +0000993 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000994 if (g_allow_neg_field_keys) {
995 /*
996 * g_allow_neg_field_keys exists to allow users to add explicitly
997 * specified key values to old .thrift files without breaking
998 * protocol compatibility.
999 */
1000 if ($1 != y_field_val) {
1001 /*
1002 * warn if the user-specified negative value isn't what
1003 * thrift would have auto-assigned.
1004 */
Roger Meier5f2d34e2013-11-16 16:43:41 +01001005 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001006 "auto-assigned by thrift (%d).\n", $1, y_field_val);
1007 }
1008 /*
1009 * Leave $1 as-is, and update y_field_val to be one less than $1.
1010 * The FieldList parsing will catch any duplicate key values.
1011 */
Ben Craige9576752013-10-11 08:19:16 -05001012 y_field_val = static_cast<int32_t>($1 - 1);
1013 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001014 $$.auto_assigned = false;
1015 } else {
Ben Craige9576752013-10-11 08:19:16 -05001016 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001017 $1);
1018 $$.value = y_field_val--;
1019 $$.auto_assigned = true;
1020 }
1021 } else {
Ben Craige9576752013-10-11 08:19:16 -05001022 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001023 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001024 }
Mark Slee7ff32452007-02-01 05:26:18 +00001025 }
1026|
1027 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001028 $$.value = y_field_val--;
1029 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +00001030 }
1031
David Reiss8320a922007-08-14 19:59:26 +00001032FieldRequiredness:
1033 tok_required
1034 {
David Reiss45603e92009-09-02 22:15:55 +00001035 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001036 }
1037| tok_optional
1038 {
Mark Slee78165722007-09-10 22:08:49 +00001039 if (g_arglist) {
1040 if (g_parse_mode == PROGRAM) {
1041 pwarning(1, "optional keyword is ignored in argument lists.\n");
1042 }
David Reiss204420f2008-01-11 20:59:03 +00001043 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001044 } else {
David Reiss204420f2008-01-11 20:59:03 +00001045 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001046 }
David Reiss8320a922007-08-14 19:59:26 +00001047 }
1048|
1049 {
David Reiss204420f2008-01-11 20:59:03 +00001050 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001051 }
1052
Mark Slee7ff32452007-02-01 05:26:18 +00001053FieldValue:
1054 '=' ConstValue
1055 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001056 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001057 $$ = $2;
1058 } else {
1059 $$ = NULL;
1060 }
1061 }
1062|
1063 {
1064 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001065 }
Mark Slee31985722006-05-24 21:45:31 +00001066
Mark Slee31985722006-05-24 21:45:31 +00001067FunctionType:
1068 FieldType
1069 {
Mark Sleee8540632006-05-30 09:24:40 +00001070 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001071 $$ = $1;
1072 }
1073| tok_void
1074 {
Mark Sleee8540632006-05-30 09:24:40 +00001075 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001076 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001077 }
1078
1079FieldType:
1080 tok_identifier
1081 {
Mark Sleee8540632006-05-30 09:24:40 +00001082 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001083 if (g_parse_mode == INCLUDES) {
1084 // Ignore identifiers in include mode
1085 $$ = NULL;
1086 } else {
1087 // Lookup the identifier in the current scope
1088 $$ = g_scope->get_type($1);
1089 if ($$ == NULL) {
1090 yyerror("Type \"%s\" has not been defined.", $1);
1091 exit(1);
1092 }
Mark Sleee8540632006-05-30 09:24:40 +00001093 }
Mark Slee31985722006-05-24 21:45:31 +00001094 }
1095| BaseType
1096 {
Mark Sleee8540632006-05-30 09:24:40 +00001097 pdebug("FieldType -> BaseType");
1098 $$ = $1;
1099 }
1100| ContainerType
1101 {
1102 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001103 $$ = $1;
1104 }
1105
David Reissc8e30052009-07-27 17:02:42 +00001106BaseType: SimpleBaseType TypeAnnotations
1107 {
1108 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1109 if ($2 != NULL) {
1110 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1111 $$->annotations_ = $2->annotations_;
1112 delete $2;
1113 } else {
1114 $$ = $1;
1115 }
1116 }
1117
1118SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001119 tok_string
1120 {
1121 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001122 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001123 }
Mark Slee8d725a22007-04-13 01:57:12 +00001124| tok_binary
1125 {
1126 pdebug("BaseType -> tok_binary");
1127 $$ = g_type_binary;
1128 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001129| tok_slist
1130 {
1131 pdebug("BaseType -> tok_slist");
1132 $$ = g_type_slist;
1133 }
Mark Slee78f58e22006-09-02 04:17:07 +00001134| tok_bool
1135 {
1136 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001137 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001138 }
Mark Slee31985722006-05-24 21:45:31 +00001139| tok_byte
1140 {
1141 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001142 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001143 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001144| tok_i16
1145 {
1146 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001147 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001148 }
Mark Slee31985722006-05-24 21:45:31 +00001149| tok_i32
1150 {
1151 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001152 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001153 }
Mark Slee31985722006-05-24 21:45:31 +00001154| tok_i64
1155 {
1156 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001157 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001158 }
Mark Sleec98d0502006-09-06 02:42:25 +00001159| tok_double
1160 {
1161 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001162 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001163 }
Mark Slee31985722006-05-24 21:45:31 +00001164
David Reissa2309992008-12-10 01:52:48 +00001165ContainerType: SimpleContainerType TypeAnnotations
1166 {
1167 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1168 $$ = $1;
1169 if ($2 != NULL) {
1170 $$->annotations_ = $2->annotations_;
1171 delete $2;
1172 }
1173 }
1174
1175SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001176 MapType
1177 {
David Reissa2309992008-12-10 01:52:48 +00001178 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001179 $$ = $1;
1180 }
1181| SetType
1182 {
David Reissa2309992008-12-10 01:52:48 +00001183 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001184 $$ = $1;
1185 }
1186| ListType
1187 {
David Reissa2309992008-12-10 01:52:48 +00001188 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001189 $$ = $1;
1190 }
1191
1192MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001193 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001194 {
1195 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001196 $$ = new t_map($4, $6);
1197 if ($2 != NULL) {
1198 ((t_container*)$$)->set_cpp_name(std::string($2));
1199 }
Mark Sleee8540632006-05-30 09:24:40 +00001200 }
1201
1202SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001203 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001204 {
1205 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001206 $$ = new t_set($4);
1207 if ($2 != NULL) {
1208 ((t_container*)$$)->set_cpp_name(std::string($2));
1209 }
Mark Sleee8540632006-05-30 09:24:40 +00001210 }
1211
1212ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001213 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001214 {
1215 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer6fe77e82014-03-16 16:48:53 +02001216 check_for_list_of_bytes($3);
Mark Sleef0712dc2006-10-25 19:03:57 +00001217 $$ = new t_list($3);
1218 if ($5 != NULL) {
1219 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001220 }
1221 }
1222
Mark Slee36bfa2e2007-01-19 20:09:51 +00001223CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001224 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001225 {
Mark Sleeafc76542007-02-09 21:55:44 +00001226 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001227 }
1228|
1229 {
1230 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001231 }
1232
David Reissa2309992008-12-10 01:52:48 +00001233TypeAnnotations:
1234 '(' TypeAnnotationList ')'
1235 {
1236 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1237 $$ = $2;
1238 }
1239|
1240 {
1241 $$ = NULL;
1242 }
1243
1244TypeAnnotationList:
1245 TypeAnnotationList TypeAnnotation
1246 {
1247 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1248 $$ = $1;
1249 $$->annotations_[$2->key] = $2->val;
1250 delete $2;
1251 }
1252|
1253 {
1254 /* Just use a dummy structure to hold the annotations. */
1255 $$ = new t_struct(g_program);
1256 }
1257
1258TypeAnnotation:
1259 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1260 {
1261 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1262 $$ = new t_annotation;
1263 $$->key = $1;
1264 $$->val = $3;
1265 }
1266
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001267%%