Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / heimdal / lib / asn1 / parse.y
1 /*
2  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 /* $Id: parse.y,v 1.19 2001/09/27 16:21:47 assar Exp $ */
35
36 %{
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "symbol.h"
44 #include "lex.h"
45 #include "gen_locl.h"
46
47 RCSID("$Id: parse.y,v 1.19 2001/09/27 16:21:47 assar Exp $");
48
49 static Type *new_type (Typetype t);
50 void yyerror (char *);
51
52 static void append (Member *l, Member *r);
53
54 %}
55
56 %union {
57   int constant;
58   char *name;
59   Type *type;
60   Member *member;
61 }
62
63 %token INTEGER SEQUENCE OF OCTET STRING GeneralizedTime GeneralString
64 %token BIT APPLICATION OPTIONAL EEQUAL TBEGIN END DEFINITIONS ENUMERATED
65 %token EXTERNAL
66 %token DOTDOT
67 %token IMPORTS FROM
68 %token OBJECT IDENTIFIER
69 %token <name> IDENT 
70 %token <constant> CONSTANT
71
72 %type <constant> constant optional2
73 %type <type> type
74 %type <member> memberdecls memberdecl bitdecls bitdecl
75
76 %start envelope
77
78 %%
79
80 envelope        : IDENT DEFINITIONS EEQUAL TBEGIN specification END {}
81                 ;
82
83 specification   :
84                 | specification declaration
85                 ;
86
87 declaration     : imports_decl
88                 | type_decl
89                 | constant_decl
90                 ;
91
92 referencenames  : IDENT ',' referencenames
93                 {
94                         Symbol *s = addsym($1);
95                         s->stype = Stype;
96                 }
97                 | IDENT
98                 {
99                         Symbol *s = addsym($1);
100                         s->stype = Stype;
101                 }
102                 ;
103
104 imports_decl    : IMPORTS referencenames FROM IDENT ';'
105                 { add_import($4); }
106                 ;
107
108 type_decl       : IDENT EEQUAL type
109                 {
110                   Symbol *s = addsym ($1);
111                   s->stype = Stype;
112                   s->type = $3;
113                   generate_type (s);
114                 }
115                 ;
116
117 constant_decl   : IDENT type EEQUAL constant
118                 {
119                   Symbol *s = addsym ($1);
120                   s->stype = SConstant;
121                   s->constant = $4;
122                   generate_constant (s);
123                 }
124                 ;
125
126 type            : INTEGER     { $$ = new_type(TInteger); }
127                 | INTEGER '(' constant DOTDOT constant ')' {
128                     if($3 != 0)
129                         error_message("Only 0 supported as low range");
130                     if($5 != INT_MIN && $5 != UINT_MAX && $5 != INT_MAX)
131                         error_message("Only %u supported as high range",
132                                       UINT_MAX);
133                     $$ = new_type(TUInteger);
134                 }
135                 | INTEGER '{' bitdecls '}'
136                 {
137                         $$ = new_type(TInteger);
138                         $$->members = $3;
139                 }
140                 | OBJECT IDENTIFIER { $$ = new_type(TOID); }
141                 | ENUMERATED '{' bitdecls '}'
142                 {
143                         $$ = new_type(TEnumerated);
144                         $$->members = $3;
145                 }
146                 | OCTET STRING { $$ = new_type(TOctetString); }
147                 | GeneralString { $$ = new_type(TGeneralString); }
148                 | GeneralizedTime { $$ = new_type(TGeneralizedTime); }
149                 | SEQUENCE OF type
150                 {
151                   $$ = new_type(TSequenceOf);
152                   $$->subtype = $3;
153                 }
154                 | SEQUENCE '{' memberdecls '}'
155                 {
156                   $$ = new_type(TSequence);
157                   $$->members = $3;
158                 }
159                 | BIT STRING '{' bitdecls '}'
160                 {
161                   $$ = new_type(TBitString);
162                   $$->members = $4;
163                 }
164                 | IDENT
165                 {
166                   Symbol *s = addsym($1);
167                   $$ = new_type(TType);
168                   if(s->stype != Stype)
169                     error_message ("%s is not a type\n", $1);
170                   else
171                     $$->symbol = s;
172                 }
173                 | '[' APPLICATION constant ']' type
174                 {
175                   $$ = new_type(TApplication);
176                   $$->subtype = $5;
177                   $$->application = $3;
178                 }
179                 ;
180
181 memberdecls     : { $$ = NULL; }
182                 | memberdecl    { $$ = $1; }
183                 | memberdecls ',' memberdecl { $$ = $1; append($$, $3); }
184                 ;
185
186 memberdecl      : IDENT '[' constant ']' type optional2
187                 {
188                   $$ = malloc(sizeof(*$$));
189                   $$->name = $1;
190                   $$->gen_name = strdup($1);
191                   output_name ($$->gen_name);
192                   $$->val = $3;
193                   $$->optional = $6;
194                   $$->type = $5;
195                   $$->next = $$->prev = $$;
196                 }
197                 ;
198
199 optional2       : { $$ = 0; }
200                 | OPTIONAL { $$ = 1; }
201                 ;
202
203 bitdecls        : { $$ = NULL; }
204                 | bitdecl { $$ = $1; }
205                 | bitdecls ',' bitdecl { $$ = $1; append($$, $3); }
206                 ;
207
208 bitdecl         : IDENT '(' constant ')'
209                 {
210                   $$ = malloc(sizeof(*$$));
211                   $$->name = $1;
212                   $$->gen_name = strdup($1);
213                   output_name ($$->gen_name);
214                   $$->val = $3;
215                   $$->optional = 0;
216                   $$->type = NULL;
217                   $$->prev = $$->next = $$;
218                 }
219                 ;
220
221 constant        : CONSTANT      { $$ = $1; }
222                 | IDENT {
223                                   Symbol *s = addsym($1);
224                                   if(s->stype != SConstant)
225                                     error_message ("%s is not a constant\n",
226                                                    s->name);
227                                   else
228                                     $$ = s->constant;
229                                 }
230                 ;
231 %%
232
233 void
234 yyerror (char *s)
235 {
236      error_message ("%s\n", s);
237 }
238
239 static Type *
240 new_type (Typetype tt)
241 {
242   Type *t = malloc(sizeof(*t));
243   if (t == NULL) {
244       error_message ("out of memory in malloc(%lu)", 
245                      (unsigned long)sizeof(*t));
246       exit (1);
247   }
248   t->type = tt;
249   t->application = 0;
250   t->members = NULL;
251   t->subtype = NULL;
252   t->symbol  = NULL;
253   return t;
254 }
255
256 static void
257 append (Member *l, Member *r)
258 {
259   l->prev->next = r;
260   r->prev = l->prev;
261   l->prev = r;
262   r->next = l;
263 }