[BACK]Return to parse.y CVS log [TXT][DIR] Up to [local] / src / usr.bin / asn1_compile

Annotation of src/usr.bin/asn1_compile/parse.y, Revision 1.2

1.1       hin         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:
1.2     ! biorn      34: /* $KTH: parse.y,v 1.23 2004/10/13 17:41:48 lha Exp $ */
1.1       hin        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: /*
1.2     ! biorn      48: RCSID("$KTH: parse.y,v 1.23 2004/10/13 17:41:48 lha Exp $");
1.1       hin        49: */
                     50:
                     51: static Type *new_type (Typetype t);
                     52: void yyerror (char *);
                     53:
                     54: static void append (Member *l, Member *r);
                     55:
                     56: %}
                     57:
                     58: %union {
                     59:   int constant;
                     60:   char *name;
                     61:   Type *type;
                     62:   Member *member;
1.2     ! biorn      63:   char *defval;
1.1       hin        64: }
                     65:
1.2     ! biorn      66: %token INTEGER SEQUENCE CHOICE OF OCTET STRING GeneralizedTime GeneralString
1.1       hin        67: %token BIT APPLICATION OPTIONAL EEQUAL TBEGIN END DEFINITIONS ENUMERATED
1.2     ! biorn      68: %token UTF8String NULLTYPE
        !            69: %token EXTERNAL DEFAULT
        !            70: %token DOTDOT DOTDOTDOT
        !            71: %token BOOLEAN
1.1       hin        72: %token IMPORTS FROM
                     73: %token OBJECT IDENTIFIER
                     74: %token <name> IDENT
                     75: %token <constant> CONSTANT
                     76:
                     77: %type <constant> constant optional2
                     78: %type <type> type
1.2     ! biorn      79: %type <member> memberdecls memberdecl memberdeclstart bitdecls bitdecl
        !            80:
        !            81: %type <defval> defvalue
1.1       hin        82:
                     83: %start envelope
                     84:
                     85: %%
                     86:
                     87: envelope       : IDENT DEFINITIONS EEQUAL TBEGIN specification END {}
                     88:                ;
                     89:
                     90: specification  :
                     91:                | specification declaration
                     92:                ;
                     93:
                     94: declaration    : imports_decl
                     95:                | type_decl
                     96:                | constant_decl
                     97:                ;
                     98:
                     99: referencenames : IDENT ',' referencenames
                    100:                {
                    101:                        Symbol *s = addsym($1);
                    102:                        s->stype = Stype;
                    103:                }
                    104:                | IDENT
                    105:                {
                    106:                        Symbol *s = addsym($1);
                    107:                        s->stype = Stype;
                    108:                }
                    109:                ;
                    110:
                    111: imports_decl   : IMPORTS referencenames FROM IDENT ';'
                    112:                { add_import($4); }
                    113:                ;
                    114:
                    115: type_decl      : IDENT EEQUAL type
                    116:                {
                    117:                  Symbol *s = addsym ($1);
                    118:                  s->stype = Stype;
                    119:                  s->type = $3;
                    120:                  generate_type (s);
                    121:                }
                    122:                ;
                    123:
                    124: constant_decl  : IDENT type EEQUAL constant
                    125:                {
                    126:                  Symbol *s = addsym ($1);
                    127:                  s->stype = SConstant;
                    128:                  s->constant = $4;
                    129:                  generate_constant (s);
                    130:                }
                    131:                ;
                    132:
                    133: type           : INTEGER     { $$ = new_type(TInteger); }
                    134:                | INTEGER '(' constant DOTDOT constant ')' {
                    135:                    if($3 != 0)
                    136:                        error_message("Only 0 supported as low range");
                    137:                    if($5 != INT_MIN && $5 != UINT_MAX && $5 != INT_MAX)
                    138:                        error_message("Only %u supported as high range",
                    139:                                      UINT_MAX);
                    140:                    $$ = new_type(TUInteger);
                    141:                }
                    142:                 | INTEGER '{' bitdecls '}'
                    143:                 {
                    144:                        $$ = new_type(TInteger);
                    145:                        $$->members = $3;
                    146:                 }
                    147:                | OBJECT IDENTIFIER { $$ = new_type(TOID); }
                    148:                | ENUMERATED '{' bitdecls '}'
                    149:                {
                    150:                        $$ = new_type(TEnumerated);
                    151:                        $$->members = $3;
                    152:                }
                    153:                | OCTET STRING { $$ = new_type(TOctetString); }
                    154:                | GeneralString { $$ = new_type(TGeneralString); }
1.2     ! biorn     155:                | UTF8String { $$ = new_type(TUTF8String); }
        !           156:                 | NULLTYPE { $$ = new_type(TNull); }
1.1       hin       157:                | GeneralizedTime { $$ = new_type(TGeneralizedTime); }
                    158:                | SEQUENCE OF type
                    159:                {
                    160:                  $$ = new_type(TSequenceOf);
                    161:                  $$->subtype = $3;
                    162:                }
                    163:                | SEQUENCE '{' memberdecls '}'
                    164:                {
                    165:                  $$ = new_type(TSequence);
                    166:                  $$->members = $3;
                    167:                }
1.2     ! biorn     168:                | CHOICE '{' memberdecls '}'
        !           169:                {
        !           170:                  $$ = new_type(TChoice);
        !           171:                  $$->members = $3;
        !           172:                }
1.1       hin       173:                | BIT STRING '{' bitdecls '}'
                    174:                {
                    175:                  $$ = new_type(TBitString);
                    176:                  $$->members = $4;
                    177:                }
                    178:                | IDENT
                    179:                {
                    180:                  Symbol *s = addsym($1);
                    181:                  $$ = new_type(TType);
                    182:                  if(s->stype != Stype)
                    183:                    error_message ("%s is not a type\n", $1);
                    184:                  else
                    185:                    $$->symbol = s;
                    186:                }
                    187:                | '[' APPLICATION constant ']' type
                    188:                {
                    189:                  $$ = new_type(TApplication);
                    190:                  $$->subtype = $5;
                    191:                  $$->application = $3;
                    192:                }
1.2     ! biorn     193:                | BOOLEAN     { $$ = new_type(TBoolean); }
1.1       hin       194:                ;
                    195:
                    196: memberdecls    : { $$ = NULL; }
                    197:                | memberdecl    { $$ = $1; }
1.2     ! biorn     198:                | memberdecls  ',' DOTDOTDOT { $$ = $1; }
1.1       hin       199:                | memberdecls ',' memberdecl { $$ = $1; append($$, $3); }
                    200:                ;
                    201:
1.2     ! biorn     202: memberdeclstart : IDENT '[' constant ']' type
1.1       hin       203:                {
                    204:                  $$ = malloc(sizeof(*$$));
                    205:                  $$->name = $1;
                    206:                  $$->gen_name = strdup($1);
                    207:                  output_name ($$->gen_name);
                    208:                  $$->val = $3;
1.2     ! biorn     209:                  $$->optional = 0;
        !           210:                  $$->defval = NULL;
1.1       hin       211:                  $$->type = $5;
                    212:                  $$->next = $$->prev = $$;
                    213:                }
                    214:                ;
                    215:
1.2     ! biorn     216:
        !           217: memberdecl     : memberdeclstart optional2
        !           218:                { $1->optional = $2 ; $$ = $1; }
        !           219:                | memberdeclstart defvalue
        !           220:                { $1->defval = $2 ; $$ = $1; }
        !           221:                | memberdeclstart
        !           222:                { $$ = $1; }
        !           223:                ;
        !           224:
        !           225:
        !           226: optional2      : OPTIONAL { $$ = 1; }
        !           227:                ;
        !           228:
        !           229: defvalue       : DEFAULT constant
        !           230:                { asprintf(&$$, "%d", $2); }
        !           231:                | DEFAULT '"' IDENT '"'
        !           232:                { $$ = strdup ($3); }
1.1       hin       233:                ;
                    234:
                    235: bitdecls       : { $$ = NULL; }
                    236:                | bitdecl { $$ = $1; }
1.2     ! biorn     237:                | bitdecls ',' DOTDOTDOT { $$ = $1; }
1.1       hin       238:                | bitdecls ',' bitdecl { $$ = $1; append($$, $3); }
                    239:                ;
                    240:
                    241: bitdecl                : IDENT '(' constant ')'
                    242:                {
                    243:                  $$ = malloc(sizeof(*$$));
                    244:                  $$->name = $1;
                    245:                  $$->gen_name = strdup($1);
                    246:                  output_name ($$->gen_name);
                    247:                  $$->val = $3;
                    248:                  $$->optional = 0;
                    249:                  $$->type = NULL;
                    250:                  $$->prev = $$->next = $$;
                    251:                }
                    252:                ;
                    253:
                    254: constant       : CONSTANT      { $$ = $1; }
1.2     ! biorn     255:                | '-' CONSTANT  { $$ = -$2; }
1.1       hin       256:                | IDENT {
                    257:                                  Symbol *s = addsym($1);
                    258:                                  if(s->stype != SConstant)
                    259:                                    error_message ("%s is not a constant\n",
                    260:                                                   s->name);
                    261:                                  else
                    262:                                    $$ = s->constant;
                    263:                                }
                    264:                ;
                    265: %%
                    266:
                    267: void
                    268: yyerror (char *s)
                    269: {
                    270:      error_message ("%s\n", s);
                    271: }
                    272:
                    273: static Type *
                    274: new_type (Typetype tt)
                    275: {
                    276:   Type *t = malloc(sizeof(*t));
                    277:   if (t == NULL) {
                    278:       error_message ("out of memory in malloc(%lu)",
                    279:                     (unsigned long)sizeof(*t));
                    280:       exit (1);
                    281:   }
                    282:   t->type = tt;
                    283:   t->application = 0;
                    284:   t->members = NULL;
                    285:   t->subtype = NULL;
                    286:   t->symbol  = NULL;
                    287:   return t;
                    288: }
                    289:
                    290: static void
                    291: append (Member *l, Member *r)
                    292: {
                    293:   l->prev->next = r;
                    294:   r->prev = l->prev;
                    295:   l->prev = r;
                    296:   r->next = l;
                    297: }