blob: 822a8efb059f75ac864b171d1ff38bcdc78826f9 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001%{
2
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <errno.h>
7#include <assert.h>
8
9#include "asn1parser.h"
10
11#define YYPARSE_PARAM param
12#define YYERROR_VERBOSE
13
14int yylex(void);
15int yyerror(const char *msg);
16void asn1p_lexer_hack_push_opaque_state(void);
17void asn1p_lexer_hack_enable_with_syntax(void);
18#define yylineno asn1p_lineno
19extern int asn1p_lineno;
20
21
22static asn1p_value_t *
23 _convert_bitstring2binary(char *str, int base);
24
25#define checkmem(ptr) do { \
26 if(!(ptr)) \
27 return yyerror("Memory failure"); \
28 } while(0)
29
30#define CONSTRAINT_INSERT(root, constr_type, arg1, arg2) do { \
31 if(arg1->type != constr_type) { \
32 int __ret; \
33 root = asn1p_constraint_new(yylineno); \
34 checkmem(root); \
35 root->type = constr_type; \
36 __ret = asn1p_constraint_insert(root, \
37 arg1); \
38 checkmem(__ret == 0); \
39 } else { \
40 root = arg1; \
41 } \
42 if(arg2) { \
43 int __ret \
44 = asn1p_constraint_insert(root, arg2); \
45 checkmem(__ret == 0); \
46 } \
47 } while(0)
48
49%}
50
51
52/*
53 * Token value definition.
54 * a_*: ASN-specific types.
55 * tv_*: Locally meaningful types.
56 */
57%union {
58 asn1p_t *a_grammar;
59 asn1p_module_flags_e a_module_flags;
60 asn1p_module_t *a_module;
61 asn1p_expr_type_e a_type; /* ASN.1 Type */
62 asn1p_expr_t *a_expr; /* Constructed collection */
63 asn1p_constraint_t *a_constr; /* Constraint */
64 enum asn1p_constraint_type_e a_ctype;/* Constraint type */
65 asn1p_xports_t *a_xports; /* IMports/EXports */
66 asn1p_oid_t *a_oid; /* Object Identifier */
67 asn1p_oid_arc_t a_oid_arc; /* Single OID's arc */
68 struct asn1p_type_tag_s a_tag; /* A tag */
69 asn1p_ref_t *a_ref; /* Reference to custom type */
70 asn1p_wsyntx_t *a_wsynt; /* WITH SYNTAX contents */
71 asn1p_wsyntx_chunk_t *a_wchunk; /* WITH SYNTAX chunk */
72 struct asn1p_ref_component_s a_refcomp; /* Component of a reference */
73 asn1p_value_t *a_value; /* Number, DefinedValue, etc */
74 struct asn1p_param_s a_parg; /* A parameter argument */
75 asn1p_paramlist_t *a_plist; /* A pargs list */
76 enum asn1p_expr_marker_e a_marker; /* OPTIONAL/DEFAULT */
77 enum asn1p_constr_pres_e a_pres; /* PRESENT/ABSENT/OPTIONAL */
78 asn1_integer_t a_int;
79 char *tv_str;
80 struct {
81 char *buf;
82 int len;
83 } tv_opaque;
84 struct {
85 char *name;
86 struct asn1p_type_tag_s tag;
87 } tv_nametag;
88};
89
90/*
91 * Token types returned by scanner.
92 */
93%token TOK_PPEQ /* "::=", Pseudo Pascal EQuality */
94%token <tv_opaque> TOK_opaque /* opaque data (driven from .y) */
95%token <tv_str> TOK_bstring
96%token <tv_opaque> TOK_cstring
97%token <tv_str> TOK_hstring
98%token <tv_str> TOK_identifier
99%token <a_int> TOK_number
100%token <a_int> TOK_number_negative
101%token <tv_str> TOK_typereference
102%token <tv_str> TOK_objectclassreference /* "CLASS1" */
103%token <tv_str> TOK_typefieldreference /* "&Pork" */
104%token <tv_str> TOK_valuefieldreference /* "&id" */
105
106/*
107 * Token types representing ASN.1 standard keywords.
108 */
109%token TOK_ABSENT
110%token TOK_ABSTRACT_SYNTAX
111%token TOK_ALL
112%token TOK_ANY
113%token TOK_APPLICATION
114%token TOK_AUTOMATIC
115%token TOK_BEGIN
116%token TOK_BIT
117%token TOK_BMPString
118%token TOK_BOOLEAN
119%token TOK_BY
120%token TOK_CHARACTER
121%token TOK_CHOICE
122%token TOK_CLASS
123%token TOK_COMPONENT
124%token TOK_COMPONENTS
125%token TOK_CONSTRAINED
126%token TOK_CONTAINING
127%token TOK_DEFAULT
128%token TOK_DEFINITIONS
129%token TOK_DEFINED
130%token TOK_EMBEDDED
131%token TOK_ENCODED
132%token TOK_END
133%token TOK_ENUMERATED
134%token TOK_EXPLICIT
135%token TOK_EXPORTS
136%token TOK_EXTENSIBILITY
137%token TOK_EXTERNAL
138%token TOK_FALSE
139%token TOK_FROM
140%token TOK_GeneralizedTime
141%token TOK_GeneralString
142%token TOK_GraphicString
143%token TOK_IA5String
144%token TOK_IDENTIFIER
145%token TOK_IMPLICIT
146%token TOK_IMPLIED
147%token TOK_IMPORTS
148%token TOK_INCLUDES
149%token TOK_INSTANCE
150%token TOK_INTEGER
151%token TOK_ISO646String
152%token TOK_MAX
153%token TOK_MIN
154%token TOK_MINUS_INFINITY
155%token TOK_NULL
156%token TOK_NumericString
157%token TOK_OBJECT
158%token TOK_ObjectDescriptor
159%token TOK_OCTET
160%token TOK_OF
161%token TOK_OPTIONAL
162%token TOK_PATTERN
163%token TOK_PDV
164%token TOK_PLUS_INFINITY
165%token TOK_PRESENT
166%token TOK_PrintableString
167%token TOK_PRIVATE
168%token TOK_REAL
169%token TOK_RELATIVE_OID
170%token TOK_SEQUENCE
171%token TOK_SET
172%token TOK_SIZE
173%token TOK_STRING
174%token TOK_SYNTAX
175%token TOK_T61String
176%token TOK_TAGS
177%token TOK_TeletexString
178%token TOK_TRUE
179%token TOK_TYPE_IDENTIFIER
180%token TOK_UNIQUE
181%token TOK_UNIVERSAL
182%token TOK_UniversalString
183%token TOK_UTCTime
184%token TOK_UTF8String
185%token TOK_VideotexString
186%token TOK_VisibleString
187%token TOK_WITH
188
189%left '|' TOK_UNION
190%left '^' TOK_INTERSECTION
191%left TOK_EXCEPT
192
193/* Misc tags */
194%token TOK_TwoDots /* .. */
195%token TOK_ThreeDots /* ... */
196%token <a_tag> TOK_tag /* [0] */
197
198
199/*
200 * Types defined herein.
201 */
202%type <a_grammar> ModuleList
203%type <a_module> ModuleSpecification
204%type <a_module> ModuleSpecificationBody
205%type <a_module> ModuleSpecificationElement
206%type <a_module> optModuleSpecificationBody /* Optional */
207%type <a_module_flags> optModuleSpecificationFlags
208%type <a_module_flags> ModuleSpecificationFlags /* Set of FL */
209%type <a_module_flags> ModuleSpecificationFlag /* Single FL */
210%type <a_module> ImportsDefinition
211%type <a_module> ImportsBundleSet
212%type <a_xports> ImportsBundle
213%type <a_xports> ImportsList
214%type <a_xports> ExportsDefinition
215%type <a_xports> ExportsBody
216%type <a_expr> ImportsElement
217%type <a_expr> ExportsElement
218%type <a_expr> DataTypeMember
219%type <a_expr> ExtensionAndException
220%type <a_expr> TypeDeclaration
221%type <a_ref> ComplexTypeReference
222%type <a_ref> ComplexTypeReferenceAmpList
223%type <a_refcomp> ComplexTypeReferenceElement
224%type <a_refcomp> ClassFieldIdentifier
225%type <a_refcomp> ClassFieldName
226%type <a_expr> ClassFieldList
227%type <a_expr> ClassField
228%type <a_expr> ClassDeclaration
229%type <a_expr> ConstrainedTypeDeclaration
230%type <a_expr> DataTypeReference /* Type1 ::= Type2 */
231%type <a_expr> DefinedTypeRef
232%type <a_expr> ValueSetDefinition /* Val INTEGER ::= {1|2} */
233%type <a_expr> ValueDefinition /* val INTEGER ::= 1*/
234%type <a_value> optValueSetBody
235%type <a_value> InlineOrDefinedValue
236%type <a_value> DefinedValue
237%type <a_value> SignedNumber
238%type <a_expr> ConstructedType
239%type <a_expr> ConstructedDataTypeDefinition
240//%type <a_expr> optUniverationDefinition
241%type <a_expr> UniverationDefinition
242%type <a_expr> UniverationList
243%type <a_expr> UniverationElement
244%type <tv_str> TypeRefName
245%type <tv_str> ObjectClassReference
246%type <tv_nametag> TaggedIdentifier
247%type <tv_str> Identifier
248%type <a_parg> ParameterArgumentName
249%type <a_plist> ParameterArgumentList
250%type <a_expr> ActualParameter
251%type <a_expr> ActualParameterList
252%type <a_oid> ObjectIdentifier /* OID */
253%type <a_oid> optObjectIdentifier /* Optional OID */
254%type <a_oid> ObjectIdentifierBody
255%type <a_oid_arc> ObjectIdentifierElement
256%type <a_expr> BasicType
257%type <a_type> BasicTypeId
258%type <a_type> BasicTypeId_UniverationCompatible
259%type <a_type> BasicString
260%type <tv_opaque> Opaque
261//%type <tv_opaque> StringValue
262%type <a_tag> Tag /* [UNIVERSAL 0] IMPLICIT */
263%type <a_tag> optTag /* [UNIVERSAL 0] IMPLICIT */
264%type <a_constr> optConstraints
265%type <a_constr> Constraints
266%type <a_constr> SingleConstraint /* (SIZE(2)) */
267%type <a_constr> ConstraintElementSet /* 1..2,...,3 */
268%type <a_constr> ConstraintSubtypeElement /* 1..2 */
269%type <a_constr> ConstraintElementIntersection
270%type <a_constr> ConstraintElementException
271%type <a_constr> ConstraintElementUnion
272%type <a_constr> ConstraintElement
273%type <a_constr> SimpleTableConstraint
274%type <a_constr> TableConstraint
275%type <a_constr> WithComponents
276%type <a_constr> WithComponentsList
277%type <a_constr> WithComponentsElement
278%type <a_constr> ComponentRelationConstraint
279%type <a_constr> AtNotationList
280%type <a_ref> AtNotationElement
281%type <a_value> ConstraintValue
282%type <a_ctype> ConstraintSpec
283%type <a_ctype> ConstraintRangeSpec
284%type <a_wsynt> optWithSyntax
285%type <a_wsynt> WithSyntax
286%type <a_wsynt> WithSyntaxFormat
287%type <a_wchunk> WithSyntaxFormatToken
288%type <a_marker> optMarker Marker
289%type <a_int> optUnique
290%type <a_pres> optPresenceConstraint PresenceConstraint
291%type <tv_str> ComponentIdList
292
293
294%%
295
296
297ParsedGrammar:
298 ModuleList {
299 *(void **)param = $1;
300 }
301 ;
302
303ModuleList:
304 ModuleSpecification {
305 $$ = asn1p_new();
306 checkmem($$);
307 TQ_ADD(&($$->modules), $1, mod_next);
308 }
309 | ModuleList ModuleSpecification {
310 $$ = $1;
311 TQ_ADD(&($$->modules), $2, mod_next);
312 }
313 ;
314
315/*
316 * ASN module definition.
317 * === EXAMPLE ===
318 * MySyntax DEFINITIONS AUTOMATIC TAGS ::=
319 * BEGIN
320 * ...
321 * END
322 * === EOF ===
323 */
324
325ModuleSpecification:
326 TypeRefName optObjectIdentifier TOK_DEFINITIONS
327 optModuleSpecificationFlags
328 TOK_PPEQ TOK_BEGIN
329 optModuleSpecificationBody
330 TOK_END {
331
332 if($7) {
333 $$ = $7;
334 } else {
335 /* There's a chance that a module is just plain empty */
336 $$ = asn1p_module_new();
337 }
338 checkmem($$);
339
340 $$->Identifier = $1;
341 $$->module_oid = $2;
342 $$->module_flags = $4;
343 }
344 ;
345
346/*
347 * Object Identifier Definition
348 * { iso member-body(2) 3 }
349 */
350optObjectIdentifier:
351 { $$ = 0; }
352 | ObjectIdentifier { $$ = $1; }
353 ;
354
355ObjectIdentifier:
356 '{' ObjectIdentifierBody '}' {
357 $$ = $2;
358 }
359 | '{' '}' {
360 $$ = 0;
361 }
362 ;
363
364ObjectIdentifierBody:
365 ObjectIdentifierElement {
366 $$ = asn1p_oid_new();
367 asn1p_oid_add_arc($$, &$1);
368 if($1.name)
369 free($1.name);
370 }
371 | ObjectIdentifierBody ObjectIdentifierElement {
372 $$ = $1;
373 asn1p_oid_add_arc($$, &$2);
374 if($2.name)
375 free($2.name);
376 }
377 ;
378
379ObjectIdentifierElement:
380 Identifier { /* iso */
381 $$.name = $1;
382 $$.number = -1;
383 }
384 | Identifier '(' TOK_number ')' { /* iso(1) */
385 $$.name = $1;
386 $$.number = $3;
387 }
388 | TOK_number { /* 1 */
389 $$.name = 0;
390 $$.number = $1;
391 }
392 ;
393
394/*
395 * Optional module flags.
396 */
397optModuleSpecificationFlags:
398 { $$ = MSF_NOFLAGS; }
399 | ModuleSpecificationFlags {
400 $$ = $1;
401 }
402 ;
403
404/*
405 * Module flags.
406 */
407ModuleSpecificationFlags:
408 ModuleSpecificationFlag {
409 $$ = $1;
410 }
411 | ModuleSpecificationFlags ModuleSpecificationFlag {
412 $$ = $1 | $2;
413 }
414 ;
415
416/*
417 * Single module flag.
418 */
419ModuleSpecificationFlag:
420 TOK_EXPLICIT TOK_TAGS {
421 $$ = MSF_EXPLICIT_TAGS;
422 }
423 | TOK_IMPLICIT TOK_TAGS {
424 $$ = MSF_IMPLICIT_TAGS;
425 }
426 | TOK_AUTOMATIC TOK_TAGS {
427 $$ = MSF_AUTOMATIC_TAGS;
428 }
429 | TOK_EXTENSIBILITY TOK_IMPLIED {
430 $$ = MSF_EXTENSIBILITY_IMPLIED;
431 }
432 ;
433
434/*
435 * Optional module body.
436 */
437optModuleSpecificationBody:
438 { $$ = 0; }
439 | ModuleSpecificationBody {
440 assert($1);
441 $$ = $1;
442 }
443 ;
444
445/*
446 * ASN.1 Module body.
447 */
448ModuleSpecificationBody:
449 ModuleSpecificationElement {
450 $$ = $1;
451 }
452 | ModuleSpecificationBody ModuleSpecificationElement {
453 $$ = $1;
454
455#ifdef MY_IMPORT
456#error MY_IMPORT DEFINED ELSEWHERE!
457#endif
458#define MY_IMPORT(foo,field) do { \
459 if(TQ_FIRST(&($2->foo))) { \
460 TQ_ADD(&($$->foo), \
461 TQ_REMOVE(&($2->foo), field), \
462 field); \
463 assert(TQ_FIRST(&($2->foo)) == 0); \
464 } } while(0)
465
466 MY_IMPORT(imports, xp_next);
467 MY_IMPORT(exports, xp_next);
468 MY_IMPORT(members, next);
469#undef MY_IMPORT
470
471 }
472 ;
473
474/*
475 * One of the elements of ASN.1 module specification.
476 */
477ModuleSpecificationElement:
478 ImportsDefinition {
479 $$ = $1;
480 }
481 | ExportsDefinition {
482 $$ = asn1p_module_new();
483 checkmem($$);
484 if($1) {
485 TQ_ADD(&($$->exports), $1, xp_next);
486 } else {
487 /* "EXPORTS ALL;" ? */
488 }
489 }
490 | DataTypeReference {
491 $$ = asn1p_module_new();
492 checkmem($$);
493 assert($1->expr_type != A1TC_INVALID);
494 assert($1->meta_type != AMT_INVALID);
495 TQ_ADD(&($$->members), $1, next);
496 }
497 | ValueDefinition {
498 $$ = asn1p_module_new();
499 checkmem($$);
500 assert($1->expr_type != A1TC_INVALID);
501 assert($1->meta_type != AMT_INVALID);
502 TQ_ADD(&($$->members), $1, next);
503 }
504 /*
505 * Value set definition
506 * === EXAMPLE ===
507 * EvenNumbers INTEGER ::= { 2 | 4 | 6 | 8 }
508 * === EOF ===
509 */
510 | ValueSetDefinition {
511 $$ = asn1p_module_new();
512 checkmem($$);
513 assert($1->expr_type != A1TC_INVALID);
514 assert($1->meta_type != AMT_INVALID);
515 TQ_ADD(&($$->members), $1, next);
516 }
517
518 /*
519 * Erroneous attemps
520 */
521 | BasicString {
522 return yyerror(
523 "Attempt to redefine a standard basic type, "
524 "use -ftypesXY to switch back "
525 "to older version of ASN.1 standard");
526 }
527 ;
528
529/*
530 * === EXAMPLE ===
531 * IMPORTS Type1, value FROM Module { iso standard(0) } ;
532 * === EOF ===
533 */
534ImportsDefinition:
535 TOK_IMPORTS ImportsBundleSet ';' {
536 $$ = $2;
537 }
538 /*
539 * Some error cases.
540 */
541 | TOK_IMPORTS TOK_FROM /* ... */ {
542 return yyerror("Empty IMPORTS list");
543 }
544 ;
545
546ImportsBundleSet:
547 ImportsBundle {
548 $$ = asn1p_module_new();
549 checkmem($$);
550 TQ_ADD(&($$->imports), $1, xp_next);
551 }
552 | ImportsBundleSet ImportsBundle {
553 $$ = $1;
554 TQ_ADD(&($$->imports), $2, xp_next);
555 }
556 ;
557
558ImportsBundle:
559 ImportsList TOK_FROM TypeRefName optObjectIdentifier {
560 $$ = $1;
561 $$->from = $3;
562 $$->from_oid = $4;
563 checkmem($$);
564 }
565 ;
566
567ImportsList:
568 ImportsElement {
569 $$ = asn1p_xports_new();
570 checkmem($$);
571 TQ_ADD(&($$->members), $1, next);
572 }
573 | ImportsList ',' ImportsElement {
574 $$ = $1;
575 TQ_ADD(&($$->members), $3, next);
576 }
577 ;
578
579ImportsElement:
580 TypeRefName {
581 $$ = asn1p_expr_new(yylineno);
582 checkmem($$);
583 $$->Identifier = $1;
584 $$->expr_type = A1TC_REFERENCE;
585 }
586 | Identifier {
587 $$ = asn1p_expr_new(yylineno);
588 checkmem($$);
589 $$->Identifier = $1;
590 $$->expr_type = A1TC_REFERENCE;
591 }
592 ;
593
594ExportsDefinition:
595 TOK_EXPORTS ExportsBody ';' {
596 $$ = $2;
597 }
598 | TOK_EXPORTS TOK_ALL ';' {
599 $$ = 0;
600 }
601 | TOK_EXPORTS ';' {
602 /* Empty EXPORTS clause effectively prohibits export. */
603 $$ = asn1p_xports_new();
604 checkmem($$);
605 }
606 ;
607
608ExportsBody:
609 ExportsElement {
610 $$ = asn1p_xports_new();
611 assert($$);
612 TQ_ADD(&($$->members), $1, next);
613 }
614 | ExportsBody ',' ExportsElement {
615 $$ = $1;
616 TQ_ADD(&($$->members), $3, next);
617 }
618 ;
619
620ExportsElement:
621 TypeRefName {
622 $$ = asn1p_expr_new(yylineno);
623 checkmem($$);
624 $$->Identifier = $1;
625 $$->expr_type = A1TC_EXPORTVAR;
626 }
627 | Identifier {
628 $$ = asn1p_expr_new(yylineno);
629 checkmem($$);
630 $$->Identifier = $1;
631 $$->expr_type = A1TC_EXPORTVAR;
632 }
633 ;
634
635
636ValueSetDefinition:
637 TypeRefName DefinedTypeRef TOK_PPEQ '{' optValueSetBody '}' {
638 $$ = $2;
639 assert($$->Identifier == 0);
640 $$->Identifier = $1;
641 $$->meta_type = AMT_VALUESET;
642 // take care of optValueSetBody
643 }
644 ;
645
646DefinedTypeRef:
647 ComplexTypeReference {
648 $$ = asn1p_expr_new(yylineno);
649 checkmem($$);
650 $$->reference = $1;
651 $$->expr_type = A1TC_REFERENCE;
652 $$->meta_type = AMT_TYPEREF;
653 }
654 | BasicTypeId {
655 $$ = asn1p_expr_new(yylineno);
656 checkmem($$);
657 $$->expr_type = $1;
658 $$->meta_type = AMT_TYPE;
659 }
660 ;
661
662optValueSetBody:
663 { }
664 | ConstraintElementSet {
665 }
666 ;
667
668
669/*
670 * Data Type Reference.
671 * === EXAMPLE ===
672 * Type3 ::= CHOICE { a Type1, b Type 2 }
673 * === EOF ===
674 */
675
676DataTypeReference:
677 /*
678 * Optionally tagged type definition.
679 */
680 TypeRefName TOK_PPEQ optTag TOK_TYPE_IDENTIFIER {
681 $$ = asn1p_expr_new(yylineno);
682 checkmem($$);
683 $$->Identifier = $1;
684 $$->tag = $3;
685 $$->expr_type = A1TC_TYPEID;
686 $$->meta_type = AMT_TYPE;
687 }
688 | TypeRefName TOK_PPEQ optTag ConstrainedTypeDeclaration {
689 $$ = $4;
690 $$->Identifier = $1;
691 $$->tag = $3;
692 assert($$->expr_type);
693 assert($$->meta_type);
694 }
695 | TypeRefName TOK_PPEQ ClassDeclaration {
696 $$ = $3;
697 $$->Identifier = $1;
698 assert($$->expr_type == A1TC_CLASSDEF);
699 assert($$->meta_type == AMT_OBJECT);
700 }
701 /*
702 * Parametrized <Type> declaration:
703 * === EXAMPLE ===
704 * SIGNED { ToBeSigned } ::= SEQUENCE {
705 * toBeSigned ToBeSigned,
706 * algorithm AlgorithmIdentifier,
707 * signature BIT STRING
708 * }
709 * === EOF ===
710 */
711 | TypeRefName '{' ParameterArgumentList '}'
712 TOK_PPEQ ConstrainedTypeDeclaration {
713 $$ = $6;
714 assert($$->Identifier == 0);
715 $$->Identifier = $1;
716 $$->params = $3;
717 $$->meta_type = AMT_PARAMTYPE;
718 }
719 ;
720
721ParameterArgumentList:
722 ParameterArgumentName {
723 int ret;
724 $$ = asn1p_paramlist_new(yylineno);
725 checkmem($$);
726 ret = asn1p_paramlist_add_param($$, $1.governor, $1.argument);
727 checkmem(ret == 0);
728 if($1.governor) asn1p_ref_free($1.governor);
729 if($1.argument) free($1.argument);
730 }
731 | ParameterArgumentList ',' ParameterArgumentName {
732 int ret;
733 $$ = $1;
734 ret = asn1p_paramlist_add_param($$, $3.governor, $3.argument);
735 checkmem(ret == 0);
736 if($3.governor) asn1p_ref_free($3.governor);
737 if($3.argument) free($3.argument);
738 }
739 ;
740
741ParameterArgumentName:
742 TypeRefName {
743 $$.governor = NULL;
744 $$.argument = $1;
745 }
746 | TypeRefName ':' Identifier {
747 int ret;
748 $$.governor = asn1p_ref_new(yylineno);
749 ret = asn1p_ref_add_component($$.governor, $1, 0);
750 checkmem(ret == 0);
751 $$.argument = $3;
752 }
753 | BasicTypeId ':' Identifier {
754 int ret;
755 $$.governor = asn1p_ref_new(yylineno);
756 ret = asn1p_ref_add_component($$.governor,
757 ASN_EXPR_TYPE2STR($1), 1);
758 checkmem(ret == 0);
759 $$.argument = $3;
760 }
761 ;
762
763ActualParameterList:
764 ActualParameter {
765 $$ = asn1p_expr_new(yylineno);
766 checkmem($$);
767 TQ_ADD(&($$->members), $1, next);
768 }
769 | ActualParameterList ',' ActualParameter {
770 $$ = $1;
771 TQ_ADD(&($$->members), $3, next);
772 }
773 ;
774
775ActualParameter:
776 TypeDeclaration {
777 $$ = $1;
778 }
779 | Identifier {
780 $$ = asn1p_expr_new(yylineno);
781 checkmem($$);
782 $$->Identifier = $1;
783 $$->expr_type = A1TC_REFERENCE;
784 $$->meta_type = AMT_VALUE;
785 }
786 ;
787
788/*
789 * A collection of constructed data type members.
790 */
791ConstructedDataTypeDefinition:
792 DataTypeMember {
793 $$ = asn1p_expr_new(yylineno);
794 checkmem($$);
795 TQ_ADD(&($$->members), $1, next);
796 }
797 | ConstructedDataTypeDefinition ',' DataTypeMember {
798 $$ = $1;
799 TQ_ADD(&($$->members), $3, next);
800 }
801 ;
802
803ClassDeclaration:
804 TOK_CLASS '{' ClassFieldList '}' optWithSyntax {
805 $$ = $3;
806 checkmem($$);
807 $$->with_syntax = $5;
808 assert($$->expr_type == A1TC_CLASSDEF);
809 assert($$->meta_type == AMT_OBJECT);
810 }
811 ;
812
813optUnique:
814 { $$ = 0; }
815 | TOK_UNIQUE { $$ = 1; }
816 ;
817
818ClassFieldList:
819 ClassField {
820 $$ = asn1p_expr_new(yylineno);
821 checkmem($$);
822 $$->expr_type = A1TC_CLASSDEF;
823 $$->meta_type = AMT_OBJECT;
824 TQ_ADD(&($$->members), $1, next);
825 }
826 | ClassFieldList ',' ClassField {
827 $$ = $1;
828 TQ_ADD(&($$->members), $3, next);
829 }
830 ;
831
832ClassField:
833 ClassFieldIdentifier optMarker {
834 $$ = asn1p_expr_new(yylineno);
835 checkmem($$);
836 $$->Identifier = $1.name;
837 $$->expr_type = A1TC_CLASSFIELD;
838 $$->meta_type = AMT_OBJECTFIELD;
839 $$->marker = $2;
840 }
841 | ClassFieldIdentifier ConstrainedTypeDeclaration optUnique {
842 $$ = $2;
843 $$->Identifier = $1.name;
844 $$->unique = $3;
845 }
846 | ClassFieldIdentifier ClassFieldIdentifier optMarker optUnique {
847 int ret;
848 $$ = asn1p_expr_new(yylineno);
849 checkmem($$);
850 $$->Identifier = $1.name;
851 $$->reference = asn1p_ref_new(yylineno);
852 checkmem($$->reference);
853 ret = asn1p_ref_add_component($$->reference,
854 $2.name, $2.lex_type);
855 checkmem(ret == 0);
856 $$->expr_type = A1TC_CLASSFIELD;
857 $$->meta_type = AMT_OBJECTFIELD;
858 $$->marker = $3;
859 $$->unique = $4;
860 }
861 ;
862
863optWithSyntax:
864 { $$ = 0; }
865 | WithSyntax {
866 $$ = $1;
867 }
868 ;
869
870WithSyntax:
871 TOK_WITH TOK_SYNTAX '{'
872 { asn1p_lexer_hack_enable_with_syntax(); }
873 WithSyntaxFormat
874 '}' {
875 $$ = $5;
876 }
877 ;
878
879WithSyntaxFormat:
880 WithSyntaxFormatToken {
881 $$ = asn1p_wsyntx_new();
882 TQ_ADD(&($$->chunks), $1, next);
883 }
884 | WithSyntaxFormat WithSyntaxFormatToken {
885 $$ = $1;
886 TQ_ADD(&($$->chunks), $2, next);
887 }
888 ;
889
890WithSyntaxFormatToken:
891 TOK_opaque {
892 $$ = asn1p_wsyntx_chunk_frombuf($1.buf, $1.len, 0);
893 }
894 | ClassFieldIdentifier {
895 asn1p_ref_t *ref;
896 int ret;
897 ref = asn1p_ref_new(yylineno);
898 checkmem(ref);
899 ret = asn1p_ref_add_component(ref, $1.name, $1.lex_type);
900 checkmem(ret == 0);
901 $$ = asn1p_wsyntx_chunk_fromref(ref, 0);
902 }
903 ;
904
905/*
906 * A data type member goes like this
907 * ===
908 * memb1 [0] Type1 { a(1), b(2) } (2)
909 * ===
910 * Therefore, we propose a split.
911 * ^^^^^^^^^ ^^^^^^^^^^
912 * ^TaggedIdentifier ^TypeDeclaration
913 * ^ConstrainedTypeDeclaration
914 */
915
916/*
917 * A member of a constructed data type ("a" or "b" in above example).
918 */
919DataTypeMember:
920 TaggedIdentifier ConstrainedTypeDeclaration {
921 $$ = $2;
922 assert($$->Identifier == 0);
923 $$->Identifier = $1.name;
924 $$->tag = $1.tag;
925 }
926 | ExtensionAndException {
927 $$ = $1;
928 }
929 ;
930
931ConstrainedTypeDeclaration:
932 TypeDeclaration optConstraints optMarker {
933 $$ = $1;
934 $$->constraints = $2;
935 $$->marker = $3;
936 }
937 ;
938
939ExtensionAndException:
940 TOK_ThreeDots {
941 $$ = asn1p_expr_new(asn1p_lineno);
942 checkmem($$);
943 $$->Identifier = strdup("...");
944 checkmem($$->Identifier);
945 $$->expr_type = A1TC_EXTENSIBLE;
946 $$->meta_type = AMT_TYPE;
947 }
948 | TOK_ThreeDots '!' DefinedValue {
949 $$ = asn1p_expr_new(asn1p_lineno);
950 checkmem($$);
951 $$->Identifier = strdup("...");
952 checkmem($$->Identifier);
953 $$->value = $3;
954 $$->expr_type = A1TC_EXTENSIBLE;
955 $$->meta_type = AMT_TYPE;
956 }
957 | TOK_ThreeDots '!' SignedNumber {
958 $$ = asn1p_expr_new(asn1p_lineno);
959 checkmem($$);
960 $$->Identifier = strdup("...");
961 $$->value = $3;
962 checkmem($$->Identifier);
963 $$->expr_type = A1TC_EXTENSIBLE;
964 $$->meta_type = AMT_TYPE;
965 }
966 ;
967
968TypeDeclaration:
969 BasicType {
970 $$ = $1;
971 }
972 | BasicString {
973 $$ = asn1p_expr_new(yylineno);
974 checkmem($$);
975 $$->expr_type = $1;
976 $$->meta_type = AMT_TYPE;
977 }
978 | ConstructedType {
979 $$ = $1;
980 checkmem($$);
981 assert($$->meta_type);
982 }
983 /*
984 * A parametrized assignment.
985 */
986 | TypeRefName '{' ActualParameterList '}' {
987 int ret;
988 $$ = $3;
989 assert($$->expr_type == 0);
990 assert($$->meta_type == 0);
991 assert($$->reference == 0);
992 $$->reference = asn1p_ref_new(yylineno);
993 checkmem($$->reference);
994 ret = asn1p_ref_add_component($$->reference, $1, RLT_UNKNOWN);
995 checkmem(ret == 0);
996 free($1);
997 $$->expr_type = A1TC_PARAMETRIZED;
998 $$->meta_type = AMT_TYPE;
999 }
1000 /*
1001 * A DefinedType reference.
1002 * "CLASS1.&id.&id2"
1003 * or
1004 * "Module.Type"
1005 * or
1006 * "Module.identifier"
1007 * or
1008 * "Type"
1009 */
1010 | ComplexTypeReference {
1011 $$ = asn1p_expr_new(yylineno);
1012 checkmem($$);
1013 $$->reference = $1;
1014 $$->expr_type = A1TC_REFERENCE;
1015 $$->meta_type = AMT_TYPEREF;
1016 }
1017 | TOK_INSTANCE TOK_OF ComplexTypeReference {
1018 $$ = asn1p_expr_new(yylineno);
1019 checkmem($$);
1020 $$->reference = $3;
1021 $$->expr_type = A1TC_INSTANCE;
1022 $$->meta_type = AMT_TYPE;
1023 }
1024 ;
1025
1026/*
1027 * A type name consisting of several components.
1028 * === EXAMPLE ===
1029 * === EOF ===
1030 */
1031ComplexTypeReference:
1032 TOK_typereference {
1033 int ret;
1034 $$ = asn1p_ref_new(yylineno);
1035 checkmem($$);
1036 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1037 checkmem(ret == 0);
1038 free($1);
1039 }
1040 | TOK_typereference '.' TypeRefName {
1041 int ret;
1042 $$ = asn1p_ref_new(yylineno);
1043 checkmem($$);
1044 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1045 checkmem(ret == 0);
1046 ret = asn1p_ref_add_component($$, $3, RLT_UNKNOWN);
1047 checkmem(ret == 0);
1048 free($1);
1049 }
1050 | TOK_typereference '.' Identifier {
1051 int ret;
1052 $$ = asn1p_ref_new(yylineno);
1053 checkmem($$);
1054 ret = asn1p_ref_add_component($$, $1, RLT_UNKNOWN);
1055 checkmem(ret == 0);
1056 ret = asn1p_ref_add_component($$, $3, RLT_lowercase);
1057 checkmem(ret == 0);
1058 free($1);
1059 }
1060 | ObjectClassReference {
1061 int ret;
1062 $$ = asn1p_ref_new(yylineno);
1063 checkmem($$);
1064 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1065 free($1);
1066 checkmem(ret == 0);
1067 }
1068 | ObjectClassReference '.' ComplexTypeReferenceAmpList {
1069 int ret;
1070 $$ = $3;
1071 ret = asn1p_ref_add_component($$, $1, RLT_CAPITALS);
1072 free($1);
1073 checkmem(ret == 0);
1074 /*
1075 * Move the last element infront.
1076 */
1077 {
1078 struct asn1p_ref_component_s tmp_comp;
1079 tmp_comp = $$->components[$$->comp_count-1];
1080 memmove(&$$->components[1],
1081 &$$->components[0],
1082 sizeof($$->components[0])
1083 * ($$->comp_count - 1));
1084 $$->components[0] = tmp_comp;
1085 }
1086 }
1087 ;
1088
1089ComplexTypeReferenceAmpList:
1090 ComplexTypeReferenceElement {
1091 int ret;
1092 $$ = asn1p_ref_new(yylineno);
1093 checkmem($$);
1094 ret = asn1p_ref_add_component($$, $1.name, $1.lex_type);
1095 free($1.name);
1096 checkmem(ret == 0);
1097 }
1098 | ComplexTypeReferenceAmpList '.' ComplexTypeReferenceElement {
1099 int ret;
1100 $$ = $1;
1101 ret = asn1p_ref_add_component($$, $3.name, $3.lex_type);
1102 free($3.name);
1103 checkmem(ret == 0);
1104 }
1105 ;
1106
1107ComplexTypeReferenceElement: ClassFieldName;
1108ClassFieldIdentifier: ClassFieldName;
1109
1110ClassFieldName:
1111 /* "&Type1" */
1112 TOK_typefieldreference {
1113 $$.lex_type = RLT_AmpUppercase;
1114 $$.name = $1;
1115 }
1116 /* "&id" */
1117 | TOK_valuefieldreference {
1118 $$.lex_type = RLT_Amplowercase;
1119 $$.name = $1;
1120 }
1121 ;
1122
1123
1124/*
1125 * === EXAMPLE ===
1126 * value INTEGER ::= 1
1127 * === EOF ===
1128 */
1129ValueDefinition:
1130 Identifier DefinedTypeRef TOK_PPEQ InlineOrDefinedValue {
1131 $$ = $2;
1132 assert($$->Identifier == NULL);
1133 $$->Identifier = $1;
1134 $$->meta_type = AMT_VALUE;
1135 $$->value = $4;
1136 }
1137 ;
1138
1139InlineOrDefinedValue:
1140 '{' { asn1p_lexer_hack_push_opaque_state(); }
1141 Opaque /* '}' */ {
1142 $$ = asn1p_value_frombuf($3.buf, $3.len, 0);
1143 checkmem($$);
1144 $$->type = ATV_UNPARSED;
1145 }
1146 | TOK_bstring {
1147 $$ = _convert_bitstring2binary($1, 'B');
1148 checkmem($$);
1149 }
1150 | TOK_hstring {
1151 $$ = _convert_bitstring2binary($1, 'H');
1152 checkmem($$);
1153 }
1154 | TOK_cstring {
1155 $$ = asn1p_value_frombuf($1.buf, $1.len, 0);
1156 checkmem($$);
1157 }
1158 | SignedNumber {
1159 $$ = $1;
1160 }
1161 | DefinedValue {
1162 $$ = $1;
1163 }
1164 ;
1165
1166DefinedValue:
1167 Identifier {
1168 asn1p_ref_t *ref;
1169 int ret;
1170 ref = asn1p_ref_new(yylineno);
1171 checkmem(ref);
1172 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1173 checkmem(ret == 0);
1174 $$ = asn1p_value_fromref(ref, 0);
1175 checkmem($$);
1176 free($1);
1177 }
1178 | TypeRefName '.' Identifier {
1179 asn1p_ref_t *ref;
1180 int ret;
1181 ref = asn1p_ref_new(yylineno);
1182 checkmem(ref);
1183 ret = asn1p_ref_add_component(ref, $1, RLT_UNKNOWN);
1184 checkmem(ret == 0);
1185 ret = asn1p_ref_add_component(ref, $3, RLT_lowercase);
1186 checkmem(ret == 0);
1187 $$ = asn1p_value_fromref(ref, 0);
1188 checkmem($$);
1189 free($1);
1190 free($3);
1191 }
1192 ;
1193
1194Opaque:
1195 TOK_opaque {
1196 $$.len = $1.len + 2;
1197 $$.buf = malloc($$.len + 1);
1198 checkmem($$.buf);
1199 $$.buf[0] = '{';
1200 $$.buf[1] = ' ';
1201 memcpy($$.buf + 2, $1.buf, $1.len);
1202 $$.buf[$$.len] = '\0';
1203 free($1.buf);
1204 }
1205 | Opaque TOK_opaque {
1206 int newsize = $1.len + $2.len;
1207 char *p = malloc(newsize + 1);
1208 checkmem(p);
1209 memcpy(p , $1.buf, $1.len);
1210 memcpy(p + $1.len, $2.buf, $2.len);
1211 p[newsize] = '\0';
1212 free($1.buf);
1213 free($2.buf);
1214 $$.buf = p;
1215 $$.len = newsize;
1216 }
1217 ;
1218
1219BasicTypeId:
1220 TOK_BOOLEAN { $$ = ASN_BASIC_BOOLEAN; }
1221 | TOK_NULL { $$ = ASN_BASIC_NULL; }
1222 | TOK_REAL { $$ = ASN_BASIC_REAL; }
1223 | BasicTypeId_UniverationCompatible { $$ = $1; }
1224 | TOK_OCTET TOK_STRING { $$ = ASN_BASIC_OCTET_STRING; }
1225 | TOK_OBJECT TOK_IDENTIFIER { $$ = ASN_BASIC_OBJECT_IDENTIFIER; }
1226 | TOK_RELATIVE_OID { $$ = ASN_BASIC_RELATIVE_OID; }
1227 | TOK_EXTERNAL { $$ = ASN_BASIC_EXTERNAL; }
1228 | TOK_EMBEDDED TOK_PDV { $$ = ASN_BASIC_EMBEDDED_PDV; }
1229 | TOK_CHARACTER TOK_STRING { $$ = ASN_BASIC_CHARACTER_STRING; }
1230 | TOK_UTCTime { $$ = ASN_BASIC_UTCTime; }
1231 | TOK_GeneralizedTime { $$ = ASN_BASIC_GeneralizedTime; }
1232 ;
1233
1234/*
1235 * A type identifier which may be used with "{ a(1), b(2) }" clause.
1236 */
1237BasicTypeId_UniverationCompatible:
1238 TOK_INTEGER { $$ = ASN_BASIC_INTEGER; }
1239 | TOK_ENUMERATED { $$ = ASN_BASIC_ENUMERATED; }
1240 | TOK_BIT TOK_STRING { $$ = ASN_BASIC_BIT_STRING; }
1241 ;
1242
1243BasicType:
1244 BasicTypeId {
1245 $$ = asn1p_expr_new(asn1p_lineno);
1246 checkmem($$);
1247 $$->expr_type = $1;
1248 $$->meta_type = AMT_TYPE;
1249 }
1250 | BasicTypeId_UniverationCompatible UniverationDefinition {
1251 if($2) {
1252 $$ = $2;
1253 } else {
1254 $$ = asn1p_expr_new(asn1p_lineno);
1255 checkmem($$);
1256 }
1257 $$->expr_type = $1;
1258 $$->meta_type = AMT_TYPE;
1259 }
1260 ;
1261
1262BasicString:
1263 TOK_BMPString { $$ = ASN_STRING_BMPString; }
1264 | TOK_GeneralString {
1265 $$ = ASN_STRING_GeneralString;
1266 return yyerror("GeneralString is not supported");
1267 }
1268 | TOK_GraphicString {
1269 $$ = ASN_STRING_GraphicString;
1270 return yyerror("GraphicString is not supported");
1271 }
1272 | TOK_IA5String { $$ = ASN_STRING_IA5String; }
1273 | TOK_ISO646String { $$ = ASN_STRING_ISO646String; }
1274 | TOK_NumericString { $$ = ASN_STRING_NumericString; }
1275 | TOK_PrintableString { $$ = ASN_STRING_PrintableString; }
1276 | TOK_T61String {
1277 $$ = ASN_STRING_T61String;
1278 return yyerror("T61String not implemented yet");
1279 }
1280 | TOK_TeletexString { $$ = ASN_STRING_TeletexString; }
1281 | TOK_UniversalString { $$ = ASN_STRING_UniversalString; }
1282 | TOK_UTF8String { $$ = ASN_STRING_UTF8String; }
1283 | TOK_VideotexString {
1284 $$ = ASN_STRING_VideotexString;
1285 return yyerror("VideotexString is no longer supported");
1286 }
1287 | TOK_VisibleString { $$ = ASN_STRING_VisibleString; }
1288 | TOK_ObjectDescriptor { $$ = ASN_STRING_ObjectDescriptor; }
1289 ;
1290
1291ConstructedType:
1292 TOK_CHOICE '{' ConstructedDataTypeDefinition '}' {
1293 $$ = $3;
1294 assert($$->expr_type == A1TC_INVALID);
1295 $$->expr_type = ASN_CONSTR_CHOICE;
1296 $$->meta_type = AMT_TYPE;
1297 }
1298 | TOK_SEQUENCE '{' ConstructedDataTypeDefinition '}' {
1299 $$ = $3;
1300 assert($$->expr_type == A1TC_INVALID);
1301 $$->expr_type = ASN_CONSTR_SEQUENCE;
1302 $$->meta_type = AMT_TYPE;
1303 }
1304 | TOK_SET '{' ConstructedDataTypeDefinition '}' {
1305 $$ = $3;
1306 assert($$->expr_type == A1TC_INVALID);
1307 $$->expr_type = ASN_CONSTR_SET;
1308 $$->meta_type = AMT_TYPE;
1309 }
1310 | TOK_SEQUENCE optConstraints TOK_OF TypeDeclaration {
1311 $$ = asn1p_expr_new(asn1p_lineno);
1312 checkmem($$);
1313 $$->constraints = $2;
1314 $$->expr_type = ASN_CONSTR_SEQUENCE_OF;
1315 $$->meta_type = AMT_TYPE;
1316 TQ_ADD(&($$->members), $4, next);
1317 }
1318 | TOK_SET optConstraints TOK_OF TypeDeclaration {
1319 $$ = asn1p_expr_new(asn1p_lineno);
1320 checkmem($$);
1321 $$->constraints = $2;
1322 $$->expr_type = ASN_CONSTR_SET_OF;
1323 $$->meta_type = AMT_TYPE;
1324 TQ_ADD(&($$->members), $4, next);
1325 }
1326 | TOK_ANY {
1327 $$ = asn1p_expr_new(asn1p_lineno);
1328 checkmem($$);
1329 $$->expr_type = ASN_CONSTR_ANY;
1330 $$->meta_type = AMT_TYPE;
1331 }
1332 | TOK_ANY TOK_DEFINED TOK_BY Identifier {
1333 int ret;
1334 $$ = asn1p_expr_new(asn1p_lineno);
1335 checkmem($$);
1336 $$->reference = asn1p_ref_new(yylineno);
1337 ret = asn1p_ref_add_component($$->reference,
1338 $4, RLT_lowercase);
1339 checkmem(ret == 0);
1340 $$->expr_type = ASN_CONSTR_ANY;
1341 $$->meta_type = AMT_TYPE;
1342 }
1343 ;
1344
1345/*
1346 * Data type constraints.
1347 */
1348optConstraints:
1349 { $$ = 0; }
1350 | Constraints { $$ = $1; }
1351 ;
1352
1353Union: '|' | TOK_UNION;
1354Intersection: '^' | TOK_INTERSECTION;
1355Except: TOK_EXCEPT;
1356
1357Constraints:
1358 TOK_SIZE '(' ConstraintElementSet ')' {
1359 /*
1360 * This is a special case, for compatibility purposes.
1361 * It goes without parenthesis.
1362 */
1363 int ret;
1364 $$ = asn1p_constraint_new(yylineno);
1365 checkmem($$);
1366 $$->type = ACT_CT_SIZE;
1367 ret = asn1p_constraint_insert($$, $3);
1368 checkmem(ret == 0);
1369 }
1370 | SingleConstraint {
1371 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, 0);
1372 }
1373 | Constraints SingleConstraint {
1374 CONSTRAINT_INSERT($$, ACT_CA_SET, $1, $2);
1375 }
1376 ;
1377
1378SingleConstraint:
1379 '(' ConstraintElementSet ')' {
1380 $$ = $2;
1381 }
1382 ;
1383
1384ConstraintElementSet:
1385 ConstraintElement {
1386 $$ = $1;
1387 }
1388 | ConstraintElement ',' TOK_ThreeDots {
1389 asn1p_constraint_t *ct;
1390 ct = asn1p_constraint_new(yylineno);
1391 checkmem(ct);
1392 ct->type = ACT_EL_EXT;
1393 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1394 }
1395 | ConstraintElement ',' TOK_ThreeDots ',' ConstraintElement {
1396 asn1p_constraint_t *ct;
1397 ct = asn1p_constraint_new(yylineno);
1398 checkmem(ct);
1399 ct->type = ACT_EL_EXT;
1400 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1401 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, $5);
1402 }
1403 | TOK_ThreeDots {
1404 $$ = asn1p_constraint_new(yylineno);
1405 checkmem($$);
1406 $$->type = ACT_EL_EXT;
1407 }
1408 | TOK_ThreeDots ',' ConstraintElement {
1409 asn1p_constraint_t *ct;
1410 ct = asn1p_constraint_new(yylineno);
1411 checkmem(ct);
1412 ct->type = ACT_EL_EXT;
1413 CONSTRAINT_INSERT($$, ACT_CA_CSV, ct, $3);
1414 }
1415 ;
1416
1417ConstraintElement: ConstraintElementUnion { $$ = $1; } ;
1418
1419ConstraintElementUnion:
1420 ConstraintElementIntersection { $$ = $1; }
1421 | ConstraintElementUnion Union ConstraintElementIntersection {
1422 CONSTRAINT_INSERT($$, ACT_CA_UNI, $1, $3);
1423 }
1424 ;
1425
1426ConstraintElementIntersection:
1427 ConstraintElementException { $$ = $1; }
1428 | ConstraintElementIntersection Intersection
1429 ConstraintElementException {
1430 CONSTRAINT_INSERT($$, ACT_CA_INT, $1, $3);
1431 }
1432 ;
1433
1434ConstraintElementException:
1435 ConstraintSubtypeElement { $$ = $1; }
1436 | ConstraintElementException Except ConstraintSubtypeElement {
1437 CONSTRAINT_INSERT($$, ACT_CA_EXC, $1, $3);
1438 }
1439 ;
1440
1441ConstraintSubtypeElement:
1442 ConstraintValue {
1443 $$ = asn1p_constraint_new(yylineno);
1444 checkmem($$);
1445 $$->type = ACT_EL_VALUE;
1446 $$->value = $1;
1447 }
1448 | ConstraintValue ConstraintRangeSpec ConstraintValue {
1449 $$ = asn1p_constraint_new(yylineno);
1450 checkmem($$);
1451 $$->type = $2;
1452 $$->range_start = $1;
1453 $$->range_stop = $3;
1454 }
1455 | ConstraintSpec '(' ConstraintElementSet ')' {
1456 int ret;
1457 $$ = asn1p_constraint_new(yylineno);
1458 checkmem($$);
1459 $$->type = $1;
1460 ret = asn1p_constraint_insert($$, $3);
1461 checkmem(ret == 0);
1462 }
1463 | TableConstraint {
1464 $$ = $1;
1465 }
1466 | WithComponents {
1467 $$ = $1;
1468 }
1469 ;
1470
1471ConstraintRangeSpec:
1472 TOK_TwoDots { $$ = ACT_EL_RANGE; }
1473 | TOK_TwoDots '<' { $$ = ACT_EL_RLRANGE; }
1474 | '<' TOK_TwoDots { $$ = ACT_EL_LLRANGE; }
1475 | '<' TOK_TwoDots '<' { $$ = ACT_EL_ULRANGE; }
1476 ;
1477
1478ConstraintSpec:
1479 TOK_SIZE {
1480 $$ = ACT_CT_SIZE;
1481 }
1482 | TOK_FROM {
1483 $$ = ACT_CT_FROM;
1484 }
1485 ;
1486
1487ConstraintValue:
1488 SignedNumber {
1489 $$ = $1;
1490 }
1491 | Identifier {
1492 asn1p_ref_t *ref;
1493 int ret;
1494 ref = asn1p_ref_new(yylineno);
1495 checkmem(ref);
1496 ret = asn1p_ref_add_component(ref, $1, RLT_lowercase);
1497 checkmem(ret == 0);
1498 $$ = asn1p_value_fromref(ref, 0);
1499 checkmem($$);
1500 free($1);
1501 }
1502 | TOK_cstring {
1503 $$ = asn1p_value_frombuf($1.buf, $1.len, 0);
1504 checkmem($$);
1505 }
1506 | TOK_MIN {
1507 $$ = asn1p_value_fromint(123);
1508 checkmem($$);
1509 $$->type = ATV_MIN;
1510 }
1511 | TOK_MAX {
1512 $$ = asn1p_value_fromint(321);
1513 checkmem($$);
1514 $$->type = ATV_MAX;
1515 }
1516 | TOK_FALSE {
1517 $$ = asn1p_value_fromint(0);
1518 checkmem($$);
1519 $$->type = ATV_FALSE;
1520 }
1521 | TOK_TRUE {
1522 $$ = asn1p_value_fromint(1);
1523 checkmem($$);
1524 $$->type = ATV_TRUE;
1525 }
1526 ;
1527
1528WithComponents:
1529 TOK_WITH TOK_COMPONENTS '{' WithComponentsList '}' {
1530 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $4, 0);
1531 }
1532 ;
1533
1534WithComponentsList:
1535 WithComponentsElement {
1536 $$ = $1;
1537 }
1538 | WithComponentsList ',' WithComponentsElement {
1539 CONSTRAINT_INSERT($$, ACT_CT_WCOMPS, $1, $3);
1540 }
1541 ;
1542
1543WithComponentsElement:
1544 TOK_ThreeDots {
1545 $$ = asn1p_constraint_new(yylineno);
1546 checkmem($$);
1547 $$->type = ACT_EL_EXT;
1548 }
1549 | Identifier optConstraints optPresenceConstraint {
1550 $$ = asn1p_constraint_new(yylineno);
1551 checkmem($$);
1552 $$->type = ACT_EL_VALUE;
1553 $$->value = asn1p_value_frombuf($1, strlen($1), 0);
1554 $$->presence = $3;
1555 }
1556 ;
1557
1558/*
1559 * presence constraint for WithComponents
1560 */
1561optPresenceConstraint:
1562 { $$ = ACPRES_DEFAULT; }
1563 | PresenceConstraint { $$ = $1; }
1564 ;
1565
1566PresenceConstraint:
1567 TOK_PRESENT {
1568 $$ = ACPRES_PRESENT;
1569 }
1570 | TOK_ABSENT {
1571 $$ = ACPRES_ABSENT;
1572 }
1573 | TOK_OPTIONAL {
1574 $$ = ACPRES_OPTIONAL;
1575 }
1576 ;
1577
1578TableConstraint:
1579 SimpleTableConstraint {
1580 $$ = $1;
1581 }
1582 | ComponentRelationConstraint {
1583 $$ = $1;
1584 }
1585 ;
1586
1587/*
1588 * "{ExtensionSet}"
1589 */
1590SimpleTableConstraint:
1591 '{' TypeRefName '}' {
1592 asn1p_ref_t *ref = asn1p_ref_new(yylineno);
1593 asn1p_constraint_t *ct;
1594 int ret;
1595 ret = asn1p_ref_add_component(ref, $2, 0);
1596 checkmem(ret == 0);
1597 ct = asn1p_constraint_new(yylineno);
1598 checkmem($$);
1599 ct->type = ACT_EL_VALUE;
1600 ct->value = asn1p_value_fromref(ref, 0);
1601 CONSTRAINT_INSERT($$, ACT_CA_CRC, ct, 0);
1602 }
1603 ;
1604
1605ComponentRelationConstraint:
1606 SimpleTableConstraint '{' AtNotationList '}' {
1607 CONSTRAINT_INSERT($$, ACT_CA_CRC, $1, $3);
1608 }
1609 ;
1610
1611AtNotationList:
1612 AtNotationElement {
1613 $$ = asn1p_constraint_new(yylineno);
1614 checkmem($$);
1615 $$->type = ACT_EL_VALUE;
1616 $$->value = asn1p_value_fromref($1, 0);
1617 }
1618 | AtNotationList ',' AtNotationElement {
1619 asn1p_constraint_t *ct;
1620 ct = asn1p_constraint_new(yylineno);
1621 checkmem(ct);
1622 ct->type = ACT_EL_VALUE;
1623 ct->value = asn1p_value_fromref($3, 0);
1624 CONSTRAINT_INSERT($$, ACT_CA_CSV, $1, ct);
1625 }
1626 ;
1627
1628/*
1629 * @blah
1630 */
1631AtNotationElement:
1632 '@' ComponentIdList {
1633 char *p = malloc(strlen($2) + 2);
1634 int ret;
1635 *p = '@';
1636 strcpy(p + 1, $2);
1637 $$ = asn1p_ref_new(yylineno);
1638 ret = asn1p_ref_add_component($$, p, 0);
1639 checkmem(ret == 0);
1640 free(p);
1641 free($2);
1642 }
1643 | '@' '.' ComponentIdList {
1644 char *p = malloc(strlen($3) + 3);
1645 int ret;
1646 p[0] = '@';
1647 p[1] = '.';
1648 strcpy(p + 2, $3);
1649 $$ = asn1p_ref_new(yylineno);
1650 ret = asn1p_ref_add_component($$, p, 0);
1651 checkmem(ret == 0);
1652 free(p);
1653 free($3);
1654 }
1655 ;
1656
1657/* identifier "." ... */
1658ComponentIdList:
1659 Identifier {
1660 $$ = $1;
1661 }
1662 | ComponentIdList '.' Identifier {
1663 int l1 = strlen($1);
1664 int l3 = strlen($3);
1665 $$ = malloc(l1 + 1 + l3 + 1);
1666 memcpy($$, $1, l1);
1667 $$[l1] = '.';
1668 memcpy($$ + l1 + 1, $3, l3);
1669 $$[l1 + 1 + l3] = '\0';
1670 }
1671 ;
1672
1673
1674
1675/*
1676 * MARKERS
1677 */
1678
1679optMarker:
1680 { $$ = EM_NOMARK; }
1681 | Marker { $$ = $1; }
1682 ;
1683
1684Marker:
1685 TOK_OPTIONAL {
1686 $$ = EM_OPTIONAL;
1687 }
1688 | TOK_DEFAULT DefaultValue {
1689 $$ = EM_DEFAULT;
1690 /* FIXME: store DefaultValue somewhere */
1691 }
1692 ;
1693
1694DefaultValue:
1695 ConstraintValue {
1696 }
1697 | BasicTypeId {
1698 }
1699 | '{' { asn1p_lexer_hack_push_opaque_state(); } Opaque /* '}' */ {
1700 }
1701 ;
1702
1703/*
1704 * Universal enumeration definition to use in INTEGER and ENUMERATED.
1705 * === EXAMPLE ===
1706 * Gender ::= ENUMERATED { unknown(0), male(1), female(2) }
1707 * Temperature ::= INTEGER { absolute-zero(-273), freezing(0), boiling(100) }
1708 * === EOF ===
1709 */
1710/*
1711optUniverationDefinition:
1712 { $$ = 0; }
1713 | UniverationDefinition {
1714 $$ = $1;
1715 }
1716 ;
1717*/
1718
1719UniverationDefinition:
1720 '{' '}' {
1721 $$ = asn1p_expr_new(asn1p_lineno);
1722 checkmem($$);
1723 }
1724 | '{' UniverationList '}' {
1725 $$ = $2;
1726 }
1727 ;
1728
1729UniverationList:
1730 UniverationElement {
1731 $$ = asn1p_expr_new(asn1p_lineno);
1732 checkmem($$);
1733 TQ_ADD(&($$->members), $1, next);
1734 }
1735 | UniverationList ',' UniverationElement {
1736 $$ = $1;
1737 TQ_ADD(&($$->members), $3, next);
1738 }
1739 ;
1740
1741UniverationElement:
1742 Identifier {
1743 $$ = asn1p_expr_new(asn1p_lineno);
1744 checkmem($$);
1745 $$->expr_type = A1TC_UNIVERVAL;
1746 $$->meta_type = AMT_VALUE;
1747 $$->Identifier = $1;
1748 }
1749 | Identifier '(' SignedNumber ')' {
1750 $$ = asn1p_expr_new(asn1p_lineno);
1751 checkmem($$);
1752 $$->expr_type = A1TC_UNIVERVAL;
1753 $$->meta_type = AMT_VALUE;
1754 $$->Identifier = $1;
1755 $$->value = $3;
1756 }
1757 | Identifier '(' DefinedValue ')' {
1758 $$ = asn1p_expr_new(asn1p_lineno);
1759 checkmem($$);
1760 $$->expr_type = A1TC_UNIVERVAL;
1761 $$->meta_type = AMT_VALUE;
1762 $$->Identifier = $1;
1763 $$->value = $3;
1764 }
1765 | SignedNumber {
1766 $$ = asn1p_expr_new(asn1p_lineno);
1767 checkmem($$);
1768 $$->expr_type = A1TC_UNIVERVAL;
1769 $$->meta_type = AMT_VALUE;
1770 $$->value = $1;
1771 }
1772 | TOK_ThreeDots {
1773 $$ = asn1p_expr_new(asn1p_lineno);
1774 checkmem($$);
1775 $$->Identifier = strdup("...");
1776 checkmem($$->Identifier);
1777 $$->expr_type = A1TC_EXTENSIBLE;
1778 $$->meta_type = AMT_VALUE;
1779 }
1780 ;
1781
1782SignedNumber:
1783 TOK_number {
1784 $$ = asn1p_value_fromint($1);
1785 checkmem($$);
1786 }
1787 | TOK_number_negative {
1788 $$ = asn1p_value_fromint($1);
1789 checkmem($$);
1790 }
1791 ;
1792
1793/*
1794 * SEQUENCE definition.
1795 * === EXAMPLE ===
1796 * Struct1 ::= SEQUENCE {
1797 * memb1 Struct2,
1798 * memb2 SEQUENCE OF {
1799 * memb2-1 Struct 3
1800 * }
1801 * }
1802 * === EOF ===
1803 */
1804
1805
1806
1807/*
1808 * SET definition.
1809 * === EXAMPLE ===
1810 * Person ::= SET {
1811 * name [0] PrintableString (SIZE(1..20)),
1812 * country [1] PrintableString (SIZE(1..20)) DEFAULT default-country,
1813 * }
1814 * === EOF ===
1815 */
1816
1817optTag:
1818 { memset(&$$, 0, sizeof($$)); }
1819 | Tag { $$ = $1; }
1820 ;
1821
1822Tag:
1823 TOK_tag {
1824 $$ = $1;
1825 $$.tag_mode = TM_DEFAULT;
1826 }
1827 | TOK_tag TOK_IMPLICIT {
1828 $$ = $1;
1829 $$.tag_mode = TM_IMPLICIT;
1830 }
1831 | TOK_tag TOK_EXPLICIT {
1832 $$ = $1;
1833 $$.tag_mode = TM_EXPLICIT;
1834 }
1835 ;
1836
1837TypeRefName:
1838 TOK_typereference {
1839 checkmem($1);
1840 $$ = $1;
1841 }
1842 | TOK_objectclassreference {
1843 checkmem($1);
1844 $$ = $1;
1845 }
1846 ;
1847
1848ObjectClassReference:
1849 TOK_objectclassreference {
1850 checkmem($1);
1851 $$ = $1;
1852 }
1853 ;
1854
1855Identifier:
1856 TOK_identifier {
1857 checkmem($1);
1858 $$ = $1;
1859 }
1860 ;
1861
1862TaggedIdentifier:
1863 Identifier {
1864 memset(&$$, 0, sizeof($$));
1865 $$.name = $1;
1866 }
1867 | Identifier Tag {
1868 $$.name = $1;
1869 $$.tag = $2;
1870 }
1871 ;
1872
1873
1874%%
1875
1876
1877/*
1878 * Convert Xstring ('0101'B or '5'H) to the binary vector.
1879 */
1880static asn1p_value_t *
1881_convert_bitstring2binary(char *str, int base) {
1882 asn1p_value_t *val;
1883 int slen;
1884 int memlen;
1885 int baselen;
1886 int bits;
1887 uint8_t *binary_vector;
1888 uint8_t *bv_ptr;
1889 uint8_t cur_val;
1890
1891 assert(str);
1892 assert(str[0] == '\'');
1893
1894 switch(base) {
1895 case 'B':
1896 baselen = 1;
1897 break;
1898 case 'H':
1899 baselen = 4;
1900 break;
1901 default:
1902 assert(base == 'B' || base == 'H');
1903 errno = EINVAL;
1904 return NULL;
1905 }
1906
1907 slen = strlen(str);
1908 assert(str[slen - 1] == base);
1909 assert(str[slen - 2] == '\'');
1910
1911 memlen = slen / (8 / baselen); /* Conservative estimate */
1912
1913 bv_ptr = binary_vector = malloc(memlen + 1);
1914 if(bv_ptr == NULL)
1915 /* ENOMEM */
1916 return NULL;
1917
1918 cur_val = 0;
1919 bits = 0;
1920 while(*(++str) != '\'') {
1921 switch(baselen) {
1922 case 1:
1923 switch(*str) {
1924 case '1':
1925 cur_val |= 1 << (7 - (bits % 8));
1926 case '0':
1927 break;
1928 default:
1929 assert(!"_y UNREACH1");
1930 case ' ': case '\r': case '\n':
1931 continue;
1932 }
1933 break;
1934 case 4:
1935 switch(*str) {
1936 case '0': case '1': case '2': case '3': case '4':
1937 case '5': case '6': case '7': case '8': case '9':
1938 cur_val |= (*str - '0') << (4 - (bits % 8));
1939 break;
1940 case 'A': case 'B': case 'C':
1941 case 'D': case 'E': case 'F':
1942 cur_val |= ((*str - 'A') + 10)
1943 << (4 - (bits % 8));
1944 break;
1945 default:
1946 assert(!"_y UNREACH2");
1947 case ' ': case '\r': case '\n':
1948 continue;
1949 }
1950 break;
1951 }
1952
1953 bits += baselen;
1954 if((bits % 8) == 0) {
1955 *bv_ptr++ = cur_val;
1956 cur_val = 0;
1957 }
1958 }
1959
1960 *bv_ptr = cur_val;
1961 assert((bv_ptr - binary_vector) <= memlen);
1962
1963 val = asn1p_value_frombits(binary_vector, bits, 0);
1964 if(val == NULL) {
1965 free(binary_vector);
1966 }
1967
1968 return val;
1969}
1970
1971extern char *asn1p_text;
1972
1973int
1974yyerror(const char *msg) {
1975 fprintf(stderr,
1976 "ASN.1 grammar parse error "
1977 "near line %d (token \"%s\"): %s\n",
1978 asn1p_lineno, asn1p_text, msg);
1979 return -1;
1980}
1981
1982