blob: d12cca39b4a4a6658065ff9de9d2730d0b030366 [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
Mark Slee31985722006-05-24 21:45:31 +0000171
David Reissa2309992008-12-10 01:52:48 +0000172%type<ttype> TypeAnnotations
173%type<ttype> TypeAnnotationList
174%type<tannot> TypeAnnotation
175
Mark Slee31985722006-05-24 21:45:31 +0000176%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000177%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000178%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000179%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000180%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000181%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000182
183%type<tenum> Enum
184%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000185%type<tenumv> EnumDef
186
Mark Slee6a47fed2007-02-07 02:40:59 +0000187%type<ttypedef> Senum
188%type<tbase> SenumDefList
189%type<id> SenumDef
190
Mark Slee30152872006-11-28 01:24:07 +0000191%type<tconst> Const
192%type<tconstv> ConstValue
193%type<tconstv> ConstList
194%type<tconstv> ConstListContents
195%type<tconstv> ConstMap
196%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000197
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000198%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000199%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000200%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000201%type<tservice> Service
202
203%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000204%type<ttype> FunctionType
205%type<tservice> FunctionList
206
Mark Slee36bfa2e2007-01-19 20:09:51 +0000207%type<tstruct> Throws
208%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000209%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000210%type<tbool> XsdAll
211%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000212%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000213%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000214%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000215
David Reisscbd4bac2007-08-14 17:12:33 +0000216%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000217
Mark Slee31985722006-05-24 21:45:31 +0000218%%
219
Mark Sleef5377b32006-10-10 01:42:59 +0000220/**
221 * Thrift Grammar Implementation.
222 *
223 * For the most part this source file works its way top down from what you
224 * might expect to find in a typical .thrift file, i.e. type definitions and
225 * namespaces up top followed by service definitions using those types.
226 */
Mark Slee31985722006-05-24 21:45:31 +0000227
228Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000229 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000230 {
231 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000232 /*
233 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000234 if ($1 != NULL) {
235 g_program->set_doc($1);
236 }
David Reisscbd4bac2007-08-14 17:12:33 +0000237 */
238 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000239 }
240
David Reisscbd4bac2007-08-14 17:12:33 +0000241CaptureDocText:
242 {
243 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000244 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000245 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000246 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000247 $$ = NULL;
248 }
249 }
250
251/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
252DestroyDocText:
253 {
254 if (g_parse_mode == PROGRAM) {
255 clear_doctext();
256 }
257 }
258
259/* We have to DestroyDocText here, otherwise it catches the doctext
260 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000261HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000262 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000263 {
264 pdebug("HeaderList -> HeaderList Header");
265 }
266|
267 {
268 pdebug("HeaderList -> ");
269 }
270
271Header:
272 Include
273 {
274 pdebug("Header -> Include");
275 }
David Reiss79eca142008-02-27 01:55:13 +0000276| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000277 {
David Reiss79eca142008-02-27 01:55:13 +0000278 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000279 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000280 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000281 }
282 }
David Reiss9a08dc62008-02-27 01:55:17 +0000283/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000284| tok_cpp_namespace tok_identifier
285 {
David Reiss9a08dc62008-02-27 01:55:17 +0000286 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 pdebug("Header -> tok_cpp_namespace tok_identifier");
288 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000289 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000290 }
291 }
292| tok_cpp_include tok_literal
293 {
294 pdebug("Header -> tok_cpp_include tok_literal");
295 if (g_parse_mode == PROGRAM) {
296 g_program->add_cpp_include($2);
297 }
298 }
Mark Sleee888b372007-01-12 01:06:24 +0000299| tok_php_namespace tok_identifier
300 {
David Reiss554ea6f2009-02-17 20:28:37 +0000301 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000302 pdebug("Header -> tok_php_namespace tok_identifier");
303 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000304 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000305 }
306 }
David Reiss320e45c2008-03-27 21:41:54 +0000307/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000308| tok_py_module tok_identifier
309 {
David Reiss320e45c2008-03-27 21:41:54 +0000310 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000311 pdebug("Header -> tok_py_module tok_identifier");
312 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000313 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000314 }
315 }
David Reiss07ef3a92008-03-27 21:42:39 +0000316/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000317| tok_perl_package tok_identifier
318 {
David Reiss07ef3a92008-03-27 21:42:39 +0000319 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000320 pdebug("Header -> tok_perl_namespace tok_identifier");
321 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000322 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000323 }
324 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000325/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000326| tok_ruby_namespace tok_identifier
327 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000328 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000329 pdebug("Header -> tok_ruby_namespace tok_identifier");
330 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000331 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000332 }
333 }
David Reiss3b455012008-03-27 21:40:46 +0000334/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000335| tok_smalltalk_category tok_st_identifier
336 {
David Reiss3b455012008-03-27 21:40:46 +0000337 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000338 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
339 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000340 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000341 }
342 }
David Reiss3b455012008-03-27 21:40:46 +0000343/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000344| tok_smalltalk_prefix tok_identifier
345 {
David Reiss3b455012008-03-27 21:40:46 +0000346 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000347 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
348 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000349 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000350 }
351 }
David Reiss771f8c72008-02-27 01:55:25 +0000352/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000353| tok_java_package tok_identifier
354 {
David Reiss9f646152008-03-02 21:59:48 +0000355 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000356 pdebug("Header -> tok_java_package tok_identifier");
357 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000358 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000359 }
360 }
David Reiss54b602b2008-03-27 21:41:06 +0000361/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000362| tok_cocoa_prefix tok_identifier
363 {
David Reiss54b602b2008-03-27 21:41:06 +0000364 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000365 pdebug("Header -> tok_cocoa_prefix tok_identifier");
366 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000367 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000368 }
369 }
David Reiss92e10d82009-02-17 20:28:19 +0000370/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000371| tok_xsd_namespace tok_literal
372 {
David Reiss92e10d82009-02-17 20:28:19 +0000373 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000374 pdebug("Header -> tok_xsd_namespace tok_literal");
375 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000376 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000377 }
378 }
David Reiss9d65bf02008-03-27 21:41:37 +0000379/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000380| tok_csharp_namespace tok_identifier
381 {
David Reiss9d65bf02008-03-27 21:41:37 +0000382 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000383 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000384 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000385 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000386 }
387 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000388
389Include:
390 tok_include tok_literal
391 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000392 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000393 if (g_parse_mode == INCLUDES) {
394 std::string path = include_file(std::string($2));
395 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000396 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000397 }
398 }
399 }
Mark Slee31985722006-05-24 21:45:31 +0000400
401DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000402 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000403 {
404 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000405 if ($2 != NULL && $3 != NULL) {
406 $3->set_doc($2);
407 }
Mark Slee31985722006-05-24 21:45:31 +0000408 }
409|
410 {
411 pdebug("DefinitionList -> ");
412 }
413
414Definition:
Mark Slee30152872006-11-28 01:24:07 +0000415 Const
416 {
417 pdebug("Definition -> Const");
418 if (g_parse_mode == PROGRAM) {
419 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000420 }
David Reisscdffe262007-08-14 17:12:31 +0000421 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000422 }
423| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000424 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000425 pdebug("Definition -> TypeDefinition");
426 if (g_parse_mode == PROGRAM) {
427 g_scope->add_type($1->get_name(), $1);
428 if (g_parent_scope != NULL) {
429 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
430 }
431 }
David Reisscdffe262007-08-14 17:12:31 +0000432 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000433 }
Mark Slee31985722006-05-24 21:45:31 +0000434| Service
435 {
436 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000437 if (g_parse_mode == PROGRAM) {
438 g_scope->add_service($1->get_name(), $1);
439 if (g_parent_scope != NULL) {
440 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
441 }
442 g_program->add_service($1);
443 }
David Reisscdffe262007-08-14 17:12:31 +0000444 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000445 }
446
Mark Sleef0712dc2006-10-25 19:03:57 +0000447TypeDefinition:
448 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000449 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000450 pdebug("TypeDefinition -> Typedef");
451 if (g_parse_mode == PROGRAM) {
452 g_program->add_typedef($1);
453 }
454 }
455| Enum
456 {
457 pdebug("TypeDefinition -> Enum");
458 if (g_parse_mode == PROGRAM) {
459 g_program->add_enum($1);
460 }
461 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000462| Senum
463 {
464 pdebug("TypeDefinition -> Senum");
465 if (g_parse_mode == PROGRAM) {
466 g_program->add_typedef($1);
467 }
468 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000469| Struct
470 {
471 pdebug("TypeDefinition -> Struct");
472 if (g_parse_mode == PROGRAM) {
473 g_program->add_struct($1);
474 }
475 }
476| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000477 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000478 pdebug("TypeDefinition -> Xception");
479 if (g_parse_mode == PROGRAM) {
480 g_program->add_xception($1);
481 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000482 }
Mark Slee31985722006-05-24 21:45:31 +0000483
484Typedef:
David Reiss4dd78012010-03-09 05:19:08 +0000485 tok_typedef FieldType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000486 {
David Reiss4dd78012010-03-09 05:19:08 +0000487 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000488 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000489 $$ = td;
490 }
491
Mark Slee6a47fed2007-02-07 02:40:59 +0000492CommaOrSemicolonOptional:
493 ','
494 {}
495| ';'
496 {}
497|
498 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000499
Mark Slee31985722006-05-24 21:45:31 +0000500Enum:
David Reisscdffe262007-08-14 17:12:31 +0000501 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000502 {
503 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000504 $$ = $4;
505 $$->set_name($2);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000506 $$->resolve_values();
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) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000586 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000587 $$ = new t_const($2, $3, $5);
588 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000589
590 g_scope->add_constant($3, $$);
591 if (g_parent_scope != NULL) {
592 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
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");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000623 $$ = new t_const_value();
624 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000625 }
Mark Slee30152872006-11-28 01:24:07 +0000626| ConstList
627 {
628 pdebug("ConstValue => ConstList");
629 $$ = $1;
630 }
631| ConstMap
632 {
633 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000634 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000635 }
636
637ConstList:
638 '[' ConstListContents ']'
639 {
640 pdebug("ConstList => [ ConstListContents ]");
641 $$ = $2;
642 }
643
644ConstListContents:
645 ConstListContents ConstValue CommaOrSemicolonOptional
646 {
647 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
648 $$ = $1;
649 $$->add_list($2);
650 }
651|
652 {
653 pdebug("ConstListContents =>");
654 $$ = new t_const_value();
655 $$->set_list();
656 }
657
658ConstMap:
659 '{' ConstMapContents '}'
660 {
661 pdebug("ConstMap => { ConstMapContents }");
662 $$ = $2;
663 }
664
665ConstMapContents:
666 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
667 {
668 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
669 $$ = $1;
670 $$->add_map($2, $4);
671 }
672|
673 {
674 pdebug("ConstMapContents =>");
675 $$ = new t_const_value();
676 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000677 }
678
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000679StructHead:
680 tok_struct
681 {
682 $$ = struct_is_struct;
683 }
684| tok_union
685 {
686 $$ = struct_is_union;
687 }
688
Mark Slee31985722006-05-24 21:45:31 +0000689Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000690 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000691 {
692 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000693 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000694 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000695 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000696 $$->set_name($2);
697 if ($7 != NULL) {
698 $$->annotations_ = $7->annotations_;
699 delete $7;
700 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000701 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000702
Mark Slee36bfa2e2007-01-19 20:09:51 +0000703XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000704 tok_xsd_all
705 {
706 $$ = true;
707 }
708|
709 {
710 $$ = false;
711 }
712
Mark Slee36bfa2e2007-01-19 20:09:51 +0000713XsdOptional:
714 tok_xsd_optional
715 {
716 $$ = true;
717 }
718|
719 {
720 $$ = false;
721 }
722
Mark Slee7df0e2a2007-02-06 21:03:18 +0000723XsdNillable:
724 tok_xsd_nillable
725 {
726 $$ = true;
727 }
728|
729 {
730 $$ = false;
731 }
732
Mark Slee21135c32007-02-05 21:52:08 +0000733XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000734 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000735 {
Mark Slee748d83f2007-02-07 01:20:08 +0000736 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000737 }
738|
739 {
740 $$ = NULL;
741 }
742
Mark Slee9cb7c612006-09-01 22:17:45 +0000743Xception:
744 tok_xception tok_identifier '{' FieldList '}'
745 {
746 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
747 $4->set_name($2);
748 $4->set_xception(true);
749 $$ = $4;
Mark Slee31985722006-05-24 21:45:31 +0000750 }
751
752Service:
Mark Slee78165722007-09-10 22:08:49 +0000753 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000754 {
755 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000756 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000757 $$->set_name($2);
758 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000759 }
760
Mark Slee78165722007-09-10 22:08:49 +0000761FlagArgs:
762 {
763 g_arglist = 1;
764 }
765
766UnflagArgs:
767 {
768 g_arglist = 0;
769 }
770
Mark Slee36bfa2e2007-01-19 20:09:51 +0000771Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000772 tok_extends tok_identifier
773 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000774 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000775 $$ = NULL;
776 if (g_parse_mode == PROGRAM) {
777 $$ = g_scope->get_service($2);
778 if ($$ == NULL) {
779 yyerror("Service \"%s\" has not been defined.", $2);
780 exit(1);
781 }
782 }
783 }
784|
785 {
786 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000787 }
788
789FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000790 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000791 {
792 pdebug("FunctionList -> FunctionList Function");
793 $$ = $1;
794 $1->add_function($2);
795 }
796|
797 {
798 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000800 }
801
802Function:
David Reiss6985a422009-03-24 20:00:47 +0000803 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000804 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000805 $6->set_name(std::string($4) + "_args");
806 $$ = new t_function($3, $4, $6, $8, $2);
807 if ($1 != NULL) {
808 $$->set_doc($1);
809 }
Mark Slee31985722006-05-24 21:45:31 +0000810 }
811
David Reiss6985a422009-03-24 20:00:47 +0000812Oneway:
813 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000814 {
Mark Slee52f643d2006-08-09 00:03:43 +0000815 $$ = true;
816 }
817|
818 {
819 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000820 }
821
Mark Slee36bfa2e2007-01-19 20:09:51 +0000822Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000823 tok_throws '(' FieldList ')'
824 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000825 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000826 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000827 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000828 yyerror("Throws clause may not contain non-exception types");
829 exit(1);
830 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000831 }
832|
833 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000835 }
836
Mark Slee31985722006-05-24 21:45:31 +0000837FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000838 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000839 {
840 pdebug("FieldList -> FieldList , Field");
841 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000842 if (!($$->append($2))) {
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000843 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
844 exit(1);
845 }
Mark Slee31985722006-05-24 21:45:31 +0000846 }
847|
848 {
849 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000850 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000851 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000852 }
853
854Field:
David Reiss53c10e02010-03-05 07:51:51 +0000855 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000856 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000857 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000858 if ($2 < 0) {
David Reissbb461362009-04-02 19:23:59 +0000859 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 +0000860 if (g_strict >= 192) {
861 yyerror("Implicit field keys are deprecated and not allowed with -strict");
862 exit(1);
863 }
Mark Slee31985722006-05-24 21:45:31 +0000864 }
David Reiss8320a922007-08-14 19:59:26 +0000865 $$ = new t_field($4, $5, $2);
866 $$->set_req($3);
867 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000868 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000869 validate_field_value($$, $6);
870 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000871 }
David Reiss8320a922007-08-14 19:59:26 +0000872 $$->set_xsd_optional($7);
873 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000874 if ($1 != NULL) {
875 $$->set_doc($1);
876 }
David Reiss8320a922007-08-14 19:59:26 +0000877 if ($9 != NULL) {
878 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000879 }
David Reiss53c10e02010-03-05 07:51:51 +0000880 if ($10 != NULL) {
881 $$->annotations_ = $10->annotations_;
882 delete $10;
883 }
Mark Slee31985722006-05-24 21:45:31 +0000884 }
Mark Slee7ff32452007-02-01 05:26:18 +0000885
886FieldIdentifier:
887 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000888 {
Mark Slee7ff32452007-02-01 05:26:18 +0000889 if ($1 <= 0) {
890 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
891 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000892 }
Mark Slee7ff32452007-02-01 05:26:18 +0000893 $$ = $1;
894 }
895|
896 {
897 $$ = y_field_val--;
898 }
899
David Reiss8320a922007-08-14 19:59:26 +0000900FieldRequiredness:
901 tok_required
902 {
David Reiss45603e92009-09-02 22:15:55 +0000903 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +0000904 }
905| tok_optional
906 {
Mark Slee78165722007-09-10 22:08:49 +0000907 if (g_arglist) {
908 if (g_parse_mode == PROGRAM) {
909 pwarning(1, "optional keyword is ignored in argument lists.\n");
910 }
David Reiss204420f2008-01-11 20:59:03 +0000911 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000912 } else {
David Reiss204420f2008-01-11 20:59:03 +0000913 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000914 }
David Reiss8320a922007-08-14 19:59:26 +0000915 }
916|
917 {
David Reiss204420f2008-01-11 20:59:03 +0000918 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000919 }
920
Mark Slee7ff32452007-02-01 05:26:18 +0000921FieldValue:
922 '=' ConstValue
923 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000924 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000925 $$ = $2;
926 } else {
927 $$ = NULL;
928 }
929 }
930|
931 {
932 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000933 }
Mark Slee31985722006-05-24 21:45:31 +0000934
Mark Slee31985722006-05-24 21:45:31 +0000935FunctionType:
936 FieldType
937 {
Mark Sleee8540632006-05-30 09:24:40 +0000938 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000939 $$ = $1;
940 }
941| tok_void
942 {
Mark Sleee8540632006-05-30 09:24:40 +0000943 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000944 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000945 }
946
947FieldType:
948 tok_identifier
949 {
Mark Sleee8540632006-05-30 09:24:40 +0000950 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000951 if (g_parse_mode == INCLUDES) {
952 // Ignore identifiers in include mode
953 $$ = NULL;
954 } else {
955 // Lookup the identifier in the current scope
956 $$ = g_scope->get_type($1);
957 if ($$ == NULL) {
958 yyerror("Type \"%s\" has not been defined.", $1);
959 exit(1);
960 }
Mark Sleee8540632006-05-30 09:24:40 +0000961 }
Mark Slee31985722006-05-24 21:45:31 +0000962 }
963| BaseType
964 {
Mark Sleee8540632006-05-30 09:24:40 +0000965 pdebug("FieldType -> BaseType");
966 $$ = $1;
967 }
968| ContainerType
969 {
970 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000971 $$ = $1;
972 }
973
David Reissc8e30052009-07-27 17:02:42 +0000974BaseType: SimpleBaseType TypeAnnotations
975 {
976 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
977 if ($2 != NULL) {
978 $$ = new t_base_type(*static_cast<t_base_type*>($1));
979 $$->annotations_ = $2->annotations_;
980 delete $2;
981 } else {
982 $$ = $1;
983 }
984 }
985
986SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +0000987 tok_string
988 {
989 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000990 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000991 }
Mark Slee8d725a22007-04-13 01:57:12 +0000992| tok_binary
993 {
994 pdebug("BaseType -> tok_binary");
995 $$ = g_type_binary;
996 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000997| tok_slist
998 {
999 pdebug("BaseType -> tok_slist");
1000 $$ = g_type_slist;
1001 }
Mark Slee78f58e22006-09-02 04:17:07 +00001002| tok_bool
1003 {
1004 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001005 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001006 }
Mark Slee31985722006-05-24 21:45:31 +00001007| tok_byte
1008 {
1009 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001010 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001011 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001012| tok_i16
1013 {
1014 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001015 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001016 }
Mark Slee31985722006-05-24 21:45:31 +00001017| tok_i32
1018 {
1019 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001020 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001021 }
Mark Slee31985722006-05-24 21:45:31 +00001022| tok_i64
1023 {
1024 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001025 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001026 }
Mark Sleec98d0502006-09-06 02:42:25 +00001027| tok_double
1028 {
1029 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001030 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001031 }
Mark Slee31985722006-05-24 21:45:31 +00001032
David Reissa2309992008-12-10 01:52:48 +00001033ContainerType: SimpleContainerType TypeAnnotations
1034 {
1035 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1036 $$ = $1;
1037 if ($2 != NULL) {
1038 $$->annotations_ = $2->annotations_;
1039 delete $2;
1040 }
1041 }
1042
1043SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001044 MapType
1045 {
David Reissa2309992008-12-10 01:52:48 +00001046 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001047 $$ = $1;
1048 }
1049| SetType
1050 {
David Reissa2309992008-12-10 01:52:48 +00001051 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001052 $$ = $1;
1053 }
1054| ListType
1055 {
David Reissa2309992008-12-10 01:52:48 +00001056 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001057 $$ = $1;
1058 }
1059
1060MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001061 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001062 {
1063 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001064 $$ = new t_map($4, $6);
1065 if ($2 != NULL) {
1066 ((t_container*)$$)->set_cpp_name(std::string($2));
1067 }
Mark Sleee8540632006-05-30 09:24:40 +00001068 }
1069
1070SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001071 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001072 {
1073 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001074 $$ = new t_set($4);
1075 if ($2 != NULL) {
1076 ((t_container*)$$)->set_cpp_name(std::string($2));
1077 }
Mark Sleee8540632006-05-30 09:24:40 +00001078 }
1079
1080ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001081 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001082 {
1083 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001084 $$ = new t_list($3);
1085 if ($5 != NULL) {
1086 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001087 }
1088 }
1089
Mark Slee36bfa2e2007-01-19 20:09:51 +00001090CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001091 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001092 {
Mark Sleeafc76542007-02-09 21:55:44 +00001093 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001094 }
1095|
1096 {
1097 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001098 }
1099
David Reissa2309992008-12-10 01:52:48 +00001100TypeAnnotations:
1101 '(' TypeAnnotationList ')'
1102 {
1103 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1104 $$ = $2;
1105 }
1106|
1107 {
1108 $$ = NULL;
1109 }
1110
1111TypeAnnotationList:
1112 TypeAnnotationList TypeAnnotation
1113 {
1114 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1115 $$ = $1;
1116 $$->annotations_[$2->key] = $2->val;
1117 delete $2;
1118 }
1119|
1120 {
1121 /* Just use a dummy structure to hold the annotations. */
1122 $$ = new t_struct(g_program);
1123 }
1124
1125TypeAnnotation:
1126 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1127 {
1128 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1129 $$ = new t_annotation;
1130 $$->key = $1;
1131 $$->val = $3;
1132 }
1133
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001134%%