blob: 51de58f8e26edbbe9749679ed2ccc8bedc586a69 [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"
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +090041#include "common.h"
Mark Slee31985722006-05-24 21:45:31 +000042#include "globals.h"
43#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000044#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000045
Ben Craige9576752013-10-11 08:19:16 -050046#ifdef _MSC_VER
47//warning C4065: switch statement contains 'default' but no 'case' labels
48#pragma warning(disable:4065)
49#endif
50
Mark Sleef5377b32006-10-10 01:42:59 +000051/**
52 * This global variable is used for automatic numbering of field indices etc.
53 * when parsing the members of a struct. Field values are automatically
54 * assigned starting from -1 and working their way down.
55 */
Mark Slee9cb7c612006-09-01 22:17:45 +000056int y_field_val = -1;
Jens Geyerae0b22c2014-09-04 23:04:21 +020057/**
58 * This global variable is used for automatic numbering of enum values.
59 * y_enum_val is the last value assigned; the next auto-assigned value will be
60 * y_enum_val+1, and then it continues working upwards. Explicitly specified
61 * enum values reset y_enum_val to that value.
62 */
63int32_t y_enum_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000064int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000065const int struct_is_struct = 0;
66const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000067
68%}
69
Mark Sleef5377b32006-10-10 01:42:59 +000070/**
71 * This structure is used by the parser to hold the data types associated with
72 * various parse nodes.
73 */
Mark Slee31985722006-05-24 21:45:31 +000074%union {
Mark Slee30152872006-11-28 01:24:07 +000075 char* id;
David Reissf1454162008-06-30 20:45:47 +000076 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000077 double dconst;
78 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000079 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000080 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000081 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000082 t_typedef* ttypedef;
83 t_enum* tenum;
84 t_enum_value* tenumv;
85 t_const* tconst;
86 t_const_value* tconstv;
87 t_struct* tstruct;
88 t_service* tservice;
89 t_function* tfunction;
90 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000091 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000092 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000093 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000094 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000095}
96
Mark Sleef5377b32006-10-10 01:42:59 +000097/**
98 * Strings identifier
99 */
Mark Slee31985722006-05-24 21:45:31 +0000100%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000101%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +0000102%token<dtext> tok_doctext
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_include
116%token tok_cpp_type
Mark Slee782abbb2007-01-19 00:17:02 +0000117%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000118%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000119%token tok_xsd_nillable
Mark Slee21135c32007-02-05 21:52:08 +0000120%token tok_xsd_attrs
Mark Slee9cb7c612006-09-01 22:17:45 +0000121
Mark Sleef5377b32006-10-10 01:42:59 +0000122/**
123 * Base datatype keywords
124 */
125%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000126%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000127%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000128%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000129%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000130%token tok_senum
Jens Geyer40c28d32015-10-20 23:13:02 +0200131%token tok_i8
Mark Slee9cb7c612006-09-01 22:17:45 +0000132%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000133%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000134%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000135%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000136
Mark Sleef5377b32006-10-10 01:42:59 +0000137/**
138 * Complex type keywords
139 */
Mark Slee31985722006-05-24 21:45:31 +0000140%token tok_map
141%token tok_list
142%token tok_set
143
Mark Sleef5377b32006-10-10 01:42:59 +0000144/**
145 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000146 */
David Reiss6985a422009-03-24 20:00:47 +0000147%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000148
Mark Sleef5377b32006-10-10 01:42:59 +0000149/**
150 * Thrift language keywords
151 */
Mark Slee31985722006-05-24 21:45:31 +0000152%token tok_typedef
153%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000154%token tok_xception
155%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000156%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000157%token tok_service
158%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000159%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000160%token tok_required
161%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000162%token tok_union
Jens Geyer885c6792014-05-02 21:31:55 +0200163%token tok_reference
Mark Slee31985722006-05-24 21:45:31 +0000164
Mark Sleef5377b32006-10-10 01:42:59 +0000165/**
166 * Grammar nodes
167 */
168
Mark Slee31985722006-05-24 21:45:31 +0000169%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000170%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000171%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000172%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000173%type<ttype> MapType
174%type<ttype> SetType
175%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000176
David Reisscdffe262007-08-14 17:12:31 +0000177%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000178%type<ttype> TypeDefinition
179
Mark Slee31985722006-05-24 21:45:31 +0000180%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000181
David Reissa2309992008-12-10 01:52:48 +0000182%type<ttype> TypeAnnotations
183%type<ttype> TypeAnnotationList
184%type<tannot> TypeAnnotation
Jens Geyer89847df2014-05-02 23:50:04 +0200185%type<id> TypeAnnotationValue
David Reissa2309992008-12-10 01:52:48 +0000186
Mark Slee31985722006-05-24 21:45:31 +0000187%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000188%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000189%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000190%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000191%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000192%type<tstruct> FieldList
Jens Geyer885c6792014-05-02 21:31:55 +0200193%type<tbool> FieldReference
Mark Slee31985722006-05-24 21:45:31 +0000194
195%type<tenum> Enum
196%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000197%type<tenumv> EnumDef
Jens Geyerae0b22c2014-09-04 23:04:21 +0200198%type<tenumv> EnumValue
Mark Slee30152872006-11-28 01:24:07 +0000199
Mark Slee6a47fed2007-02-07 02:40:59 +0000200%type<ttypedef> Senum
201%type<tbase> SenumDefList
202%type<id> SenumDef
203
Mark Slee30152872006-11-28 01:24:07 +0000204%type<tconst> Const
205%type<tconstv> ConstValue
206%type<tconstv> ConstList
207%type<tconstv> ConstListContents
208%type<tconstv> ConstMap
209%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000210
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000211%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000212%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000213%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000214%type<tservice> Service
215
216%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000217%type<ttype> FunctionType
218%type<tservice> FunctionList
219
Mark Slee36bfa2e2007-01-19 20:09:51 +0000220%type<tstruct> Throws
221%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000222%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000223%type<tbool> XsdAll
224%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000225%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000226%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000227%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000228
David Reisscbd4bac2007-08-14 17:12:33 +0000229%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000230
Mark Slee31985722006-05-24 21:45:31 +0000231%%
232
Mark Sleef5377b32006-10-10 01:42:59 +0000233/**
234 * Thrift Grammar Implementation.
235 *
236 * For the most part this source file works its way top down from what you
237 * might expect to find in a typical .thrift file, i.e. type definitions and
238 * namespaces up top followed by service definitions using those types.
239 */
Mark Slee31985722006-05-24 21:45:31 +0000240
241Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000242 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000243 {
244 pdebug("Program -> Headers DefinitionList");
Jens Geyere8379b52014-01-25 00:59:45 +0100245 if((g_program_doctext_candidate != NULL) && (g_program_doctext_status != ALREADY_PROCESSED))
246 {
247 g_program->set_doc(g_program_doctext_candidate);
248 g_program_doctext_status = ALREADY_PROCESSED;
David Reissc2532a92007-07-30 23:46:11 +0000249 }
David Reisscbd4bac2007-08-14 17:12:33 +0000250 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000251 }
252
David Reisscbd4bac2007-08-14 17:12:33 +0000253CaptureDocText:
254 {
255 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000256 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000257 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000258 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000259 $$ = NULL;
260 }
261 }
262
263/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
264DestroyDocText:
265 {
266 if (g_parse_mode == PROGRAM) {
267 clear_doctext();
268 }
269 }
270
271/* We have to DestroyDocText here, otherwise it catches the doctext
272 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000273HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000274 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 {
276 pdebug("HeaderList -> HeaderList Header");
277 }
278|
279 {
280 pdebug("HeaderList -> ");
281 }
282
283Header:
284 Include
285 {
286 pdebug("Header -> Include");
287 }
BCGcc193c12015-11-12 21:02:51 -0500288| tok_namespace tok_identifier tok_identifier TypeAnnotations
Mark Sleef0712dc2006-10-25 19:03:57 +0000289 {
David Reiss79eca142008-02-27 01:55:13 +0000290 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100291 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000292 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000293 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000294 }
BCGcc193c12015-11-12 21:02:51 -0500295 if ($4 != NULL) {
296 g_program->set_namespace_annotations($2, $4->annotations_);
297 delete $4;
298 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000299 }
David Reissfb790d72010-09-02 16:41:45 +0000300| tok_namespace '*' tok_identifier
301 {
302 pdebug("Header -> tok_namespace * tok_identifier");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100303 declare_valid_program_doctext();
David Reissfb790d72010-09-02 16:41:45 +0000304 if (g_parse_mode == PROGRAM) {
305 g_program->set_namespace("*", $3);
306 }
307 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000308| tok_cpp_include tok_literal
309 {
310 pdebug("Header -> tok_cpp_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100311 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000312 if (g_parse_mode == PROGRAM) {
313 g_program->add_cpp_include($2);
314 }
315 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000316
317Include:
318 tok_include tok_literal
319 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000320 pdebug("Include -> tok_include tok_literal");
Roger Meier4f4b15b2014-11-05 16:51:04 +0100321 declare_valid_program_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000322 if (g_parse_mode == INCLUDES) {
323 std::string path = include_file(std::string($2));
324 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000325 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000326 }
327 }
328 }
Mark Slee31985722006-05-24 21:45:31 +0000329
330DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000331 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000332 {
333 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000334 if ($2 != NULL && $3 != NULL) {
335 $3->set_doc($2);
336 }
Mark Slee31985722006-05-24 21:45:31 +0000337 }
338|
339 {
340 pdebug("DefinitionList -> ");
341 }
342
343Definition:
Mark Slee30152872006-11-28 01:24:07 +0000344 Const
345 {
346 pdebug("Definition -> Const");
347 if (g_parse_mode == PROGRAM) {
348 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000349 }
David Reisscdffe262007-08-14 17:12:31 +0000350 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000351 }
352| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000353 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000354 pdebug("Definition -> TypeDefinition");
355 if (g_parse_mode == PROGRAM) {
356 g_scope->add_type($1->get_name(), $1);
357 if (g_parent_scope != NULL) {
358 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
359 }
Roger Meierba406d32013-07-15 22:41:34 +0200360 if (! g_program->is_unique_typename($1)) {
361 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
362 exit(1);
363 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000364 }
David Reisscdffe262007-08-14 17:12:31 +0000365 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000366 }
Mark Slee31985722006-05-24 21:45:31 +0000367| Service
368 {
369 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000370 if (g_parse_mode == PROGRAM) {
371 g_scope->add_service($1->get_name(), $1);
372 if (g_parent_scope != NULL) {
373 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
374 }
375 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200376 if (! g_program->is_unique_typename($1)) {
377 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
378 exit(1);
379 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000380 }
David Reisscdffe262007-08-14 17:12:31 +0000381 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000382 }
383
Mark Sleef0712dc2006-10-25 19:03:57 +0000384TypeDefinition:
385 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000386 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000387 pdebug("TypeDefinition -> Typedef");
388 if (g_parse_mode == PROGRAM) {
389 g_program->add_typedef($1);
390 }
391 }
392| Enum
393 {
394 pdebug("TypeDefinition -> Enum");
395 if (g_parse_mode == PROGRAM) {
396 g_program->add_enum($1);
397 }
398 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000399| Senum
400 {
401 pdebug("TypeDefinition -> Senum");
402 if (g_parse_mode == PROGRAM) {
403 g_program->add_typedef($1);
404 }
405 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000406| 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;
Roger Meier30877382012-09-17 21:18:05 +0000436 if ($4 != NULL) {
437 $$->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);
Roger Meier30877382012-09-17 21:18:05 +0000449 if ($6 != NULL) {
450 $$->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));
463 if (g_parent_scope != NULL) {
464 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;
ccheeverf53b5cf2007-02-05 20:33:11 +0000489 if ($1 != NULL) {
490 $$->set_doc($1);
491 }
Jens Geyer86b309c2014-09-11 23:54:35 +0200492 if ($3 != NULL) {
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 Slee6a47fed2007-02-07 02:40:59 +0000525Senum:
Roger Meier30877382012-09-17 21:18:05 +0000526 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000527 {
528 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200529 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000530 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000531 if ($6 != NULL) {
532 $$->annotations_ = $6->annotations_;
533 delete $6;
534 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000535 }
536
537SenumDefList:
538 SenumDefList SenumDef
539 {
540 pdebug("SenumDefList -> SenumDefList SenumDef");
541 $$ = $1;
542 $$->add_string_enum_val($2);
543 }
544|
545 {
546 pdebug("SenumDefList -> ");
547 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
548 $$->set_string_enum(true);
549 }
550
551SenumDef:
552 tok_literal CommaOrSemicolonOptional
553 {
554 pdebug("SenumDef -> tok_literal");
555 $$ = $1;
556 }
557
Mark Slee30152872006-11-28 01:24:07 +0000558Const:
559 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
560 {
561 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000562 if (g_parse_mode == PROGRAM) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200563 validate_simple_identifier( $3);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000564 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000565 $$ = new t_const($2, $3, $5);
566 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000567
568 g_scope->add_constant($3, $$);
569 if (g_parent_scope != NULL) {
570 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
571 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000572 } else {
573 $$ = NULL;
574 }
Mark Slee30152872006-11-28 01:24:07 +0000575 }
576
577ConstValue:
578 tok_int_constant
579 {
580 pdebug("ConstValue => tok_int_constant");
581 $$ = new t_const_value();
582 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000583 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
Roger Meier5f2d34e2013-11-16 16:43:41 +0100584 pwarning(1, "64-bit constant \"%" PRIi64"\" may not work in all languages.\n", $1);
David Reissf1454162008-06-30 20:45:47 +0000585 }
Mark Slee30152872006-11-28 01:24:07 +0000586 }
587| tok_dub_constant
588 {
589 pdebug("ConstValue => tok_dub_constant");
590 $$ = new t_const_value();
591 $$->set_double($1);
592 }
593| tok_literal
594 {
595 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000596 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000597 }
Mark Slee67fc6342006-11-29 03:37:04 +0000598| tok_identifier
599 {
600 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000601 $$ = new t_const_value();
602 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000603 }
Mark Slee30152872006-11-28 01:24:07 +0000604| ConstList
605 {
606 pdebug("ConstValue => ConstList");
607 $$ = $1;
608 }
609| ConstMap
610 {
611 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000612 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000613 }
614
615ConstList:
616 '[' ConstListContents ']'
617 {
618 pdebug("ConstList => [ ConstListContents ]");
619 $$ = $2;
620 }
621
622ConstListContents:
623 ConstListContents ConstValue CommaOrSemicolonOptional
624 {
625 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
626 $$ = $1;
627 $$->add_list($2);
628 }
629|
630 {
631 pdebug("ConstListContents =>");
632 $$ = new t_const_value();
633 $$->set_list();
634 }
635
636ConstMap:
637 '{' ConstMapContents '}'
638 {
639 pdebug("ConstMap => { ConstMapContents }");
640 $$ = $2;
641 }
642
643ConstMapContents:
644 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
645 {
646 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
647 $$ = $1;
648 $$->add_map($2, $4);
649 }
650|
651 {
652 pdebug("ConstMapContents =>");
653 $$ = new t_const_value();
654 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000655 }
656
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000657StructHead:
658 tok_struct
659 {
660 $$ = struct_is_struct;
661 }
662| tok_union
663 {
664 $$ = struct_is_union;
665 }
666
Mark Slee31985722006-05-24 21:45:31 +0000667Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000668 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000669 {
670 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200671 validate_simple_identifier( $2);
David Reisscdffe262007-08-14 17:12:31 +0000672 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000673 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000674 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000675 $$->set_name($2);
676 if ($7 != NULL) {
677 $$->annotations_ = $7->annotations_;
678 delete $7;
679 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000680 }
Ben Craige9576752013-10-11 08:19:16 -0500681
Mark Slee36bfa2e2007-01-19 20:09:51 +0000682XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000683 tok_xsd_all
684 {
685 $$ = true;
686 }
687|
688 {
689 $$ = false;
690 }
691
Mark Slee36bfa2e2007-01-19 20:09:51 +0000692XsdOptional:
693 tok_xsd_optional
694 {
695 $$ = true;
696 }
697|
698 {
699 $$ = false;
700 }
701
Mark Slee7df0e2a2007-02-06 21:03:18 +0000702XsdNillable:
703 tok_xsd_nillable
704 {
705 $$ = true;
706 }
707|
708 {
709 $$ = false;
710 }
711
Mark Slee21135c32007-02-05 21:52:08 +0000712XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000713 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000714 {
Mark Slee748d83f2007-02-07 01:20:08 +0000715 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000716 }
717|
718 {
719 $$ = NULL;
720 }
721
Mark Slee9cb7c612006-09-01 22:17:45 +0000722Xception:
Roger Meier30877382012-09-17 21:18:05 +0000723 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000724 {
725 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200726 validate_simple_identifier( $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000727 $4->set_name($2);
728 $4->set_xception(true);
729 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000730 if ($6 != NULL) {
731 $$->annotations_ = $6->annotations_;
732 delete $6;
733 }
Mark Slee31985722006-05-24 21:45:31 +0000734 }
735
736Service:
Roger Meier30877382012-09-17 21:18:05 +0000737 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000738 {
739 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Jens Geyer12c09f42013-08-25 14:16:27 +0200740 validate_simple_identifier( $2);
Mark Slee78165722007-09-10 22:08:49 +0000741 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000742 $$->set_name($2);
743 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000744 if ($9 != NULL) {
745 $$->annotations_ = $9->annotations_;
746 delete $9;
747 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000748 }
749
Mark Slee78165722007-09-10 22:08:49 +0000750FlagArgs:
751 {
752 g_arglist = 1;
753 }
754
755UnflagArgs:
756 {
757 g_arglist = 0;
758 }
759
Mark Slee36bfa2e2007-01-19 20:09:51 +0000760Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000761 tok_extends tok_identifier
762 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000763 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000764 $$ = NULL;
765 if (g_parse_mode == PROGRAM) {
766 $$ = g_scope->get_service($2);
767 if ($$ == NULL) {
768 yyerror("Service \"%s\" has not been defined.", $2);
769 exit(1);
770 }
771 }
772 }
773|
774 {
775 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000776 }
777
778FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000779 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000780 {
781 pdebug("FunctionList -> FunctionList Function");
782 $$ = $1;
783 $1->add_function($2);
784 }
785|
786 {
787 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000788 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000789 }
790
791Function:
Roger Meier30877382012-09-17 21:18:05 +0000792 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000793 {
Jens Geyer12c09f42013-08-25 14:16:27 +0200794 validate_simple_identifier( $4);
ccheeverf53b5cf2007-02-05 20:33:11 +0000795 $6->set_name(std::string($4) + "_args");
796 $$ = new t_function($3, $4, $6, $8, $2);
797 if ($1 != NULL) {
798 $$->set_doc($1);
799 }
Roger Meier30877382012-09-17 21:18:05 +0000800 if ($9 != NULL) {
801 $$->annotations_ = $9->annotations_;
802 delete $9;
803 }
Mark Slee31985722006-05-24 21:45:31 +0000804 }
805
David Reiss6985a422009-03-24 20:00:47 +0000806Oneway:
807 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000808 {
Mark Slee52f643d2006-08-09 00:03:43 +0000809 $$ = true;
810 }
811|
812 {
813 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000814 }
815
Mark Slee36bfa2e2007-01-19 20:09:51 +0000816Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000817 tok_throws '(' FieldList ')'
818 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000819 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000820 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000821 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000822 yyerror("Throws clause may not contain non-exception types");
823 exit(1);
824 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000825 }
826|
827 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000828 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000829 }
830
Mark Slee31985722006-05-24 21:45:31 +0000831FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000832 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000833 {
834 pdebug("FieldList -> FieldList , Field");
835 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000836 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100837 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 +0000838 exit(1);
839 }
Mark Slee31985722006-05-24 21:45:31 +0000840 }
841|
842 {
843 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000844 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000846 }
847
848Field:
Jens Geyer885c6792014-05-02 21:31:55 +0200849 CaptureDocText FieldIdentifier FieldRequiredness FieldType FieldReference tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000850 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000851 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000852 if ($2.auto_assigned) {
Jens Geyer885c6792014-05-02 21:31:55 +0200853 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 +0000854 if (g_strict >= 192) {
855 yyerror("Implicit field keys are deprecated and not allowed with -strict");
856 exit(1);
857 }
Mark Slee31985722006-05-24 21:45:31 +0000858 }
Jens Geyer885c6792014-05-02 21:31:55 +0200859 validate_simple_identifier($6);
860 $$ = new t_field($4, $6, $2.value);
861 $$->set_reference($5);
David Reiss8320a922007-08-14 19:59:26 +0000862 $$->set_req($3);
Jens Geyer885c6792014-05-02 21:31:55 +0200863 if ($7 != NULL) {
864 g_scope->resolve_const_value($7, $4);
865 validate_field_value($$, $7);
866 $$->set_value($7);
Mark Slee7ff32452007-02-01 05:26:18 +0000867 }
Jens Geyer885c6792014-05-02 21:31:55 +0200868 $$->set_xsd_optional($8);
869 $$->set_xsd_nillable($9);
ccheeverf53b5cf2007-02-05 20:33:11 +0000870 if ($1 != NULL) {
871 $$->set_doc($1);
872 }
David Reiss53c10e02010-03-05 07:51:51 +0000873 if ($10 != NULL) {
Jens Geyer885c6792014-05-02 21:31:55 +0200874 $$->set_xsd_attrs($10);
875 }
876 if ($11 != NULL) {
877 $$->annotations_ = $11->annotations_;
878 delete $11;
David Reiss53c10e02010-03-05 07:51:51 +0000879 }
Mark Slee31985722006-05-24 21:45:31 +0000880 }
Mark Slee7ff32452007-02-01 05:26:18 +0000881
882FieldIdentifier:
883 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000884 {
Mark Slee7ff32452007-02-01 05:26:18 +0000885 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000886 if (g_allow_neg_field_keys) {
887 /*
888 * g_allow_neg_field_keys exists to allow users to add explicitly
889 * specified key values to old .thrift files without breaking
890 * protocol compatibility.
891 */
892 if ($1 != y_field_val) {
893 /*
894 * warn if the user-specified negative value isn't what
895 * thrift would have auto-assigned.
896 */
Roger Meier5f2d34e2013-11-16 16:43:41 +0100897 pwarning(1, "Nonpositive field key (%" PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000898 "auto-assigned by thrift (%d).\n", $1, y_field_val);
899 }
900 /*
901 * Leave $1 as-is, and update y_field_val to be one less than $1.
902 * The FieldList parsing will catch any duplicate key values.
903 */
Ben Craige9576752013-10-11 08:19:16 -0500904 y_field_val = static_cast<int32_t>($1 - 1);
905 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000906 $$.auto_assigned = false;
907 } else {
Ben Craige9576752013-10-11 08:19:16 -0500908 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000909 $1);
910 $$.value = y_field_val--;
911 $$.auto_assigned = true;
912 }
913 } else {
Ben Craige9576752013-10-11 08:19:16 -0500914 $$.value = static_cast<int32_t>($1);
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000915 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000916 }
Jens Geyer06340a42016-03-25 01:34:03 +0200917 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
918 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
919 $$.value, SHRT_MIN, SHRT_MAX);
920 }
Mark Slee7ff32452007-02-01 05:26:18 +0000921 }
922|
923 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000924 $$.value = y_field_val--;
925 $$.auto_assigned = true;
Jens Geyer06340a42016-03-25 01:34:03 +0200926 if( (SHRT_MIN > $$.value) || ($$.value > SHRT_MAX)) {
927 pwarning(1, "Field key (%d) exceeds allowed range (%d..%d).\n",
928 $$.value, SHRT_MIN, SHRT_MAX);
929 }
Mark Slee7ff32452007-02-01 05:26:18 +0000930 }
931
Jens Geyer885c6792014-05-02 21:31:55 +0200932FieldReference:
933 tok_reference
934 {
935 $$ = true;
936 }
937|
938 {
939 $$ = false;
940 }
941
David Reiss8320a922007-08-14 19:59:26 +0000942FieldRequiredness:
943 tok_required
944 {
David Reiss45603e92009-09-02 22:15:55 +0000945 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +0000946 }
947| tok_optional
948 {
Mark Slee78165722007-09-10 22:08:49 +0000949 if (g_arglist) {
950 if (g_parse_mode == PROGRAM) {
951 pwarning(1, "optional keyword is ignored in argument lists.\n");
952 }
David Reiss204420f2008-01-11 20:59:03 +0000953 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000954 } else {
David Reiss204420f2008-01-11 20:59:03 +0000955 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000956 }
David Reiss8320a922007-08-14 19:59:26 +0000957 }
958|
959 {
David Reiss204420f2008-01-11 20:59:03 +0000960 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000961 }
962
Mark Slee7ff32452007-02-01 05:26:18 +0000963FieldValue:
964 '=' ConstValue
965 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000966 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000967 $$ = $2;
968 } else {
969 $$ = NULL;
970 }
971 }
972|
973 {
974 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000975 }
Mark Slee31985722006-05-24 21:45:31 +0000976
Mark Slee31985722006-05-24 21:45:31 +0000977FunctionType:
978 FieldType
979 {
Mark Sleee8540632006-05-30 09:24:40 +0000980 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000981 $$ = $1;
982 }
983| tok_void
984 {
Mark Sleee8540632006-05-30 09:24:40 +0000985 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000986 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000987 }
988
989FieldType:
990 tok_identifier
991 {
Mark Sleee8540632006-05-30 09:24:40 +0000992 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000993 if (g_parse_mode == INCLUDES) {
994 // Ignore identifiers in include mode
995 $$ = NULL;
996 } else {
997 // Lookup the identifier in the current scope
998 $$ = g_scope->get_type($1);
999 if ($$ == NULL) {
jfarrelle0e83162014-04-08 22:45:01 -04001000 /*
1001 * Either this type isn't yet declared, or it's never
1002 declared. Either way allow it and we'll figure it out
1003 during generation.
1004 */
Jens Geyerd000b242014-04-13 21:58:47 +02001005 $$ = new t_typedef(g_program, $1, true);
Mark Sleef0712dc2006-10-25 19:03:57 +00001006 }
Mark Sleee8540632006-05-30 09:24:40 +00001007 }
Mark Slee31985722006-05-24 21:45:31 +00001008 }
1009| BaseType
1010 {
Mark Sleee8540632006-05-30 09:24:40 +00001011 pdebug("FieldType -> BaseType");
1012 $$ = $1;
1013 }
1014| ContainerType
1015 {
1016 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001017 $$ = $1;
1018 }
1019
David Reissc8e30052009-07-27 17:02:42 +00001020BaseType: SimpleBaseType TypeAnnotations
1021 {
1022 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1023 if ($2 != NULL) {
1024 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1025 $$->annotations_ = $2->annotations_;
1026 delete $2;
1027 } else {
1028 $$ = $1;
1029 }
1030 }
1031
1032SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001033 tok_string
1034 {
1035 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001036 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001037 }
Mark Slee8d725a22007-04-13 01:57:12 +00001038| tok_binary
1039 {
1040 pdebug("BaseType -> tok_binary");
1041 $$ = g_type_binary;
1042 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001043| tok_slist
1044 {
1045 pdebug("BaseType -> tok_slist");
1046 $$ = g_type_slist;
1047 }
Mark Slee78f58e22006-09-02 04:17:07 +00001048| tok_bool
1049 {
1050 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001051 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001052 }
Jens Geyer40c28d32015-10-20 23:13:02 +02001053| tok_i8
Mark Slee31985722006-05-24 21:45:31 +00001054 {
Jens Geyer40c28d32015-10-20 23:13:02 +02001055 pdebug("BaseType -> tok_i8");
1056 $$ = g_type_i8;
Mark Slee31985722006-05-24 21:45:31 +00001057 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001058| tok_i16
1059 {
1060 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001061 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001062 }
Mark Slee31985722006-05-24 21:45:31 +00001063| tok_i32
1064 {
1065 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001066 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001067 }
Mark Slee31985722006-05-24 21:45:31 +00001068| tok_i64
1069 {
1070 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001071 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001072 }
Mark Sleec98d0502006-09-06 02:42:25 +00001073| tok_double
1074 {
1075 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001076 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001077 }
Mark Slee31985722006-05-24 21:45:31 +00001078
David Reissa2309992008-12-10 01:52:48 +00001079ContainerType: SimpleContainerType TypeAnnotations
1080 {
1081 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1082 $$ = $1;
1083 if ($2 != NULL) {
1084 $$->annotations_ = $2->annotations_;
1085 delete $2;
1086 }
1087 }
1088
1089SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001090 MapType
1091 {
David Reissa2309992008-12-10 01:52:48 +00001092 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001093 $$ = $1;
1094 }
1095| SetType
1096 {
David Reissa2309992008-12-10 01:52:48 +00001097 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001098 $$ = $1;
1099 }
1100| ListType
1101 {
David Reissa2309992008-12-10 01:52:48 +00001102 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001103 $$ = $1;
1104 }
1105
1106MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001107 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001108 {
1109 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001110 $$ = new t_map($4, $6);
1111 if ($2 != NULL) {
1112 ((t_container*)$$)->set_cpp_name(std::string($2));
1113 }
Mark Sleee8540632006-05-30 09:24:40 +00001114 }
1115
1116SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001117 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001118 {
1119 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001120 $$ = new t_set($4);
1121 if ($2 != NULL) {
1122 ((t_container*)$$)->set_cpp_name(std::string($2));
1123 }
Mark Sleee8540632006-05-30 09:24:40 +00001124 }
1125
1126ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001127 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001128 {
1129 pdebug("ListType -> tok_list<FieldType>");
Jens Geyer6fe77e82014-03-16 16:48:53 +02001130 check_for_list_of_bytes($3);
Mark Sleef0712dc2006-10-25 19:03:57 +00001131 $$ = new t_list($3);
1132 if ($5 != NULL) {
1133 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001134 }
1135 }
1136
Mark Slee36bfa2e2007-01-19 20:09:51 +00001137CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001138 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001139 {
Mark Sleeafc76542007-02-09 21:55:44 +00001140 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001141 }
1142|
1143 {
1144 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001145 }
1146
David Reissa2309992008-12-10 01:52:48 +00001147TypeAnnotations:
1148 '(' TypeAnnotationList ')'
1149 {
1150 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1151 $$ = $2;
1152 }
1153|
1154 {
1155 $$ = NULL;
1156 }
1157
1158TypeAnnotationList:
1159 TypeAnnotationList TypeAnnotation
1160 {
1161 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1162 $$ = $1;
1163 $$->annotations_[$2->key] = $2->val;
1164 delete $2;
1165 }
1166|
1167 {
1168 /* Just use a dummy structure to hold the annotations. */
1169 $$ = new t_struct(g_program);
1170 }
1171
1172TypeAnnotation:
Jens Geyer89847df2014-05-02 23:50:04 +02001173 tok_identifier TypeAnnotationValue CommaOrSemicolonOptional
David Reissa2309992008-12-10 01:52:48 +00001174 {
Jens Geyer89847df2014-05-02 23:50:04 +02001175 pdebug("TypeAnnotation -> TypeAnnotationValue");
David Reissa2309992008-12-10 01:52:48 +00001176 $$ = new t_annotation;
1177 $$->key = $1;
Jens Geyer89847df2014-05-02 23:50:04 +02001178 $$->val = $2;
1179 }
1180
1181TypeAnnotationValue:
1182 '=' tok_literal
1183 {
1184 pdebug("TypeAnnotationValue -> = tok_literal");
1185 $$ = $2;
1186 }
1187|
1188 {
1189 pdebug("TypeAnnotationValue ->");
1190 $$ = strdup("1");
David Reissa2309992008-12-10 01:52:48 +00001191 }
1192
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001193%%