blob: 5b91a0a21c9bf8088601569feb34165210816213 [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>
Mark Slee31985722006-05-24 21:45:31 +000037#include "main.h"
38#include "globals.h"
39#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000040#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000041
Mark Sleef5377b32006-10-10 01:42:59 +000042/**
43 * This global variable is used for automatic numbering of field indices etc.
44 * when parsing the members of a struct. Field values are automatically
45 * assigned starting from -1 and working their way down.
46 */
Mark Slee9cb7c612006-09-01 22:17:45 +000047int y_field_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000048int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000049const int struct_is_struct = 0;
50const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000051
52%}
53
Mark Sleef5377b32006-10-10 01:42:59 +000054/**
55 * This structure is used by the parser to hold the data types associated with
56 * various parse nodes.
57 */
Mark Slee31985722006-05-24 21:45:31 +000058%union {
Mark Slee30152872006-11-28 01:24:07 +000059 char* id;
David Reissf1454162008-06-30 20:45:47 +000060 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000061 double dconst;
62 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000063 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000064 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000065 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000066 t_typedef* ttypedef;
67 t_enum* tenum;
68 t_enum_value* tenumv;
69 t_const* tconst;
70 t_const_value* tconstv;
71 t_struct* tstruct;
72 t_service* tservice;
73 t_function* tfunction;
74 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000075 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000076 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000077 t_annotation* tannot;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000078 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000079}
80
Mark Sleef5377b32006-10-10 01:42:59 +000081/**
82 * Strings identifier
83 */
Mark Slee31985722006-05-24 21:45:31 +000084%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000085%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000086%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000087%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000088
89/**
Mark Slee30152872006-11-28 01:24:07 +000090 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000091 */
Mark Slee31985722006-05-24 21:45:31 +000092%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000093%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000094
Mark Sleef5377b32006-10-10 01:42:59 +000095/**
David Reiss399442b2008-02-20 02:28:05 +000096 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +000097 */
Mark Sleef0712dc2006-10-25 19:03:57 +000098%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000099%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +0000100%token tok_cpp_namespace
101%token tok_cpp_include
102%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +0000103%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +0000104%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000105%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000106%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000107%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000108%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000109%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000110%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000111%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000112%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000113%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000114%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000115%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000116%token tok_csharp_namespace
Jake Farrell7ae13e12011-10-18 14:35:26 +0000117%token tok_delphi_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
120 * Base datatype keywords
121 */
122%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000123%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000124%token tok_byte
125%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000126%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000127%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000128%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000129%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000130%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000131%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000132%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000133
Mark Sleef5377b32006-10-10 01:42:59 +0000134/**
135 * Complex type keywords
136 */
Mark Slee31985722006-05-24 21:45:31 +0000137%token tok_map
138%token tok_list
139%token tok_set
140
Mark Sleef5377b32006-10-10 01:42:59 +0000141/**
142 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000143 */
David Reiss6985a422009-03-24 20:00:47 +0000144%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000145
Mark Sleef5377b32006-10-10 01:42:59 +0000146/**
147 * Thrift language keywords
148 */
Mark Slee31985722006-05-24 21:45:31 +0000149%token tok_typedef
150%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000151%token tok_xception
152%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000153%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000154%token tok_service
155%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000156%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000157%token tok_required
158%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000159%token tok_union
Mark Slee31985722006-05-24 21:45:31 +0000160
Mark Sleef5377b32006-10-10 01:42:59 +0000161/**
162 * Grammar nodes
163 */
164
Mark Slee31985722006-05-24 21:45:31 +0000165%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000166%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000167%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000168%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000169%type<ttype> MapType
170%type<ttype> SetType
171%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000172
David Reisscdffe262007-08-14 17:12:31 +0000173%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000174%type<ttype> TypeDefinition
175
Mark Slee31985722006-05-24 21:45:31 +0000176%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000177
David Reissa2309992008-12-10 01:52:48 +0000178%type<ttype> TypeAnnotations
179%type<ttype> TypeAnnotationList
180%type<tannot> TypeAnnotation
181
Mark Slee31985722006-05-24 21:45:31 +0000182%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000183%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000184%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000185%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000186%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000187%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000188
189%type<tenum> Enum
190%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000191%type<tenumv> EnumDef
192
Mark Slee6a47fed2007-02-07 02:40:59 +0000193%type<ttypedef> Senum
194%type<tbase> SenumDefList
195%type<id> SenumDef
196
Mark Slee30152872006-11-28 01:24:07 +0000197%type<tconst> Const
198%type<tconstv> ConstValue
199%type<tconstv> ConstList
200%type<tconstv> ConstListContents
201%type<tconstv> ConstMap
202%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000203
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000204%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000205%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000206%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000207%type<tservice> Service
208
209%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000210%type<ttype> FunctionType
211%type<tservice> FunctionList
212
Mark Slee36bfa2e2007-01-19 20:09:51 +0000213%type<tstruct> Throws
214%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000215%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000216%type<tbool> XsdAll
217%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000218%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000219%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000220%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000221
David Reisscbd4bac2007-08-14 17:12:33 +0000222%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000223
Mark Slee31985722006-05-24 21:45:31 +0000224%%
225
Mark Sleef5377b32006-10-10 01:42:59 +0000226/**
227 * Thrift Grammar Implementation.
228 *
229 * For the most part this source file works its way top down from what you
230 * might expect to find in a typical .thrift file, i.e. type definitions and
231 * namespaces up top followed by service definitions using those types.
232 */
Mark Slee31985722006-05-24 21:45:31 +0000233
234Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000235 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000236 {
237 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000238 /*
239 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000240 if ($1 != NULL) {
241 g_program->set_doc($1);
242 }
David Reisscbd4bac2007-08-14 17:12:33 +0000243 */
244 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000245 }
246
David Reisscbd4bac2007-08-14 17:12:33 +0000247CaptureDocText:
248 {
249 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000250 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000251 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000252 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000253 $$ = NULL;
254 }
255 }
256
257/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
258DestroyDocText:
259 {
260 if (g_parse_mode == PROGRAM) {
261 clear_doctext();
262 }
263 }
264
265/* We have to DestroyDocText here, otherwise it catches the doctext
266 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000267HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000268 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000269 {
270 pdebug("HeaderList -> HeaderList Header");
271 }
272|
273 {
274 pdebug("HeaderList -> ");
275 }
276
277Header:
278 Include
279 {
280 pdebug("Header -> Include");
281 }
David Reiss79eca142008-02-27 01:55:13 +0000282| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 {
David Reiss79eca142008-02-27 01:55:13 +0000284 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000285 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000286 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 }
288 }
David Reissfb790d72010-09-02 16:41:45 +0000289| tok_namespace '*' tok_identifier
290 {
291 pdebug("Header -> tok_namespace * tok_identifier");
292 if (g_parse_mode == PROGRAM) {
293 g_program->set_namespace("*", $3);
294 }
295 }
David Reiss9a08dc62008-02-27 01:55:17 +0000296/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000297| tok_cpp_namespace tok_identifier
298 {
David Reiss9a08dc62008-02-27 01:55:17 +0000299 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000300 pdebug("Header -> tok_cpp_namespace tok_identifier");
301 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000302 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000303 }
304 }
305| tok_cpp_include tok_literal
306 {
307 pdebug("Header -> tok_cpp_include tok_literal");
308 if (g_parse_mode == PROGRAM) {
309 g_program->add_cpp_include($2);
310 }
311 }
Mark Sleee888b372007-01-12 01:06:24 +0000312| tok_php_namespace tok_identifier
313 {
David Reiss554ea6f2009-02-17 20:28:37 +0000314 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000315 pdebug("Header -> tok_php_namespace tok_identifier");
316 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000317 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000318 }
319 }
David Reiss320e45c2008-03-27 21:41:54 +0000320/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000321| tok_py_module tok_identifier
322 {
David Reiss320e45c2008-03-27 21:41:54 +0000323 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000324 pdebug("Header -> tok_py_module tok_identifier");
325 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000326 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000327 }
328 }
David Reiss07ef3a92008-03-27 21:42:39 +0000329/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000330| tok_perl_package tok_identifier
331 {
David Reiss07ef3a92008-03-27 21:42:39 +0000332 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000333 pdebug("Header -> tok_perl_namespace tok_identifier");
334 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000335 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000336 }
337 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000338/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000339| tok_ruby_namespace tok_identifier
340 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000341 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000342 pdebug("Header -> tok_ruby_namespace tok_identifier");
343 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000344 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000345 }
346 }
David Reiss3b455012008-03-27 21:40:46 +0000347/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000348| tok_smalltalk_category tok_st_identifier
349 {
David Reiss3b455012008-03-27 21:40:46 +0000350 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000351 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
352 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000353 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000354 }
355 }
David Reiss3b455012008-03-27 21:40:46 +0000356/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000357| tok_smalltalk_prefix tok_identifier
358 {
David Reiss3b455012008-03-27 21:40:46 +0000359 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000360 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
361 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000362 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000363 }
364 }
David Reiss771f8c72008-02-27 01:55:25 +0000365/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000366| tok_java_package tok_identifier
367 {
David Reiss9f646152008-03-02 21:59:48 +0000368 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000369 pdebug("Header -> tok_java_package tok_identifier");
370 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000371 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000372 }
373 }
David Reiss54b602b2008-03-27 21:41:06 +0000374/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000375| tok_cocoa_prefix tok_identifier
376 {
David Reiss54b602b2008-03-27 21:41:06 +0000377 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000378 pdebug("Header -> tok_cocoa_prefix tok_identifier");
379 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000380 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000381 }
382 }
David Reiss92e10d82009-02-17 20:28:19 +0000383/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000384| tok_xsd_namespace tok_literal
385 {
David Reiss92e10d82009-02-17 20:28:19 +0000386 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000387 pdebug("Header -> tok_xsd_namespace tok_literal");
388 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000389 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000390 }
391 }
David Reiss9d65bf02008-03-27 21:41:37 +0000392/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000393| tok_csharp_namespace tok_identifier
394 {
David Reiss9d65bf02008-03-27 21:41:37 +0000395 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000396 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000397 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000398 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000399 }
400 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000401/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
402| tok_delphi_namespace tok_identifier
403 {
404 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
405 pdebug("Header -> tok_delphi_namespace tok_identifier");
406 if (g_parse_mode == PROGRAM) {
407 g_program->set_namespace("delphi", $2);
408 }
409 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000410
411Include:
412 tok_include tok_literal
413 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000414 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000415 if (g_parse_mode == INCLUDES) {
416 std::string path = include_file(std::string($2));
417 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000418 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000419 }
420 }
421 }
Mark Slee31985722006-05-24 21:45:31 +0000422
423DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000424 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000425 {
426 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000427 if ($2 != NULL && $3 != NULL) {
428 $3->set_doc($2);
429 }
Mark Slee31985722006-05-24 21:45:31 +0000430 }
431|
432 {
433 pdebug("DefinitionList -> ");
434 }
435
436Definition:
Mark Slee30152872006-11-28 01:24:07 +0000437 Const
438 {
439 pdebug("Definition -> Const");
440 if (g_parse_mode == PROGRAM) {
441 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000442 }
David Reisscdffe262007-08-14 17:12:31 +0000443 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000444 }
445| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000446 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000447 pdebug("Definition -> TypeDefinition");
448 if (g_parse_mode == PROGRAM) {
449 g_scope->add_type($1->get_name(), $1);
450 if (g_parent_scope != NULL) {
451 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
452 }
Roger Meierba406d32013-07-15 22:41:34 +0200453 if (! g_program->is_unique_typename($1)) {
454 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
455 exit(1);
456 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000457 }
David Reisscdffe262007-08-14 17:12:31 +0000458 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000459 }
Mark Slee31985722006-05-24 21:45:31 +0000460| Service
461 {
462 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000463 if (g_parse_mode == PROGRAM) {
464 g_scope->add_service($1->get_name(), $1);
465 if (g_parent_scope != NULL) {
466 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
467 }
468 g_program->add_service($1);
Roger Meierba406d32013-07-15 22:41:34 +0200469 if (! g_program->is_unique_typename($1)) {
470 yyerror("Type \"%s\" is already defined.", $1->get_name().c_str());
471 exit(1);
472 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000473 }
David Reisscdffe262007-08-14 17:12:31 +0000474 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000475 }
476
Mark Sleef0712dc2006-10-25 19:03:57 +0000477TypeDefinition:
478 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000479 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000480 pdebug("TypeDefinition -> Typedef");
481 if (g_parse_mode == PROGRAM) {
482 g_program->add_typedef($1);
483 }
484 }
485| Enum
486 {
487 pdebug("TypeDefinition -> Enum");
488 if (g_parse_mode == PROGRAM) {
489 g_program->add_enum($1);
490 }
491 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000492| Senum
493 {
494 pdebug("TypeDefinition -> Senum");
495 if (g_parse_mode == PROGRAM) {
496 g_program->add_typedef($1);
497 }
498 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000499| Struct
500 {
501 pdebug("TypeDefinition -> Struct");
502 if (g_parse_mode == PROGRAM) {
503 g_program->add_struct($1);
504 }
505 }
506| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000507 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000508 pdebug("TypeDefinition -> Xception");
509 if (g_parse_mode == PROGRAM) {
510 g_program->add_xception($1);
511 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000512 }
Mark Slee31985722006-05-24 21:45:31 +0000513
514Typedef:
Roger Meier30877382012-09-17 21:18:05 +0000515 tok_typedef FieldType tok_identifier TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000516 {
David Reiss4dd78012010-03-09 05:19:08 +0000517 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000518 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000519 $$ = td;
Roger Meier30877382012-09-17 21:18:05 +0000520 if ($4 != NULL) {
521 $$->annotations_ = $4->annotations_;
522 delete $4;
523 }
Mark Slee31985722006-05-24 21:45:31 +0000524 }
525
Mark Slee6a47fed2007-02-07 02:40:59 +0000526CommaOrSemicolonOptional:
527 ','
528 {}
529| ';'
530 {}
531|
532 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000533
Mark Slee31985722006-05-24 21:45:31 +0000534Enum:
Roger Meier30877382012-09-17 21:18:05 +0000535 tok_enum tok_identifier '{' EnumDefList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000536 {
537 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000538 $$ = $4;
539 $$->set_name($2);
Roger Meier30877382012-09-17 21:18:05 +0000540 if ($6 != NULL) {
541 $$->annotations_ = $6->annotations_;
542 delete $6;
543 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000544 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000545 // make constants for all the enum values
546 if (g_parse_mode == PROGRAM) {
547 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
548 std::vector<t_enum_value*>::const_iterator c_iter;
549 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
550 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
551 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
552 const_val->set_enum($$);
553 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
554 if (g_parent_scope != NULL) {
555 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
556 }
557 }
558 }
Mark Slee31985722006-05-24 21:45:31 +0000559 }
560
561EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000562 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000563 {
564 pdebug("EnumDefList -> EnumDefList EnumDef");
565 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000566 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000567 }
568|
569 {
570 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000571 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000572 }
573
574EnumDef:
Roger Meier30877382012-09-17 21:18:05 +0000575 CaptureDocText tok_identifier '=' tok_int_constant TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000576 {
Mark Slee30152872006-11-28 01:24:07 +0000577 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000578 if ($4 < 0) {
579 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000580 }
David Reissf1454162008-06-30 20:45:47 +0000581 if ($4 > INT_MAX) {
582 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
583 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000584 $$ = new t_enum_value($2, $4);
585 if ($1 != NULL) {
586 $$->set_doc($1);
587 }
Roger Meier30877382012-09-17 21:18:05 +0000588 if ($5 != NULL) {
589 $$->annotations_ = $5->annotations_;
590 delete $5;
591 }
Mark Slee31985722006-05-24 21:45:31 +0000592 }
593|
Roger Meier30877382012-09-17 21:18:05 +0000594 CaptureDocText tok_identifier TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000595 {
Mark Slee30152872006-11-28 01:24:07 +0000596 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000597 $$ = new t_enum_value($2);
598 if ($1 != NULL) {
599 $$->set_doc($1);
600 }
Roger Meier30877382012-09-17 21:18:05 +0000601 if ($3 != NULL) {
602 $$->annotations_ = $3->annotations_;
603 delete $3;
604 }
Mark Slee30152872006-11-28 01:24:07 +0000605 }
606
Mark Slee6a47fed2007-02-07 02:40:59 +0000607Senum:
Roger Meier30877382012-09-17 21:18:05 +0000608 tok_senum tok_identifier '{' SenumDefList '}' TypeAnnotations
Mark Slee6a47fed2007-02-07 02:40:59 +0000609 {
610 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000611 $$ = new t_typedef(g_program, $4, $2);
Roger Meier30877382012-09-17 21:18:05 +0000612 if ($6 != NULL) {
613 $$->annotations_ = $6->annotations_;
614 delete $6;
615 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000616 }
617
618SenumDefList:
619 SenumDefList SenumDef
620 {
621 pdebug("SenumDefList -> SenumDefList SenumDef");
622 $$ = $1;
623 $$->add_string_enum_val($2);
624 }
625|
626 {
627 pdebug("SenumDefList -> ");
628 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
629 $$->set_string_enum(true);
630 }
631
632SenumDef:
633 tok_literal CommaOrSemicolonOptional
634 {
635 pdebug("SenumDef -> tok_literal");
636 $$ = $1;
637 }
638
Mark Slee30152872006-11-28 01:24:07 +0000639Const:
640 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
641 {
642 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000643 if (g_parse_mode == PROGRAM) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000644 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000645 $$ = new t_const($2, $3, $5);
646 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000647
648 g_scope->add_constant($3, $$);
649 if (g_parent_scope != NULL) {
650 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
651 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000652 } else {
653 $$ = NULL;
654 }
Mark Slee30152872006-11-28 01:24:07 +0000655 }
656
657ConstValue:
658 tok_int_constant
659 {
660 pdebug("ConstValue => tok_int_constant");
661 $$ = new t_const_value();
662 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000663 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
David Reissf1454162008-06-30 20:45:47 +0000664 pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
665 }
Mark Slee30152872006-11-28 01:24:07 +0000666 }
667| tok_dub_constant
668 {
669 pdebug("ConstValue => tok_dub_constant");
670 $$ = new t_const_value();
671 $$->set_double($1);
672 }
673| tok_literal
674 {
675 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000676 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000677 }
Mark Slee67fc6342006-11-29 03:37:04 +0000678| tok_identifier
679 {
680 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000681 $$ = new t_const_value();
682 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000683 }
Mark Slee30152872006-11-28 01:24:07 +0000684| ConstList
685 {
686 pdebug("ConstValue => ConstList");
687 $$ = $1;
688 }
689| ConstMap
690 {
691 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000692 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000693 }
694
695ConstList:
696 '[' ConstListContents ']'
697 {
698 pdebug("ConstList => [ ConstListContents ]");
699 $$ = $2;
700 }
701
702ConstListContents:
703 ConstListContents ConstValue CommaOrSemicolonOptional
704 {
705 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
706 $$ = $1;
707 $$->add_list($2);
708 }
709|
710 {
711 pdebug("ConstListContents =>");
712 $$ = new t_const_value();
713 $$->set_list();
714 }
715
716ConstMap:
717 '{' ConstMapContents '}'
718 {
719 pdebug("ConstMap => { ConstMapContents }");
720 $$ = $2;
721 }
722
723ConstMapContents:
724 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
725 {
726 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
727 $$ = $1;
728 $$->add_map($2, $4);
729 }
730|
731 {
732 pdebug("ConstMapContents =>");
733 $$ = new t_const_value();
734 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000735 }
736
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000737StructHead:
738 tok_struct
739 {
740 $$ = struct_is_struct;
741 }
742| tok_union
743 {
744 $$ = struct_is_union;
745 }
746
Mark Slee31985722006-05-24 21:45:31 +0000747Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000748 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000749 {
750 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000751 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000752 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000753 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000754 $$->set_name($2);
755 if ($7 != NULL) {
756 $$->annotations_ = $7->annotations_;
757 delete $7;
758 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000759 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000760
Mark Slee36bfa2e2007-01-19 20:09:51 +0000761XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000762 tok_xsd_all
763 {
764 $$ = true;
765 }
766|
767 {
768 $$ = false;
769 }
770
Mark Slee36bfa2e2007-01-19 20:09:51 +0000771XsdOptional:
772 tok_xsd_optional
773 {
774 $$ = true;
775 }
776|
777 {
778 $$ = false;
779 }
780
Mark Slee7df0e2a2007-02-06 21:03:18 +0000781XsdNillable:
782 tok_xsd_nillable
783 {
784 $$ = true;
785 }
786|
787 {
788 $$ = false;
789 }
790
Mark Slee21135c32007-02-05 21:52:08 +0000791XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000792 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000793 {
Mark Slee748d83f2007-02-07 01:20:08 +0000794 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000795 }
796|
797 {
798 $$ = NULL;
799 }
800
Mark Slee9cb7c612006-09-01 22:17:45 +0000801Xception:
Roger Meier30877382012-09-17 21:18:05 +0000802 tok_xception tok_identifier '{' FieldList '}' TypeAnnotations
Mark Slee9cb7c612006-09-01 22:17:45 +0000803 {
804 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
805 $4->set_name($2);
806 $4->set_xception(true);
807 $$ = $4;
Roger Meier30877382012-09-17 21:18:05 +0000808 if ($6 != NULL) {
809 $$->annotations_ = $6->annotations_;
810 delete $6;
811 }
Mark Slee31985722006-05-24 21:45:31 +0000812 }
813
814Service:
Roger Meier30877382012-09-17 21:18:05 +0000815 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000816 {
817 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000818 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000819 $$->set_name($2);
820 $$->set_extends($3);
Roger Meier30877382012-09-17 21:18:05 +0000821 if ($9 != NULL) {
822 $$->annotations_ = $9->annotations_;
823 delete $9;
824 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000825 }
826
Mark Slee78165722007-09-10 22:08:49 +0000827FlagArgs:
828 {
829 g_arglist = 1;
830 }
831
832UnflagArgs:
833 {
834 g_arglist = 0;
835 }
836
Mark Slee36bfa2e2007-01-19 20:09:51 +0000837Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000838 tok_extends tok_identifier
839 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000840 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000841 $$ = NULL;
842 if (g_parse_mode == PROGRAM) {
843 $$ = g_scope->get_service($2);
844 if ($$ == NULL) {
845 yyerror("Service \"%s\" has not been defined.", $2);
846 exit(1);
847 }
848 }
849 }
850|
851 {
852 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000853 }
854
855FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000856 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000857 {
858 pdebug("FunctionList -> FunctionList Function");
859 $$ = $1;
860 $1->add_function($2);
861 }
862|
863 {
864 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000865 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000866 }
867
868Function:
Roger Meier30877382012-09-17 21:18:05 +0000869 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000870 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000871 $6->set_name(std::string($4) + "_args");
872 $$ = new t_function($3, $4, $6, $8, $2);
873 if ($1 != NULL) {
874 $$->set_doc($1);
875 }
Roger Meier30877382012-09-17 21:18:05 +0000876 if ($9 != NULL) {
877 $$->annotations_ = $9->annotations_;
878 delete $9;
879 }
Mark Slee31985722006-05-24 21:45:31 +0000880 }
881
David Reiss6985a422009-03-24 20:00:47 +0000882Oneway:
883 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000884 {
Mark Slee52f643d2006-08-09 00:03:43 +0000885 $$ = true;
886 }
887|
888 {
889 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000890 }
891
Mark Slee36bfa2e2007-01-19 20:09:51 +0000892Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000893 tok_throws '(' FieldList ')'
894 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000895 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000896 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000897 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000898 yyerror("Throws clause may not contain non-exception types");
899 exit(1);
900 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000901 }
902|
903 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000905 }
906
Mark Slee31985722006-05-24 21:45:31 +0000907FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000908 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000909 {
910 pdebug("FieldList -> FieldList , Field");
911 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000912 if (!($$->append($2))) {
Jens Geyer3a67c2f2013-02-03 22:30:41 +0100913 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 +0000914 exit(1);
915 }
Mark Slee31985722006-05-24 21:45:31 +0000916 }
917|
918 {
919 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000920 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000921 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000922 }
923
924Field:
David Reiss53c10e02010-03-05 07:51:51 +0000925 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000926 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000927 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000928 if ($2.auto_assigned) {
David Reissbb461362009-04-02 19:23:59 +0000929 pwarning(1, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $5);
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000930 if (g_strict >= 192) {
931 yyerror("Implicit field keys are deprecated and not allowed with -strict");
932 exit(1);
933 }
Mark Slee31985722006-05-24 21:45:31 +0000934 }
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000935 $$ = new t_field($4, $5, $2.value);
David Reiss8320a922007-08-14 19:59:26 +0000936 $$->set_req($3);
937 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000938 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000939 validate_field_value($$, $6);
940 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000941 }
David Reiss8320a922007-08-14 19:59:26 +0000942 $$->set_xsd_optional($7);
943 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000944 if ($1 != NULL) {
945 $$->set_doc($1);
946 }
David Reiss8320a922007-08-14 19:59:26 +0000947 if ($9 != NULL) {
948 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000949 }
David Reiss53c10e02010-03-05 07:51:51 +0000950 if ($10 != NULL) {
951 $$->annotations_ = $10->annotations_;
952 delete $10;
953 }
Mark Slee31985722006-05-24 21:45:31 +0000954 }
Mark Slee7ff32452007-02-01 05:26:18 +0000955
956FieldIdentifier:
957 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000958 {
Mark Slee7ff32452007-02-01 05:26:18 +0000959 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000960 if (g_allow_neg_field_keys) {
961 /*
962 * g_allow_neg_field_keys exists to allow users to add explicitly
963 * specified key values to old .thrift files without breaking
964 * protocol compatibility.
965 */
966 if ($1 != y_field_val) {
967 /*
968 * warn if the user-specified negative value isn't what
969 * thrift would have auto-assigned.
970 */
Jens Geyer77407392012-12-11 23:38:12 +0100971 pwarning(1, "Nonpositive field key (%"PRIi64") differs from what would be "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000972 "auto-assigned by thrift (%d).\n", $1, y_field_val);
973 }
974 /*
975 * Leave $1 as-is, and update y_field_val to be one less than $1.
976 * The FieldList parsing will catch any duplicate key values.
977 */
978 y_field_val = $1 - 1;
979 $$.value = $1;
980 $$.auto_assigned = false;
981 } else {
Jens Geyer77407392012-12-11 23:38:12 +0100982 pwarning(1, "Nonpositive value (%"PRIi64") not allowed as a field key.\n",
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000983 $1);
984 $$.value = y_field_val--;
985 $$.auto_assigned = true;
986 }
987 } else {
988 $$.value = $1;
989 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000990 }
Mark Slee7ff32452007-02-01 05:26:18 +0000991 }
992|
993 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000994 $$.value = y_field_val--;
995 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +0000996 }
997
David Reiss8320a922007-08-14 19:59:26 +0000998FieldRequiredness:
999 tok_required
1000 {
David Reiss45603e92009-09-02 22:15:55 +00001001 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +00001002 }
1003| tok_optional
1004 {
Mark Slee78165722007-09-10 22:08:49 +00001005 if (g_arglist) {
1006 if (g_parse_mode == PROGRAM) {
1007 pwarning(1, "optional keyword is ignored in argument lists.\n");
1008 }
David Reiss204420f2008-01-11 20:59:03 +00001009 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +00001010 } else {
David Reiss204420f2008-01-11 20:59:03 +00001011 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +00001012 }
David Reiss8320a922007-08-14 19:59:26 +00001013 }
1014|
1015 {
David Reiss204420f2008-01-11 20:59:03 +00001016 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +00001017 }
1018
Mark Slee7ff32452007-02-01 05:26:18 +00001019FieldValue:
1020 '=' ConstValue
1021 {
Mark Slee27ed6ec2007-08-16 01:26:31 +00001022 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +00001023 $$ = $2;
1024 } else {
1025 $$ = NULL;
1026 }
1027 }
1028|
1029 {
1030 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +00001031 }
Mark Slee31985722006-05-24 21:45:31 +00001032
Mark Slee31985722006-05-24 21:45:31 +00001033FunctionType:
1034 FieldType
1035 {
Mark Sleee8540632006-05-30 09:24:40 +00001036 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +00001037 $$ = $1;
1038 }
1039| tok_void
1040 {
Mark Sleee8540632006-05-30 09:24:40 +00001041 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +00001042 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +00001043 }
1044
1045FieldType:
1046 tok_identifier
1047 {
Mark Sleee8540632006-05-30 09:24:40 +00001048 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001049 if (g_parse_mode == INCLUDES) {
1050 // Ignore identifiers in include mode
1051 $$ = NULL;
1052 } else {
1053 // Lookup the identifier in the current scope
1054 $$ = g_scope->get_type($1);
1055 if ($$ == NULL) {
1056 yyerror("Type \"%s\" has not been defined.", $1);
1057 exit(1);
1058 }
Mark Sleee8540632006-05-30 09:24:40 +00001059 }
Mark Slee31985722006-05-24 21:45:31 +00001060 }
1061| BaseType
1062 {
Mark Sleee8540632006-05-30 09:24:40 +00001063 pdebug("FieldType -> BaseType");
1064 $$ = $1;
1065 }
1066| ContainerType
1067 {
1068 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001069 $$ = $1;
1070 }
1071
David Reissc8e30052009-07-27 17:02:42 +00001072BaseType: SimpleBaseType TypeAnnotations
1073 {
1074 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1075 if ($2 != NULL) {
1076 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1077 $$->annotations_ = $2->annotations_;
1078 delete $2;
1079 } else {
1080 $$ = $1;
1081 }
1082 }
1083
1084SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001085 tok_string
1086 {
1087 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001088 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001089 }
Mark Slee8d725a22007-04-13 01:57:12 +00001090| tok_binary
1091 {
1092 pdebug("BaseType -> tok_binary");
1093 $$ = g_type_binary;
1094 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001095| tok_slist
1096 {
1097 pdebug("BaseType -> tok_slist");
1098 $$ = g_type_slist;
1099 }
Mark Slee78f58e22006-09-02 04:17:07 +00001100| tok_bool
1101 {
1102 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001103 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001104 }
Mark Slee31985722006-05-24 21:45:31 +00001105| tok_byte
1106 {
1107 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001108 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001109 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001110| tok_i16
1111 {
1112 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001113 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001114 }
Mark Slee31985722006-05-24 21:45:31 +00001115| tok_i32
1116 {
1117 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001118 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001119 }
Mark Slee31985722006-05-24 21:45:31 +00001120| tok_i64
1121 {
1122 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001123 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001124 }
Mark Sleec98d0502006-09-06 02:42:25 +00001125| tok_double
1126 {
1127 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001128 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001129 }
Mark Slee31985722006-05-24 21:45:31 +00001130
David Reissa2309992008-12-10 01:52:48 +00001131ContainerType: SimpleContainerType TypeAnnotations
1132 {
1133 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1134 $$ = $1;
1135 if ($2 != NULL) {
1136 $$->annotations_ = $2->annotations_;
1137 delete $2;
1138 }
1139 }
1140
1141SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001142 MapType
1143 {
David Reissa2309992008-12-10 01:52:48 +00001144 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001145 $$ = $1;
1146 }
1147| SetType
1148 {
David Reissa2309992008-12-10 01:52:48 +00001149 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001150 $$ = $1;
1151 }
1152| ListType
1153 {
David Reissa2309992008-12-10 01:52:48 +00001154 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001155 $$ = $1;
1156 }
1157
1158MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001159 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001160 {
1161 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001162 $$ = new t_map($4, $6);
1163 if ($2 != NULL) {
1164 ((t_container*)$$)->set_cpp_name(std::string($2));
1165 }
Mark Sleee8540632006-05-30 09:24:40 +00001166 }
1167
1168SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001169 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001170 {
1171 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001172 $$ = new t_set($4);
1173 if ($2 != NULL) {
1174 ((t_container*)$$)->set_cpp_name(std::string($2));
1175 }
Mark Sleee8540632006-05-30 09:24:40 +00001176 }
1177
1178ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001179 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001180 {
1181 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001182 $$ = new t_list($3);
1183 if ($5 != NULL) {
1184 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001185 }
1186 }
1187
Mark Slee36bfa2e2007-01-19 20:09:51 +00001188CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001189 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001190 {
Mark Sleeafc76542007-02-09 21:55:44 +00001191 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001192 }
1193|
1194 {
1195 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001196 }
1197
David Reissa2309992008-12-10 01:52:48 +00001198TypeAnnotations:
1199 '(' TypeAnnotationList ')'
1200 {
1201 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1202 $$ = $2;
1203 }
1204|
1205 {
1206 $$ = NULL;
1207 }
1208
1209TypeAnnotationList:
1210 TypeAnnotationList TypeAnnotation
1211 {
1212 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1213 $$ = $1;
1214 $$->annotations_[$2->key] = $2->val;
1215 delete $2;
1216 }
1217|
1218 {
1219 /* Just use a dummy structure to hold the annotations. */
1220 $$ = new t_struct(g_program);
1221 }
1222
1223TypeAnnotation:
1224 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1225 {
1226 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1227 $$ = new t_annotation;
1228 $$->key = $1;
1229 $$->val = $3;
1230 }
1231
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001232%%