blob: 2c16ca018c50e6787f192b2ff0eb16ca70f28c2b [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;
Mark Slee31985722006-05-24 21:45:31 +000045
46%}
47
Mark Sleef5377b32006-10-10 01:42:59 +000048/**
49 * This structure is used by the parser to hold the data types associated with
50 * various parse nodes.
51 */
Mark Slee31985722006-05-24 21:45:31 +000052%union {
Mark Slee30152872006-11-28 01:24:07 +000053 char* id;
David Reissf1454162008-06-30 20:45:47 +000054 int64_t iconst;
Mark Slee30152872006-11-28 01:24:07 +000055 double dconst;
56 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000057 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000058 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000059 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000060 t_typedef* ttypedef;
61 t_enum* tenum;
62 t_enum_value* tenumv;
63 t_const* tconst;
64 t_const_value* tconstv;
65 t_struct* tstruct;
66 t_service* tservice;
67 t_function* tfunction;
68 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000069 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000070 t_field::e_req ereq;
David Reissa2309992008-12-10 01:52:48 +000071 t_annotation* tannot;
Mark Slee31985722006-05-24 21:45:31 +000072}
73
Mark Sleef5377b32006-10-10 01:42:59 +000074/**
75 * Strings identifier
76 */
Mark Slee31985722006-05-24 21:45:31 +000077%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000078%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000079%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000080%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000081
82/**
Mark Slee30152872006-11-28 01:24:07 +000083 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000084 */
Mark Slee31985722006-05-24 21:45:31 +000085%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000086%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000087
Mark Sleef5377b32006-10-10 01:42:59 +000088/**
David Reiss399442b2008-02-20 02:28:05 +000089 * Header keywords
Mark Sleef5377b32006-10-10 01:42:59 +000090 */
Mark Sleef0712dc2006-10-25 19:03:57 +000091%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000092%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000093%token tok_cpp_namespace
94%token tok_cpp_include
95%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000096%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000097%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +000098%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000099%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +0000100%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +0000101%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000102%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +0000103%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +0000104%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +0000105%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +0000106%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +0000107%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +0000108%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +0000109%token tok_csharp_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +0000110
Mark Sleef5377b32006-10-10 01:42:59 +0000111/**
112 * Base datatype keywords
113 */
114%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +0000115%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +0000116%token tok_byte
117%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000118%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000119%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000120%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000121%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000122%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000123%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000124%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000125
Mark Sleef5377b32006-10-10 01:42:59 +0000126/**
127 * Complex type keywords
128 */
Mark Slee31985722006-05-24 21:45:31 +0000129%token tok_map
130%token tok_list
131%token tok_set
132
Mark Sleef5377b32006-10-10 01:42:59 +0000133/**
134 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000135 */
David Reiss6985a422009-03-24 20:00:47 +0000136%token tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000137
Mark Sleef5377b32006-10-10 01:42:59 +0000138/**
139 * Thrift language keywords
140 */
Mark Slee31985722006-05-24 21:45:31 +0000141%token tok_typedef
142%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000143%token tok_xception
144%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000145%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000146%token tok_service
147%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000148%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000149%token tok_required
150%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000151
Mark Sleef5377b32006-10-10 01:42:59 +0000152/**
153 * Grammar nodes
154 */
155
Mark Slee31985722006-05-24 21:45:31 +0000156%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000157%type<ttype> ContainerType
David Reissa2309992008-12-10 01:52:48 +0000158%type<ttype> SimpleContainerType
Mark Sleee8540632006-05-30 09:24:40 +0000159%type<ttype> MapType
160%type<ttype> SetType
161%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000162
David Reisscdffe262007-08-14 17:12:31 +0000163%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000164%type<ttype> TypeDefinition
165
Mark Slee31985722006-05-24 21:45:31 +0000166%type<ttypedef> Typedef
167%type<ttype> DefinitionType
168
David Reissa2309992008-12-10 01:52:48 +0000169%type<ttype> TypeAnnotations
170%type<ttype> TypeAnnotationList
171%type<tannot> TypeAnnotation
172
Mark Slee31985722006-05-24 21:45:31 +0000173%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000174%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000175%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000176%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000177%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000178%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000179
180%type<tenum> Enum
181%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000182%type<tenumv> EnumDef
183
Mark Slee6a47fed2007-02-07 02:40:59 +0000184%type<ttypedef> Senum
185%type<tbase> SenumDefList
186%type<id> SenumDef
187
Mark Slee30152872006-11-28 01:24:07 +0000188%type<tconst> Const
189%type<tconstv> ConstValue
190%type<tconstv> ConstList
191%type<tconstv> ConstListContents
192%type<tconstv> ConstMap
193%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000194
195%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000196%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000197%type<tservice> Service
198
199%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000200%type<ttype> FunctionType
201%type<tservice> FunctionList
202
Mark Slee36bfa2e2007-01-19 20:09:51 +0000203%type<tstruct> Throws
204%type<tservice> Extends
David Reiss6985a422009-03-24 20:00:47 +0000205%type<tbool> Oneway
Mark Slee36bfa2e2007-01-19 20:09:51 +0000206%type<tbool> XsdAll
207%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000208%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000209%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000210%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000211
David Reisscbd4bac2007-08-14 17:12:33 +0000212%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000213
Mark Slee31985722006-05-24 21:45:31 +0000214%%
215
Mark Sleef5377b32006-10-10 01:42:59 +0000216/**
217 * Thrift Grammar Implementation.
218 *
219 * For the most part this source file works its way top down from what you
220 * might expect to find in a typical .thrift file, i.e. type definitions and
221 * namespaces up top followed by service definitions using those types.
222 */
Mark Slee31985722006-05-24 21:45:31 +0000223
224Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000225 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000226 {
227 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000228 /*
229 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000230 if ($1 != NULL) {
231 g_program->set_doc($1);
232 }
David Reisscbd4bac2007-08-14 17:12:33 +0000233 */
234 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000235 }
236
David Reisscbd4bac2007-08-14 17:12:33 +0000237CaptureDocText:
238 {
239 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000240 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000241 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000242 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000243 $$ = NULL;
244 }
245 }
246
247/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
248DestroyDocText:
249 {
250 if (g_parse_mode == PROGRAM) {
251 clear_doctext();
252 }
253 }
254
255/* We have to DestroyDocText here, otherwise it catches the doctext
256 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000257HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000258 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000259 {
260 pdebug("HeaderList -> HeaderList Header");
261 }
262|
263 {
264 pdebug("HeaderList -> ");
265 }
266
267Header:
268 Include
269 {
270 pdebug("Header -> Include");
271 }
David Reiss79eca142008-02-27 01:55:13 +0000272| tok_namespace tok_identifier tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +0000273 {
David Reiss79eca142008-02-27 01:55:13 +0000274 pdebug("Header -> tok_namespace tok_identifier tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 if (g_parse_mode == PROGRAM) {
David Reiss79eca142008-02-27 01:55:13 +0000276 g_program->set_namespace($2, $3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000277 }
278 }
David Reiss9a08dc62008-02-27 01:55:17 +0000279/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000280| tok_cpp_namespace tok_identifier
281 {
David Reiss9a08dc62008-02-27 01:55:17 +0000282 pwarning(1, "'cpp_namespace' is deprecated. Use 'namespace cpp' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 pdebug("Header -> tok_cpp_namespace tok_identifier");
284 if (g_parse_mode == PROGRAM) {
David Reiss9a08dc62008-02-27 01:55:17 +0000285 g_program->set_namespace("cpp", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000286 }
287 }
288| tok_cpp_include tok_literal
289 {
290 pdebug("Header -> tok_cpp_include tok_literal");
291 if (g_parse_mode == PROGRAM) {
292 g_program->add_cpp_include($2);
293 }
294 }
Mark Sleee888b372007-01-12 01:06:24 +0000295| tok_php_namespace tok_identifier
296 {
David Reiss554ea6f2009-02-17 20:28:37 +0000297 pwarning(1, "'php_namespace' is deprecated. Use 'namespace php' instead");
Mark Sleee888b372007-01-12 01:06:24 +0000298 pdebug("Header -> tok_php_namespace tok_identifier");
299 if (g_parse_mode == PROGRAM) {
David Reiss554ea6f2009-02-17 20:28:37 +0000300 g_program->set_namespace("php", $2);
Mark Sleee888b372007-01-12 01:06:24 +0000301 }
302 }
David Reiss320e45c2008-03-27 21:41:54 +0000303/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reissc6fc3292007-08-30 00:58:43 +0000304| tok_py_module tok_identifier
305 {
David Reiss320e45c2008-03-27 21:41:54 +0000306 pwarning(1, "'py_module' is deprecated. Use 'namespace py' instead");
David Reissc6fc3292007-08-30 00:58:43 +0000307 pdebug("Header -> tok_py_module tok_identifier");
308 if (g_parse_mode == PROGRAM) {
David Reiss320e45c2008-03-27 21:41:54 +0000309 g_program->set_namespace("py", $2);
David Reissc6fc3292007-08-30 00:58:43 +0000310 }
311 }
David Reiss07ef3a92008-03-27 21:42:39 +0000312/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee27ed6ec2007-08-16 01:26:31 +0000313| tok_perl_package tok_identifier
314 {
David Reiss07ef3a92008-03-27 21:42:39 +0000315 pwarning(1, "'perl_package' is deprecated. Use 'namespace perl' instead");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000316 pdebug("Header -> tok_perl_namespace tok_identifier");
317 if (g_parse_mode == PROGRAM) {
David Reiss07ef3a92008-03-27 21:42:39 +0000318 g_program->set_namespace("perl", $2);
Mark Slee27ed6ec2007-08-16 01:26:31 +0000319 }
320 }
David Reiss6a4b82c2008-03-27 21:42:16 +0000321/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee58dfb4f2007-07-06 02:45:25 +0000322| tok_ruby_namespace tok_identifier
323 {
David Reiss6a4b82c2008-03-27 21:42:16 +0000324 pwarning(1, "'ruby_namespace' is deprecated. Use 'namespace rb' instead");
Mark Slee58dfb4f2007-07-06 02:45:25 +0000325 pdebug("Header -> tok_ruby_namespace tok_identifier");
326 if (g_parse_mode == PROGRAM) {
David Reiss6a4b82c2008-03-27 21:42:16 +0000327 g_program->set_namespace("rb", $2);
Mark Slee58dfb4f2007-07-06 02:45:25 +0000328 }
329 }
David Reiss3b455012008-03-27 21:40:46 +0000330/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleebd588222007-11-21 08:43:35 +0000331| tok_smalltalk_category tok_st_identifier
332 {
David Reiss3b455012008-03-27 21:40:46 +0000333 pwarning(1, "'smalltalk_category' is deprecated. Use 'namespace smalltalk.category' instead");
Mark Sleebd588222007-11-21 08:43:35 +0000334 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
335 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000336 g_program->set_namespace("smalltalk.category", $2);
Mark Sleebd588222007-11-21 08:43:35 +0000337 }
338 }
David Reiss3b455012008-03-27 21:40:46 +0000339/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss15457c92007-12-14 07:03:03 +0000340| tok_smalltalk_prefix tok_identifier
341 {
David Reiss3b455012008-03-27 21:40:46 +0000342 pwarning(1, "'smalltalk_prefix' is deprecated. Use 'namespace smalltalk.prefix' instead");
David Reiss15457c92007-12-14 07:03:03 +0000343 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
344 if (g_parse_mode == PROGRAM) {
David Reiss3b455012008-03-27 21:40:46 +0000345 g_program->set_namespace("smalltalk.prefix", $2);
David Reiss15457c92007-12-14 07:03:03 +0000346 }
347 }
David Reiss771f8c72008-02-27 01:55:25 +0000348/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000349| tok_java_package tok_identifier
350 {
David Reiss9f646152008-03-02 21:59:48 +0000351 pwarning(1, "'java_package' is deprecated. Use 'namespace java' instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000352 pdebug("Header -> tok_java_package tok_identifier");
353 if (g_parse_mode == PROGRAM) {
David Reiss771f8c72008-02-27 01:55:25 +0000354 g_program->set_namespace("java", $2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000355 }
356 }
David Reiss54b602b2008-03-27 21:41:06 +0000357/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee7e9eea42007-09-10 21:00:23 +0000358| tok_cocoa_prefix tok_identifier
359 {
David Reiss54b602b2008-03-27 21:41:06 +0000360 pwarning(1, "'cocoa_prefix' is deprecated. Use 'namespace cocoa' instead");
Mark Slee7e9eea42007-09-10 21:00:23 +0000361 pdebug("Header -> tok_cocoa_prefix tok_identifier");
362 if (g_parse_mode == PROGRAM) {
David Reiss54b602b2008-03-27 21:41:06 +0000363 g_program->set_namespace("cocoa", $2);
Mark Slee7e9eea42007-09-10 21:00:23 +0000364 }
365 }
David Reiss92e10d82009-02-17 20:28:19 +0000366/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
Mark Slee0d9199e2007-01-31 02:08:30 +0000367| tok_xsd_namespace tok_literal
368 {
David Reiss92e10d82009-02-17 20:28:19 +0000369 pwarning(1, "'xsd_namespace' is deprecated. Use 'namespace xsd' instead");
Mark Slee0d9199e2007-01-31 02:08:30 +0000370 pdebug("Header -> tok_xsd_namespace tok_literal");
371 if (g_parse_mode == PROGRAM) {
David Reiss92e10d82009-02-17 20:28:19 +0000372 g_program->set_namespace("cocoa", $2);
Mark Slee0d9199e2007-01-31 02:08:30 +0000373 }
374 }
David Reiss9d65bf02008-03-27 21:41:37 +0000375/* TODO(dreiss): Get rid of this once everyone is using the new hotness. */
David Reiss7f42bcf2008-01-11 20:59:12 +0000376| tok_csharp_namespace tok_identifier
377 {
David Reiss9d65bf02008-03-27 21:41:37 +0000378 pwarning(1, "'csharp_namespace' is deprecated. Use 'namespace csharp' instead");
David Reiss919ae802008-03-27 21:41:11 +0000379 pdebug("Header -> tok_csharp_namespace tok_identifier");
David Reiss7f42bcf2008-01-11 20:59:12 +0000380 if (g_parse_mode == PROGRAM) {
David Reiss9d65bf02008-03-27 21:41:37 +0000381 g_program->set_namespace("csharp", $2);
David Reiss7f42bcf2008-01-11 20:59:12 +0000382 }
383 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000384
385Include:
386 tok_include tok_literal
387 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000388 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000389 if (g_parse_mode == INCLUDES) {
390 std::string path = include_file(std::string($2));
391 if (!path.empty()) {
kholst76f2c882008-01-16 02:47:41 +0000392 g_program->add_include(path, std::string($2));
Mark Sleef0712dc2006-10-25 19:03:57 +0000393 }
394 }
395 }
Mark Slee31985722006-05-24 21:45:31 +0000396
397DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000398 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000399 {
400 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000401 if ($2 != NULL && $3 != NULL) {
402 $3->set_doc($2);
403 }
Mark Slee31985722006-05-24 21:45:31 +0000404 }
405|
406 {
407 pdebug("DefinitionList -> ");
408 }
409
410Definition:
Mark Slee30152872006-11-28 01:24:07 +0000411 Const
412 {
413 pdebug("Definition -> Const");
414 if (g_parse_mode == PROGRAM) {
415 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000416 }
David Reisscdffe262007-08-14 17:12:31 +0000417 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000418 }
419| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000420 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000421 pdebug("Definition -> TypeDefinition");
422 if (g_parse_mode == PROGRAM) {
423 g_scope->add_type($1->get_name(), $1);
424 if (g_parent_scope != NULL) {
425 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
426 }
427 }
David Reisscdffe262007-08-14 17:12:31 +0000428 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000429 }
Mark Slee31985722006-05-24 21:45:31 +0000430| Service
431 {
432 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000433 if (g_parse_mode == PROGRAM) {
434 g_scope->add_service($1->get_name(), $1);
435 if (g_parent_scope != NULL) {
436 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
437 }
438 g_program->add_service($1);
439 }
David Reisscdffe262007-08-14 17:12:31 +0000440 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000441 }
442
Mark Sleef0712dc2006-10-25 19:03:57 +0000443TypeDefinition:
444 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000445 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000446 pdebug("TypeDefinition -> Typedef");
447 if (g_parse_mode == PROGRAM) {
448 g_program->add_typedef($1);
449 }
450 }
451| Enum
452 {
453 pdebug("TypeDefinition -> Enum");
454 if (g_parse_mode == PROGRAM) {
455 g_program->add_enum($1);
456 }
457 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000458| Senum
459 {
460 pdebug("TypeDefinition -> Senum");
461 if (g_parse_mode == PROGRAM) {
462 g_program->add_typedef($1);
463 }
464 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000465| Struct
466 {
467 pdebug("TypeDefinition -> Struct");
468 if (g_parse_mode == PROGRAM) {
469 g_program->add_struct($1);
470 }
471 }
472| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000473 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000474 pdebug("TypeDefinition -> Xception");
475 if (g_parse_mode == PROGRAM) {
476 g_program->add_xception($1);
477 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000478 }
Mark Slee31985722006-05-24 21:45:31 +0000479
480Typedef:
Mark Sleebd588222007-11-21 08:43:35 +0000481 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000482 {
483 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000484 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000485 $$ = td;
486 }
487
Mark Slee6a47fed2007-02-07 02:40:59 +0000488CommaOrSemicolonOptional:
489 ','
490 {}
491| ';'
492 {}
493|
494 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000495
Mark Slee31985722006-05-24 21:45:31 +0000496Enum:
David Reisscdffe262007-08-14 17:12:31 +0000497 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000498 {
499 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000500 $$ = $4;
501 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000502 }
503
504EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000505 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000506 {
507 pdebug("EnumDefList -> EnumDefList EnumDef");
508 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000509 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000510 }
511|
512 {
513 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000514 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000515 }
516
517EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000518 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000519 {
Mark Slee30152872006-11-28 01:24:07 +0000520 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000521 if ($4 < 0) {
522 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000523 }
David Reissf1454162008-06-30 20:45:47 +0000524 if ($4 > INT_MAX) {
525 pwarning(1, "64-bit value supplied for enum %s.\n", $2);
526 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000527 $$ = new t_enum_value($2, $4);
528 if ($1 != NULL) {
529 $$->set_doc($1);
530 }
Mark Sleed0767c52007-07-27 22:14:41 +0000531 if (g_parse_mode == PROGRAM) {
532 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
533 if (g_parent_scope != NULL) {
534 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
535 }
536 }
Mark Slee31985722006-05-24 21:45:31 +0000537 }
538|
David Reisscbd4bac2007-08-14 17:12:33 +0000539 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000540 {
Mark Slee30152872006-11-28 01:24:07 +0000541 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000542 $$ = new t_enum_value($2);
543 if ($1 != NULL) {
544 $$->set_doc($1);
545 }
Mark Slee30152872006-11-28 01:24:07 +0000546 }
547
Mark Slee6a47fed2007-02-07 02:40:59 +0000548Senum:
David Reisscdffe262007-08-14 17:12:31 +0000549 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000550 {
551 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000552 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000553 }
554
555SenumDefList:
556 SenumDefList SenumDef
557 {
558 pdebug("SenumDefList -> SenumDefList SenumDef");
559 $$ = $1;
560 $$->add_string_enum_val($2);
561 }
562|
563 {
564 pdebug("SenumDefList -> ");
565 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
566 $$->set_string_enum(true);
567 }
568
569SenumDef:
570 tok_literal CommaOrSemicolonOptional
571 {
572 pdebug("SenumDef -> tok_literal");
573 $$ = $1;
574 }
575
Mark Slee30152872006-11-28 01:24:07 +0000576Const:
577 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
578 {
579 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000580 if (g_parse_mode == PROGRAM) {
581 $$ = new t_const($2, $3, $5);
582 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000583
584 g_scope->add_constant($3, $$);
585 if (g_parent_scope != NULL) {
586 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
587 }
588
Mark Sleeaa7671d2006-11-29 03:19:31 +0000589 } else {
590 $$ = NULL;
591 }
Mark Slee30152872006-11-28 01:24:07 +0000592 }
593
594ConstValue:
595 tok_int_constant
596 {
597 pdebug("ConstValue => tok_int_constant");
598 $$ = new t_const_value();
599 $$->set_integer($1);
David Reissf1454162008-06-30 20:45:47 +0000600 if ($1 < INT32_MIN || $1 > INT32_MAX) {
601 pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
602 }
Mark Slee30152872006-11-28 01:24:07 +0000603 }
604| tok_dub_constant
605 {
606 pdebug("ConstValue => tok_dub_constant");
607 $$ = new t_const_value();
608 $$->set_double($1);
609 }
610| tok_literal
611 {
612 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000613 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000614 }
Mark Slee67fc6342006-11-29 03:37:04 +0000615| tok_identifier
616 {
617 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000618 t_const* constant = g_scope->get_constant($1);
619 if (constant != NULL) {
620 $$ = constant->get_value();
621 } else {
622 if (g_parse_mode == PROGRAM) {
623 pwarning(1, "Constant strings should be quoted: %s\n", $1);
624 }
625 $$ = new t_const_value($1);
626 }
Mark Slee67fc6342006-11-29 03:37:04 +0000627 }
Mark Slee30152872006-11-28 01:24:07 +0000628| ConstList
629 {
630 pdebug("ConstValue => ConstList");
631 $$ = $1;
632 }
633| ConstMap
634 {
635 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000636 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000637 }
638
639ConstList:
640 '[' ConstListContents ']'
641 {
642 pdebug("ConstList => [ ConstListContents ]");
643 $$ = $2;
644 }
645
646ConstListContents:
647 ConstListContents ConstValue CommaOrSemicolonOptional
648 {
649 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
650 $$ = $1;
651 $$->add_list($2);
652 }
653|
654 {
655 pdebug("ConstListContents =>");
656 $$ = new t_const_value();
657 $$->set_list();
658 }
659
660ConstMap:
661 '{' ConstMapContents '}'
662 {
663 pdebug("ConstMap => { ConstMapContents }");
664 $$ = $2;
665 }
666
667ConstMapContents:
668 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
669 {
670 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
671 $$ = $1;
672 $$->add_map($2, $4);
673 }
674|
675 {
676 pdebug("ConstMapContents =>");
677 $$ = new t_const_value();
678 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000679 }
680
681Struct:
David Reissa2309992008-12-10 01:52:48 +0000682 tok_struct tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
Mark Slee31985722006-05-24 21:45:31 +0000683 {
684 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000685 $5->set_xsd_all($3);
686 $$ = $5;
David Reissa2309992008-12-10 01:52:48 +0000687 $$->set_name($2);
688 if ($7 != NULL) {
689 $$->annotations_ = $7->annotations_;
690 delete $7;
691 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000692 }
693
Mark Slee36bfa2e2007-01-19 20:09:51 +0000694XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000695 tok_xsd_all
696 {
697 $$ = true;
698 }
699|
700 {
701 $$ = false;
702 }
703
Mark Slee36bfa2e2007-01-19 20:09:51 +0000704XsdOptional:
705 tok_xsd_optional
706 {
707 $$ = true;
708 }
709|
710 {
711 $$ = false;
712 }
713
Mark Slee7df0e2a2007-02-06 21:03:18 +0000714XsdNillable:
715 tok_xsd_nillable
716 {
717 $$ = true;
718 }
719|
720 {
721 $$ = false;
722 }
723
Mark Slee21135c32007-02-05 21:52:08 +0000724XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000725 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000726 {
Mark Slee748d83f2007-02-07 01:20:08 +0000727 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000728 }
729|
730 {
731 $$ = NULL;
732 }
733
Mark Slee9cb7c612006-09-01 22:17:45 +0000734Xception:
735 tok_xception tok_identifier '{' FieldList '}'
736 {
737 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
738 $4->set_name($2);
739 $4->set_xception(true);
740 $$ = $4;
Mark Slee31985722006-05-24 21:45:31 +0000741 }
742
743Service:
Mark Slee78165722007-09-10 22:08:49 +0000744 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000745 {
746 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000747 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000748 $$->set_name($2);
749 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000750 }
751
Mark Slee78165722007-09-10 22:08:49 +0000752FlagArgs:
753 {
754 g_arglist = 1;
755 }
756
757UnflagArgs:
758 {
759 g_arglist = 0;
760 }
761
Mark Slee36bfa2e2007-01-19 20:09:51 +0000762Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000763 tok_extends tok_identifier
764 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000765 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000766 $$ = NULL;
767 if (g_parse_mode == PROGRAM) {
768 $$ = g_scope->get_service($2);
769 if ($$ == NULL) {
770 yyerror("Service \"%s\" has not been defined.", $2);
771 exit(1);
772 }
773 }
774 }
775|
776 {
777 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000778 }
779
780FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000781 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000782 {
783 pdebug("FunctionList -> FunctionList Function");
784 $$ = $1;
785 $1->add_function($2);
786 }
787|
788 {
789 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000790 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000791 }
792
793Function:
David Reiss6985a422009-03-24 20:00:47 +0000794 CaptureDocText Oneway FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000795 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000796 $6->set_name(std::string($4) + "_args");
797 $$ = new t_function($3, $4, $6, $8, $2);
798 if ($1 != NULL) {
799 $$->set_doc($1);
800 }
Mark Slee31985722006-05-24 21:45:31 +0000801 }
802
David Reiss6985a422009-03-24 20:00:47 +0000803Oneway:
804 tok_oneway
Mark Slee31985722006-05-24 21:45:31 +0000805 {
Mark Slee52f643d2006-08-09 00:03:43 +0000806 $$ = true;
807 }
808|
809 {
810 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000811 }
812
Mark Slee36bfa2e2007-01-19 20:09:51 +0000813Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000814 tok_throws '(' FieldList ')'
815 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000816 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000817 $$ = $3;
Mark Sleef07d48e2008-02-01 01:36:26 +0000818 if (g_parse_mode == PROGRAM && !validate_throws($$)) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000819 yyerror("Throws clause may not contain non-exception types");
820 exit(1);
821 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000822 }
823|
824 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000825 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000826 }
827
Mark Slee31985722006-05-24 21:45:31 +0000828FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000829 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000830 {
831 pdebug("FieldList -> FieldList , Field");
832 $$ = $1;
David Reissebb6cc42009-04-09 20:02:56 +0000833 if (!($$->validate_field($2))) {
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000834 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
835 exit(1);
836 }
David Reissebb6cc42009-04-09 20:02:56 +0000837 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000838 }
839|
840 {
841 pdebug("FieldList -> ");
David Reiss00a8dd62009-03-19 08:14:12 +0000842 y_field_val = -1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000843 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000844 }
845
846Field:
David Reiss8320a922007-08-14 19:59:26 +0000847 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000848 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000849 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000850 if ($2 < 0) {
David Reissbb461362009-04-02 19:23:59 +0000851 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 +0000852 if (g_strict >= 192) {
853 yyerror("Implicit field keys are deprecated and not allowed with -strict");
854 exit(1);
855 }
Mark Slee31985722006-05-24 21:45:31 +0000856 }
David Reiss8320a922007-08-14 19:59:26 +0000857 $$ = new t_field($4, $5, $2);
858 $$->set_req($3);
859 if ($6 != NULL) {
860 validate_field_value($$, $6);
861 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000862 }
David Reiss8320a922007-08-14 19:59:26 +0000863 $$->set_xsd_optional($7);
864 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000865 if ($1 != NULL) {
866 $$->set_doc($1);
867 }
David Reiss8320a922007-08-14 19:59:26 +0000868 if ($9 != NULL) {
869 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000870 }
Mark Slee31985722006-05-24 21:45:31 +0000871 }
Mark Slee7ff32452007-02-01 05:26:18 +0000872
873FieldIdentifier:
874 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000875 {
Mark Slee7ff32452007-02-01 05:26:18 +0000876 if ($1 <= 0) {
877 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
878 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000879 }
Mark Slee7ff32452007-02-01 05:26:18 +0000880 $$ = $1;
881 }
882|
883 {
884 $$ = y_field_val--;
885 }
886
David Reiss8320a922007-08-14 19:59:26 +0000887FieldRequiredness:
888 tok_required
889 {
Mark Slee78165722007-09-10 22:08:49 +0000890 if (g_arglist) {
891 if (g_parse_mode == PROGRAM) {
892 pwarning(1, "required keyword is ignored in argument lists.\n");
893 }
David Reiss204420f2008-01-11 20:59:03 +0000894 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000895 } else {
David Reiss204420f2008-01-11 20:59:03 +0000896 $$ = t_field::T_REQUIRED;
Mark Slee78165722007-09-10 22:08:49 +0000897 }
David Reiss8320a922007-08-14 19:59:26 +0000898 }
899| tok_optional
900 {
Mark Slee78165722007-09-10 22:08:49 +0000901 if (g_arglist) {
902 if (g_parse_mode == PROGRAM) {
903 pwarning(1, "optional keyword is ignored in argument lists.\n");
904 }
David Reiss204420f2008-01-11 20:59:03 +0000905 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000906 } else {
David Reiss204420f2008-01-11 20:59:03 +0000907 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000908 }
David Reiss8320a922007-08-14 19:59:26 +0000909 }
910|
911 {
David Reiss204420f2008-01-11 20:59:03 +0000912 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000913 }
914
Mark Slee7ff32452007-02-01 05:26:18 +0000915FieldValue:
916 '=' ConstValue
917 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000918 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000919 $$ = $2;
920 } else {
921 $$ = NULL;
922 }
923 }
924|
925 {
926 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000927 }
Mark Slee31985722006-05-24 21:45:31 +0000928
929DefinitionType:
930 BaseType
931 {
932 pdebug("DefinitionType -> BaseType");
933 $$ = $1;
934 }
Mark Sleee8540632006-05-30 09:24:40 +0000935| ContainerType
936 {
937 pdebug("DefinitionType -> ContainerType");
938 $$ = $1;
939 }
Mark Slee31985722006-05-24 21:45:31 +0000940
941FunctionType:
942 FieldType
943 {
Mark Sleee8540632006-05-30 09:24:40 +0000944 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000945 $$ = $1;
946 }
947| tok_void
948 {
Mark Sleee8540632006-05-30 09:24:40 +0000949 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000950 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000951 }
952
953FieldType:
954 tok_identifier
955 {
Mark Sleee8540632006-05-30 09:24:40 +0000956 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000957 if (g_parse_mode == INCLUDES) {
958 // Ignore identifiers in include mode
959 $$ = NULL;
960 } else {
961 // Lookup the identifier in the current scope
962 $$ = g_scope->get_type($1);
963 if ($$ == NULL) {
964 yyerror("Type \"%s\" has not been defined.", $1);
965 exit(1);
966 }
Mark Sleee8540632006-05-30 09:24:40 +0000967 }
Mark Slee31985722006-05-24 21:45:31 +0000968 }
969| BaseType
970 {
Mark Sleee8540632006-05-30 09:24:40 +0000971 pdebug("FieldType -> BaseType");
972 $$ = $1;
973 }
974| ContainerType
975 {
976 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000977 $$ = $1;
978 }
979
980BaseType:
981 tok_string
982 {
983 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000984 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000985 }
Mark Slee8d725a22007-04-13 01:57:12 +0000986| tok_binary
987 {
988 pdebug("BaseType -> tok_binary");
989 $$ = g_type_binary;
990 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000991| tok_slist
992 {
993 pdebug("BaseType -> tok_slist");
994 $$ = g_type_slist;
995 }
Mark Slee78f58e22006-09-02 04:17:07 +0000996| tok_bool
997 {
998 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000999 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +00001000 }
Mark Slee31985722006-05-24 21:45:31 +00001001| tok_byte
1002 {
1003 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +00001004 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +00001005 }
Mark Slee9cb7c612006-09-01 22:17:45 +00001006| tok_i16
1007 {
1008 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +00001009 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +00001010 }
Mark Slee31985722006-05-24 21:45:31 +00001011| tok_i32
1012 {
1013 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +00001014 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +00001015 }
Mark Slee31985722006-05-24 21:45:31 +00001016| tok_i64
1017 {
1018 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +00001019 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +00001020 }
Mark Sleec98d0502006-09-06 02:42:25 +00001021| tok_double
1022 {
1023 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +00001024 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +00001025 }
Mark Slee31985722006-05-24 21:45:31 +00001026
David Reissa2309992008-12-10 01:52:48 +00001027ContainerType: SimpleContainerType TypeAnnotations
1028 {
1029 pdebug("ContainerType -> SimpleContainerType TypeAnnotations");
1030 $$ = $1;
1031 if ($2 != NULL) {
1032 $$->annotations_ = $2->annotations_;
1033 delete $2;
1034 }
1035 }
1036
1037SimpleContainerType:
Mark Sleee8540632006-05-30 09:24:40 +00001038 MapType
1039 {
David Reissa2309992008-12-10 01:52:48 +00001040 pdebug("SimpleContainerType -> MapType");
Mark Sleee8540632006-05-30 09:24:40 +00001041 $$ = $1;
1042 }
1043| SetType
1044 {
David Reissa2309992008-12-10 01:52:48 +00001045 pdebug("SimpleContainerType -> SetType");
Mark Sleee8540632006-05-30 09:24:40 +00001046 $$ = $1;
1047 }
1048| ListType
1049 {
David Reissa2309992008-12-10 01:52:48 +00001050 pdebug("SimpleContainerType -> ListType");
Mark Sleee8540632006-05-30 09:24:40 +00001051 $$ = $1;
1052 }
1053
1054MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001055 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001056 {
1057 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001058 $$ = new t_map($4, $6);
1059 if ($2 != NULL) {
1060 ((t_container*)$$)->set_cpp_name(std::string($2));
1061 }
Mark Sleee8540632006-05-30 09:24:40 +00001062 }
1063
1064SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001065 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +00001066 {
1067 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001068 $$ = new t_set($4);
1069 if ($2 != NULL) {
1070 ((t_container*)$$)->set_cpp_name(std::string($2));
1071 }
Mark Sleee8540632006-05-30 09:24:40 +00001072 }
1073
1074ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001075 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001076 {
1077 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001078 $$ = new t_list($3);
1079 if ($5 != NULL) {
1080 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001081 }
1082 }
1083
Mark Slee36bfa2e2007-01-19 20:09:51 +00001084CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001085 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001086 {
Mark Sleeafc76542007-02-09 21:55:44 +00001087 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001088 }
1089|
1090 {
1091 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001092 }
1093
David Reissa2309992008-12-10 01:52:48 +00001094TypeAnnotations:
1095 '(' TypeAnnotationList ')'
1096 {
1097 pdebug("TypeAnnotations -> ( TypeAnnotationList )");
1098 $$ = $2;
1099 }
1100|
1101 {
1102 $$ = NULL;
1103 }
1104
1105TypeAnnotationList:
1106 TypeAnnotationList TypeAnnotation
1107 {
1108 pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation");
1109 $$ = $1;
1110 $$->annotations_[$2->key] = $2->val;
1111 delete $2;
1112 }
1113|
1114 {
1115 /* Just use a dummy structure to hold the annotations. */
1116 $$ = new t_struct(g_program);
1117 }
1118
1119TypeAnnotation:
1120 tok_identifier '=' tok_literal CommaOrSemicolonOptional
1121 {
1122 pdebug("TypeAnnotation -> tok_identifier = tok_literal");
1123 $$ = new t_annotation;
1124 $$->key = $1;
1125 $$->val = $3;
1126 }
1127
Mark Slee31985722006-05-24 21:45:31 +00001128%%