Added GPL.
[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 DOUBLE
63 %token STRING
64 %token OPAQUE
65 %token BOOL
66
67 /* This is sometimes lumped together with the other types, but
68  * the special keyword void can only occur after "default:" in
69  * union statements.
70  */
71 %token VOID
72
73 %token <str> IDENT
74 %token <str> INTLIT
75 %token <str> STRLIT
76
77 %%
78
79 file    : /* empty */
80         | stmts
81         ;
82
83 /* Statements. */
84 stmts   : stmt ';'
85         | stmts stmt ';'
86         ;
87
88 stmt    : ENUM IDENT '{' enum_values '}'
89         {
90           struct cons *enums = list_rev ($4);
91           gen_enum ($2, enums);
92           free ($2);
93           list_free (enums);
94         }
95         | STRUCT IDENT '{' decls '}'
96         {
97           struct cons *decls = list_rev ($4);
98           gen_struct ($2, decls);
99           free ($2);
100           list_free (decls);
101         }
102         | UNION IDENT SWITCH '(' decl ')' '{' union_cases '}'
103         {
104           struct cons *cases = list_rev ($8);
105           gen_union ($2, $5, cases);
106           free ($2);
107           free_decl ($5);
108           list_free (cases);
109         }
110         | TYPEDEF decl
111         {
112           gen_typedef ($2);
113           free_decl ($2);
114         }
115         | CONST IDENT '=' const
116         {
117           gen_const ($2, $4);
118           free ($2);
119           free ($4);
120         }
121         | PROGRAM
122         {
123           error ("PortableXDR does not support SunRPC program statements");
124         }
125         ;
126
127 /* Declarations used inside structs and unions.  eg. "int foo;" */
128 decls   : decl ';'
129         { $$ = new_cons (NULL, $1, (free_fn) free_decl); }
130         | decls decl ';'
131         { $$ = new_cons ($1, $2, (free_fn) free_decl); }
132         ;
133
134 decl    : string_decl
135         | opaque_decl
136         | simple_decl
137         | fixed_array_decl
138         | variable_array_decl
139         | pointer_decl
140         ;
141
142 string_decl
143         : STRING IDENT '<' const '>'
144         {
145           $$ = new_decl (decl_type_string, NULL, $2, $4);
146         }
147         | STRING IDENT '<' '>'
148         {
149           $$ = new_decl (decl_type_string, NULL, $2, NULL);
150         }
151         ;
152
153 opaque_decl
154         : OPAQUE IDENT '[' const ']'
155         {
156           $$ = new_decl (decl_type_opaque_fixed, NULL, $2, $4);
157         }
158         | OPAQUE IDENT '<' const '>'
159         {
160           $$ = new_decl (decl_type_opaque_variable, NULL, $2, $4);
161         }
162         ;
163
164 simple_decl
165         : type_ident IDENT
166         { $$ = new_decl (decl_type_simple, $1, $2, NULL); }
167         ;
168
169 fixed_array_decl
170         : type_ident IDENT '[' const ']'
171         { $$ = new_decl (decl_type_fixed_array, $1, $2, $4); }
172         ;
173
174 variable_array_decl
175         : type_ident IDENT '<' const '>'
176         { $$ = new_decl (decl_type_variable_array, $1, $2, $4); }
177         | type_ident IDENT '<' '>'
178         { $$ = new_decl (decl_type_variable_array, $1, $2, NULL); }
179         ;
180
181 pointer_decl
182         : type_ident '*' IDENT
183         { $$ = new_decl (decl_type_pointer, $1, $3, NULL); }
184         ;
185
186 /* Enumerations. */
187 enum_values
188         : enum_value
189         { $$ = new_cons (NULL, $1, (free_fn) free_enum_value); }
190         | enum_values ',' enum_value
191         { $$ = new_cons ($1, $3, (free_fn) free_enum_value); }
192         ;
193
194 enum_value
195         : IDENT
196         { $$ = new_enum_value ($1, NULL); }
197         | IDENT '=' const
198         { $$ = new_enum_value ($1, $3); }
199         ;
200
201 /* Case list inside a union. */
202 union_cases
203         : union_case ';'
204         { $$ = new_cons (NULL, $1, (free_fn) free_union_case); }
205         | union_cases union_case ';'
206         { $$ = new_cons ($1, $2, (free_fn) free_union_case); }
207         ;
208
209 union_case
210         : CASE const ':' decl
211         { $$ = new_union_case (union_case_normal, $2, $4); }
212         | DEFAULT ':' VOID
213         { $$ = new_union_case (union_case_default_void, NULL, NULL); }
214         | DEFAULT ':' decl
215         { $$ = new_union_case (union_case_default_decl, NULL, $3); }
216         ;
217
218 /* Constants, which may be integer literals or refer to previously
219  * defined constants (using "const" keyword).
220  * XXX In future we should probably allow computed constants.
221  */
222 const   : INTLIT
223         | IDENT
224         ;
225
226 /* Types.  Note 'string', 'opaque' and 'void' are handled by
227  * special cases above.
228  */
229 type_ident
230         : CHAR
231         /* NB: Unlike SunRPC we make char explicitly signed.  This
232          * will give some warnings in GCC if you mix PortableXDR
233          * code with SunRPC headers or vice versa.
234          */
235         { $$ = new_type (type_char, 1, NULL); }
236         | SIGNED CHAR
237         { $$ = new_type (type_char, 1, NULL); }
238         | UNSIGNED CHAR
239         { $$ = new_type (type_char, 0, NULL); }
240         | SHORT
241         { $$ = new_type (type_short, 1, NULL); }
242         | SIGNED SHORT
243         { $$ = new_type (type_short, 1, NULL); }
244         | UNSIGNED SHORT
245         { $$ = new_type (type_short, 0, NULL); }
246         | INT
247         { $$ = new_type (type_int, 1, NULL); }
248         | SIGNED INT
249         { $$ = new_type (type_int, 1, NULL); }
250         | UNSIGNED INT
251         { $$ = new_type (type_int, 0, NULL); }
252         | HYPER
253         { $$ = new_type (type_hyper, 1, NULL); }
254         | SIGNED HYPER
255         { $$ = new_type (type_hyper, 1, NULL); }
256         | UNSIGNED HYPER
257         { $$ = new_type (type_hyper, 0, NULL); }
258         | SIGNED
259         { $$ = new_type (type_int, 1, NULL); }
260         | UNSIGNED
261         { $$ = new_type (type_int, 0, NULL); }
262         | DOUBLE
263         { $$ = new_type (type_double, 0, NULL); }
264         | BOOL
265         { $$ = new_type (type_bool, 0, NULL); }
266         | IDENT
267         { $$ = new_type (type_ident, 0, $1); }
268         ;
269
270 %%
271
272 void
273 yyerror (const char *str)
274 {
275   error ("%s", str);
276 }