blob: 2d67c1a78a0d8ac8dda8dd398ed8642983979dee [file] [log] [blame]
James E. King, III330b3f82017-01-23 08:52:04 -05001%code requires {
2#include "thrift/parse/t_program.h"
3}
Mark Slee31985722006-05-24 21:45:31 +00004%{
David Reissea2cba82009-03-30 21:35:00 +00005/*
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
Mark Slee31985722006-05-24 21:45:31 +000023
24/**
25 * Thrift parser.
26 *
27 * This parser is used on a thrift definition file.
28 *
Mark Slee31985722006-05-24 21:45:31 +000029 */
30
James E. King, III7edc8fa2017-01-20 10:11:41 -050031#ifndef __STDC_LIMIT_MACROS
David Reissf1454162008-06-30 20:45:47 +000032#define __STDC_LIMIT_MACROS
James E. King, III7edc8fa2017-01-20 10:11:41 -050033#endif
34#ifndef __STDC_FORMAT_MACROS
David Reissf1454162008-06-30 20:45:47 +000035#define __STDC_FORMAT_MACROS
James E. King, III7edc8fa2017-01-20 10:11:41 -050036#endif
Mark Slee31985722006-05-24 21:45:31 +000037#include <stdio.h>
James E. King III860a5f12018-03-06 14:23:23 -050038#include <string.h>
Roger Meier12d70532011-12-14 23:35:28 +000039#ifndef _MSC_VER
David Reissf1454162008-06-30 20:45:47 +000040#include <inttypes.h>
Roger Meier12d70532011-12-14 23:35:28 +000041#else
42#include <stdint.h>
43#endif
David Reiss400a5432008-07-25 19:48:39 +000044#include <limits.h>
Ben Craige9576752013-10-11 08:19:16 -050045#ifdef _MSC_VER
dtmuller052abc32016-07-26 11:58:28 +020046#include "thrift/windows/config.h"
Ben Craige9576752013-10-11 08:19:16 -050047#endif
dtmuller052abc32016-07-26 11:58:28 +020048#include "thrift/main.h"
49#include "thrift/common.h"
50#include "thrift/globals.h"
51#include "thrift/parse/t_program.h"
52#include "thrift/parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000053
Ben Craige9576752013-10-11 08:19:16 -050054#ifdef _MSC_VER
55//warning C4065: switch statement contains 'default' but no 'case' labels
56#pragma warning(disable:4065)
57#endif
58
Mark Sleef5377b32006-10-10 01:42:59 +000059/**
60 * This global variable is used for automatic numbering of field indices etc.
61 * when parsing the members of a struct. Field values are automatically
62 * assigned starting from -1 and working their way down.
63 */
Mark Slee9cb7c612006-09-01 22:17:45 +000064int y_field_val = -1;
Jens Geyerae0b22c2014-09-04 23:04:21 +020065/**
66 * This global variable is used for automatic numbering of enum values.
67 * y_enum_val is the last value assigned; the next auto-assigned value will be
68 * y_enum_val+1, and then it continues working upwards. Explicitly specified
69 * enum values reset y_enum_val to that value.
70 */
71int32_t y_enum_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000072int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000073const int struct_is_struct = 0;
74const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000075
76%}
77
Mark Sleef5377b32006-10-10 01:42:59 +000078/**
79 * This structure is used by the parser to hold the data types associated with
80 * various parse nodes.
81 */
Mark Slee31985722006-05-24 21:45:31 +000082%union {
Mark Slee30152872006-11-28 01:24:07 +000083 char* id;
David Reissf1454162008-06-30 20:45:47 +000084 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000085 double dconst;
86 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000087 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000088 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000089 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000090 t_typedef* ttypedef;
91 t_enum* tenum;
92 t_enum_value* tenumv;
93 t_const* tconst;
94 t_const_value* tconstv;
95 t_struct* tstruct;
96 t_service* tservice;
97 t_function* tfunction;
98 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000099 char* dtext;
Jens Geyer154d1542022-09-10 14:30:15 +0200100 char* keyword;
David Reiss8320a922007-08-14 19:59:26 +0000101 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +0000102 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000103 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +0000104}
105
Mark Sleef5377b32006-10-10 01:42:59 +0000106/**
107 * Strings identifier
108 */
Mark Slee31985722006-05-24 21:45:31 +0000109%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000110%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +0000111%token<dtext> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +0000112
113/**
Mark Slee30152872006-11-28 01:24:07 +0000114 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +0000115 */
Mark Slee31985722006-05-24 21:45:31 +0000116%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +0000117%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +0000118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
David Reiss399442b2008-02-20 02:28:05 +0000120 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +0000121 */
Jens Geyer154d1542022-09-10 14:30:15 +0200122%token<keyword> tok_include
123%token<keyword> tok_namespace
124%token<keyword> tok_cpp_include
125%token<keyword> tok_cpp_type
126%token<keyword> tok_xsd_all
127%token<keyword> tok_xsd_optional
128%token<keyword> tok_xsd_nillable
129%token<keyword> tok_xsd_attrs
Mark Slee9cb7c612006-09-01 22:17:45 +0000130
Mark Sleef5377b32006-10-10 01:42:59 +0000131/**
132 * Base datatype keywords
133 */
Jens Geyer154d1542022-09-10 14:30:15 +0200134%token<keyword> tok_void
135%token<keyword> tok_bool
136%token<keyword> tok_string
137%token<keyword> tok_binary
138%token<keyword> tok_uuid
139%token<keyword> tok_byte
140%token<keyword> tok_i8
141%token<keyword> tok_i16
142%token<keyword> tok_i32
143%token<keyword> tok_i64
144%token<keyword> tok_double
Mark Slee31985722006-05-24 21:45:31 +0000145
Mark Sleef5377b32006-10-10 01:42:59 +0000146/**
147 * Complex type keywords
148 */
Jens Geyer154d1542022-09-10 14:30:15 +0200149%token<keyword> tok_map
150%token<keyword> tok_list
151%token<keyword> tok_set
Mark Slee31985722006-05-24 21:45:31 +0000152
Mark Sleef5377b32006-10-10 01:42:59 +0000153/**
154 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000155 */
Jens Geyer154d1542022-09-10 14:30:15 +0200156%token<keyword> tok_oneway
157%token<keyword> tok_async
Mark Slee31985722006-05-24 21:45:31 +0000158
Mark Sleef5377b32006-10-10 01:42:59 +0000159/**
160 * Thrift language keywords
161 */
Jens Geyer154d1542022-09-10 14:30:15 +0200162%token<keyword> tok_typedef
163%token<keyword> tok_struct
164%token<keyword> tok_xception
165%token<keyword> tok_throws
166%token<keyword> tok_extends
167%token<keyword> tok_service
168%token<keyword> tok_enum
169%token<keyword> tok_const
170%token<keyword> tok_required
171%token<keyword> tok_optional
172%token<keyword> tok_union
173%token<keyword> tok_reference
Mark Slee31985722006-05-24 21:45:31 +0000174
Mark Sleef5377b32006-10-10 01:42:59 +0000175/**
176 * Grammar nodes
177 */
178
Mark Slee31985722006-05-24 21:45:31 +0000179%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000180%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000181%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000182%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000183%type<ttype> MapType
184%type<ttype> SetType
185%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000186
David Reisscdffe262007-08-14 17:12:31 +0000187%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000188%type<ttype> TypeDefinition
189
Mark Slee31985722006-05-24 21:45:31 +0000190%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000191
David Reissa2309992008-12-10 01:52:48 +0000192%type<ttype> TypeAnnotations
193%type<ttype> TypeAnnotationList
194%type<tannot> TypeAnnotation
Jens Geyer89847df2014-05-02 23:50:04 +0200195%type<id> TypeAnnotationValue
David Reissa2309992008-12-10 01:52:48 +0000196
Mark Slee31985722006-05-24 21:45:31 +0000197%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000198%type<tfieldid> FieldIdentifier
Jens Geyer154d1542022-09-10 14:30:15 +0200199%type<id> FieldName
David Reiss8320a922007-08-14 19:59:26 +0000200%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000201%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000202%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000203%type<tstruct> FieldList
Jens Geyer885c6792014-05-02 21:31:55 +0200204%type<tbool> FieldReference
Mark Slee31985722006-05-24 21:45:31 +0000205
206%type<tenum> Enum
207%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000208%type<tenumv> EnumDef
Jens Geyerae0b22c2014-09-04 23:04:21 +0200209%type<tenumv> EnumValue
Mark Slee30152872006-11-28 01:24:07 +0000210
211%type<tconst> Const
212%type<tconstv> ConstValue
213%type<tconstv> ConstList
214%type<tconstv> ConstListContents
215%type<tconstv> ConstMap
216%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000217
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000218%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000219%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000220%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000221%type<tservice> Service
222
223%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000224%type<ttype> FunctionType
225%type<tservice> FunctionList
226
Mark Slee36bfa2e2007-01-19 20:09:51 +0000227%type<tstruct> Throws
228%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000229%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000230%type<tbool> XsdAll
231%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000232%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000233%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000234%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000235
David Reisscbd4bac2007-08-14 17:12:33 +0000236%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000237
Mark Slee31985722006-05-24 21:45:31 +0000238%%
239
Mark Sleef5377b32006-10-10 01:42:59 +0000240/**
241 * Thrift Grammar Implementation.
242 *
243 * For the most part this source file works its way top down from what you
244 * might expect to find in a typical .thrift file, i.e. type definitions and
245 * namespaces up top followed by service definitions using those types.
246 */
Mark Slee31985722006-05-24 21:45:31 +0000247
248Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000249 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000250 {
251 pdebug("Program -> Headers DefinitionList");
zeshuai00726681fb2020-06-03 17:24:38 +0800252 if((g_program_doctext_candidate != nullptr) && (g_program_doctext_status != ALREADY_PROCESSED))
Jens Geyere8379b52014-01-25 00:59:45 +0100253 {
254 g_program->set_doc(g_program_doctext_candidate);
255 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000256 }
David Reisscbd4bac2007-08-14 17:12:33 +0000257 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000258 }
259
David Reisscbd4bac2007-08-14 17:12:33 +0000260CaptureDocText:
261 {
262 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000263 $$ = g_doctext;
zeshuai00726681fb2020-06-03 17:24:38 +0800264 g_doctext = nullptr;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000265 } else {
zeshuai00726681fb2020-06-03 17:24:38 +0800266 $$ = nullptr;
David Reisscbd4bac2007-08-14 17:12:33 +0000267 }
268 }
269
270/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
271DestroyDocText:
272 {
273 if (g_parse_mode == PROGRAM) {
274 clear_doctext();
275 }
276 }
277
278/* We have to DestroyDocText here, otherwise it catches the doctext
279 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000280HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000281 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000282 {
283 pdebug("HeaderList -> HeaderList Header");
284 }
285|
286 {
287 pdebug("HeaderList -> ");
288 }
289
290Header:
291 Include
292 {
293 pdebug("Header -> Include");
294 }
BCGcc193c12015-11-12 21:02:51 -0500295| tok_namespace tok_identifier tok_identifier TypeAnnotations
Mark Sleef0712dc2006-10-25 19:03:57 +0000296 {
David Reiss79eca142008-02-27 01:55:13 +0000297 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100298 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000299 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000300 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000301 }
zeshuai00726681fb2020-06-03 17:24:38 +0800302 if ($4 != nullptr) {
BCGcc193c12015-11-12 21:02:51 -0500303 g_program->set_namespace_annotations($2, $4->annotations_);
304 delete $4;
305 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000306 }
David Reissfb790d72010-09-02 16:41:45 +0000307| tok_namespace '*' tok_identifier
308 {
309 pdebug("Header -> tok_namespace * tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100310 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000311 if (g_parse_mode == PROGRAM) {
312 g_program->set_namespace("*", $3);
313 }
314 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000315| tok_cpp_include tok_literal
316 {
317 pdebug("Header -> tok_cpp_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100318 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000319 if (g_parse_mode == PROGRAM) {
320 g_program->add_cpp_include($2);
321 }
322 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000323
324Include:
325 tok_include tok_literal
326 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000327 pdebug("Include -> tok_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100328 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000329 if (g_parse_mode == INCLUDES) {
330 std::string path = include_file(std::string($2));
331 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000332 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000333 }
334 }
335 }
Mark Slee31985722006-05-24 21:45:31 +0000336
337DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000338 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000339 {
340 pdebug("DefinitionList -> DefinitionList Definition");
zeshuai00726681fb2020-06-03 17:24:38 +0800341 if ($2 != nullptr && $3 != nullptr) {
David Reisscdffe262007-08-14 17:12:31 +0000342 $3->set_doc($2);
343 }
Mark Slee31985722006-05-24 21:45:31 +0000344 }
345|
346 {
347 pdebug("DefinitionList -> ");
348 }
349
350Definition:
Mark Slee30152872006-11-28 01:24:07 +0000351 Const
352 {
353 pdebug("Definition -> Const");
354 if (g_parse_mode == PROGRAM) {
355 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000356 }
David Reisscdffe262007-08-14 17:12:31 +0000357 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000358 }
359| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000360 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000361 pdebug("Definition -> TypeDefinition");
362 if (g_parse_mode == PROGRAM) {
363 g_scope->add_type($1->get_name(), $1);
zeshuai00726681fb2020-06-03 17:24:38 +0800364 if (g_parent_scope != nullptr) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
366 }
Roger Meierba406d32013-07-15 22:41:34 +0200367 if (! g_program->is_unique_typename($1)) {
368 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
369 exit(1);
370 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000371 }
David Reisscdffe262007-08-14 17:12:31 +0000372 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000373 }
Mark Slee31985722006-05-24 21:45:31 +0000374| Service
375 {
376 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000377 if (g_parse_mode == PROGRAM) {
378 g_scope->add_service($1->get_name(), $1);
zeshuai00726681fb2020-06-03 17:24:38 +0800379 if (g_parent_scope != nullptr) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000380 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
381 }
382 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200383 if (! g_program->is_unique_typename($1)) {
384 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
385 exit(1);
386 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000387 }
David Reisscdffe262007-08-14 17:12:31 +0000388 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000389 }
390
Mark Sleef0712dc2006-10-25 19:03:57 +0000391TypeDefinition:
392 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000393 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000394 pdebug("TypeDefinition -> Typedef");
395 if (g_parse_mode == PROGRAM) {
396 g_program->add_typedef($1);
397 }
398 }
399| Enum
400 {
401 pdebug("TypeDefinition -> Enum");
402 if (g_parse_mode == PROGRAM) {
403 g_program->add_enum($1);
404 }
405 }
406| Struct
407 {
408 pdebug("TypeDefinition -> Struct");
409 if (g_parse_mode == PROGRAM) {
410 g_program->add_struct($1);
411 }
412 }
413| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000414 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000415 pdebug("TypeDefinition -> Xception");
416 if (g_parse_mode == PROGRAM) {
417 g_program->add_xception($1);
418 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000419 }
Mark Slee31985722006-05-24 21:45:31 +0000420
Jens Geyer089bcd32014-09-11 22:36:41 +0200421CommaOrSemicolonOptional:
422 ','
423 {}
424| ';'
425 {}
426|
427 {}
428
Mark Slee31985722006-05-24 21:45:31 +0000429Typedef:
Jens Geyer089bcd32014-09-11 22:36:41 +0200430 tok_typedef FieldType tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000431 {
David Reiss4dd78012010-03-09 05:19:08 +0000432 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
Jens Geyer12c09f42013-08-25 14:16:27 +0200433 validate_simple_identifier( $3);
David Reisscdffe262007-08-14 17:12:31 +0000434 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000435 $$ = td;
zeshuai00726681fb2020-06-03 17:24:38 +0800436 if ($4 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000437 $$->annotations_ = $4->annotations_;
438 delete $4;
439 }
Mark Slee31985722006-05-24 21:45:31 +0000440 }
441
442Enum:
Roger Meier30877382012-09-17 21:18:05 +0000443 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000444 {
445 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000446 $$ = $4;
Jens Geyer12c09f42013-08-25 14:16:27 +0200447 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000448 $$->set_name($2);
zeshuai00726681fb2020-06-03 17:24:38 +0800449 if ($6 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000450 $$->annotations_ = $6->annotations_;
451 delete $6;
452 }
Jens Geyerae0b22c2014-09-04 23:04:21 +0200453
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000454 // make constants for all the enum values
455 if (g_parse_mode == PROGRAM) {
456 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
457 std::vector<t_enum_value*>::const_iterator c_iter;
458 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
459 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
460 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
461 const_val->set_enum($$);
462 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
zeshuai00726681fb2020-06-03 17:24:38 +0800463 if (g_parent_scope != nullptr) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000464 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
465 }
466 }
467 }
Mark Slee31985722006-05-24 21:45:31 +0000468 }
469
470EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000471 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000472 {
473 pdebug("EnumDefList -> EnumDefList EnumDef");
474 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000475 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000476 }
477|
478 {
479 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000480 $$ = new t_enum(g_program);
Jens Geyerae0b22c2014-09-04 23:04:21 +0200481 y_enum_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000482 }
483
484EnumDef:
Jens Geyerae0b22c2014-09-04 23:04:21 +0200485 CaptureDocText EnumValue TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000486 {
Jens Geyerae0b22c2014-09-04 23:04:21 +0200487 pdebug("EnumDef -> EnumValue");
488 $$ = $2;
zeshuai00726681fb2020-06-03 17:24:38 +0800489 if ($1 != nullptr) {
ccheeverf53b5cf2007-02-05 20:33:11 +0000490 $$->set_doc($1);
491 }
zeshuai00726681fb2020-06-03 17:24:38 +0800492 if ($3 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000493 $$->annotations_ = $3->annotations_;
494 delete $3;
495 }
Mark Slee30152872006-11-28 01:24:07 +0000496 }
497
Jens Geyerae0b22c2014-09-04 23:04:21 +0200498EnumValue:
499 tok_identifier '=' tok_int_constant
500 {
501 pdebug("EnumValue -> tok_identifier = tok_int_constant");
502 if ($3 < INT32_MIN || $3 > INT32_MAX) {
503 // Note: this used to be just a warning. However, since thrift always
504 // treats enums as i32 values, I'm changing it to a fatal error.
505 // I doubt this will affect many people, but users who run into this
506 // will have to update their thrift files to manually specify the
507 // truncated i32 value that thrift has always been using anyway.
508 failure("64-bit value supplied for enum %s will be truncated.", $1);
509 }
510 y_enum_val = static_cast<int32_t>($3);
511 $$ = new t_enum_value($1, y_enum_val);
512 }
513 |
514 tok_identifier
515 {
516 pdebug("EnumValue -> tok_identifier");
517 validate_simple_identifier( $1);
518 if (y_enum_val == INT32_MAX) {
519 failure("enum value overflow at enum %s", $1);
520 }
521 ++y_enum_val;
522 $$ = new t_enum_value($1, y_enum_val);
523 }
524
Mark Slee30152872006-11-28 01:24:07 +0000525Const:
526 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
527 {
528 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000529 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200530 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000531 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000532 $$ = new t_const($2, $3, $5);
533 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000534
535 g_scope->add_constant($3, $$);
zeshuai00726681fb2020-06-03 17:24:38 +0800536 if (g_parent_scope != nullptr) {
Mark Sleed0767c52007-07-27 22:14:41 +0000537 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
538 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000539 } else {
zeshuai00726681fb2020-06-03 17:24:38 +0800540 $$ = nullptr;
Mark Sleeaa7671d2006-11-29 03:19:31 +0000541 }
Mark Slee30152872006-11-28 01:24:07 +0000542 }
543
544ConstValue:
545 tok_int_constant
546 {
547 pdebug("ConstValue => tok_int_constant");
548 $$ = new t_const_value();
549 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000550 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100551 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000552 }
Mark Slee30152872006-11-28 01:24:07 +0000553 }
554| tok_dub_constant
555 {
556 pdebug("ConstValue => tok_dub_constant");
557 $$ = new t_const_value();
558 $$->set_double($1);
559 }
560| tok_literal
561 {
562 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000563 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000564 }
Mark Slee67fc6342006-11-29 03:37:04 +0000565| tok_identifier
566 {
567 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000568 $$ = new t_const_value();
569 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000570 }
Mark Slee30152872006-11-28 01:24:07 +0000571| ConstList
572 {
573 pdebug("ConstValue => ConstList");
574 $$ = $1;
575 }
576| ConstMap
577 {
578 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000579 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000580 }
581
582ConstList:
583 '[' ConstListContents ']'
584 {
585 pdebug("ConstList => [ ConstListContents ]");
586 $$ = $2;
587 }
588
589ConstListContents:
590 ConstListContents ConstValue CommaOrSemicolonOptional
591 {
592 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
593 $$ = $1;
594 $$->add_list($2);
595 }
596|
597 {
598 pdebug("ConstListContents =>");
599 $$ = new t_const_value();
600 $$->set_list();
601 }
602
603ConstMap:
604 '{' ConstMapContents '}'
605 {
606 pdebug("ConstMap => { ConstMapContents }");
607 $$ = $2;
608 }
609
610ConstMapContents:
611 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
612 {
613 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
614 $$ = $1;
615 $$->add_map($2, $4);
616 }
617|
618 {
619 pdebug("ConstMapContents =>");
620 $$ = new t_const_value();
621 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000622 }
623
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000624StructHead:
625 tok_struct
626 {
627 $$ = struct_is_struct;
628 }
629| tok_union
630 {
631 $$ = struct_is_union;
632 }
633
Mark Slee31985722006-05-24 21:45:31 +0000634Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000635 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000636 {
637 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200638 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000639 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000640 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000641 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000642 $$->set_name($2);
zeshuai00726681fb2020-06-03 17:24:38 +0800643 if ($7 != nullptr) {
David Reissa2309992008-12-10 01:52:48 +0000644 $$->annotations_ = $7->annotations_;
645 delete $7;
646 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000647 }
Ben Craige9576752013-10-11 08:19:16 -0500648
Mark Slee36bfa2e2007-01-19 20:09:51 +0000649XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000650 tok_xsd_all
651 {
652 $$ = true;
653 }
654|
655 {
656 $$ = false;
657 }
658
Mark Slee36bfa2e2007-01-19 20:09:51 +0000659XsdOptional:
660 tok_xsd_optional
661 {
662 $$ = true;
663 }
664|
665 {
666 $$ = false;
667 }
668
Mark Slee7df0e2a2007-02-06 21:03:18 +0000669XsdNillable:
670 tok_xsd_nillable
671 {
672 $$ = true;
673 }
674|
675 {
676 $$ = false;
677 }
678
Mark Slee21135c32007-02-05 21:52:08 +0000679XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000680 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000681 {
Mark Slee748d83f2007-02-07 01:20:08 +0000682 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000683 }
684|
685 {
zeshuai00726681fb2020-06-03 17:24:38 +0800686 $$ = nullptr;
Mark Slee21135c32007-02-05 21:52:08 +0000687 }
688
Mark Slee9cb7c612006-09-01 22:17:45 +0000689Xception:
Roger Meier30877382012-09-17 21:18:05 +0000690 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000691 {
692 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200693 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000694 $4->set_name($2);
695 $4->set_xception(true);
696 $$ = $4;
zeshuai00726681fb2020-06-03 17:24:38 +0800697 if ($6 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000698 $$->annotations_ = $6->annotations_;
699 delete $6;
700 }
Mark Slee31985722006-05-24 21:45:31 +0000701 }
702
703Service:
Roger Meier30877382012-09-17 21:18:05 +0000704 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000705 {
706 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200707 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000708 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000709 $$->set_name($2);
710 $$->set_extends($3);
zeshuai00726681fb2020-06-03 17:24:38 +0800711 if ($9 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000712 $$->annotations_ = $9->annotations_;
713 delete $9;
714 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000715 }
716
Mark Slee78165722007-09-10 22:08:49 +0000717FlagArgs:
718 {
719 g_arglist = 1;
720 }
721
722UnflagArgs:
723 {
724 g_arglist = 0;
725 }
726
Mark Slee36bfa2e2007-01-19 20:09:51 +0000727Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000728 tok_extends tok_identifier
729 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000730 pdebug("Extends -> tok_extends tok_identifier");
zeshuai00726681fb2020-06-03 17:24:38 +0800731 $$ = nullptr;
Mark Sleef0712dc2006-10-25 19:03:57 +0000732 if (g_parse_mode == PROGRAM) {
733 $$ = g_scope->get_service($2);
zeshuai00726681fb2020-06-03 17:24:38 +0800734 if ($$ == nullptr) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000735 yyerror("Service \"%s\" has not been defined.", $2);
736 exit(1);
737 }
738 }
739 }
740|
741 {
zeshuai00726681fb2020-06-03 17:24:38 +0800742 $$ = nullptr;
Mark Slee31985722006-05-24 21:45:31 +0000743 }
744
745FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000746 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000747 {
748 pdebug("FunctionList -> FunctionList Function");
749 $$ = $1;
750 $1->add_function($2);
751 }
752|
753 {
754 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000755 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000756 }
757
758Function:
Roger Meier30877382012-09-17 21:18:05 +0000759 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000760 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200761 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000762 $6->set_name(std::string($4) + "_args");
763 $$ = new t_function($3, $4, $6, $8, $2);
zeshuai00726681fb2020-06-03 17:24:38 +0800764 if ($1 != nullptr) {
ccheeverf53b5cf2007-02-05 20:33:11 +0000765 $$->set_doc($1);
766 }
zeshuai00726681fb2020-06-03 17:24:38 +0800767 if ($9 != nullptr) {
Roger Meier30877382012-09-17 21:18:05 +0000768 $$->annotations_ = $9->annotations_;
769 delete $9;
770 }
Mark Slee31985722006-05-24 21:45:31 +0000771 }
772
David Reiss6985a422009-03-24 20:00:47 +0000773Oneway:
774 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000775 {
Mark Slee52f643d2006-08-09 00:03:43 +0000776 $$ = true;
777 }
Jens Geyer154d1542022-09-10 14:30:15 +0200778| tok_async // deprecated
779 {
780 $$ = true;
781 }
Mark Slee52f643d2006-08-09 00:03:43 +0000782|
783 {
784 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000785 }
786
Mark Slee36bfa2e2007-01-19 20:09:51 +0000787Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000788 tok_throws '(' FieldList ')'
789 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000790 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000791 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000792 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000793 yyerror("Throws clause may not contain non-exception types");
794 exit(1);
795 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000796 }
797|
798 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000800 }
801
Mark Slee31985722006-05-24 21:45:31 +0000802FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000803 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000804 {
805 pdebug("FieldList -> FieldList , Field");
806 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000807 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100808 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 +0000809 exit(1);
810 }
Mark Slee31985722006-05-24 21:45:31 +0000811 }
812|
813 {
814 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000815 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000816 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000817 }
818
819Field:
Jens Geyer154d1542022-09-10 14:30:15 +0200820 CaptureDocText FieldIdentifier FieldRequiredness FieldType FieldReference FieldName FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000821 {
Jens Geyer154d1542022-09-10 14:30:15 +0200822 pdebug("tok_int_constant : Field -> FieldType FieldName");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000823 if ($2.auto_assigned) {
Jens Geyer885c6792014-05-02 21:31:55 +0200824 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 +0000825 if (g_strict >= 192) {
826 yyerror("Implicit field keys are deprecated and not allowed with -strict");
827 exit(1);
828 }
Mark Slee31985722006-05-24 21:45:31 +0000829 }
Jens Geyer885c6792014-05-02 21:31:55 +0200830 validate_simple_identifier($6);
831 $$ = new t_field($4, $6, $2.value);
832 $$->set_reference($5);
David Reiss8320a922007-08-14 19:59:26 +0000833 $$->set_req($3);
zeshuai00726681fb2020-06-03 17:24:38 +0800834 if ($7 != nullptr) {
Jens Geyer885c6792014-05-02 21:31:55 +0200835 g_scope->resolve_const_value($7, $4);
836 validate_field_value($$, $7);
837 $$->set_value($7);
Mark Slee7ff32452007-02-01 05:26:18 +0000838 }
Jens Geyer885c6792014-05-02 21:31:55 +0200839 $$->set_xsd_optional($8);
840 $$->set_xsd_nillable($9);
zeshuai00726681fb2020-06-03 17:24:38 +0800841 if ($1 != nullptr) {
ccheeverf53b5cf2007-02-05 20:33:11 +0000842 $$->set_doc($1);
843 }
zeshuai00726681fb2020-06-03 17:24:38 +0800844 if ($10 != nullptr) {
Jens Geyer885c6792014-05-02 21:31:55 +0200845 $$->set_xsd_attrs($10);
846 }
zeshuai00726681fb2020-06-03 17:24:38 +0800847 if ($11 != nullptr) {
Jens Geyer885c6792014-05-02 21:31:55 +0200848 $$->annotations_ = $11->annotations_;
849 delete $11;
David Reiss53c10e02010-03-05 07:51:51 +0000850 }
Mark Slee31985722006-05-24 21:45:31 +0000851 }
Mark Slee7ff32452007-02-01 05:26:18 +0000852
Jens Geyer154d1542022-09-10 14:30:15 +0200853FieldName: // identifiers and everything that could be one if it would not be identified as a different token already and excluding the "xsd*" keywords to follow a FieldName
854 tok_identifier
855 {
856 pdebug("FieldName -> tok_identifier");
857 $$ = $1;
858 }
859| tok_namespace
860 {
861 pdebug("FieldName -> tok_namespace");
862 $$ = strdup("namespace");
863 }
864| tok_cpp_include
865 {
866 pdebug("FieldName -> tok_cpp_include");
867 $$ = strdup("cpp_include");
868 }
869/* see THRIFT-5627 "More consistent syntax for cpp_type" -> activate when this issue is resolved
870| tok_cpp_type
871 {
872 pdebug("FieldName -> tok_cpp_type");
873 $$ = $strdup("cpp_type");
874 }
875*/
876| tok_include
877 {
878 pdebug("FieldName -> tok_include");
879 $$ = strdup("include");
880 }
881| tok_void
882 {
883 pdebug("FieldName -> tok_void");
884 $$ = strdup("void");
885 }
886| tok_bool
887 {
888 pdebug("FieldName -> tok_bool");
889 $$ = strdup("bool");
890 }
891| tok_byte
892 {
893 pdebug("FieldName -> tok_byte");
894 $$ = strdup("byte");
895 }
896| tok_i8
897 {
898 pdebug("FieldName -> tok_i8");
899 $$ = strdup("i8");
900 }
901| tok_i16
902 {
903 pdebug("FieldName -> tok_i16");
904 $$ = strdup("i16");
905 }
906| tok_i32
907 {
908 pdebug("FieldName -> tok_i32");
909 $$ = strdup("i32");
910 }
911| tok_i64
912 {
913 pdebug("FieldName -> tok_i64");
914 $$ = strdup("i64");
915 }
916| tok_double
917 {
918 pdebug("FieldName -> tok_double");
919 $$ = strdup("double");
920 }
921| tok_string
922 {
923 pdebug("FieldName -> tok_string");
924 $$ = strdup("string");
925 }
926| tok_binary
927 {
928 pdebug("FieldName -> tok_binary");
929 $$ = strdup("binary");
930 }
931| tok_uuid
932 {
933 pdebug("FieldName -> tok_uuid");
934 $$ = strdup("uuid");
935 }
936| tok_map
937 {
938 pdebug("FieldName -> tok_map");
939 $$ = strdup("map");
940 }
941| tok_list
942 {
943 pdebug("FieldName -> tok_list");
944 $$ = strdup("list");
945 }
946| tok_set
947 {
948 pdebug("FieldName -> tok_set");
949 $$ = strdup("set");
950 }
951| tok_oneway
952 {
953 pdebug("FieldName -> tok_oneway");
954 $$ = strdup("oneway");
955 }
956| tok_async
957 {
958 pdebug("FieldName -> tok_async");
959 $$ = strdup("async");
960 }
961| tok_typedef
962 {
963 pdebug("FieldName -> tok_typedef");
964 $$ = strdup("typedef");
965 }
966| tok_struct
967 {
968 pdebug("FieldName -> tok_struct");
969 $$ = strdup("struct");
970 }
971| tok_union
972 {
973 pdebug("FieldName -> tok_union");
974 $$ = strdup("union");
975 }
976| tok_xception
977 {
978 pdebug("FieldName -> tok_xception");
979 $$ = strdup("exception");
980 }
981| tok_extends
982 {
983 pdebug("FieldName -> tok_extends");
984 $$ = strdup("extends");
985 }
986| tok_throws
987 {
988 pdebug("FieldName -> tok_throws");
989 $$ = strdup("throws");
990 }
991| tok_service
992 {
993 pdebug("FieldName -> tok_service");
994 $$ = strdup("service");
995 }
996| tok_enum
997 {
998 pdebug("FieldName -> tok_enum");
999 $$ = strdup("enum");
1000 }
1001| tok_const
1002 {
1003 pdebug("FieldName -> tok_const");
1004 $$ = strdup("const");
1005 }
1006| tok_required
1007 {
1008 pdebug("FieldName -> tok_required");
1009 $$ = strdup("required");
1010 }
1011| tok_optional
1012 {
1013 pdebug("FieldName -> tok_optional");
1014 $$ = strdup("optional");
1015 }
1016
1017
Mark Slee7ff32452007-02-01 05:26:18 +00001018FieldIdentifier:
1019 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +00001020 {
Mark Slee7ff32452007-02-01 05:26:18 +00001021 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001022 if (g_allow_neg_field_keys) {
1023 /*
1024 * g_allow_neg_field_keys exists to allow users to add explicitly
1025 * specified key values to old .thrift files without breaking
1026 * protocol compatibility.
1027 */
1028 if ($1 != y_field_val) {
1029 /*
1030 * warn if the user-specified negative value isn't what
1031 * thrift would have auto-assigned.
1032 */
Roger Meier5f2d34e2013-11-16 16:43:41 +01001033 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001034 "auto-assigned by thrift (%d).\n", $1, y_field_val);
1035 }
1036 /*
1037 * Leave $1 as-is, and update y_field_val to be one less than $1.
1038 * The FieldList parsing will catch any duplicate key values.
1039 */
Ben Craige9576752013-10-11 08:19:16 -05001040 y_field_val = static_cast<int32_t>($1 - 1);
1041 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001042 $$.auto_assigned = false;
1043 } else {
Ben Craige9576752013-10-11 08:19:16 -05001044 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001045 $1);
1046 $$.value = y_field_val--;
1047 $$.auto_assigned = true;
1048 }
1049 } else {
Ben Craige9576752013-10-11 08:19:16 -05001050 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001051 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +00001052 }
Jens Geyer06340a42016-03-25 01:34:03 +02001053 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
1054 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
1055 $$.value, SHRT_MIN, SHRT_MAX);
1056 }
Mark Slee7ff32452007-02-01 05:26:18 +00001057 }
1058|
1059 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001060 $$.value = y_field_val--;
1061 $$.auto_assigned = true;
Jens Geyer06340a42016-03-25 01:34:03 +02001062 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
1063 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
1064 $$.value, SHRT_MIN, SHRT_MAX);
1065 }
Mark Slee7ff32452007-02-01 05:26:18 +00001066 }
1067
Jens Geyer885c6792014-05-02 21:31:55 +02001068FieldReference:
1069 tok_reference
1070 {
1071 $$ = true;
1072 }
1073|
1074 {
1075 $$ = false;
1076 }
1077
David Reiss8320a922007-08-14 19:59:26 +00001078FieldRequiredness:
1079 tok_required
1080 {
David Reiss45603e92009-09-02 22:15:55 +00001081 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001082 }
1083| tok_optional
1084 {
Mark Slee78165722007-09-10 22:08:49 +00001085 if (g_arglist) {
1086 if (g_parse_mode == PROGRAM) {
1087 pwarning(1, "optional keyword is ignored in argument lists.\n");
1088 }
David Reiss204420f2008-01-11 20:59:03 +00001089 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001090 } else {
David Reiss204420f2008-01-11 20:59:03 +00001091 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001092 }
David Reiss8320a922007-08-14 19:59:26 +00001093 }
1094|
1095 {
David Reiss204420f2008-01-11 20:59:03 +00001096 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001097 }
1098
Mark Slee7ff32452007-02-01 05:26:18 +00001099FieldValue:
1100 '=' ConstValue
1101 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001102 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001103 $$ = $2;
1104 } else {
zeshuai00726681fb2020-06-03 17:24:38 +08001105 $$ = nullptr;
Mark Slee7ff32452007-02-01 05:26:18 +00001106 }
1107 }
1108|
1109 {
zeshuai00726681fb2020-06-03 17:24:38 +08001110 $$ = nullptr;
Mark Sleef0712dc2006-10-25 19:03:57 +00001111 }
Mark Slee31985722006-05-24 21:45:31 +00001112
Mark Slee31985722006-05-24 21:45:31 +00001113FunctionType:
1114 FieldType
1115 {
Mark Sleee8540632006-05-30 09:24:40 +00001116 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001117 $$ = $1;
1118 }
1119| tok_void
1120 {
Mark Sleee8540632006-05-30 09:24:40 +00001121 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001122 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001123 }
1124
1125FieldType:
1126 tok_identifier
1127 {
Mark Sleee8540632006-05-30 09:24:40 +00001128 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001129 if (g_parse_mode == INCLUDES) {
1130 // Ignore identifiers in include mode
zeshuai00726681fb2020-06-03 17:24:38 +08001131 $$ = nullptr;
Mark Sleef0712dc2006-10-25 19:03:57 +00001132 } else {
1133 // Lookup the identifier in the current scope
1134 $$ = g_scope->get_type($1);
zeshuai00726681fb2020-06-03 17:24:38 +08001135 if ($$ == nullptr) {
jfarrelle0e83162014-04-08 22:45:01 -04001136 /*
1137 * Either this type isn't yet declared, or it's never
1138 declared. Either way allow it and we'll figure it out
1139 during generation.
1140 */
Jens Geyerd000b242014-04-13 21:58:47 +02001141 $$ = new t_typedef(g_program, $1, true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001142 }
Mark Sleee8540632006-05-30 09:24:40 +00001143 }
Mark Slee31985722006-05-24 21:45:31 +00001144 }
1145| BaseType
1146 {
Mark Sleee8540632006-05-30 09:24:40 +00001147 pdebug("FieldType -> BaseType");
1148 $$ = $1;
1149 }
1150| ContainerType
1151 {
1152 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001153 $$ = $1;
1154 }
1155
David Reissc8e30052009-07-27 17:02:42 +00001156BaseType: SimpleBaseType TypeAnnotations
1157 {
1158 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
zeshuai00726681fb2020-06-03 17:24:38 +08001159 if ($2 != nullptr) {
David Reissc8e30052009-07-27 17:02:42 +00001160 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1161 $$->annotations_ = $2->annotations_;
1162 delete $2;
1163 } else {
1164 $$ = $1;
1165 }
1166 }
1167
1168SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001169 tok_string
1170 {
1171 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001172 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001173 }
Mark Slee8d725a22007-04-13 01:57:12 +00001174| tok_binary
1175 {
1176 pdebug("BaseType -> tok_binary");
1177 $$ = g_type_binary;
1178 }
Jens Geyer62445c12022-06-29 00:00:00 +02001179| tok_uuid
1180 {
1181 pdebug("BaseType -> tok_uuid");
1182 $$ = g_type_uuid;
1183 }
Mark Slee78f58e22006-09-02 04:17:07 +00001184| tok_bool
1185 {
1186 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001187 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001188 }
Jens Geyer154d1542022-09-10 14:30:15 +02001189| tok_byte
1190 {
1191 pdebug("BaseType -> tok_byte");
1192 $$ = g_type_i8; // byte is signed in Thrift, just an alias for i8
1193 }
Jens Geyer40c28d32015-10-20 23:13:02 +02001194| tok_i8
Mark Slee31985722006-05-24 21:45:31 +00001195 {
Jens Geyer40c28d32015-10-20 23:13:02 +02001196 pdebug("BaseType -> tok_i8");
1197 $$ = g_type_i8;
Mark Slee31985722006-05-24 21:45:31 +00001198 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001199| tok_i16
1200 {
1201 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001202 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001203 }
Mark Slee31985722006-05-24 21:45:31 +00001204| tok_i32
1205 {
1206 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001207 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001208 }
Mark Slee31985722006-05-24 21:45:31 +00001209| tok_i64
1210 {
1211 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001212 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001213 }
Mark Sleec98d0502006-09-06 02:42:25 +00001214| tok_double
1215 {
1216 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001217 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001218 }
Mark Slee31985722006-05-24 21:45:31 +00001219
David Reissa2309992008-12-10 01:52:48 +00001220ContainerType: SimpleContainerType TypeAnnotations
1221 {
1222 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1223 $$ = $1;
zeshuai00726681fb2020-06-03 17:24:38 +08001224 if ($2 != nullptr) {
David Reissa2309992008-12-10 01:52:48 +00001225 $$->annotations_ = $2->annotations_;
1226 delete $2;
1227 }
1228 }
1229
1230SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001231 MapType
1232 {
David Reissa2309992008-12-10 01:52:48 +00001233 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001234 $$ = $1;
1235 }
1236| SetType
1237 {
David Reissa2309992008-12-10 01:52:48 +00001238 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001239 $$ = $1;
1240 }
1241| ListType
1242 {
David Reissa2309992008-12-10 01:52:48 +00001243 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001244 $$ = $1;
1245 }
1246
1247MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001248 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001249 {
1250 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001251 $$ = new t_map($4, $6);
zeshuai00726681fb2020-06-03 17:24:38 +08001252 if ($2 != nullptr) {
Mark Slee4f8da1d2006-10-12 02:47:27 +00001253 ((t_container*)$$)->set_cpp_name(std::string($2));
1254 }
Mark Sleee8540632006-05-30 09:24:40 +00001255 }
1256
1257SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001258 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001259 {
1260 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001261 $$ = new t_set($4);
zeshuai00726681fb2020-06-03 17:24:38 +08001262 if ($2 != nullptr) {
Mark Slee4f8da1d2006-10-12 02:47:27 +00001263 ((t_container*)$$)->set_cpp_name(std::string($2));
1264 }
Mark Sleee8540632006-05-30 09:24:40 +00001265 }
1266
1267ListType:
Jens Geyer9b09a112022-09-14 00:29:07 +02001268 tok_list CppType '<' FieldType '>' CppType // the second CppType is for compatibility reasons = deprecated
Mark Sleee8540632006-05-30 09:24:40 +00001269 {
1270 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer9b09a112022-09-14 00:29:07 +02001271 check_for_list_of_bytes($4);
1272 $$ = new t_list($4);
1273 if ($2 != nullptr) {
1274 ((t_container*)$$)->set_cpp_name(std::string($2));
1275 }
1276 if ($6 != nullptr) {
1277 ((t_container*)$$)->set_cpp_name(std::string($6));
1278 pwarning(1, "The syntax 'list<type> cpp_type \"c++ type\"' is deprecated. Use 'list cpp_type \"c++ type\" <type>' instead.\n");
1279 }
1280 if (($2 != nullptr) && ($6 != nullptr)) {
1281 pwarning(1, "Two cpp_types clauses at list<%>\n", $2);
Mark Slee4f8da1d2006-10-12 02:47:27 +00001282 }
1283 }
1284
Mark Slee36bfa2e2007-01-19 20:09:51 +00001285CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001286 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001287 {
Mark Sleeafc76542007-02-09 21:55:44 +00001288 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001289 }
1290|
1291 {
zeshuai00726681fb2020-06-03 17:24:38 +08001292 $$ = nullptr;
Mark Sleee8540632006-05-30 09:24:40 +00001293 }
1294
David Reissa2309992008-12-10 01:52:48 +00001295TypeAnnotations:
1296 '(' TypeAnnotationList ')'
1297 {
1298 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1299 $$ = $2;
1300 }
1301|
1302 {
zeshuai00726681fb2020-06-03 17:24:38 +08001303 $$ = nullptr;
David Reissa2309992008-12-10 01:52:48 +00001304 }
1305
1306TypeAnnotationList:
1307 TypeAnnotationList TypeAnnotation
1308 {
1309 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1310 $$ = $1;
Simon Wangd5927a92021-09-13 19:50:45 +08001311 $$->annotations_[$2->key].push_back($2->val);
David Reissa2309992008-12-10 01:52:48 +00001312 delete $2;
1313 }
1314|
1315 {
1316 /* Just use a dummy structure to hold the annotations. */
1317 $$ = new t_struct(g_program);
1318 }
1319
1320TypeAnnotation:
Jens Geyer89847df2014-05-02 23:50:04 +02001321 tok_identifier TypeAnnotationValue CommaOrSemicolonOptional
David Reissa2309992008-12-10 01:52:48 +00001322 {
Jens Geyer89847df2014-05-02 23:50:04 +02001323 pdebug("TypeAnnotation -> TypeAnnotationValue");
David Reissa2309992008-12-10 01:52:48 +00001324 $$ = new t_annotation;
1325 $$->key = $1;
Jens Geyer89847df2014-05-02 23:50:04 +02001326 $$->val = $2;
1327 }
1328
1329TypeAnnotationValue:
1330 '=' tok_literal
1331 {
1332 pdebug("TypeAnnotationValue -> = tok_literal");
1333 $$ = $2;
1334 }
1335|
1336 {
1337 pdebug("TypeAnnotationValue ->");
1338 $$ = strdup("1");
David Reissa2309992008-12-10 01:52:48 +00001339 }
1340
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001341%%