blob: 00e939c85843e0b9e947085c0ba4decdbc29a3ba [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>
David Reissf1454162008-06-30 20:45:47 +000031#include <inttypes.h>
David Reiss400a5432008-07-25 19:48:39 +000032#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000033#include "main.h"
34#include "globals.h"
35#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000036#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000037
Mark Sleef5377b32006-10-10 01:42:59 +000038/**
39 * This global variable is used for automatic numbering of field indices etc.
40 * when parsing the members of a struct. Field values are automatically
41 * assigned starting from -1 and working their way down.
42 */
Mark Slee9cb7c612006-09-01 22:17:45 +000043int y_field_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000044int g_arglist = 0;
Bryan Duxburyab3666e2009-09-01 23:03:47 +000045const int struct_is_struct = 0;
46const int struct_is_union = 1;
Mark Slee31985722006-05-24 21:45:31 +000047
48%}
49
Mark Sleef5377b32006-10-10 01:42:59 +000050/**
51 * This structure is used by the parser to hold the data types associated with
52 * various parse nodes.
53 */
Mark Slee31985722006-05-24 21:45:31 +000054%union {
Mark Slee30152872006-11-28 01:24:07 +000055 char* id;
David Reissf1454162008-06-30 20:45:47 +000056 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000057 double dconst;
58 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000059 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000060 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000061 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000062 t_typedef* ttypedef;
63 t_enum* tenum;
64 t_enum_value* tenumv;
65 t_const* tconst;
66 t_const_value* tconstv;
67 t_struct* tstruct;
68 t_service* tservice;
69 t_function* tfunction;
70 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000071 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000072 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000073 t_annotation* tannot;
Mark Slee31985722006-05-24 21:45:31 +000074}
75
Mark Sleef5377b32006-10-10 01:42:59 +000076/**
77 * Strings identifier
78 */
Mark Slee31985722006-05-24 21:45:31 +000079%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000080%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000081%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000082%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000083
84/**
Mark Slee30152872006-11-28 01:24:07 +000085 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000086 */
Mark Slee31985722006-05-24 21:45:31 +000087%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000088%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000089
Mark Sleef5377b32006-10-10 01:42:59 +000090/**
David Reiss399442b2008-02-20 02:28:05 +000091 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +000092 */
Mark Sleef0712dc2006-10-25 19:03:57 +000093%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000094%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000095%token tok_cpp_namespace
96%token tok_cpp_include
97%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000098%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000099%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000100%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000101%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000102%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000103%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000104%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000105%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000106%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000107%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000108%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000109%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000110%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000111%token tok_csharp_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000112
Mark Sleef5377b32006-10-10 01:42:59 +0000113/**
114 * Base datatype keywords
115 */
116%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000117%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000118%token tok_byte
119%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000120%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000121%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000122%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000123%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000124%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000125%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000126%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000127
Mark Sleef5377b32006-10-10 01:42:59 +0000128/**
129 * Complex type keywords
130 */
Mark Slee31985722006-05-24 21:45:31 +0000131%token tok_map
132%token tok_list
133%token tok_set
134
Mark Sleef5377b32006-10-10 01:42:59 +0000135/**
136 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000137 */
David Reiss6985a422009-03-24 20:00:47 +0000138%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000139
Mark Sleef5377b32006-10-10 01:42:59 +0000140/**
141 * Thrift language keywords
142 */
Mark Slee31985722006-05-24 21:45:31 +0000143%token tok_typedef
144%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000145%token tok_xception
146%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000147%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000148%token tok_service
149%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000150%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000151%token tok_required
152%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000153%token tok_union
Mark Slee31985722006-05-24 21:45:31 +0000154
Mark Sleef5377b32006-10-10 01:42:59 +0000155/**
156 * Grammar nodes
157 */
158
Mark Slee31985722006-05-24 21:45:31 +0000159%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000160%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000161%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000162%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000163%type<ttype> MapType
164%type<ttype> SetType
165%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000166
David Reisscdffe262007-08-14 17:12:31 +0000167%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000168%type<ttype> TypeDefinition
169
Mark Slee31985722006-05-24 21:45:31 +0000170%type<ttypedef> Typedef
171%type<ttype> DefinitionType
172
David Reissa2309992008-12-10 01:52:48 +0000173%type<ttype> TypeAnnotations
174%type<ttype> TypeAnnotationList
175%type<tannot> TypeAnnotation
176
Mark Slee31985722006-05-24 21:45:31 +0000177%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000178%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000179%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000180%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000181%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000182%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000183
184%type<tenum> Enum
185%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000186%type<tenumv> EnumDef
187
Mark Slee6a47fed2007-02-07 02:40:59 +0000188%type<ttypedef> Senum
189%type<tbase> SenumDefList
190%type<id> SenumDef
191
Mark Slee30152872006-11-28 01:24:07 +0000192%type<tconst> Const
193%type<tconstv> ConstValue
194%type<tconstv> ConstList
195%type<tconstv> ConstListContents
196%type<tconstv> ConstMap
197%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000198
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000199%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000200%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000201%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000202%type<tservice> Service
203
204%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000205%type<ttype> FunctionType
206%type<tservice> FunctionList
207
Mark Slee36bfa2e2007-01-19 20:09:51 +0000208%type<tstruct> Throws
209%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000210%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000211%type<tbool> XsdAll
212%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000213%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000214%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000215%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000216
David Reisscbd4bac2007-08-14 17:12:33 +0000217%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000218
Mark Slee31985722006-05-24 21:45:31 +0000219%%
220
Mark Sleef5377b32006-10-10 01:42:59 +0000221/**
222 * Thrift Grammar Implementation.
223 *
224 * For the most part this source file works its way top down from what you
225 * might expect to find in a typical .thrift file, i.e. type definitions and
226 * namespaces up top followed by service definitions using those types.
227 */
Mark Slee31985722006-05-24 21:45:31 +0000228
229Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000230 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000231 {
232 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000233 /*
234 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000235 if ($1 != NULL) {
236 g_program->set_doc($1);
237 }
David Reisscbd4bac2007-08-14 17:12:33 +0000238 */
239 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000240 }
241
David Reisscbd4bac2007-08-14 17:12:33 +0000242CaptureDocText:
243 {
244 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000245 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000246 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000247 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000248 $$ = NULL;
249 }
250 }
251
252/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
253DestroyDocText:
254 {
255 if (g_parse_mode == PROGRAM) {
256 clear_doctext();
257 }
258 }
259
260/* We have to DestroyDocText here, otherwise it catches the doctext
261 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000262HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000263 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000264 {
265 pdebug("HeaderList -> HeaderList Header");
266 }
267|
268 {
269 pdebug("HeaderList -> ");
270 }
271
272Header:
273 Include
274 {
275 pdebug("Header -> Include");
276 }
David Reiss79eca142008-02-27 01:55:13 +0000277| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000278 {
David Reiss79eca142008-02-27 01:55:13 +0000279 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000280 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000281 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000282 }
283 }
David Reiss9a08dc62008-02-27 01:55:17 +0000284/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000285| tok_cpp_namespace tok_identifier
286 {
David Reiss9a08dc62008-02-27 01:55:17 +0000287 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000288 pdebug("Header -> tok_cpp_namespace tok_identifier");
289 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000290 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000291 }
292 }
293| tok_cpp_include tok_literal
294 {
295 pdebug("Header -> tok_cpp_include tok_literal");
296 if (g_parse_mode == PROGRAM) {
297 g_program->add_cpp_include($2);
298 }
299 }
Mark Sleee888b372007-01-12 01:06:24 +0000300| tok_php_namespace tok_identifier
301 {
David Reiss554ea6f2009-02-17 20:28:37 +0000302 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000303 pdebug("Header -> tok_php_namespace tok_identifier");
304 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000305 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000306 }
307 }
David Reiss320e45c2008-03-27 21:41:54 +0000308/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000309| tok_py_module tok_identifier
310 {
David Reiss320e45c2008-03-27 21:41:54 +0000311 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000312 pdebug("Header -> tok_py_module tok_identifier");
313 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000314 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000315 }
316 }
David Reiss07ef3a92008-03-27 21:42:39 +0000317/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000318| tok_perl_package tok_identifier
319 {
David Reiss07ef3a92008-03-27 21:42:39 +0000320 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000321 pdebug("Header -> tok_perl_namespace tok_identifier");
322 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000323 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000324 }
325 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000326/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000327| tok_ruby_namespace tok_identifier
328 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000329 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000330 pdebug("Header -> tok_ruby_namespace tok_identifier");
331 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000332 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000333 }
334 }
David Reiss3b455012008-03-27 21:40:46 +0000335/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000336| tok_smalltalk_category tok_st_identifier
337 {
David Reiss3b455012008-03-27 21:40:46 +0000338 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000339 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
340 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000341 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000342 }
343 }
David Reiss3b455012008-03-27 21:40:46 +0000344/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000345| tok_smalltalk_prefix tok_identifier
346 {
David Reiss3b455012008-03-27 21:40:46 +0000347 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000348 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
349 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000350 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000351 }
352 }
David Reiss771f8c72008-02-27 01:55:25 +0000353/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000354| tok_java_package tok_identifier
355 {
David Reiss9f646152008-03-02 21:59:48 +0000356 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000357 pdebug("Header -> tok_java_package tok_identifier");
358 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000359 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000360 }
361 }
David Reiss54b602b2008-03-27 21:41:06 +0000362/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000363| tok_cocoa_prefix tok_identifier
364 {
David Reiss54b602b2008-03-27 21:41:06 +0000365 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000366 pdebug("Header -> tok_cocoa_prefix tok_identifier");
367 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000368 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000369 }
370 }
David Reiss92e10d82009-02-17 20:28:19 +0000371/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000372| tok_xsd_namespace tok_literal
373 {
David Reiss92e10d82009-02-17 20:28:19 +0000374 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000375 pdebug("Header -> tok_xsd_namespace tok_literal");
376 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000377 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000378 }
379 }
David Reiss9d65bf02008-03-27 21:41:37 +0000380/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000381| tok_csharp_namespace tok_identifier
382 {
David Reiss9d65bf02008-03-27 21:41:37 +0000383 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000384 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000385 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000386 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000387 }
388 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000389
390Include:
391 tok_include tok_literal
392 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000393 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000394 if (g_parse_mode == INCLUDES) {
395 std::string path = include_file(std::string($2));
396 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000397 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000398 }
399 }
400 }
Mark Slee31985722006-05-24 21:45:31 +0000401
402DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000403 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000404 {
405 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000406 if ($2 != NULL && $3 != NULL) {
407 $3->set_doc($2);
408 }
Mark Slee31985722006-05-24 21:45:31 +0000409 }
410|
411 {
412 pdebug("DefinitionList -> ");
413 }
414
415Definition:
Mark Slee30152872006-11-28 01:24:07 +0000416 Const
417 {
418 pdebug("Definition -> Const");
419 if (g_parse_mode == PROGRAM) {
420 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000421 }
David Reisscdffe262007-08-14 17:12:31 +0000422 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000423 }
424| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000425 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000426 pdebug("Definition -> TypeDefinition");
427 if (g_parse_mode == PROGRAM) {
428 g_scope->add_type($1->get_name(), $1);
429 if (g_parent_scope != NULL) {
430 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
431 }
432 }
David Reisscdffe262007-08-14 17:12:31 +0000433 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000434 }
Mark Slee31985722006-05-24 21:45:31 +0000435| Service
436 {
437 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000438 if (g_parse_mode == PROGRAM) {
439 g_scope->add_service($1->get_name(), $1);
440 if (g_parent_scope != NULL) {
441 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
442 }
443 g_program->add_service($1);
444 }
David Reisscdffe262007-08-14 17:12:31 +0000445 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000446 }
447
Mark Sleef0712dc2006-10-25 19:03:57 +0000448TypeDefinition:
449 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000450 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000451 pdebug("TypeDefinition -> Typedef");
452 if (g_parse_mode == PROGRAM) {
453 g_program->add_typedef($1);
454 }
455 }
456| Enum
457 {
458 pdebug("TypeDefinition -> Enum");
459 if (g_parse_mode == PROGRAM) {
460 g_program->add_enum($1);
461 }
462 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000463| Senum
464 {
465 pdebug("TypeDefinition -> Senum");
466 if (g_parse_mode == PROGRAM) {
467 g_program->add_typedef($1);
468 }
469 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000470| Struct
471 {
472 pdebug("TypeDefinition -> Struct");
473 if (g_parse_mode == PROGRAM) {
474 g_program->add_struct($1);
475 }
476 }
477| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000478 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000479 pdebug("TypeDefinition -> Xception");
480 if (g_parse_mode == PROGRAM) {
481 g_program->add_xception($1);
482 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000483 }
Mark Slee31985722006-05-24 21:45:31 +0000484
485Typedef:
Mark Sleebd588222007-11-21 08:43:35 +0000486 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000487 {
488 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000489 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000490 $$ = td;
491 }
492
Mark Slee6a47fed2007-02-07 02:40:59 +0000493CommaOrSemicolonOptional:
494 ','
495 {}
496| ';'
497 {}
498|
499 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000500
Mark Slee31985722006-05-24 21:45:31 +0000501Enum:
David Reisscdffe262007-08-14 17:12:31 +0000502 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000503 {
504 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000505 $$ = $4;
506 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000507 }
508
509EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000510 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000511 {
512 pdebug("EnumDefList -> EnumDefList EnumDef");
513 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000514 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000515 }
516|
517 {
518 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000519 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000520 }
521
522EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000523 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000524 {
Mark Slee30152872006-11-28 01:24:07 +0000525 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000526 if ($4 < 0) {
527 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000528 }
David Reissf1454162008-06-30 20:45:47 +0000529 if ($4 > INT_MAX) {
530 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
531 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000532 $$ = new t_enum_value($2, $4);
533 if ($1 != NULL) {
534 $$->set_doc($1);
535 }
Mark Sleed0767c52007-07-27 22:14:41 +0000536 if (g_parse_mode == PROGRAM) {
537 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
538 if (g_parent_scope != NULL) {
539 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
540 }
541 }
Mark Slee31985722006-05-24 21:45:31 +0000542 }
543|
David Reisscbd4bac2007-08-14 17:12:33 +0000544 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000545 {
Mark Slee30152872006-11-28 01:24:07 +0000546 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000547 $$ = new t_enum_value($2);
548 if ($1 != NULL) {
549 $$->set_doc($1);
550 }
Mark Slee30152872006-11-28 01:24:07 +0000551 }
552
Mark Slee6a47fed2007-02-07 02:40:59 +0000553Senum:
David Reisscdffe262007-08-14 17:12:31 +0000554 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000555 {
556 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000557 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000558 }
559
560SenumDefList:
561 SenumDefList SenumDef
562 {
563 pdebug("SenumDefList -> SenumDefList SenumDef");
564 $$ = $1;
565 $$->add_string_enum_val($2);
566 }
567|
568 {
569 pdebug("SenumDefList -> ");
570 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
571 $$->set_string_enum(true);
572 }
573
574SenumDef:
575 tok_literal CommaOrSemicolonOptional
576 {
577 pdebug("SenumDef -> tok_literal");
578 $$ = $1;
579 }
580
Mark Slee30152872006-11-28 01:24:07 +0000581Const:
582 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
583 {
584 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000585 if (g_parse_mode == PROGRAM) {
586 $$ = new t_const($2, $3, $5);
587 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000588
589 g_scope->add_constant($3, $$);
590 if (g_parent_scope != NULL) {
591 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
592 }
593
Mark Sleeaa7671d2006-11-29 03:19:31 +0000594 } else {
595 $$ = NULL;
596 }
Mark Slee30152872006-11-28 01:24:07 +0000597 }
598
599ConstValue:
600 tok_int_constant
601 {
602 pdebug("ConstValue => tok_int_constant");
603 $$ = new t_const_value();
604 $$->set_integer($1);
David Reissf1454162008-06-30 20:45:47 +0000605 if ($1 < INT32_MIN || $1 > INT32_MAX) {
606 pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
607 }
Mark Slee30152872006-11-28 01:24:07 +0000608 }
609| tok_dub_constant
610 {
611 pdebug("ConstValue => tok_dub_constant");
612 $$ = new t_const_value();
613 $$->set_double($1);
614 }
615| tok_literal
616 {
617 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000618 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000619 }
Mark Slee67fc6342006-11-29 03:37:04 +0000620| tok_identifier
621 {
622 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000623 t_const* constant = g_scope->get_constant($1);
624 if (constant != NULL) {
625 $$ = constant->get_value();
626 } else {
627 if (g_parse_mode == PROGRAM) {
628 pwarning(1, "Constant strings should be quoted: %s\n", $1);
629 }
630 $$ = new t_const_value($1);
631 }
Mark Slee67fc6342006-11-29 03:37:04 +0000632 }
Mark Slee30152872006-11-28 01:24:07 +0000633| ConstList
634 {
635 pdebug("ConstValue => ConstList");
636 $$ = $1;
637 }
638| ConstMap
639 {
640 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000641 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000642 }
643
644ConstList:
645 '[' ConstListContents ']'
646 {
647 pdebug("ConstList => [ ConstListContents ]");
648 $$ = $2;
649 }
650
651ConstListContents:
652 ConstListContents ConstValue CommaOrSemicolonOptional
653 {
654 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
655 $$ = $1;
656 $$->add_list($2);
657 }
658|
659 {
660 pdebug("ConstListContents =>");
661 $$ = new t_const_value();
662 $$->set_list();
663 }
664
665ConstMap:
666 '{' ConstMapContents '}'
667 {
668 pdebug("ConstMap => { ConstMapContents }");
669 $$ = $2;
670 }
671
672ConstMapContents:
673 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
674 {
675 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
676 $$ = $1;
677 $$->add_map($2, $4);
678 }
679|
680 {
681 pdebug("ConstMapContents =>");
682 $$ = new t_const_value();
683 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000684 }
685
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000686StructHead:
687 tok_struct
688 {
689 $$ = struct_is_struct;
690 }
691| tok_union
692 {
693 $$ = struct_is_union;
694 }
695
Mark Slee31985722006-05-24 21:45:31 +0000696Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000697 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000698 {
699 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000700 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000701 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000702 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000703 $$->set_name($2);
704 if ($7 != NULL) {
705 $$->annotations_ = $7->annotations_;
706 delete $7;
707 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000708 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000709
Mark Slee36bfa2e2007-01-19 20:09:51 +0000710XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000711 tok_xsd_all
712 {
713 $$ = true;
714 }
715|
716 {
717 $$ = false;
718 }
719
Mark Slee36bfa2e2007-01-19 20:09:51 +0000720XsdOptional:
721 tok_xsd_optional
722 {
723 $$ = true;
724 }
725|
726 {
727 $$ = false;
728 }
729
Mark Slee7df0e2a2007-02-06 21:03:18 +0000730XsdNillable:
731 tok_xsd_nillable
732 {
733 $$ = true;
734 }
735|
736 {
737 $$ = false;
738 }
739
Mark Slee21135c32007-02-05 21:52:08 +0000740XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000741 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000742 {
Mark Slee748d83f2007-02-07 01:20:08 +0000743 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000744 }
745|
746 {
747 $$ = NULL;
748 }
749
Mark Slee9cb7c612006-09-01 22:17:45 +0000750Xception:
751 tok_xception tok_identifier '{' FieldList '}'
752 {
753 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
754 $4->set_name($2);
755 $4->set_xception(true);
756 $$ = $4;
Mark Slee31985722006-05-24 21:45:31 +0000757 }
758
759Service:
Mark Slee78165722007-09-10 22:08:49 +0000760 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000761 {
762 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000763 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000764 $$->set_name($2);
765 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000766 }
767
Mark Slee78165722007-09-10 22:08:49 +0000768FlagArgs:
769 {
770 g_arglist = 1;
771 }
772
773UnflagArgs:
774 {
775 g_arglist = 0;
776 }
777
Mark Slee36bfa2e2007-01-19 20:09:51 +0000778Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000779 tok_extends tok_identifier
780 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000781 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000782 $$ = NULL;
783 if (g_parse_mode == PROGRAM) {
784 $$ = g_scope->get_service($2);
785 if ($$ == NULL) {
786 yyerror("Service \"%s\" has not been defined.", $2);
787 exit(1);
788 }
789 }
790 }
791|
792 {
793 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000794 }
795
796FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000797 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000798 {
799 pdebug("FunctionList -> FunctionList Function");
800 $$ = $1;
801 $1->add_function($2);
802 }
803|
804 {
805 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000806 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000807 }
808
809Function:
David Reiss6985a422009-03-24 20:00:47 +0000810 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000811 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000812 $6->set_name(std::string($4) + "_args");
813 $$ = new t_function($3, $4, $6, $8, $2);
814 if ($1 != NULL) {
815 $$->set_doc($1);
816 }
Mark Slee31985722006-05-24 21:45:31 +0000817 }
818
David Reiss6985a422009-03-24 20:00:47 +0000819Oneway:
820 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000821 {
Mark Slee52f643d2006-08-09 00:03:43 +0000822 $$ = true;
823 }
824|
825 {
826 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000827 }
828
Mark Slee36bfa2e2007-01-19 20:09:51 +0000829Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000830 tok_throws '(' FieldList ')'
831 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000832 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000833 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000834 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000835 yyerror("Throws clause may not contain non-exception types");
836 exit(1);
837 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000838 }
839|
840 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000841 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000842 }
843
Mark Slee31985722006-05-24 21:45:31 +0000844FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000845 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000846 {
847 pdebug("FieldList -> FieldList , Field");
848 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000849 if (!($$->append($2))) {
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000850 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
851 exit(1);
852 }
Mark Slee31985722006-05-24 21:45:31 +0000853 }
854|
855 {
856 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000857 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000858 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000859 }
860
861Field:
David Reiss8320a922007-08-14 19:59:26 +0000862 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000863 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000864 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000865 if ($2 < 0) {
David Reissbb461362009-04-02 19:23:59 +0000866 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 +0000867 if (g_strict >= 192) {
868 yyerror("Implicit field keys are deprecated and not allowed with -strict");
869 exit(1);
870 }
Mark Slee31985722006-05-24 21:45:31 +0000871 }
David Reiss8320a922007-08-14 19:59:26 +0000872 $$ = new t_field($4, $5, $2);
873 $$->set_req($3);
874 if ($6 != NULL) {
875 validate_field_value($$, $6);
876 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000877 }
David Reiss8320a922007-08-14 19:59:26 +0000878 $$->set_xsd_optional($7);
879 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000880 if ($1 != NULL) {
881 $$->set_doc($1);
882 }
David Reiss8320a922007-08-14 19:59:26 +0000883 if ($9 != NULL) {
884 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000885 }
Mark Slee31985722006-05-24 21:45:31 +0000886 }
Mark Slee7ff32452007-02-01 05:26:18 +0000887
888FieldIdentifier:
889 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000890 {
Mark Slee7ff32452007-02-01 05:26:18 +0000891 if ($1 <= 0) {
892 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
893 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000894 }
Mark Slee7ff32452007-02-01 05:26:18 +0000895 $$ = $1;
896 }
897|
898 {
899 $$ = y_field_val--;
900 }
901
David Reiss8320a922007-08-14 19:59:26 +0000902FieldRequiredness:
903 tok_required
904 {
David Reiss45603e92009-09-02 22:15:55 +0000905 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +0000906 }
907| tok_optional
908 {
Mark Slee78165722007-09-10 22:08:49 +0000909 if (g_arglist) {
910 if (g_parse_mode == PROGRAM) {
911 pwarning(1, "optional keyword is ignored in argument lists.\n");
912 }
David Reiss204420f2008-01-11 20:59:03 +0000913 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000914 } else {
David Reiss204420f2008-01-11 20:59:03 +0000915 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000916 }
David Reiss8320a922007-08-14 19:59:26 +0000917 }
918|
919 {
David Reiss204420f2008-01-11 20:59:03 +0000920 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000921 }
922
Mark Slee7ff32452007-02-01 05:26:18 +0000923FieldValue:
924 '=' ConstValue
925 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000926 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000927 $$ = $2;
928 } else {
929 $$ = NULL;
930 }
931 }
932|
933 {
934 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000935 }
Mark Slee31985722006-05-24 21:45:31 +0000936
937DefinitionType:
938 BaseType
939 {
940 pdebug("DefinitionType -> BaseType");
941 $$ = $1;
942 }
Mark Sleee8540632006-05-30 09:24:40 +0000943| ContainerType
944 {
945 pdebug("DefinitionType -> ContainerType");
946 $$ = $1;
947 }
Mark Slee31985722006-05-24 21:45:31 +0000948
949FunctionType:
950 FieldType
951 {
Mark Sleee8540632006-05-30 09:24:40 +0000952 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000953 $$ = $1;
954 }
955| tok_void
956 {
Mark Sleee8540632006-05-30 09:24:40 +0000957 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000958 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000959 }
960
961FieldType:
962 tok_identifier
963 {
Mark Sleee8540632006-05-30 09:24:40 +0000964 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000965 if (g_parse_mode == INCLUDES) {
966 // Ignore identifiers in include mode
967 $$ = NULL;
968 } else {
969 // Lookup the identifier in the current scope
970 $$ = g_scope->get_type($1);
971 if ($$ == NULL) {
972 yyerror("Type \"%s\" has not been defined.", $1);
973 exit(1);
974 }
Mark Sleee8540632006-05-30 09:24:40 +0000975 }
Mark Slee31985722006-05-24 21:45:31 +0000976 }
977| BaseType
978 {
Mark Sleee8540632006-05-30 09:24:40 +0000979 pdebug("FieldType -> BaseType");
980 $$ = $1;
981 }
982| ContainerType
983 {
984 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000985 $$ = $1;
986 }
987
David Reissc8e30052009-07-27 17:02:42 +0000988BaseType: SimpleBaseType TypeAnnotations
989 {
990 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
991 if ($2 != NULL) {
992 $$ = new t_base_type(*static_cast<t_base_type*>($1));
993 $$->annotations_ = $2->annotations_;
994 delete $2;
995 } else {
996 $$ = $1;
997 }
998 }
999
1000SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001001 tok_string
1002 {
1003 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001004 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001005 }
Mark Slee8d725a22007-04-13 01:57:12 +00001006| tok_binary
1007 {
1008 pdebug("BaseType -> tok_binary");
1009 $$ = g_type_binary;
1010 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001011| tok_slist
1012 {
1013 pdebug("BaseType -> tok_slist");
1014 $$ = g_type_slist;
1015 }
Mark Slee78f58e22006-09-02 04:17:07 +00001016| tok_bool
1017 {
1018 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001019 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001020 }
Mark Slee31985722006-05-24 21:45:31 +00001021| tok_byte
1022 {
1023 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001024 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001025 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001026| tok_i16
1027 {
1028 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001029 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001030 }
Mark Slee31985722006-05-24 21:45:31 +00001031| tok_i32
1032 {
1033 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001034 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001035 }
Mark Slee31985722006-05-24 21:45:31 +00001036| tok_i64
1037 {
1038 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001039 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001040 }
Mark Sleec98d0502006-09-06 02:42:25 +00001041| tok_double
1042 {
1043 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001044 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001045 }
Mark Slee31985722006-05-24 21:45:31 +00001046
David Reissa2309992008-12-10 01:52:48 +00001047ContainerType: SimpleContainerType TypeAnnotations
1048 {
1049 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1050 $$ = $1;
1051 if ($2 != NULL) {
1052 $$->annotations_ = $2->annotations_;
1053 delete $2;
1054 }
1055 }
1056
1057SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001058 MapType
1059 {
David Reissa2309992008-12-10 01:52:48 +00001060 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001061 $$ = $1;
1062 }
1063| SetType
1064 {
David Reissa2309992008-12-10 01:52:48 +00001065 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001066 $$ = $1;
1067 }
1068| ListType
1069 {
David Reissa2309992008-12-10 01:52:48 +00001070 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001071 $$ = $1;
1072 }
1073
1074MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001075 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001076 {
1077 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001078 $$ = new t_map($4, $6);
1079 if ($2 != NULL) {
1080 ((t_container*)$$)->set_cpp_name(std::string($2));
1081 }
Mark Sleee8540632006-05-30 09:24:40 +00001082 }
1083
1084SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001085 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001086 {
1087 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001088 $$ = new t_set($4);
1089 if ($2 != NULL) {
1090 ((t_container*)$$)->set_cpp_name(std::string($2));
1091 }
Mark Sleee8540632006-05-30 09:24:40 +00001092 }
1093
1094ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001095 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001096 {
1097 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001098 $$ = new t_list($3);
1099 if ($5 != NULL) {
1100 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001101 }
1102 }
1103
Mark Slee36bfa2e2007-01-19 20:09:51 +00001104CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001105 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001106 {
Mark Sleeafc76542007-02-09 21:55:44 +00001107 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001108 }
1109|
1110 {
1111 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001112 }
1113
David Reissa2309992008-12-10 01:52:48 +00001114TypeAnnotations:
1115 '(' TypeAnnotationList ')'
1116 {
1117 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1118 $$ = $2;
1119 }
1120|
1121 {
1122 $$ = NULL;
1123 }
1124
1125TypeAnnotationList:
1126 TypeAnnotationList TypeAnnotation
1127 {
1128 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1129 $$ = $1;
1130 $$->annotations_[$2->key] = $2->val;
1131 delete $2;
1132 }
1133|
1134 {
1135 /* Just use a dummy structure to hold the annotations. */
1136 $$ = new t_struct(g_program);
1137 }
1138
1139TypeAnnotation:
1140 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1141 {
1142 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1143 $$ = new t_annotation;
1144 $$->key = $1;
1145 $$->val = $3;
1146 }
1147
Bryan Duxburyab3666e2009-09-01 23:03:47 +00001148%%