blob: 62e13badd92e36f2d0f6070e29093350b979f056 [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
Jens Geyer885c6792014-05-02 21:31:55 +0200168%token tok_reference
Mark Slee31985722006-05-24 21:45:31 +0000169
Mark Sleef5377b32006-10-10 01:42:59 +0000170/**
171 * Grammar nodes
172 */
173
Mark Slee31985722006-05-24 21:45:31 +0000174%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000175%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000176%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000177%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000178%type<ttype> MapType
179%type<ttype> SetType
180%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000181
David Reisscdffe262007-08-14 17:12:31 +0000182%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000183%type<ttype> TypeDefinition
184
Mark Slee31985722006-05-24 21:45:31 +0000185%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000186
David Reissa2309992008-12-10 01:52:48 +0000187%type<ttype> TypeAnnotations
188%type<ttype> TypeAnnotationList
189%type<tannot> TypeAnnotation
190
Mark Slee31985722006-05-24 21:45:31 +0000191%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000192%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000193%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000194%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000195%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000196%type<tstruct> FieldList
Jens Geyer885c6792014-05-02 21:31:55 +0200197%type<tbool> FieldReference
Mark Slee31985722006-05-24 21:45:31 +0000198
199%type<tenum> Enum
200%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000201%type<tenumv> EnumDef
202
Mark Slee6a47fed2007-02-07 02:40:59 +0000203%type<ttypedef> Senum
204%type<tbase> SenumDefList
205%type<id> SenumDef
206
Mark Slee30152872006-11-28 01:24:07 +0000207%type<tconst> Const
208%type<tconstv> ConstValue
209%type<tconstv> ConstList
210%type<tconstv> ConstListContents
211%type<tconstv> ConstMap
212%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000213
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000214%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000215%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000216%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000217%type<tservice> Service
218
219%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000220%type<ttype> FunctionType
221%type<tservice> FunctionList
222
Mark Slee36bfa2e2007-01-19 20:09:51 +0000223%type<tstruct> Throws
224%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000225%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000226%type<tbool> XsdAll
227%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000228%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000229%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000230%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000231
David Reisscbd4bac2007-08-14 17:12:33 +0000232%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000233
Mark Slee31985722006-05-24 21:45:31 +0000234%%
235
Mark Sleef5377b32006-10-10 01:42:59 +0000236/**
237 * Thrift Grammar Implementation.
238 *
239 * For the most part this source file works its way top down from what you
240 * might expect to find in a typical .thrift file, i.e. type definitions and
241 * namespaces up top followed by service definitions using those types.
242 */
Mark Slee31985722006-05-24 21:45:31 +0000243
244Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000245 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000246 {
247 pdebug("Program -> Headers DefinitionList");
Jens Geyere8379b52014-01-25 00:59:45 +0100248 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status != ALREADY_PROCESSED))
249 {
250 g_program->set_doc(g_program_doctext_candidate);
251 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000252 }
David Reisscbd4bac2007-08-14 17:12:33 +0000253 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000254 }
255
David Reisscbd4bac2007-08-14 17:12:33 +0000256CaptureDocText:
257 {
258 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000259 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000260 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000261 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000262 $$ = NULL;
263 }
264 }
265
266/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
267DestroyDocText:
268 {
269 if (g_parse_mode == PROGRAM) {
270 clear_doctext();
271 }
272 }
273
274/* We have to DestroyDocText here, otherwise it catches the doctext
275 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000276HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000277 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000278 {
279 pdebug("HeaderList -> HeaderList Header");
280 }
281|
282 {
283 pdebug("HeaderList -> ");
284 }
285
286Header:
287 Include
288 {
289 pdebug("Header -> Include");
290 }
David Reiss79eca142008-02-27 01:55:13 +0000291| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000292 {
David Reiss79eca142008-02-27 01:55:13 +0000293 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100294 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000295 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000296 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000297 }
298 }
David Reissfb790d72010-09-02 16:41:45 +0000299| tok_namespace '*' tok_identifier
300 {
301 pdebug("Header -> tok_namespace * tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100302 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000303 if (g_parse_mode == PROGRAM) {
304 g_program->set_namespace("*", $3);
305 }
306 }
David Reiss9a08dc62008-02-27 01:55:17 +0000307/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000308| tok_cpp_namespace tok_identifier
309 {
David Reiss9a08dc62008-02-27 01:55:17 +0000310 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000311 pdebug("Header -> tok_cpp_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100312 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000313 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000314 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000315 }
316 }
317| tok_cpp_include tok_literal
318 {
319 pdebug("Header -> tok_cpp_include tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100320 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000321 if (g_parse_mode == PROGRAM) {
322 g_program->add_cpp_include($2);
323 }
324 }
Mark Sleee888b372007-01-12 01:06:24 +0000325| tok_php_namespace tok_identifier
326 {
David Reiss554ea6f2009-02-17 20:28:37 +0000327 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000328 pdebug("Header -> tok_php_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100329 declare_valid_program_doctext();
Mark Sleee888b372007-01-12 01:06:24 +0000330 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000331 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000332 }
333 }
David Reiss320e45c2008-03-27 21:41:54 +0000334/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000335| tok_py_module tok_identifier
336 {
David Reiss320e45c2008-03-27 21:41:54 +0000337 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000338 pdebug("Header -> tok_py_module tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100339 declare_valid_program_doctext();
David Reissc6fc3292007-08-30 00:58:43 +0000340 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000341 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000342 }
343 }
David Reiss07ef3a92008-03-27 21:42:39 +0000344/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000345| tok_perl_package tok_identifier
346 {
David Reiss07ef3a92008-03-27 21:42:39 +0000347 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000348 pdebug("Header -> tok_perl_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100349 declare_valid_program_doctext();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000350 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000351 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000352 }
353 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000354/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000355| tok_ruby_namespace tok_identifier
356 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000357 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000358 pdebug("Header -> tok_ruby_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100359 declare_valid_program_doctext();
Mark Slee58dfb4f2007-07-06 02:45:25 +0000360 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000361 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000362 }
363 }
David Reiss3b455012008-03-27 21:40:46 +0000364/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000365| tok_smalltalk_category tok_st_identifier
366 {
David Reiss3b455012008-03-27 21:40:46 +0000367 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000368 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100369 declare_valid_program_doctext();
Mark Sleebd588222007-11-21 08:43:35 +0000370 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000371 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000372 }
373 }
David Reiss3b455012008-03-27 21:40:46 +0000374/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000375| tok_smalltalk_prefix tok_identifier
376 {
David Reiss3b455012008-03-27 21:40:46 +0000377 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000378 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100379 declare_valid_program_doctext();
David Reiss15457c92007-12-14 07:03:03 +0000380 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000381 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000382 }
383 }
David Reiss771f8c72008-02-27 01:55:25 +0000384/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000385| tok_java_package tok_identifier
386 {
David Reiss9f646152008-03-02 21:59:48 +0000387 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000388 pdebug("Header -> tok_java_package tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100389 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000390 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000391 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000392 }
393 }
David Reiss54b602b2008-03-27 21:41:06 +0000394/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000395| tok_cocoa_prefix tok_identifier
396 {
David Reiss54b602b2008-03-27 21:41:06 +0000397 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000398 pdebug("Header -> tok_cocoa_prefix tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100399 declare_valid_program_doctext();
Mark Slee7e9eea42007-09-10 21:00:23 +0000400 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000401 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000402 }
403 }
David Reiss92e10d82009-02-17 20:28:19 +0000404/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000405| tok_xsd_namespace tok_literal
406 {
David Reiss92e10d82009-02-17 20:28:19 +0000407 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000408 pdebug("Header -> tok_xsd_namespace tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100409 declare_valid_program_doctext();
Mark Slee0d9199e2007-01-31 02:08:30 +0000410 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000411 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000412 }
413 }
David Reiss9d65bf02008-03-27 21:41:37 +0000414/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000415| tok_csharp_namespace tok_identifier
416 {
David Reiss9d65bf02008-03-27 21:41:37 +0000417 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000418 pdebug("Header -> tok_csharp_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100419 declare_valid_program_doctext();
David Reiss7f42bcf2008-01-11 20:59:12 +0000420 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000421 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000422 }
423 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000424/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
425| tok_delphi_namespace tok_identifier
426 {
427 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
428 pdebug("Header -> tok_delphi_namespace tok_identifier");
Jens Geyere8379b52014-01-25 00:59:45 +0100429 declare_valid_program_doctext();
Jake Farrell7ae13e12011-10-18 14:35:26 +0000430 if (g_parse_mode == PROGRAM) {
431 g_program->set_namespace("delphi", $2);
432 }
433 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000434
435Include:
436 tok_include tok_literal
437 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000438 pdebug("Include -> tok_include tok_literal");
Jens Geyere8379b52014-01-25 00:59:45 +0100439 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000440 if (g_parse_mode == INCLUDES) {
441 std::string path = include_file(std::string($2));
442 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000443 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000444 }
445 }
446 }
Mark Slee31985722006-05-24 21:45:31 +0000447
448DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000449 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000450 {
451 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000452 if ($2 != NULL && $3 != NULL) {
453 $3->set_doc($2);
454 }
Mark Slee31985722006-05-24 21:45:31 +0000455 }
456|
457 {
458 pdebug("DefinitionList -> ");
459 }
460
461Definition:
Mark Slee30152872006-11-28 01:24:07 +0000462 Const
463 {
464 pdebug("Definition -> Const");
465 if (g_parse_mode == PROGRAM) {
466 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000467 }
David Reisscdffe262007-08-14 17:12:31 +0000468 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000469 }
470| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000471 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000472 pdebug("Definition -> TypeDefinition");
473 if (g_parse_mode == PROGRAM) {
474 g_scope->add_type($1->get_name(), $1);
475 if (g_parent_scope != NULL) {
476 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
477 }
Roger Meierba406d32013-07-15 22:41:34 +0200478 if (! g_program->is_unique_typename($1)) {
479 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
480 exit(1);
481 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000482 }
David Reisscdffe262007-08-14 17:12:31 +0000483 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000484 }
Mark Slee31985722006-05-24 21:45:31 +0000485| Service
486 {
487 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000488 if (g_parse_mode == PROGRAM) {
489 g_scope->add_service($1->get_name(), $1);
490 if (g_parent_scope != NULL) {
491 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
492 }
493 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200494 if (! g_program->is_unique_typename($1)) {
495 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
496 exit(1);
497 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000498 }
David Reisscdffe262007-08-14 17:12:31 +0000499 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000500 }
501
Mark Sleef0712dc2006-10-25 19:03:57 +0000502TypeDefinition:
503 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000504 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000505 pdebug("TypeDefinition -> Typedef");
506 if (g_parse_mode == PROGRAM) {
507 g_program->add_typedef($1);
508 }
509 }
510| Enum
511 {
512 pdebug("TypeDefinition -> Enum");
513 if (g_parse_mode == PROGRAM) {
514 g_program->add_enum($1);
515 }
516 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000517| Senum
518 {
519 pdebug("TypeDefinition -> Senum");
520 if (g_parse_mode == PROGRAM) {
521 g_program->add_typedef($1);
522 }
523 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000524| Struct
525 {
526 pdebug("TypeDefinition -> Struct");
527 if (g_parse_mode == PROGRAM) {
528 g_program->add_struct($1);
529 }
530 }
531| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000532 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000533 pdebug("TypeDefinition -> Xception");
534 if (g_parse_mode == PROGRAM) {
535 g_program->add_xception($1);
536 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000537 }
Mark Slee31985722006-05-24 21:45:31 +0000538
539Typedef:
Roger Meier30877382012-09-17 21:18:05 +0000540 tok_typedef FieldType tok_identifier TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000541 {
David Reiss4dd78012010-03-09 05:19:08 +0000542 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200543 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000544 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000545 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000546 if ($4 != NULL) {
547 $$->annotations_ = $4->annotations_;
548 delete $4;
549 }
Mark Slee31985722006-05-24 21:45:31 +0000550 }
551
Mark Slee6a47fed2007-02-07 02:40:59 +0000552CommaOrSemicolonOptional:
553 ','
554 {}
555| ';'
556 {}
557|
558 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000559
Mark Slee31985722006-05-24 21:45:31 +0000560Enum:
Roger Meier30877382012-09-17 21:18:05 +0000561 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000562 {
563 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000564 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200565 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000566 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000567 if ($6 != NULL) {
568 $$->annotations_ = $6->annotations_;
569 delete $6;
570 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000571 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000572 // make constants for all the enum values
573 if (g_parse_mode == PROGRAM) {
574 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
575 std::vector<t_enum_value*>::const_iterator c_iter;
576 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
577 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
578 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
579 const_val->set_enum($$);
580 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
581 if (g_parent_scope != NULL) {
582 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
583 }
584 }
585 }
Mark Slee31985722006-05-24 21:45:31 +0000586 }
587
588EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000589 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000590 {
591 pdebug("EnumDefList -> EnumDefList EnumDef");
592 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000593 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000594 }
595|
596 {
597 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000598 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000599 }
600
601EnumDef:
Roger Meier30877382012-09-17 21:18:05 +0000602 CaptureDocText tok_identifier '=' tok_int_constant TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000603 {
Mark Slee30152872006-11-28 01:24:07 +0000604 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000605 if ($4 < 0) {
606 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000607 }
David Reissf1454162008-06-30 20:45:47 +0000608 if ($4 > INT_MAX) {
609 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
610 }
Jens Geyer12c09f42013-08-25 14:16:27 +0200611 validate_simple_identifier( $2);
Ben Craige9576752013-10-11 08:19:16 -0500612 $$ = new t_enum_value($2, static_cast<int>($4));
ccheeverf53b5cf2007-02-05 20:33:11 +0000613 if ($1 != NULL) {
614 $$->set_doc($1);
615 }
Roger Meier30877382012-09-17 21:18:05 +0000616 if ($5 != NULL) {
617 $$->annotations_ = $5->annotations_;
618 delete $5;
619 }
Mark Slee31985722006-05-24 21:45:31 +0000620 }
621|
Roger Meier30877382012-09-17 21:18:05 +0000622 CaptureDocText tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000623 {
Mark Slee30152872006-11-28 01:24:07 +0000624 pdebug("EnumDef -> tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200625 validate_simple_identifier( $2);
ccheeverf53b5cf2007-02-05 20:33:11 +0000626 $$ = new t_enum_value($2);
627 if ($1 != NULL) {
628 $$->set_doc($1);
629 }
Roger Meier30877382012-09-17 21:18:05 +0000630 if ($3 != NULL) {
631 $$->annotations_ = $3->annotations_;
632 delete $3;
633 }
Mark Slee30152872006-11-28 01:24:07 +0000634 }
635
Mark Slee6a47fed2007-02-07 02:40:59 +0000636Senum:
Roger Meier30877382012-09-17 21:18:05 +0000637 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000638 {
639 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200640 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000641 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000642 if ($6 != NULL) {
643 $$->annotations_ = $6->annotations_;
644 delete $6;
645 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000646 }
647
648SenumDefList:
649 SenumDefList SenumDef
650 {
651 pdebug("SenumDefList -> SenumDefList SenumDef");
652 $$ = $1;
653 $$->add_string_enum_val($2);
654 }
655|
656 {
657 pdebug("SenumDefList -> ");
658 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
659 $$->set_string_enum(true);
660 }
661
662SenumDef:
663 tok_literal CommaOrSemicolonOptional
664 {
665 pdebug("SenumDef -> tok_literal");
666 $$ = $1;
667 }
668
Mark Slee30152872006-11-28 01:24:07 +0000669Const:
670 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
671 {
672 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000673 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200674 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000675 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000676 $$ = new t_const($2, $3, $5);
677 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000678
679 g_scope->add_constant($3, $$);
680 if (g_parent_scope != NULL) {
681 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
682 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000683 } else {
684 $$ = NULL;
685 }
Mark Slee30152872006-11-28 01:24:07 +0000686 }
687
688ConstValue:
689 tok_int_constant
690 {
691 pdebug("ConstValue => tok_int_constant");
692 $$ = new t_const_value();
693 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000694 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100695 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000696 }
Mark Slee30152872006-11-28 01:24:07 +0000697 }
698| tok_dub_constant
699 {
700 pdebug("ConstValue => tok_dub_constant");
701 $$ = new t_const_value();
702 $$->set_double($1);
703 }
704| tok_literal
705 {
706 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000707 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000708 }
Mark Slee67fc6342006-11-29 03:37:04 +0000709| tok_identifier
710 {
711 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000712 $$ = new t_const_value();
713 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000714 }
Mark Slee30152872006-11-28 01:24:07 +0000715| ConstList
716 {
717 pdebug("ConstValue => ConstList");
718 $$ = $1;
719 }
720| ConstMap
721 {
722 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000723 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000724 }
725
726ConstList:
727 '[' ConstListContents ']'
728 {
729 pdebug("ConstList => [ ConstListContents ]");
730 $$ = $2;
731 }
732
733ConstListContents:
734 ConstListContents ConstValue CommaOrSemicolonOptional
735 {
736 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
737 $$ = $1;
738 $$->add_list($2);
739 }
740|
741 {
742 pdebug("ConstListContents =>");
743 $$ = new t_const_value();
744 $$->set_list();
745 }
746
747ConstMap:
748 '{' ConstMapContents '}'
749 {
750 pdebug("ConstMap => { ConstMapContents }");
751 $$ = $2;
752 }
753
754ConstMapContents:
755 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
756 {
757 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
758 $$ = $1;
759 $$->add_map($2, $4);
760 }
761|
762 {
763 pdebug("ConstMapContents =>");
764 $$ = new t_const_value();
765 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000766 }
767
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000768StructHead:
769 tok_struct
770 {
771 $$ = struct_is_struct;
772 }
773| tok_union
774 {
775 $$ = struct_is_union;
776 }
777
Mark Slee31985722006-05-24 21:45:31 +0000778Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000779 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000780 {
781 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200782 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000783 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000784 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000785 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000786 $$->set_name($2);
787 if ($7 != NULL) {
788 $$->annotations_ = $7->annotations_;
789 delete $7;
790 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000791 }
Ben Craige9576752013-10-11 08:19:16 -0500792
Mark Slee36bfa2e2007-01-19 20:09:51 +0000793XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000794 tok_xsd_all
795 {
796 $$ = true;
797 }
798|
799 {
800 $$ = false;
801 }
802
Mark Slee36bfa2e2007-01-19 20:09:51 +0000803XsdOptional:
804 tok_xsd_optional
805 {
806 $$ = true;
807 }
808|
809 {
810 $$ = false;
811 }
812
Mark Slee7df0e2a2007-02-06 21:03:18 +0000813XsdNillable:
814 tok_xsd_nillable
815 {
816 $$ = true;
817 }
818|
819 {
820 $$ = false;
821 }
822
Mark Slee21135c32007-02-05 21:52:08 +0000823XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000824 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000825 {
Mark Slee748d83f2007-02-07 01:20:08 +0000826 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000827 }
828|
829 {
830 $$ = NULL;
831 }
832
Mark Slee9cb7c612006-09-01 22:17:45 +0000833Xception:
Roger Meier30877382012-09-17 21:18:05 +0000834 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000835 {
836 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200837 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000838 $4->set_name($2);
839 $4->set_xception(true);
840 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000841 if ($6 != NULL) {
842 $$->annotations_ = $6->annotations_;
843 delete $6;
844 }
Mark Slee31985722006-05-24 21:45:31 +0000845 }
846
847Service:
Roger Meier30877382012-09-17 21:18:05 +0000848 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000849 {
850 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200851 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000852 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000853 $$->set_name($2);
854 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000855 if ($9 != NULL) {
856 $$->annotations_ = $9->annotations_;
857 delete $9;
858 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000859 }
860
Mark Slee78165722007-09-10 22:08:49 +0000861FlagArgs:
862 {
863 g_arglist = 1;
864 }
865
866UnflagArgs:
867 {
868 g_arglist = 0;
869 }
870
Mark Slee36bfa2e2007-01-19 20:09:51 +0000871Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 tok_extends tok_identifier
873 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000874 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000875 $$ = NULL;
876 if (g_parse_mode == PROGRAM) {
877 $$ = g_scope->get_service($2);
878 if ($$ == NULL) {
879 yyerror("Service \"%s\" has not been defined.", $2);
880 exit(1);
881 }
882 }
883 }
884|
885 {
886 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000887 }
888
889FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000890 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000891 {
892 pdebug("FunctionList -> FunctionList Function");
893 $$ = $1;
894 $1->add_function($2);
895 }
896|
897 {
898 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000899 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000900 }
901
902Function:
Roger Meier30877382012-09-17 21:18:05 +0000903 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000904 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200905 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000906 $6->set_name(std::string($4) + "_args");
907 $$ = new t_function($3, $4, $6, $8, $2);
908 if ($1 != NULL) {
909 $$->set_doc($1);
910 }
Roger Meier30877382012-09-17 21:18:05 +0000911 if ($9 != NULL) {
912 $$->annotations_ = $9->annotations_;
913 delete $9;
914 }
Mark Slee31985722006-05-24 21:45:31 +0000915 }
916
David Reiss6985a422009-03-24 20:00:47 +0000917Oneway:
918 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000919 {
Mark Slee52f643d2006-08-09 00:03:43 +0000920 $$ = true;
921 }
922|
923 {
924 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000925 }
926
Mark Slee36bfa2e2007-01-19 20:09:51 +0000927Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000928 tok_throws '(' FieldList ')'
929 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000930 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000931 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000932 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000933 yyerror("Throws clause may not contain non-exception types");
934 exit(1);
935 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000936 }
937|
938 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000940 }
941
Mark Slee31985722006-05-24 21:45:31 +0000942FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000943 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000944 {
945 pdebug("FieldList -> FieldList , Field");
946 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000947 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100948 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 +0000949 exit(1);
950 }
Mark Slee31985722006-05-24 21:45:31 +0000951 }
952|
953 {
954 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000955 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000956 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000957 }
958
959Field:
Jens Geyer885c6792014-05-02 21:31:55 +0200960 CaptureDocText FieldIdentifier FieldRequiredness FieldType FieldReference tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000961 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000962 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000963 if ($2.auto_assigned) {
Jens Geyer885c6792014-05-02 21:31:55 +0200964 pwarning(1, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $6);
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000965 if (g_strict >= 192) {
966 yyerror("Implicit field keys are deprecated and not allowed with -strict");
967 exit(1);
968 }
Mark Slee31985722006-05-24 21:45:31 +0000969 }
Jens Geyer885c6792014-05-02 21:31:55 +0200970 validate_simple_identifier($6);
971 $$ = new t_field($4, $6, $2.value);
972 $$->set_reference($5);
David Reiss8320a922007-08-14 19:59:26 +0000973 $$->set_req($3);
Jens Geyer885c6792014-05-02 21:31:55 +0200974 if ($7 != NULL) {
975 g_scope->resolve_const_value($7, $4);
976 validate_field_value($$, $7);
977 $$->set_value($7);
Mark Slee7ff32452007-02-01 05:26:18 +0000978 }
Jens Geyer885c6792014-05-02 21:31:55 +0200979 $$->set_xsd_optional($8);
980 $$->set_xsd_nillable($9);
ccheeverf53b5cf2007-02-05 20:33:11 +0000981 if ($1 != NULL) {
982 $$->set_doc($1);
983 }
David Reiss53c10e02010-03-05 07:51:51 +0000984 if ($10 != NULL) {
Jens Geyer885c6792014-05-02 21:31:55 +0200985 $$->set_xsd_attrs($10);
986 }
987 if ($11 != NULL) {
988 $$->annotations_ = $11->annotations_;
989 delete $11;
David Reiss53c10e02010-03-05 07:51:51 +0000990 }
Mark Slee31985722006-05-24 21:45:31 +0000991 }
Mark Slee7ff32452007-02-01 05:26:18 +0000992
993FieldIdentifier:
994 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000995 {
Mark Slee7ff32452007-02-01 05:26:18 +0000996 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000997 if (g_allow_neg_field_keys) {
998 /*
999 * g_allow_neg_field_keys exists to allow users to add explicitly
1000 * specified key values to old .thrift files without breaking
1001 * protocol compatibility.
1002 */
1003 if ($1 != y_field_val) {
1004 /*
1005 * warn if the user-specified negative value isn't what
1006 * thrift would have auto-assigned.
1007 */
Roger Meier5f2d34e2013-11-16 16:43:41 +01001008 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001009 "auto-assigned by thrift (%d).\n", $1, y_field_val);
1010 }
1011 /*
1012 * Leave $1 as-is, and update y_field_val to be one less than $1.
1013 * The FieldList parsing will catch any duplicate key values.
1014 */
Ben Craige9576752013-10-11 08:19:16 -05001015 y_field_val = static_cast<int32_t>($1 - 1);
1016 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001017 $$.auto_assigned = false;
1018 } else {
Ben Craige9576752013-10-11 08:19:16 -05001019 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001020 $1);
1021 $$.value = y_field_val--;
1022 $$.auto_assigned = true;
1023 }
1024 } else {
Ben Craige9576752013-10-11 08:19:16 -05001025 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001026 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001027 }
Mark Slee7ff32452007-02-01 05:26:18 +00001028 }
1029|
1030 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001031 $$.value = y_field_val--;
1032 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +00001033 }
1034
Jens Geyer885c6792014-05-02 21:31:55 +02001035FieldReference:
1036 tok_reference
1037 {
1038 $$ = true;
1039 }
1040|
1041 {
1042 $$ = false;
1043 }
1044
David Reiss8320a922007-08-14 19:59:26 +00001045FieldRequiredness:
1046 tok_required
1047 {
David Reiss45603e92009-09-02 22:15:55 +00001048 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001049 }
1050| tok_optional
1051 {
Mark Slee78165722007-09-10 22:08:49 +00001052 if (g_arglist) {
1053 if (g_parse_mode == PROGRAM) {
1054 pwarning(1, "optional keyword is ignored in argument lists.\n");
1055 }
David Reiss204420f2008-01-11 20:59:03 +00001056 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001057 } else {
David Reiss204420f2008-01-11 20:59:03 +00001058 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001059 }
David Reiss8320a922007-08-14 19:59:26 +00001060 }
1061|
1062 {
David Reiss204420f2008-01-11 20:59:03 +00001063 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001064 }
1065
Mark Slee7ff32452007-02-01 05:26:18 +00001066FieldValue:
1067 '=' ConstValue
1068 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001069 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001070 $$ = $2;
1071 } else {
1072 $$ = NULL;
1073 }
1074 }
1075|
1076 {
1077 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001078 }
Mark Slee31985722006-05-24 21:45:31 +00001079
Mark Slee31985722006-05-24 21:45:31 +00001080FunctionType:
1081 FieldType
1082 {
Mark Sleee8540632006-05-30 09:24:40 +00001083 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001084 $$ = $1;
1085 }
1086| tok_void
1087 {
Mark Sleee8540632006-05-30 09:24:40 +00001088 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001089 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001090 }
1091
1092FieldType:
1093 tok_identifier
1094 {
Mark Sleee8540632006-05-30 09:24:40 +00001095 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001096 if (g_parse_mode == INCLUDES) {
1097 // Ignore identifiers in include mode
1098 $$ = NULL;
1099 } else {
1100 // Lookup the identifier in the current scope
1101 $$ = g_scope->get_type($1);
1102 if ($$ == NULL) {
jfarrelle0e83162014-04-08 22:45:01 -04001103 /*
1104 * Either this type isn't yet declared, or it's never
1105 declared. Either way allow it and we'll figure it out
1106 during generation.
1107 */
Jens Geyerd000b242014-04-13 21:58:47 +02001108 $$ = new t_typedef(g_program, $1, true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001109 }
Mark Sleee8540632006-05-30 09:24:40 +00001110 }
Mark Slee31985722006-05-24 21:45:31 +00001111 }
1112| BaseType
1113 {
Mark Sleee8540632006-05-30 09:24:40 +00001114 pdebug("FieldType -> BaseType");
1115 $$ = $1;
1116 }
1117| ContainerType
1118 {
1119 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001120 $$ = $1;
1121 }
1122
David Reissc8e30052009-07-27 17:02:42 +00001123BaseType: SimpleBaseType TypeAnnotations
1124 {
1125 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1126 if ($2 != NULL) {
1127 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1128 $$->annotations_ = $2->annotations_;
1129 delete $2;
1130 } else {
1131 $$ = $1;
1132 }
1133 }
1134
1135SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001136 tok_string
1137 {
1138 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001139 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001140 }
Mark Slee8d725a22007-04-13 01:57:12 +00001141| tok_binary
1142 {
1143 pdebug("BaseType -> tok_binary");
1144 $$ = g_type_binary;
1145 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001146| tok_slist
1147 {
1148 pdebug("BaseType -> tok_slist");
1149 $$ = g_type_slist;
1150 }
Mark Slee78f58e22006-09-02 04:17:07 +00001151| tok_bool
1152 {
1153 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001154 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001155 }
Mark Slee31985722006-05-24 21:45:31 +00001156| tok_byte
1157 {
1158 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001159 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001160 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001161| tok_i16
1162 {
1163 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001164 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001165 }
Mark Slee31985722006-05-24 21:45:31 +00001166| tok_i32
1167 {
1168 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001169 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001170 }
Mark Slee31985722006-05-24 21:45:31 +00001171| tok_i64
1172 {
1173 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001174 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001175 }
Mark Sleec98d0502006-09-06 02:42:25 +00001176| tok_double
1177 {
1178 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001179 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001180 }
Mark Slee31985722006-05-24 21:45:31 +00001181
David Reissa2309992008-12-10 01:52:48 +00001182ContainerType: SimpleContainerType TypeAnnotations
1183 {
1184 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1185 $$ = $1;
1186 if ($2 != NULL) {
1187 $$->annotations_ = $2->annotations_;
1188 delete $2;
1189 }
1190 }
1191
1192SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001193 MapType
1194 {
David Reissa2309992008-12-10 01:52:48 +00001195 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001196 $$ = $1;
1197 }
1198| SetType
1199 {
David Reissa2309992008-12-10 01:52:48 +00001200 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001201 $$ = $1;
1202 }
1203| ListType
1204 {
David Reissa2309992008-12-10 01:52:48 +00001205 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001206 $$ = $1;
1207 }
1208
1209MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001210 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001211 {
1212 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001213 $$ = new t_map($4, $6);
1214 if ($2 != NULL) {
1215 ((t_container*)$$)->set_cpp_name(std::string($2));
1216 }
Mark Sleee8540632006-05-30 09:24:40 +00001217 }
1218
1219SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001220 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001221 {
1222 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001223 $$ = new t_set($4);
1224 if ($2 != NULL) {
1225 ((t_container*)$$)->set_cpp_name(std::string($2));
1226 }
Mark Sleee8540632006-05-30 09:24:40 +00001227 }
1228
1229ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001230 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001231 {
1232 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer6fe77e82014-03-16 16:48:53 +02001233 check_for_list_of_bytes($3);
Mark Sleef0712dc2006-10-25 19:03:57 +00001234 $$ = new t_list($3);
1235 if ($5 != NULL) {
1236 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001237 }
1238 }
1239
Mark Slee36bfa2e2007-01-19 20:09:51 +00001240CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001241 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001242 {
Mark Sleeafc76542007-02-09 21:55:44 +00001243 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001244 }
1245|
1246 {
1247 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001248 }
1249
David Reissa2309992008-12-10 01:52:48 +00001250TypeAnnotations:
1251 '(' TypeAnnotationList ')'
1252 {
1253 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1254 $$ = $2;
1255 }
1256|
1257 {
1258 $$ = NULL;
1259 }
1260
1261TypeAnnotationList:
1262 TypeAnnotationList TypeAnnotation
1263 {
1264 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1265 $$ = $1;
1266 $$->annotations_[$2->key] = $2->val;
1267 delete $2;
1268 }
1269|
1270 {
1271 /* Just use a dummy structure to hold the annotations. */
1272 $$ = new t_struct(g_program);
1273 }
1274
1275TypeAnnotation:
1276 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1277 {
1278 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1279 $$ = new t_annotation;
1280 $$->key = $1;
1281 $$->val = $3;
1282 }
1283
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001284%%