blob: 59f6b4f434e1cebf66fed66b534c6cc9ac07060c [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;
Jens Geyerae0b22c2014-09-04 23:04:21 +020056/**
57 * This global variable is used for automatic numbering of enum values.
58 * y_enum_val is the last value assigned; the next auto-assigned value will be
59 * y_enum_val+1, and then it continues working upwards. Explicitly specified
60 * enum values reset y_enum_val to that value.
61 */
62int32_t y_enum_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000063int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000064const int struct_is_struct = 0;
65const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000066
67%}
68
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
70 * This structure is used by the parser to hold the data types associated with
71 * various parse nodes.
72 */
Mark Slee31985722006-05-24 21:45:31 +000073%union {
Mark Slee30152872006-11-28 01:24:07 +000074 char* id;
David Reissf1454162008-06-30 20:45:47 +000075 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000076 double dconst;
77 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000078 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000079 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000080 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000081 t_typedef* ttypedef;
82 t_enum* tenum;
83 t_enum_value* tenumv;
84 t_const* tconst;
85 t_const_value* tconstv;
86 t_struct* tstruct;
87 t_service* tservice;
88 t_function* tfunction;
89 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000090 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000091 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000092 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000093 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000094}
95
Mark Sleef5377b32006-10-10 01:42:59 +000096/**
97 * Strings identifier
98 */
Mark Slee31985722006-05-24 21:45:31 +000099%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000100%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +0000101%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +0000102%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +0000103
104/**
Mark Slee30152872006-11-28 01:24:07 +0000105 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +0000106 */
Mark Slee31985722006-05-24 21:45:31 +0000107%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +0000108%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +0000109
Mark Sleef5377b32006-10-10 01:42:59 +0000110/**
David Reiss399442b2008-02-20 02:28:05 +0000111 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +0000112 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000113%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +0000114%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +0000115%token tok_cpp_namespace
116%token tok_cpp_include
117%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +0000118%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +0000119%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000120%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000121%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000122%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000123%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000124%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000125%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000126%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000127%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000128%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000129%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000130%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000131%token tok_csharp_namespace
Jake Farrell7ae13e12011-10-18 14:35:26 +0000132%token tok_delphi_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000133
Mark Sleef5377b32006-10-10 01:42:59 +0000134/**
135 * Base datatype keywords
136 */
137%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000138%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000139%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000140%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000141%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000142%token tok_senum
Jens Geyer40c28d32015-10-20 23:13:02 +0200143%token tok_i8
Mark Slee9cb7c612006-09-01 22:17:45 +0000144%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000145%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000146%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000147%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000148
Mark Sleef5377b32006-10-10 01:42:59 +0000149/**
150 * Complex type keywords
151 */
Mark Slee31985722006-05-24 21:45:31 +0000152%token tok_map
153%token tok_list
154%token tok_set
155
Mark Sleef5377b32006-10-10 01:42:59 +0000156/**
157 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000158 */
David Reiss6985a422009-03-24 20:00:47 +0000159%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000160
Mark Sleef5377b32006-10-10 01:42:59 +0000161/**
162 * Thrift language keywords
163 */
Mark Slee31985722006-05-24 21:45:31 +0000164%token tok_typedef
165%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000166%token tok_xception
167%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000168%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000169%token tok_service
170%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000171%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000172%token tok_required
173%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000174%token tok_union
Jens Geyer885c6792014-05-02 21:31:55 +0200175%token tok_reference
Mark Slee31985722006-05-24 21:45:31 +0000176
Mark Sleef5377b32006-10-10 01:42:59 +0000177/**
178 * Grammar nodes
179 */
180
Mark Slee31985722006-05-24 21:45:31 +0000181%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000182%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000183%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000184%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000185%type<ttype> MapType
186%type<ttype> SetType
187%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000188
David Reisscdffe262007-08-14 17:12:31 +0000189%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000190%type<ttype> TypeDefinition
191
Mark Slee31985722006-05-24 21:45:31 +0000192%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000193
David Reissa2309992008-12-10 01:52:48 +0000194%type<ttype> TypeAnnotations
195%type<ttype> TypeAnnotationList
196%type<tannot> TypeAnnotation
Jens Geyer89847df2014-05-02 23:50:04 +0200197%type<id> TypeAnnotationValue
David Reissa2309992008-12-10 01:52:48 +0000198
Mark Slee31985722006-05-24 21:45:31 +0000199%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000200%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000201%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000202%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000203%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000204%type<tstruct> FieldList
Jens Geyer885c6792014-05-02 21:31:55 +0200205%type<tbool> FieldReference
Mark Slee31985722006-05-24 21:45:31 +0000206
207%type<tenum> Enum
208%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000209%type<tenumv> EnumDef
Jens Geyerae0b22c2014-09-04 23:04:21 +0200210%type<tenumv> EnumValue
Mark Slee30152872006-11-28 01:24:07 +0000211
Mark Slee6a47fed2007-02-07 02:40:59 +0000212%type<ttypedef> Senum
213%type<tbase> SenumDefList
214%type<id> SenumDef
215
Mark Slee30152872006-11-28 01:24:07 +0000216%type<tconst> Const
217%type<tconstv> ConstValue
218%type<tconstv> ConstList
219%type<tconstv> ConstListContents
220%type<tconstv> ConstMap
221%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000222
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000223%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000224%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000225%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000226%type<tservice> Service
227
228%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000229%type<ttype> FunctionType
230%type<tservice> FunctionList
231
Mark Slee36bfa2e2007-01-19 20:09:51 +0000232%type<tstruct> Throws
233%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000234%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000235%type<tbool> XsdAll
236%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000237%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000238%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000239%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000240
David Reisscbd4bac2007-08-14 17:12:33 +0000241%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000242
Mark Slee31985722006-05-24 21:45:31 +0000243%%
244
Mark Sleef5377b32006-10-10 01:42:59 +0000245/**
246 * Thrift Grammar Implementation.
247 *
248 * For the most part this source file works its way top down from what you
249 * might expect to find in a typical .thrift file, i.e. type definitions and
250 * namespaces up top followed by service definitions using those types.
251 */
Mark Slee31985722006-05-24 21:45:31 +0000252
253Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000254 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000255 {
256 pdebug("Program -> Headers DefinitionList");
Jens Geyere8379b52014-01-25 00:59:45 +0100257 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status != ALREADY_PROCESSED))
258 {
259 g_program->set_doc(g_program_doctext_candidate);
260 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000261 }
David Reisscbd4bac2007-08-14 17:12:33 +0000262 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000263 }
264
David Reisscbd4bac2007-08-14 17:12:33 +0000265CaptureDocText:
266 {
267 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000268 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000269 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000270 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000271 $$ = NULL;
272 }
273 }
274
275/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
276DestroyDocText:
277 {
278 if (g_parse_mode == PROGRAM) {
279 clear_doctext();
280 }
281 }
282
283/* We have to DestroyDocText here, otherwise it catches the doctext
284 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000285HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000286 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 {
288 pdebug("HeaderList -> HeaderList Header");
289 }
290|
291 {
292 pdebug("HeaderList -> ");
293 }
294
295Header:
296 Include
297 {
298 pdebug("Header -> Include");
299 }
BCGcc193c12015-11-12 21:02:51 -0500300| tok_namespace tok_identifier tok_identifier TypeAnnotations
Mark Sleef0712dc2006-10-25 19:03:57 +0000301 {
David Reiss79eca142008-02-27 01:55:13 +0000302 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100303 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000304 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000305 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000306 }
BCGcc193c12015-11-12 21:02:51 -0500307 if ($4 != NULL) {
308 g_program->set_namespace_annotations($2, $4->annotations_);
309 delete $4;
310 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000311 }
David Reissfb790d72010-09-02 16:41:45 +0000312| tok_namespace '*' tok_identifier
313 {
314 pdebug("Header -> tok_namespace * tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100315 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000316 if (g_parse_mode == PROGRAM) {
317 g_program->set_namespace("*", $3);
318 }
319 }
David Reiss9a08dc62008-02-27 01:55:17 +0000320/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000321| tok_cpp_namespace tok_identifier
322 {
David Reiss9a08dc62008-02-27 01:55:17 +0000323 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000324 pdebug("Header -> tok_cpp_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100325 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000326 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000327 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000328 }
329 }
330| tok_cpp_include tok_literal
331 {
332 pdebug("Header -> tok_cpp_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100333 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000334 if (g_parse_mode == PROGRAM) {
335 g_program->add_cpp_include($2);
336 }
337 }
Mark Sleee888b372007-01-12 01:06:24 +0000338| tok_php_namespace tok_identifier
339 {
David Reiss554ea6f2009-02-17 20:28:37 +0000340 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000341 pdebug("Header -> tok_php_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100342 declare_valid_program_doctext();
Mark Sleee888b372007-01-12 01:06:24 +0000343 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000344 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000345 }
346 }
David Reiss320e45c2008-03-27 21:41:54 +0000347/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000348| tok_py_module tok_identifier
349 {
David Reiss320e45c2008-03-27 21:41:54 +0000350 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000351 pdebug("Header -> tok_py_module tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100352 declare_valid_program_doctext();
David Reissc6fc3292007-08-30 00:58:43 +0000353 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000354 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000355 }
356 }
David Reiss07ef3a92008-03-27 21:42:39 +0000357/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000358| tok_perl_package tok_identifier
359 {
David Reiss07ef3a92008-03-27 21:42:39 +0000360 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000361 pdebug("Header -> tok_perl_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100362 declare_valid_program_doctext();
Mark Slee27ed6ec2007-08-16 01:26:31 +0000363 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000364 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000365 }
366 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000367/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000368| tok_ruby_namespace tok_identifier
369 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000370 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000371 pdebug("Header -> tok_ruby_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100372 declare_valid_program_doctext();
Mark Slee58dfb4f2007-07-06 02:45:25 +0000373 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000374 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000375 }
376 }
David Reiss3b455012008-03-27 21:40:46 +0000377/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000378| tok_smalltalk_category tok_st_identifier
379 {
David Reiss3b455012008-03-27 21:40:46 +0000380 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000381 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100382 declare_valid_program_doctext();
Mark Sleebd588222007-11-21 08:43:35 +0000383 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000384 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000385 }
386 }
David Reiss3b455012008-03-27 21:40:46 +0000387/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000388| tok_smalltalk_prefix tok_identifier
389 {
David Reiss3b455012008-03-27 21:40:46 +0000390 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000391 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100392 declare_valid_program_doctext();
David Reiss15457c92007-12-14 07:03:03 +0000393 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000394 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000395 }
396 }
David Reiss771f8c72008-02-27 01:55:25 +0000397/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000398| tok_java_package tok_identifier
399 {
David Reiss9f646152008-03-02 21:59:48 +0000400 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000401 pdebug("Header -> tok_java_package tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100402 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000403 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000404 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000405 }
406 }
David Reiss54b602b2008-03-27 21:41:06 +0000407/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000408| tok_cocoa_prefix tok_identifier
409 {
David Reiss54b602b2008-03-27 21:41:06 +0000410 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000411 pdebug("Header -> tok_cocoa_prefix tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100412 declare_valid_program_doctext();
Mark Slee7e9eea42007-09-10 21:00:23 +0000413 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000414 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000415 }
416 }
David Reiss92e10d82009-02-17 20:28:19 +0000417/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000418| tok_xsd_namespace tok_literal
419 {
David Reiss92e10d82009-02-17 20:28:19 +0000420 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000421 pdebug("Header -> tok_xsd_namespace tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100422 declare_valid_program_doctext();
Mark Slee0d9199e2007-01-31 02:08:30 +0000423 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000424 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000425 }
426 }
David Reiss9d65bf02008-03-27 21:41:37 +0000427/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000428| tok_csharp_namespace tok_identifier
429 {
David Reiss9d65bf02008-03-27 21:41:37 +0000430 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000431 pdebug("Header -> tok_csharp_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100432 declare_valid_program_doctext();
David Reiss7f42bcf2008-01-11 20:59:12 +0000433 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000434 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000435 }
436 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000437/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
438| tok_delphi_namespace tok_identifier
439 {
440 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
441 pdebug("Header -> tok_delphi_namespace tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100442 declare_valid_program_doctext();
Jake Farrell7ae13e12011-10-18 14:35:26 +0000443 if (g_parse_mode == PROGRAM) {
444 g_program->set_namespace("delphi", $2);
445 }
446 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000447
448Include:
449 tok_include tok_literal
450 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000451 pdebug("Include -> tok_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100452 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000453 if (g_parse_mode == INCLUDES) {
454 std::string path = include_file(std::string($2));
455 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000456 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000457 }
458 }
459 }
Mark Slee31985722006-05-24 21:45:31 +0000460
461DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000462 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000463 {
464 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000465 if ($2 != NULL && $3 != NULL) {
466 $3->set_doc($2);
467 }
Mark Slee31985722006-05-24 21:45:31 +0000468 }
469|
470 {
471 pdebug("DefinitionList -> ");
472 }
473
474Definition:
Mark Slee30152872006-11-28 01:24:07 +0000475 Const
476 {
477 pdebug("Definition -> Const");
478 if (g_parse_mode == PROGRAM) {
479 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000480 }
David Reisscdffe262007-08-14 17:12:31 +0000481 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000482 }
483| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000484 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000485 pdebug("Definition -> TypeDefinition");
486 if (g_parse_mode == PROGRAM) {
487 g_scope->add_type($1->get_name(), $1);
488 if (g_parent_scope != NULL) {
489 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
490 }
Roger Meierba406d32013-07-15 22:41:34 +0200491 if (! g_program->is_unique_typename($1)) {
492 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
493 exit(1);
494 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000495 }
David Reisscdffe262007-08-14 17:12:31 +0000496 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000497 }
Mark Slee31985722006-05-24 21:45:31 +0000498| Service
499 {
500 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000501 if (g_parse_mode == PROGRAM) {
502 g_scope->add_service($1->get_name(), $1);
503 if (g_parent_scope != NULL) {
504 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
505 }
506 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200507 if (! g_program->is_unique_typename($1)) {
508 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
509 exit(1);
510 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000511 }
David Reisscdffe262007-08-14 17:12:31 +0000512 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000513 }
514
Mark Sleef0712dc2006-10-25 19:03:57 +0000515TypeDefinition:
516 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000517 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000518 pdebug("TypeDefinition -> Typedef");
519 if (g_parse_mode == PROGRAM) {
520 g_program->add_typedef($1);
521 }
522 }
523| Enum
524 {
525 pdebug("TypeDefinition -> Enum");
526 if (g_parse_mode == PROGRAM) {
527 g_program->add_enum($1);
528 }
529 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000530| Senum
531 {
532 pdebug("TypeDefinition -> Senum");
533 if (g_parse_mode == PROGRAM) {
534 g_program->add_typedef($1);
535 }
536 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000537| Struct
538 {
539 pdebug("TypeDefinition -> Struct");
540 if (g_parse_mode == PROGRAM) {
541 g_program->add_struct($1);
542 }
543 }
544| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000545 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000546 pdebug("TypeDefinition -> Xception");
547 if (g_parse_mode == PROGRAM) {
548 g_program->add_xception($1);
549 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000550 }
Mark Slee31985722006-05-24 21:45:31 +0000551
Jens Geyer089bcd32014-09-11 22:36:41 +0200552CommaOrSemicolonOptional:
553 ','
554 {}
555| ';'
556 {}
557|
558 {}
559
Mark Slee31985722006-05-24 21:45:31 +0000560Typedef:
Jens Geyer089bcd32014-09-11 22:36:41 +0200561 tok_typedef FieldType tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000562 {
David Reiss4dd78012010-03-09 05:19:08 +0000563 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200564 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000565 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000566 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000567 if ($4 != NULL) {
568 $$->annotations_ = $4->annotations_;
569 delete $4;
570 }
Mark Slee31985722006-05-24 21:45:31 +0000571 }
572
573Enum:
Roger Meier30877382012-09-17 21:18:05 +0000574 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000575 {
576 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000577 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200578 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000579 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000580 if ($6 != NULL) {
581 $$->annotations_ = $6->annotations_;
582 delete $6;
583 }
Jens Geyerae0b22c2014-09-04 23:04:21 +0200584
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000585 // make constants for all the enum values
586 if (g_parse_mode == PROGRAM) {
587 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
588 std::vector<t_enum_value*>::const_iterator c_iter;
589 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
590 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
591 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
592 const_val->set_enum($$);
593 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
594 if (g_parent_scope != NULL) {
595 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
596 }
597 }
598 }
Mark Slee31985722006-05-24 21:45:31 +0000599 }
600
601EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000602 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000603 {
604 pdebug("EnumDefList -> EnumDefList EnumDef");
605 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000606 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000607 }
608|
609 {
610 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000611 $$ = new t_enum(g_program);
Jens Geyerae0b22c2014-09-04 23:04:21 +0200612 y_enum_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000613 }
614
615EnumDef:
Jens Geyerae0b22c2014-09-04 23:04:21 +0200616 CaptureDocText EnumValue TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000617 {
Jens Geyerae0b22c2014-09-04 23:04:21 +0200618 pdebug("EnumDef -> EnumValue");
619 $$ = $2;
ccheeverf53b5cf2007-02-05 20:33:11 +0000620 if ($1 != NULL) {
621 $$->set_doc($1);
622 }
Jens Geyer86b309c2014-09-11 23:54:35 +0200623 if ($3 != NULL) {
Roger Meier30877382012-09-17 21:18:05 +0000624 $$->annotations_ = $3->annotations_;
625 delete $3;
626 }
Mark Slee30152872006-11-28 01:24:07 +0000627 }
628
Jens Geyerae0b22c2014-09-04 23:04:21 +0200629EnumValue:
630 tok_identifier '=' tok_int_constant
631 {
632 pdebug("EnumValue -> tok_identifier = tok_int_constant");
633 if ($3 < INT32_MIN || $3 > INT32_MAX) {
634 // Note: this used to be just a warning. However, since thrift always
635 // treats enums as i32 values, I'm changing it to a fatal error.
636 // I doubt this will affect many people, but users who run into this
637 // will have to update their thrift files to manually specify the
638 // truncated i32 value that thrift has always been using anyway.
639 failure("64-bit value supplied for enum %s will be truncated.", $1);
640 }
641 y_enum_val = static_cast<int32_t>($3);
642 $$ = new t_enum_value($1, y_enum_val);
643 }
644 |
645 tok_identifier
646 {
647 pdebug("EnumValue -> tok_identifier");
648 validate_simple_identifier( $1);
649 if (y_enum_val == INT32_MAX) {
650 failure("enum value overflow at enum %s", $1);
651 }
652 ++y_enum_val;
653 $$ = new t_enum_value($1, y_enum_val);
654 }
655
Mark Slee6a47fed2007-02-07 02:40:59 +0000656Senum:
Roger Meier30877382012-09-17 21:18:05 +0000657 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000658 {
659 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200660 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000661 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000662 if ($6 != NULL) {
663 $$->annotations_ = $6->annotations_;
664 delete $6;
665 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000666 }
667
668SenumDefList:
669 SenumDefList SenumDef
670 {
671 pdebug("SenumDefList -> SenumDefList SenumDef");
672 $$ = $1;
673 $$->add_string_enum_val($2);
674 }
675|
676 {
677 pdebug("SenumDefList -> ");
678 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
679 $$->set_string_enum(true);
680 }
681
682SenumDef:
683 tok_literal CommaOrSemicolonOptional
684 {
685 pdebug("SenumDef -> tok_literal");
686 $$ = $1;
687 }
688
Mark Slee30152872006-11-28 01:24:07 +0000689Const:
690 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
691 {
692 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000693 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200694 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000695 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000696 $$ = new t_const($2, $3, $5);
697 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000698
699 g_scope->add_constant($3, $$);
700 if (g_parent_scope != NULL) {
701 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
702 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000703 } else {
704 $$ = NULL;
705 }
Mark Slee30152872006-11-28 01:24:07 +0000706 }
707
708ConstValue:
709 tok_int_constant
710 {
711 pdebug("ConstValue => tok_int_constant");
712 $$ = new t_const_value();
713 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000714 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100715 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000716 }
Mark Slee30152872006-11-28 01:24:07 +0000717 }
718| tok_dub_constant
719 {
720 pdebug("ConstValue => tok_dub_constant");
721 $$ = new t_const_value();
722 $$->set_double($1);
723 }
724| tok_literal
725 {
726 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000727 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000728 }
Mark Slee67fc6342006-11-29 03:37:04 +0000729| tok_identifier
730 {
731 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000732 $$ = new t_const_value();
733 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000734 }
Mark Slee30152872006-11-28 01:24:07 +0000735| ConstList
736 {
737 pdebug("ConstValue => ConstList");
738 $$ = $1;
739 }
740| ConstMap
741 {
742 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000743 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000744 }
745
746ConstList:
747 '[' ConstListContents ']'
748 {
749 pdebug("ConstList => [ ConstListContents ]");
750 $$ = $2;
751 }
752
753ConstListContents:
754 ConstListContents ConstValue CommaOrSemicolonOptional
755 {
756 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
757 $$ = $1;
758 $$->add_list($2);
759 }
760|
761 {
762 pdebug("ConstListContents =>");
763 $$ = new t_const_value();
764 $$->set_list();
765 }
766
767ConstMap:
768 '{' ConstMapContents '}'
769 {
770 pdebug("ConstMap => { ConstMapContents }");
771 $$ = $2;
772 }
773
774ConstMapContents:
775 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
776 {
777 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
778 $$ = $1;
779 $$->add_map($2, $4);
780 }
781|
782 {
783 pdebug("ConstMapContents =>");
784 $$ = new t_const_value();
785 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000786 }
787
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000788StructHead:
789 tok_struct
790 {
791 $$ = struct_is_struct;
792 }
793| tok_union
794 {
795 $$ = struct_is_union;
796 }
797
Mark Slee31985722006-05-24 21:45:31 +0000798Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000799 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000800 {
801 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200802 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000803 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000804 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000805 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000806 $$->set_name($2);
807 if ($7 != NULL) {
808 $$->annotations_ = $7->annotations_;
809 delete $7;
810 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000811 }
Ben Craige9576752013-10-11 08:19:16 -0500812
Mark Slee36bfa2e2007-01-19 20:09:51 +0000813XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000814 tok_xsd_all
815 {
816 $$ = true;
817 }
818|
819 {
820 $$ = false;
821 }
822
Mark Slee36bfa2e2007-01-19 20:09:51 +0000823XsdOptional:
824 tok_xsd_optional
825 {
826 $$ = true;
827 }
828|
829 {
830 $$ = false;
831 }
832
Mark Slee7df0e2a2007-02-06 21:03:18 +0000833XsdNillable:
834 tok_xsd_nillable
835 {
836 $$ = true;
837 }
838|
839 {
840 $$ = false;
841 }
842
Mark Slee21135c32007-02-05 21:52:08 +0000843XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000844 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000845 {
Mark Slee748d83f2007-02-07 01:20:08 +0000846 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000847 }
848|
849 {
850 $$ = NULL;
851 }
852
Mark Slee9cb7c612006-09-01 22:17:45 +0000853Xception:
Roger Meier30877382012-09-17 21:18:05 +0000854 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000855 {
856 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200857 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000858 $4->set_name($2);
859 $4->set_xception(true);
860 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000861 if ($6 != NULL) {
862 $$->annotations_ = $6->annotations_;
863 delete $6;
864 }
Mark Slee31985722006-05-24 21:45:31 +0000865 }
866
867Service:
Roger Meier30877382012-09-17 21:18:05 +0000868 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000869 {
870 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200871 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000872 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000873 $$->set_name($2);
874 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000875 if ($9 != NULL) {
876 $$->annotations_ = $9->annotations_;
877 delete $9;
878 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000879 }
880
Mark Slee78165722007-09-10 22:08:49 +0000881FlagArgs:
882 {
883 g_arglist = 1;
884 }
885
886UnflagArgs:
887 {
888 g_arglist = 0;
889 }
890
Mark Slee36bfa2e2007-01-19 20:09:51 +0000891Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000892 tok_extends tok_identifier
893 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000894 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000895 $$ = NULL;
896 if (g_parse_mode == PROGRAM) {
897 $$ = g_scope->get_service($2);
898 if ($$ == NULL) {
899 yyerror("Service \"%s\" has not been defined.", $2);
900 exit(1);
901 }
902 }
903 }
904|
905 {
906 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000907 }
908
909FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000910 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000911 {
912 pdebug("FunctionList -> FunctionList Function");
913 $$ = $1;
914 $1->add_function($2);
915 }
916|
917 {
918 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000919 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000920 }
921
922Function:
Roger Meier30877382012-09-17 21:18:05 +0000923 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000924 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200925 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000926 $6->set_name(std::string($4) + "_args");
927 $$ = new t_function($3, $4, $6, $8, $2);
928 if ($1 != NULL) {
929 $$->set_doc($1);
930 }
Roger Meier30877382012-09-17 21:18:05 +0000931 if ($9 != NULL) {
932 $$->annotations_ = $9->annotations_;
933 delete $9;
934 }
Mark Slee31985722006-05-24 21:45:31 +0000935 }
936
David Reiss6985a422009-03-24 20:00:47 +0000937Oneway:
938 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000939 {
Mark Slee52f643d2006-08-09 00:03:43 +0000940 $$ = true;
941 }
942|
943 {
944 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000945 }
946
Mark Slee36bfa2e2007-01-19 20:09:51 +0000947Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000948 tok_throws '(' FieldList ')'
949 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000950 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000951 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000952 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000953 yyerror("Throws clause may not contain non-exception types");
954 exit(1);
955 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000956 }
957|
958 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000959 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000960 }
961
Mark Slee31985722006-05-24 21:45:31 +0000962FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000963 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000964 {
965 pdebug("FieldList -> FieldList , Field");
966 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000967 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100968 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 +0000969 exit(1);
970 }
Mark Slee31985722006-05-24 21:45:31 +0000971 }
972|
973 {
974 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000975 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000976 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000977 }
978
979Field:
Jens Geyer885c6792014-05-02 21:31:55 +0200980 CaptureDocText FieldIdentifier FieldRequiredness FieldType FieldReference tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000981 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000982 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000983 if ($2.auto_assigned) {
Jens Geyer885c6792014-05-02 21:31:55 +0200984 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 +0000985 if (g_strict >= 192) {
986 yyerror("Implicit field keys are deprecated and not allowed with -strict");
987 exit(1);
988 }
Mark Slee31985722006-05-24 21:45:31 +0000989 }
Jens Geyer885c6792014-05-02 21:31:55 +0200990 validate_simple_identifier($6);
991 $$ = new t_field($4, $6, $2.value);
992 $$->set_reference($5);
David Reiss8320a922007-08-14 19:59:26 +0000993 $$->set_req($3);
Jens Geyer885c6792014-05-02 21:31:55 +0200994 if ($7 != NULL) {
995 g_scope->resolve_const_value($7, $4);
996 validate_field_value($$, $7);
997 $$->set_value($7);
Mark Slee7ff32452007-02-01 05:26:18 +0000998 }
Jens Geyer885c6792014-05-02 21:31:55 +0200999 $$->set_xsd_optional($8);
1000 $$->set_xsd_nillable($9);
ccheeverf53b5cf2007-02-05 20:33:11 +00001001 if ($1 != NULL) {
1002 $$->set_doc($1);
1003 }
David Reiss53c10e02010-03-05 07:51:51 +00001004 if ($10 != NULL) {
Jens Geyer885c6792014-05-02 21:31:55 +02001005 $$->set_xsd_attrs($10);
1006 }
1007 if ($11 != NULL) {
1008 $$->annotations_ = $11->annotations_;
1009 delete $11;
David Reiss53c10e02010-03-05 07:51:51 +00001010 }
Mark Slee31985722006-05-24 21:45:31 +00001011 }
Mark Slee7ff32452007-02-01 05:26:18 +00001012
1013FieldIdentifier:
1014 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +00001015 {
Mark Slee7ff32452007-02-01 05:26:18 +00001016 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001017 if (g_allow_neg_field_keys) {
1018 /*
1019 * g_allow_neg_field_keys exists to allow users to add explicitly
1020 * specified key values to old .thrift files without breaking
1021 * protocol compatibility.
1022 */
1023 if ($1 != y_field_val) {
1024 /*
1025 * warn if the user-specified negative value isn't what
1026 * thrift would have auto-assigned.
1027 */
Roger Meier5f2d34e2013-11-16 16:43:41 +01001028 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001029 "auto-assigned by thrift (%d).\n", $1, y_field_val);
1030 }
1031 /*
1032 * Leave $1 as-is, and update y_field_val to be one less than $1.
1033 * The FieldList parsing will catch any duplicate key values.
1034 */
Ben Craige9576752013-10-11 08:19:16 -05001035 y_field_val = static_cast<int32_t>($1 - 1);
1036 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001037 $$.auto_assigned = false;
1038 } else {
Ben Craige9576752013-10-11 08:19:16 -05001039 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001040 $1);
1041 $$.value = y_field_val--;
1042 $$.auto_assigned = true;
1043 }
1044 } else {
Ben Craige9576752013-10-11 08:19:16 -05001045 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001046 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001047 }
Mark Slee7ff32452007-02-01 05:26:18 +00001048 }
1049|
1050 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001051 $$.value = y_field_val--;
1052 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +00001053 }
1054
Jens Geyer885c6792014-05-02 21:31:55 +02001055FieldReference:
1056 tok_reference
1057 {
1058 $$ = true;
1059 }
1060|
1061 {
1062 $$ = false;
1063 }
1064
David Reiss8320a922007-08-14 19:59:26 +00001065FieldRequiredness:
1066 tok_required
1067 {
David Reiss45603e92009-09-02 22:15:55 +00001068 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001069 }
1070| tok_optional
1071 {
Mark Slee78165722007-09-10 22:08:49 +00001072 if (g_arglist) {
1073 if (g_parse_mode == PROGRAM) {
1074 pwarning(1, "optional keyword is ignored in argument lists.\n");
1075 }
David Reiss204420f2008-01-11 20:59:03 +00001076 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001077 } else {
David Reiss204420f2008-01-11 20:59:03 +00001078 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001079 }
David Reiss8320a922007-08-14 19:59:26 +00001080 }
1081|
1082 {
David Reiss204420f2008-01-11 20:59:03 +00001083 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001084 }
1085
Mark Slee7ff32452007-02-01 05:26:18 +00001086FieldValue:
1087 '=' ConstValue
1088 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001089 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001090 $$ = $2;
1091 } else {
1092 $$ = NULL;
1093 }
1094 }
1095|
1096 {
1097 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001098 }
Mark Slee31985722006-05-24 21:45:31 +00001099
Mark Slee31985722006-05-24 21:45:31 +00001100FunctionType:
1101 FieldType
1102 {
Mark Sleee8540632006-05-30 09:24:40 +00001103 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001104 $$ = $1;
1105 }
1106| tok_void
1107 {
Mark Sleee8540632006-05-30 09:24:40 +00001108 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001109 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001110 }
1111
1112FieldType:
1113 tok_identifier
1114 {
Mark Sleee8540632006-05-30 09:24:40 +00001115 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001116 if (g_parse_mode == INCLUDES) {
1117 // Ignore identifiers in include mode
1118 $$ = NULL;
1119 } else {
1120 // Lookup the identifier in the current scope
1121 $$ = g_scope->get_type($1);
1122 if ($$ == NULL) {
jfarrelle0e83162014-04-08 22:45:01 -04001123 /*
1124 * Either this type isn't yet declared, or it's never
1125 declared. Either way allow it and we'll figure it out
1126 during generation.
1127 */
Jens Geyerd000b242014-04-13 21:58:47 +02001128 $$ = new t_typedef(g_program, $1, true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001129 }
Mark Sleee8540632006-05-30 09:24:40 +00001130 }
Mark Slee31985722006-05-24 21:45:31 +00001131 }
1132| BaseType
1133 {
Mark Sleee8540632006-05-30 09:24:40 +00001134 pdebug("FieldType -> BaseType");
1135 $$ = $1;
1136 }
1137| ContainerType
1138 {
1139 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001140 $$ = $1;
1141 }
1142
David Reissc8e30052009-07-27 17:02:42 +00001143BaseType: SimpleBaseType TypeAnnotations
1144 {
1145 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1146 if ($2 != NULL) {
1147 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1148 $$->annotations_ = $2->annotations_;
1149 delete $2;
1150 } else {
1151 $$ = $1;
1152 }
1153 }
1154
1155SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001156 tok_string
1157 {
1158 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001159 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001160 }
Mark Slee8d725a22007-04-13 01:57:12 +00001161| tok_binary
1162 {
1163 pdebug("BaseType -> tok_binary");
1164 $$ = g_type_binary;
1165 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001166| tok_slist
1167 {
1168 pdebug("BaseType -> tok_slist");
1169 $$ = g_type_slist;
1170 }
Mark Slee78f58e22006-09-02 04:17:07 +00001171| tok_bool
1172 {
1173 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001174 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001175 }
Jens Geyer40c28d32015-10-20 23:13:02 +02001176| tok_i8
Mark Slee31985722006-05-24 21:45:31 +00001177 {
Jens Geyer40c28d32015-10-20 23:13:02 +02001178 pdebug("BaseType -> tok_i8");
1179 $$ = g_type_i8;
Mark Slee31985722006-05-24 21:45:31 +00001180 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001181| tok_i16
1182 {
1183 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001184 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001185 }
Mark Slee31985722006-05-24 21:45:31 +00001186| tok_i32
1187 {
1188 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001189 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001190 }
Mark Slee31985722006-05-24 21:45:31 +00001191| tok_i64
1192 {
1193 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001194 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001195 }
Mark Sleec98d0502006-09-06 02:42:25 +00001196| tok_double
1197 {
1198 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001199 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001200 }
Mark Slee31985722006-05-24 21:45:31 +00001201
David Reissa2309992008-12-10 01:52:48 +00001202ContainerType: SimpleContainerType TypeAnnotations
1203 {
1204 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1205 $$ = $1;
1206 if ($2 != NULL) {
1207 $$->annotations_ = $2->annotations_;
1208 delete $2;
1209 }
1210 }
1211
1212SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001213 MapType
1214 {
David Reissa2309992008-12-10 01:52:48 +00001215 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001216 $$ = $1;
1217 }
1218| SetType
1219 {
David Reissa2309992008-12-10 01:52:48 +00001220 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001221 $$ = $1;
1222 }
1223| ListType
1224 {
David Reissa2309992008-12-10 01:52:48 +00001225 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001226 $$ = $1;
1227 }
1228
1229MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001230 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001231 {
1232 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001233 $$ = new t_map($4, $6);
1234 if ($2 != NULL) {
1235 ((t_container*)$$)->set_cpp_name(std::string($2));
1236 }
Mark Sleee8540632006-05-30 09:24:40 +00001237 }
1238
1239SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001240 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001241 {
1242 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001243 $$ = new t_set($4);
1244 if ($2 != NULL) {
1245 ((t_container*)$$)->set_cpp_name(std::string($2));
1246 }
Mark Sleee8540632006-05-30 09:24:40 +00001247 }
1248
1249ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001250 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001251 {
1252 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer6fe77e82014-03-16 16:48:53 +02001253 check_for_list_of_bytes($3);
Mark Sleef0712dc2006-10-25 19:03:57 +00001254 $$ = new t_list($3);
1255 if ($5 != NULL) {
1256 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001257 }
1258 }
1259
Mark Slee36bfa2e2007-01-19 20:09:51 +00001260CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001261 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001262 {
Mark Sleeafc76542007-02-09 21:55:44 +00001263 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001264 }
1265|
1266 {
1267 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001268 }
1269
David Reissa2309992008-12-10 01:52:48 +00001270TypeAnnotations:
1271 '(' TypeAnnotationList ')'
1272 {
1273 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1274 $$ = $2;
1275 }
1276|
1277 {
1278 $$ = NULL;
1279 }
1280
1281TypeAnnotationList:
1282 TypeAnnotationList TypeAnnotation
1283 {
1284 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1285 $$ = $1;
1286 $$->annotations_[$2->key] = $2->val;
1287 delete $2;
1288 }
1289|
1290 {
1291 /* Just use a dummy structure to hold the annotations. */
1292 $$ = new t_struct(g_program);
1293 }
1294
1295TypeAnnotation:
Jens Geyer89847df2014-05-02 23:50:04 +02001296 tok_identifier TypeAnnotationValue CommaOrSemicolonOptional
David Reissa2309992008-12-10 01:52:48 +00001297 {
Jens Geyer89847df2014-05-02 23:50:04 +02001298 pdebug("TypeAnnotation -> TypeAnnotationValue");
David Reissa2309992008-12-10 01:52:48 +00001299 $$ = new t_annotation;
1300 $$->key = $1;
Jens Geyer89847df2014-05-02 23:50:04 +02001301 $$->val = $2;
1302 }
1303
1304TypeAnnotationValue:
1305 '=' tok_literal
1306 {
1307 pdebug("TypeAnnotationValue -> = tok_literal");
1308 $$ = $2;
1309 }
1310|
1311 {
1312 pdebug("TypeAnnotationValue ->");
1313 $$ = strdup("1");
David Reissa2309992008-12-10 01:52:48 +00001314 }
1315
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001316%%