Added Gnulib intprops module.
[portablexdr.git] / rpcgen_parse.y
1 /* rpcgen - Generate XDR bindings automatically.    -*- text -*-
2  * Copyright (C) 2008 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 %{
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "rpcgen_int.h"
24
25 extern void yyerror (const char *str);
26 %}
27
28 %union {
29   char *str;
30   struct type *type;
31   struct decl *decl;
32   struct enum_value *enum_value;
33   struct union_case *union_case;
34   struct cons *list;
35 }
36
37 %type <type> type_ident
38 %type <str> const
39 %type <decl> decl
40 %type <decl> simple_decl fixed_array_decl variable_array_decl pointer_decl
41 %type <decl> string_decl opaque_decl
42 %type <enum_value> enum_value
43 %type <union_case> union_case
44 %type <list> decls enum_values union_cases
45
46 %token STRUCT
47 %token ENUM
48 %token CONST
49 %token TYPEDEF
50 %token UNION
51 %token SWITCH
52 %token CASE
53 %token DEFAULT
54 %token PROGRAM
55
56 %token UNSIGNED
57 %token SIGNED
58 %token CHAR
59 %token SHORT
60 %token INT
61 %token HYPER
62 %token FLOAT
63 %token DOUBLE
64 %token STRING
65 %token OPAQUE
66 %token BOOL
67
68 /* This is sometimes lumped together with the other types, but
69  * the special keyword void can only occur after "default:" in
70  * union statements.
71  */
72 %token VOID
73
74 %token <str> IDENT
75 %token <str> INTLIT
76 %token <str> STRLIT
77
78 %%
79
80 file    : /* empty */
81         | stmts
82         ;
83
84 /* Statements. */
85 stmts   : stmt ';'
86         | stmts stmt ';'
87         ;
88
89 stmt    : ENUM IDENT '{' enum_values '}'
90         {
91           struct cons *enums = list_rev ($4);
92           gen_enum ($2, enums);
93           free ($2);
94           list_free (enums);
95         }
96         | STRUCT IDENT '{' decls '}'
97         {
98           struct cons *decls = list_rev ($4);
99           gen_struct ($2, decls);
100           free ($2);
101           list_free (decls);
102         }
103         | UNION IDENT SWITCH '(' decl ')' '{' union_cases '}'
104         {
105           struct cons *cases = list_rev ($8);
106           gen_union ($2, $5, cases);
107           free ($2);
108           free_decl ($5);
109           list_free (cases);
110         }
111         | TYPEDEF decl
112         {
113           gen_typedef ($2);
114           free_decl ($2);
115         }
116         | CONST IDENT '=' const
117         {
118           gen_const ($2, $4);
119           free ($2);
120           free ($4);
121         }
122         | PROGRAM
123         {
124           error ("PortableXDR does not support SunRPC program statements");
125         }
126         ;
127
128 /* Declarations used inside structs and unions.  eg. "int foo;" */
129 decls   : decl ';'
130         { $$ = new_cons (NULL, $1, (free_fn) free_decl); }
131         | decls decl ';'
132         { $$ = new_cons ($1, $2, (free_fn) free_decl); }
133         ;
134
135 decl    : string_decl
136         | opaque_decl
137         | simple_decl
138         | fixed_array_decl
139         | variable_array_decl
140         | pointer_decl
141         ;
142
143 string_decl
144         : STRING IDENT '<' const '>'
145         {
146           $$ = new_decl (decl_type_string, NULL, $2, $4);
147         }
148         | STRING IDENT '<' '>'
149         {
150           $$ = new_decl (decl_type_string, NULL, $2, NULL);
151         }
152         ;
153
154 opaque_decl
155         : OPAQUE IDENT '[' const ']'
156         {
157           $$ = new_decl (decl_type_opaque_fixed, NULL, $2, $4);
158         }
159         | OPAQUE IDENT '<' const '>'
160         {
161           $$ = new_decl (decl_type_opaque_variable, NULL, $2, $4);
162         }
163         ;
164
165 simple_decl
166         : type_ident IDENT
167         { $$ = new_decl (decl_type_simple, $1, $2, NULL); }
168         ;
169
170 fixed_array_decl
171         : type_ident IDENT '[' const ']'
172         { $$ = new_decl (decl_type_fixed_array, $1, $2, $4); }
173         ;
174
175 variable_array_decl
176         : type_ident IDENT '<' const '>'
177         { $$ = new_decl (decl_type_variable_array, $1, $2, $4); }
178         | type_ident IDENT '<' '>'
179         { $$ = new_decl (decl_type_variable_array, $1, $2, NULL); }
180         ;
181
182 pointer_decl
183         : type_ident '*' IDENT
184         { $$ = new_decl (decl_type_pointer, $1, $3, NULL); }
185         ;
186
187 /* Enumerations. */
188 enum_values
189         : enum_value
190         { $$ = new_cons (NULL, $1, (free_fn) free_enum_value); }
191         | enum_values ',' enum_value
192         { $$ = new_cons ($1, $3, (free_fn) free_enum_value); }
193         ;
194
195 enum_value
196         : IDENT
197         { $$ = new_enum_value ($1, NULL); }
198         | IDENT '=' const
199         { $$ = new_enum_value ($1, $3); }
200         ;
201
202 /* Case list inside a union. */
203 union_cases
204         : union_case ';'
205         { $$ = new_cons (NULL, $1, (free_fn) free_union_case); }
206         | union_cases union_case ';'
207         { $$ = new_cons ($1, $2, (free_fn) free_union_case); }
208         ;
209
210 union_case
211         : CASE const ':' decl
212         { $$ = new_union_case (union_case_normal, $2, $4); }
213         | DEFAULT ':' VOID
214         { $$ = new_union_case (union_case_default_void, NULL, NULL); }
215         | DEFAULT ':' decl
216         { $$ = new_union_case (union_case_default_decl, NULL, $3); }
217         ;
218
219 /* Constants, which may be integer literals or refer to previously
220  * defined constants (using "const" keyword).
221  * XXX In future we should probably allow computed constants.
222  */
223 const   : INTLIT
224         | IDENT
225         ;
226
227 /* Types.  Note 'string', 'opaque' and 'void' are handled by
228  * special cases above.
229  */
230 type_ident
231         : CHAR
232         /* NB: Unlike SunRPC we make char explicitly signed.  This
233          * will give some warnings in GCC if you mix PortableXDR
234          * code with SunRPC headers or vice versa.
235          */
236         { $$ = new_type (type_char, 1, NULL); }
237         | SIGNED CHAR
238         { $$ = new_type (type_char, 1, NULL); }
239         | UNSIGNED CHAR
240         { $$ = new_type (type_char, 0, NULL); }
241         | SHORT
242         { $$ = new_type (type_short, 1, NULL); }
243         | SIGNED SHORT
244         { $$ = new_type (type_short, 1, NULL); }
245         | UNSIGNED SHORT
246         { $$ = new_type (type_short, 0, NULL); }
247         | INT
248         { $$ = new_type (type_int, 1, NULL); }
249         | SIGNED INT
250         { $$ = new_type (type_int, 1, NULL); }
251         | UNSIGNED INT
252         { $$ = new_type (type_int, 0, NULL); }
253         | HYPER
254         { $$ = new_type (type_hyper, 1, NULL); }
255         | SIGNED HYPER
256         { $$ = new_type (type_hyper, 1, NULL); }
257         | UNSIGNED HYPER
258         { $$ = new_type (type_hyper, 0, NULL); }
259         | SIGNED
260         { $$ = new_type (type_int, 1, NULL); }
261         | UNSIGNED
262         { $$ = new_type (type_int, 0, NULL); }
263         | FLOAT
264         { $$ = new_type (type_float, 0, NULL); }
265         | DOUBLE
266         { $$ = new_type (type_double, 0, NULL); }
267         | BOOL
268         { $$ = new_type (type_bool, 0, NULL); }
269         | IDENT
270         { $$ = new_type (type_ident, 0, $1); }
271         ;
272
273 %%
274
275 void
276 yyerror (const char *str)
277 {
278   error ("%s", str);
279 }