blob: cc024a1aa188c05971c9e209f06a6f403a01c9f7 [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;
Bryan Duxburyc7206a42011-08-17 23:17:04 +000074 t_field_id tfieldid;
Mark Slee31985722006-05-24 21:45:31 +000075}
76
Mark Sleef5377b32006-10-10 01:42:59 +000077/**
78 * Strings identifier
79 */
Mark Slee31985722006-05-24 21:45:31 +000080%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000081%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000082%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000083%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000084
85/**
Mark Slee30152872006-11-28 01:24:07 +000086 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000087 */
Mark Slee31985722006-05-24 21:45:31 +000088%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000089%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000090
Mark Sleef5377b32006-10-10 01:42:59 +000091/**
David Reiss399442b2008-02-20 02:28:05 +000092 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +000093 */
Mark Sleef0712dc2006-10-25 19:03:57 +000094%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000095%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000096%token tok_cpp_namespace
97%token tok_cpp_include
98%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000099%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +0000100%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +0000101%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +0000102%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000103%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000104%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000105%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000106%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000107%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000108%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000109%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000110%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000111%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000112%token tok_csharp_namespace
Jake Farrell7ae13e12011-10-18 14:35:26 +0000113%token tok_delphi_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000114
Mark Sleef5377b32006-10-10 01:42:59 +0000115/**
116 * Base datatype keywords
117 */
118%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000119%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000120%token tok_byte
121%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000122%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000123%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000124%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000125%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000126%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000127%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000128%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000129
Mark Sleef5377b32006-10-10 01:42:59 +0000130/**
131 * Complex type keywords
132 */
Mark Slee31985722006-05-24 21:45:31 +0000133%token tok_map
134%token tok_list
135%token tok_set
136
Mark Sleef5377b32006-10-10 01:42:59 +0000137/**
138 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000139 */
David Reiss6985a422009-03-24 20:00:47 +0000140%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000141
Mark Sleef5377b32006-10-10 01:42:59 +0000142/**
143 * Thrift language keywords
144 */
Mark Slee31985722006-05-24 21:45:31 +0000145%token tok_typedef
146%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000147%token tok_xception
148%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000149%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000150%token tok_service
151%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000152%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000153%token tok_required
154%token tok_optional
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000155%token tok_union
Mark Slee31985722006-05-24 21:45:31 +0000156
Mark Sleef5377b32006-10-10 01:42:59 +0000157/**
158 * Grammar nodes
159 */
160
Mark Slee31985722006-05-24 21:45:31 +0000161%type<ttype> BaseType
David Reissc8e30052009-07-27 17:02:42 +0000162%type<ttype> SimpleBaseType
Mark Sleee8540632006-05-30 09:24:40 +0000163%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000164%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000165%type<ttype> MapType
166%type<ttype> SetType
167%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000168
David Reisscdffe262007-08-14 17:12:31 +0000169%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000170%type<ttype> TypeDefinition
171
Mark Slee31985722006-05-24 21:45:31 +0000172%type<ttypedef> Typedef
Mark Slee31985722006-05-24 21:45:31 +0000173
David Reissa2309992008-12-10 01:52:48 +0000174%type<ttype> TypeAnnotations
175%type<ttype> TypeAnnotationList
176%type<tannot> TypeAnnotation
177
Mark Slee31985722006-05-24 21:45:31 +0000178%type<tfield> Field
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000179%type<tfieldid> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000180%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000181%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000182%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000183%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000184
185%type<tenum> Enum
186%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000187%type<tenumv> EnumDef
188
Mark Slee6a47fed2007-02-07 02:40:59 +0000189%type<ttypedef> Senum
190%type<tbase> SenumDefList
191%type<id> SenumDef
192
Mark Slee30152872006-11-28 01:24:07 +0000193%type<tconst> Const
194%type<tconstv> ConstValue
195%type<tconstv> ConstList
196%type<tconstv> ConstListContents
197%type<tconstv> ConstMap
198%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000199
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000200%type<iconst> StructHead
Mark Slee31985722006-05-24 21:45:31 +0000201%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000202%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000203%type<tservice> Service
204
205%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000206%type<ttype> FunctionType
207%type<tservice> FunctionList
208
Mark Slee36bfa2e2007-01-19 20:09:51 +0000209%type<tstruct> Throws
210%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000211%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000212%type<tbool> XsdAll
213%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000214%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000215%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000216%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000217
David Reisscbd4bac2007-08-14 17:12:33 +0000218%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000219
Mark Slee31985722006-05-24 21:45:31 +0000220%%
221
Mark Sleef5377b32006-10-10 01:42:59 +0000222/**
223 * Thrift Grammar Implementation.
224 *
225 * For the most part this source file works its way top down from what you
226 * might expect to find in a typical .thrift file, i.e. type definitions and
227 * namespaces up top followed by service definitions using those types.
228 */
Mark Slee31985722006-05-24 21:45:31 +0000229
230Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000231 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 {
233 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000234 /*
235 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000236 if ($1 != NULL) {
237 g_program->set_doc($1);
238 }
David Reisscbd4bac2007-08-14 17:12:33 +0000239 */
240 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000241 }
242
David Reisscbd4bac2007-08-14 17:12:33 +0000243CaptureDocText:
244 {
245 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000246 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000247 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000248 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000249 $$ = NULL;
250 }
251 }
252
253/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
254DestroyDocText:
255 {
256 if (g_parse_mode == PROGRAM) {
257 clear_doctext();
258 }
259 }
260
261/* We have to DestroyDocText here, otherwise it catches the doctext
262 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000263HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000264 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000265 {
266 pdebug("HeaderList -> HeaderList Header");
267 }
268|
269 {
270 pdebug("HeaderList -> ");
271 }
272
273Header:
274 Include
275 {
276 pdebug("Header -> Include");
277 }
David Reiss79eca142008-02-27 01:55:13 +0000278| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000279 {
David Reiss79eca142008-02-27 01:55:13 +0000280 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000281 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000282 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 }
284 }
David Reissfb790d72010-09-02 16:41:45 +0000285| tok_namespace '*' tok_identifier
286 {
287 pdebug("Header -> tok_namespace * tok_identifier");
288 if (g_parse_mode == PROGRAM) {
289 g_program->set_namespace("*", $3);
290 }
291 }
David Reiss9a08dc62008-02-27 01:55:17 +0000292/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000293| tok_cpp_namespace tok_identifier
294 {
David Reiss9a08dc62008-02-27 01:55:17 +0000295 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000296 pdebug("Header -> tok_cpp_namespace tok_identifier");
297 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000298 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000299 }
300 }
301| tok_cpp_include tok_literal
302 {
303 pdebug("Header -> tok_cpp_include tok_literal");
304 if (g_parse_mode == PROGRAM) {
305 g_program->add_cpp_include($2);
306 }
307 }
Mark Sleee888b372007-01-12 01:06:24 +0000308| tok_php_namespace tok_identifier
309 {
David Reiss554ea6f2009-02-17 20:28:37 +0000310 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000311 pdebug("Header -> tok_php_namespace tok_identifier");
312 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000313 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000314 }
315 }
David Reiss320e45c2008-03-27 21:41:54 +0000316/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000317| tok_py_module tok_identifier
318 {
David Reiss320e45c2008-03-27 21:41:54 +0000319 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000320 pdebug("Header -> tok_py_module tok_identifier");
321 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000322 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000323 }
324 }
David Reiss07ef3a92008-03-27 21:42:39 +0000325/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000326| tok_perl_package tok_identifier
327 {
David Reiss07ef3a92008-03-27 21:42:39 +0000328 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000329 pdebug("Header -> tok_perl_namespace tok_identifier");
330 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000331 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000332 }
333 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000334/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000335| tok_ruby_namespace tok_identifier
336 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000337 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000338 pdebug("Header -> tok_ruby_namespace tok_identifier");
339 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000340 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000341 }
342 }
David Reiss3b455012008-03-27 21:40:46 +0000343/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000344| tok_smalltalk_category tok_st_identifier
345 {
David Reiss3b455012008-03-27 21:40:46 +0000346 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000347 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
348 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000349 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000350 }
351 }
David Reiss3b455012008-03-27 21:40:46 +0000352/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000353| tok_smalltalk_prefix tok_identifier
354 {
David Reiss3b455012008-03-27 21:40:46 +0000355 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000356 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
357 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000358 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000359 }
360 }
David Reiss771f8c72008-02-27 01:55:25 +0000361/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000362| tok_java_package tok_identifier
363 {
David Reiss9f646152008-03-02 21:59:48 +0000364 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 pdebug("Header -> tok_java_package tok_identifier");
366 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000367 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000368 }
369 }
David Reiss54b602b2008-03-27 21:41:06 +0000370/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000371| tok_cocoa_prefix tok_identifier
372 {
David Reiss54b602b2008-03-27 21:41:06 +0000373 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000374 pdebug("Header -> tok_cocoa_prefix tok_identifier");
375 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000376 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000377 }
378 }
David Reiss92e10d82009-02-17 20:28:19 +0000379/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000380| tok_xsd_namespace tok_literal
381 {
David Reiss92e10d82009-02-17 20:28:19 +0000382 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000383 pdebug("Header -> tok_xsd_namespace tok_literal");
384 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000385 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000386 }
387 }
David Reiss9d65bf02008-03-27 21:41:37 +0000388/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000389| tok_csharp_namespace tok_identifier
390 {
David Reiss9d65bf02008-03-27 21:41:37 +0000391 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000392 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000393 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000394 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000395 }
396 }
Jake Farrell7ae13e12011-10-18 14:35:26 +0000397/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
398| tok_delphi_namespace tok_identifier
399 {
400 pwarning(1, "'delphi_namespace' is deprecated. Use 'namespace delphi' instead");
401 pdebug("Header -> tok_delphi_namespace tok_identifier");
402 if (g_parse_mode == PROGRAM) {
403 g_program->set_namespace("delphi", $2);
404 }
405 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000406
407Include:
408 tok_include tok_literal
409 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000410 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000411 if (g_parse_mode == INCLUDES) {
412 std::string path = include_file(std::string($2));
413 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000414 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000415 }
416 }
417 }
Mark Slee31985722006-05-24 21:45:31 +0000418
419DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000420 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000421 {
422 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000423 if ($2 != NULL && $3 != NULL) {
424 $3->set_doc($2);
425 }
Mark Slee31985722006-05-24 21:45:31 +0000426 }
427|
428 {
429 pdebug("DefinitionList -> ");
430 }
431
432Definition:
Mark Slee30152872006-11-28 01:24:07 +0000433 Const
434 {
435 pdebug("Definition -> Const");
436 if (g_parse_mode == PROGRAM) {
437 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000438 }
David Reisscdffe262007-08-14 17:12:31 +0000439 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000440 }
441| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000442 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000443 pdebug("Definition -> TypeDefinition");
444 if (g_parse_mode == PROGRAM) {
445 g_scope->add_type($1->get_name(), $1);
446 if (g_parent_scope != NULL) {
447 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
448 }
449 }
David Reisscdffe262007-08-14 17:12:31 +0000450 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000451 }
Mark Slee31985722006-05-24 21:45:31 +0000452| Service
453 {
454 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000455 if (g_parse_mode == PROGRAM) {
456 g_scope->add_service($1->get_name(), $1);
457 if (g_parent_scope != NULL) {
458 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
459 }
460 g_program->add_service($1);
461 }
David Reisscdffe262007-08-14 17:12:31 +0000462 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000463 }
464
Mark Sleef0712dc2006-10-25 19:03:57 +0000465TypeDefinition:
466 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000467 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000468 pdebug("TypeDefinition -> Typedef");
469 if (g_parse_mode == PROGRAM) {
470 g_program->add_typedef($1);
471 }
472 }
473| Enum
474 {
475 pdebug("TypeDefinition -> Enum");
476 if (g_parse_mode == PROGRAM) {
477 g_program->add_enum($1);
478 }
479 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000480| Senum
481 {
482 pdebug("TypeDefinition -> Senum");
483 if (g_parse_mode == PROGRAM) {
484 g_program->add_typedef($1);
485 }
486 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000487| Struct
488 {
489 pdebug("TypeDefinition -> Struct");
490 if (g_parse_mode == PROGRAM) {
491 g_program->add_struct($1);
492 }
493 }
494| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000495 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000496 pdebug("TypeDefinition -> Xception");
497 if (g_parse_mode == PROGRAM) {
498 g_program->add_xception($1);
499 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000500 }
Mark Slee31985722006-05-24 21:45:31 +0000501
502Typedef:
David Reiss4dd78012010-03-09 05:19:08 +0000503 tok_typedef FieldType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000504 {
David Reiss4dd78012010-03-09 05:19:08 +0000505 pdebug("TypeDef -> tok_typedef FieldType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000506 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000507 $$ = td;
508 }
509
Mark Slee6a47fed2007-02-07 02:40:59 +0000510CommaOrSemicolonOptional:
511 ','
512 {}
513| ';'
514 {}
515|
516 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000517
Mark Slee31985722006-05-24 21:45:31 +0000518Enum:
David Reisscdffe262007-08-14 17:12:31 +0000519 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000520 {
521 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000522 $$ = $4;
523 $$->set_name($2);
Bryan Duxbury2d804702009-12-18 19:41:11 +0000524 $$->resolve_values();
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000525 // make constants for all the enum values
526 if (g_parse_mode == PROGRAM) {
527 const std::vector<t_enum_value*>& enum_values = $$->get_constants();
528 std::vector<t_enum_value*>::const_iterator c_iter;
529 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
530 std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
531 t_const_value* const_val = new t_const_value((*c_iter)->get_value());
532 const_val->set_enum($$);
533 g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
534 if (g_parent_scope != NULL) {
535 g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
536 }
537 }
538 }
Mark Slee31985722006-05-24 21:45:31 +0000539 }
540
541EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000542 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000543 {
544 pdebug("EnumDefList -> EnumDefList EnumDef");
545 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000546 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000547 }
548|
549 {
550 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000551 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000552 }
553
554EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000555 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000556 {
Mark Slee30152872006-11-28 01:24:07 +0000557 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000558 if ($4 < 0) {
559 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000560 }
David Reissf1454162008-06-30 20:45:47 +0000561 if ($4 > INT_MAX) {
562 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
563 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000564 $$ = new t_enum_value($2, $4);
565 if ($1 != NULL) {
566 $$->set_doc($1);
567 }
Mark Slee31985722006-05-24 21:45:31 +0000568 }
569|
David Reisscbd4bac2007-08-14 17:12:33 +0000570 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000571 {
Mark Slee30152872006-11-28 01:24:07 +0000572 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000573 $$ = new t_enum_value($2);
574 if ($1 != NULL) {
575 $$->set_doc($1);
576 }
Mark Slee30152872006-11-28 01:24:07 +0000577 }
578
Mark Slee6a47fed2007-02-07 02:40:59 +0000579Senum:
David Reisscdffe262007-08-14 17:12:31 +0000580 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000581 {
582 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000583 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000584 }
585
586SenumDefList:
587 SenumDefList SenumDef
588 {
589 pdebug("SenumDefList -> SenumDefList SenumDef");
590 $$ = $1;
591 $$->add_string_enum_val($2);
592 }
593|
594 {
595 pdebug("SenumDefList -> ");
596 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
597 $$->set_string_enum(true);
598 }
599
600SenumDef:
601 tok_literal CommaOrSemicolonOptional
602 {
603 pdebug("SenumDef -> tok_literal");
604 $$ = $1;
605 }
606
Mark Slee30152872006-11-28 01:24:07 +0000607Const:
608 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
609 {
610 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000611 if (g_parse_mode == PROGRAM) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000612 g_scope->resolve_const_value($5, $2);
Mark Sleeaa7671d2006-11-29 03:19:31 +0000613 $$ = new t_const($2, $3, $5);
614 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000615
616 g_scope->add_constant($3, $$);
617 if (g_parent_scope != NULL) {
618 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
619 }
Mark Sleeaa7671d2006-11-29 03:19:31 +0000620 } else {
621 $$ = NULL;
622 }
Mark Slee30152872006-11-28 01:24:07 +0000623 }
624
625ConstValue:
626 tok_int_constant
627 {
628 pdebug("ConstValue => tok_int_constant");
629 $$ = new t_const_value();
630 $$->set_integer($1);
Roger Meier887ff752011-08-19 11:25:39 +0000631 if (!g_allow_64bit_consts && ($1 < INT32_MIN || $1 > INT32_MAX)) {
David Reissf1454162008-06-30 20:45:47 +0000632 pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
633 }
Mark Slee30152872006-11-28 01:24:07 +0000634 }
635| tok_dub_constant
636 {
637 pdebug("ConstValue => tok_dub_constant");
638 $$ = new t_const_value();
639 $$->set_double($1);
640 }
641| tok_literal
642 {
643 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000644 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000645 }
Mark Slee67fc6342006-11-29 03:37:04 +0000646| tok_identifier
647 {
648 pdebug("ConstValue => tok_identifier");
Bryan Duxbury2d804702009-12-18 19:41:11 +0000649 $$ = new t_const_value();
650 $$->set_identifier($1);
Mark Slee67fc6342006-11-29 03:37:04 +0000651 }
Mark Slee30152872006-11-28 01:24:07 +0000652| ConstList
653 {
654 pdebug("ConstValue => ConstList");
655 $$ = $1;
656 }
657| ConstMap
658 {
659 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000660 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000661 }
662
663ConstList:
664 '[' ConstListContents ']'
665 {
666 pdebug("ConstList => [ ConstListContents ]");
667 $$ = $2;
668 }
669
670ConstListContents:
671 ConstListContents ConstValue CommaOrSemicolonOptional
672 {
673 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
674 $$ = $1;
675 $$->add_list($2);
676 }
677|
678 {
679 pdebug("ConstListContents =>");
680 $$ = new t_const_value();
681 $$->set_list();
682 }
683
684ConstMap:
685 '{' ConstMapContents '}'
686 {
687 pdebug("ConstMap => { ConstMapContents }");
688 $$ = $2;
689 }
690
691ConstMapContents:
692 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
693 {
694 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
695 $$ = $1;
696 $$->add_map($2, $4);
697 }
698|
699 {
700 pdebug("ConstMapContents =>");
701 $$ = new t_const_value();
702 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000703 }
704
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000705StructHead:
706 tok_struct
707 {
708 $$ = struct_is_struct;
709 }
710| tok_union
711 {
712 $$ = struct_is_union;
713 }
714
Mark Slee31985722006-05-24 21:45:31 +0000715Struct:
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000716 StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000717 {
718 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000719 $5->set_xsd_all($3);
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000720 $5->set_union($1 == struct_is_union);
David Reisscdffe262007-08-14 17:12:31 +0000721 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000722 $$->set_name($2);
723 if ($7 != NULL) {
724 $$->annotations_ = $7->annotations_;
725 delete $7;
726 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000727 }
Bryan Duxburyab3666e2009-09-01 23:03:47 +0000728
Mark Slee36bfa2e2007-01-19 20:09:51 +0000729XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000730 tok_xsd_all
731 {
732 $$ = true;
733 }
734|
735 {
736 $$ = false;
737 }
738
Mark Slee36bfa2e2007-01-19 20:09:51 +0000739XsdOptional:
740 tok_xsd_optional
741 {
742 $$ = true;
743 }
744|
745 {
746 $$ = false;
747 }
748
Mark Slee7df0e2a2007-02-06 21:03:18 +0000749XsdNillable:
750 tok_xsd_nillable
751 {
752 $$ = true;
753 }
754|
755 {
756 $$ = false;
757 }
758
Mark Slee21135c32007-02-05 21:52:08 +0000759XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000760 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000761 {
Mark Slee748d83f2007-02-07 01:20:08 +0000762 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000763 }
764|
765 {
766 $$ = NULL;
767 }
768
Mark Slee9cb7c612006-09-01 22:17:45 +0000769Xception:
770 tok_xception tok_identifier '{' FieldList '}'
771 {
772 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
773 $4->set_name($2);
774 $4->set_xception(true);
775 $$ = $4;
Mark Slee31985722006-05-24 21:45:31 +0000776 }
777
778Service:
Mark Slee78165722007-09-10 22:08:49 +0000779 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000780 {
781 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000782 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000783 $$->set_name($2);
784 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000785 }
786
Mark Slee78165722007-09-10 22:08:49 +0000787FlagArgs:
788 {
789 g_arglist = 1;
790 }
791
792UnflagArgs:
793 {
794 g_arglist = 0;
795 }
796
Mark Slee36bfa2e2007-01-19 20:09:51 +0000797Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000798 tok_extends tok_identifier
799 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000800 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 $$ = NULL;
802 if (g_parse_mode == PROGRAM) {
803 $$ = g_scope->get_service($2);
804 if ($$ == NULL) {
805 yyerror("Service \"%s\" has not been defined.", $2);
806 exit(1);
807 }
808 }
809 }
810|
811 {
812 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000813 }
814
815FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000816 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000817 {
818 pdebug("FunctionList -> FunctionList Function");
819 $$ = $1;
820 $1->add_function($2);
821 }
822|
823 {
824 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000825 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000826 }
827
828Function:
David Reiss6985a422009-03-24 20:00:47 +0000829 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000830 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000831 $6->set_name(std::string($4) + "_args");
832 $$ = new t_function($3, $4, $6, $8, $2);
833 if ($1 != NULL) {
834 $$->set_doc($1);
835 }
Mark Slee31985722006-05-24 21:45:31 +0000836 }
837
David Reiss6985a422009-03-24 20:00:47 +0000838Oneway:
839 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000840 {
Mark Slee52f643d2006-08-09 00:03:43 +0000841 $$ = true;
842 }
843|
844 {
845 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000846 }
847
Mark Slee36bfa2e2007-01-19 20:09:51 +0000848Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000849 tok_throws '(' FieldList ')'
850 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000851 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000852 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000853 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000854 yyerror("Throws clause may not contain non-exception types");
855 exit(1);
856 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000857 }
858|
859 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000860 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000861 }
862
Mark Slee31985722006-05-24 21:45:31 +0000863FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000864 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000865 {
866 pdebug("FieldList -> FieldList , Field");
867 $$ = $1;
Bryan Duxburyff219ac2009-04-10 21:51:00 +0000868 if (!($$->append($2))) {
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000869 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
870 exit(1);
871 }
Mark Slee31985722006-05-24 21:45:31 +0000872 }
873|
874 {
875 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000876 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000877 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000878 }
879
880Field:
David Reiss53c10e02010-03-05 07:51:51 +0000881 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes TypeAnnotations CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000882 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000883 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000884 if ($2.auto_assigned) {
David Reissbb461362009-04-02 19:23:59 +0000885 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 +0000886 if (g_strict >= 192) {
887 yyerror("Implicit field keys are deprecated and not allowed with -strict");
888 exit(1);
889 }
Mark Slee31985722006-05-24 21:45:31 +0000890 }
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000891 $$ = new t_field($4, $5, $2.value);
David Reiss8320a922007-08-14 19:59:26 +0000892 $$->set_req($3);
893 if ($6 != NULL) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000894 g_scope->resolve_const_value($6, $4);
David Reiss8320a922007-08-14 19:59:26 +0000895 validate_field_value($$, $6);
896 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000897 }
David Reiss8320a922007-08-14 19:59:26 +0000898 $$->set_xsd_optional($7);
899 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000900 if ($1 != NULL) {
901 $$->set_doc($1);
902 }
David Reiss8320a922007-08-14 19:59:26 +0000903 if ($9 != NULL) {
904 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000905 }
David Reiss53c10e02010-03-05 07:51:51 +0000906 if ($10 != NULL) {
907 $$->annotations_ = $10->annotations_;
908 delete $10;
909 }
Mark Slee31985722006-05-24 21:45:31 +0000910 }
Mark Slee7ff32452007-02-01 05:26:18 +0000911
912FieldIdentifier:
913 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000914 {
Mark Slee7ff32452007-02-01 05:26:18 +0000915 if ($1 <= 0) {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000916 if (g_allow_neg_field_keys) {
917 /*
918 * g_allow_neg_field_keys exists to allow users to add explicitly
919 * specified key values to old .thrift files without breaking
920 * protocol compatibility.
921 */
922 if ($1 != y_field_val) {
923 /*
924 * warn if the user-specified negative value isn't what
925 * thrift would have auto-assigned.
926 */
927 pwarning(1, "Negative field key (%d) differs from what would be "
928 "auto-assigned by thrift (%d).\n", $1, y_field_val);
929 }
930 /*
931 * Leave $1 as-is, and update y_field_val to be one less than $1.
932 * The FieldList parsing will catch any duplicate key values.
933 */
934 y_field_val = $1 - 1;
935 $$.value = $1;
936 $$.auto_assigned = false;
937 } else {
938 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n",
939 $1);
940 $$.value = y_field_val--;
941 $$.auto_assigned = true;
942 }
943 } else {
944 $$.value = $1;
945 $$.auto_assigned = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000946 }
Mark Slee7ff32452007-02-01 05:26:18 +0000947 }
948|
949 {
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000950 $$.value = y_field_val--;
951 $$.auto_assigned = true;
Mark Slee7ff32452007-02-01 05:26:18 +0000952 }
953
David Reiss8320a922007-08-14 19:59:26 +0000954FieldRequiredness:
955 tok_required
956 {
David Reiss45603e92009-09-02 22:15:55 +0000957 $$ = t_field::T_REQUIRED;
David Reiss8320a922007-08-14 19:59:26 +0000958 }
959| tok_optional
960 {
Mark Slee78165722007-09-10 22:08:49 +0000961 if (g_arglist) {
962 if (g_parse_mode == PROGRAM) {
963 pwarning(1, "optional keyword is ignored in argument lists.\n");
964 }
David Reiss204420f2008-01-11 20:59:03 +0000965 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000966 } else {
David Reiss204420f2008-01-11 20:59:03 +0000967 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000968 }
David Reiss8320a922007-08-14 19:59:26 +0000969 }
970|
971 {
David Reiss204420f2008-01-11 20:59:03 +0000972 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000973 }
974
Mark Slee7ff32452007-02-01 05:26:18 +0000975FieldValue:
976 '=' ConstValue
977 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000978 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000979 $$ = $2;
980 } else {
981 $$ = NULL;
982 }
983 }
984|
985 {
986 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 }
Mark Slee31985722006-05-24 21:45:31 +0000988
Mark Slee31985722006-05-24 21:45:31 +0000989FunctionType:
990 FieldType
991 {
Mark Sleee8540632006-05-30 09:24:40 +0000992 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000993 $$ = $1;
994 }
995| tok_void
996 {
Mark Sleee8540632006-05-30 09:24:40 +0000997 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000998 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000999 }
1000
1001FieldType:
1002 tok_identifier
1003 {
Mark Sleee8540632006-05-30 09:24:40 +00001004 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +00001005 if (g_parse_mode == INCLUDES) {
1006 // Ignore identifiers in include mode
1007 $$ = NULL;
1008 } else {
1009 // Lookup the identifier in the current scope
1010 $$ = g_scope->get_type($1);
1011 if ($$ == NULL) {
1012 yyerror("Type \"%s\" has not been defined.", $1);
1013 exit(1);
1014 }
Mark Sleee8540632006-05-30 09:24:40 +00001015 }
Mark Slee31985722006-05-24 21:45:31 +00001016 }
1017| BaseType
1018 {
Mark Sleee8540632006-05-30 09:24:40 +00001019 pdebug("FieldType -> BaseType");
1020 $$ = $1;
1021 }
1022| ContainerType
1023 {
1024 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +00001025 $$ = $1;
1026 }
1027
David Reissc8e30052009-07-27 17:02:42 +00001028BaseType: SimpleBaseType TypeAnnotations
1029 {
1030 pdebug("BaseType -> SimpleBaseType TypeAnnotations");
1031 if ($2 != NULL) {
1032 $$ = new t_base_type(*static_cast<t_base_type*>($1));
1033 $$->annotations_ = $2->annotations_;
1034 delete $2;
1035 } else {
1036 $$ = $1;
1037 }
1038 }
1039
1040SimpleBaseType:
Mark Slee31985722006-05-24 21:45:31 +00001041 tok_string
1042 {
1043 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +00001044 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +00001045 }
Mark Slee8d725a22007-04-13 01:57:12 +00001046| tok_binary
1047 {
1048 pdebug("BaseType -> tok_binary");
1049 $$ = g_type_binary;
1050 }
Mark Sleeb6200d82007-01-19 19:14:36 +00001051| tok_slist
1052 {
1053 pdebug("BaseType -> tok_slist");
1054 $$ = g_type_slist;
1055 }
Mark Slee78f58e22006-09-02 04:17:07 +00001056| tok_bool
1057 {
1058 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +00001059 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001060 }
Mark Slee31985722006-05-24 21:45:31 +00001061| tok_byte
1062 {
1063 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001064 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001065 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001066| tok_i16
1067 {
1068 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001069 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001070 }
Mark Slee31985722006-05-24 21:45:31 +00001071| tok_i32
1072 {
1073 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001074 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001075 }
Mark Slee31985722006-05-24 21:45:31 +00001076| tok_i64
1077 {
1078 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001079 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001080 }
Mark Sleec98d0502006-09-06 02:42:25 +00001081| tok_double
1082 {
1083 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001084 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001085 }
Mark Slee31985722006-05-24 21:45:31 +00001086
David Reissa2309992008-12-10 01:52:48 +00001087ContainerType: SimpleContainerType TypeAnnotations
1088 {
1089 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1090 $$ = $1;
1091 if ($2 != NULL) {
1092 $$->annotations_ = $2->annotations_;
1093 delete $2;
1094 }
1095 }
1096
1097SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001098 MapType
1099 {
David Reissa2309992008-12-10 01:52:48 +00001100 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001101 $$ = $1;
1102 }
1103| SetType
1104 {
David Reissa2309992008-12-10 01:52:48 +00001105 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001106 $$ = $1;
1107 }
1108| ListType
1109 {
David Reissa2309992008-12-10 01:52:48 +00001110 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001111 $$ = $1;
1112 }
1113
1114MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001115 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001116 {
1117 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001118 $$ = new t_map($4, $6);
1119 if ($2 != NULL) {
1120 ((t_container*)$$)->set_cpp_name(std::string($2));
1121 }
Mark Sleee8540632006-05-30 09:24:40 +00001122 }
1123
1124SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001125 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001126 {
1127 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001128 $$ = new t_set($4);
1129 if ($2 != NULL) {
1130 ((t_container*)$$)->set_cpp_name(std::string($2));
1131 }
Mark Sleee8540632006-05-30 09:24:40 +00001132 }
1133
1134ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001135 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001136 {
1137 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001138 $$ = new t_list($3);
1139 if ($5 != NULL) {
1140 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001141 }
1142 }
1143
Mark Slee36bfa2e2007-01-19 20:09:51 +00001144CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001145 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001146 {
Mark Sleeafc76542007-02-09 21:55:44 +00001147 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001148 }
1149|
1150 {
1151 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001152 }
1153
David Reissa2309992008-12-10 01:52:48 +00001154TypeAnnotations:
1155 '(' TypeAnnotationList ')'
1156 {
1157 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1158 $$ = $2;
1159 }
1160|
1161 {
1162 $$ = NULL;
1163 }
1164
1165TypeAnnotationList:
1166 TypeAnnotationList TypeAnnotation
1167 {
1168 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1169 $$ = $1;
1170 $$->annotations_[$2->key] = $2->val;
1171 delete $2;
1172 }
1173|
1174 {
1175 /* Just use a dummy structure to hold the annotations. */
1176 $$ = new t_struct(g_program);
1177 }
1178
1179TypeAnnotation:
1180 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1181 {
1182 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1183 $$ = new t_annotation;
1184 $$->key = $1;
1185 $$->val = $3;
1186 }
1187
Todd Lipcon53ae9f32009-12-07 00:42:38 +00001188%%