rasn-compiler 0.16.0

An ASN.1 compiler producing bindings for the rasn framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use nom::{
    branch::alt,
    bytes::complete::{is_not, tag},
    character::complete::{
        alpha1, alphanumeric1, char, i128, multispace0, multispace1, one_of, u64,
    },
    combinator::{cut, into, map, map_res, opt, peek, recognize, rest, success, value},
    multi::{many0, many1},
    sequence::{delimited, pair, preceded, terminated},
    Parser,
};

use crate::{
    input::Input,
    intermediate::{constraints::*, types::*, *},
    lexer::error::ErrorTree,
};

use super::{
    error::{MiscError, ParserResult},
    util::{map_into, opt_delimited, take_until_or, take_until_unbalanced},
};

/// Parses an ASN1 comment.
///
/// * `input` [Input]-wrapped string slice reference used as an input for the lexer
///
/// returns a `Result` yielding a tuple containing a reference to the remaining [Input]-wrapped string slice
/// and the parsed comment in case of sucess, or a parsing error if unsuccessful.
///
/// #### X680
/// _The lexical item "comment" can have two forms:_
///    * _One-line comments which begin with "--" as defined in 12.6.3;_
///    * _Multiple-line comments which begin with "/*" as defined in 12.6.4._
pub fn comment(input: Input<'_>) -> ParserResult<'_, &str> {
    skip_ws(alt((block_comment, line_comment))).parse(input)
}

pub fn line_comment(input: Input<'_>) -> ParserResult<'_, &str> {
    delimited(
        tag(LINE_COMMENT),
        alt((
            into_inner(take_until_or("\n", LINE_COMMENT)),
            into_inner(rest),
        )),
        opt(tag(LINE_COMMENT)),
    )
    .parse(input)
}

pub fn block_comment(input: Input<'_>) -> ParserResult<'_, &str> {
    delimited(
        tag(BLOCK_COMMENT_START),
        take_until_unbalanced(BLOCK_COMMENT_START, BLOCK_COMMENT_END),
        tag(BLOCK_COMMENT_END),
    )
    .parse(input)
}

/// Parses a typereference lexical item.
///
/// #### X.680 12.2  Type references
/// _A "typereference" shall consist of an arbitrary number (one or more) of letters, digits, and
/// hyphens. The initial character shall be an upper-case letter. A hyphen shall not be the last
/// character. A hyphen shall not be immediately followed by another hyphen. NOTE – The rules
/// concerning hyphen are designed to avoid ambiguity with (possibly following) comment. 12.2.2A
/// "typereference" shall not be one of the reserved character sequences listed in 12.38._
pub fn type_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
    map_res(
        recognize(pair(
            one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
            many0(alt((
                preceded(char('-'), into_inner(alphanumeric1)),
                into_inner(alphanumeric1),
            ))),
        )),
        |identifier| {
            if ASN1_KEYWORDS.contains(&identifier.inner()) {
                Err(MiscError("Identifier is ASN.1 keyword."))
            } else {
                Ok(identifier.into_inner())
            }
        },
    )
    .parse(input.clone())
}

/// Parses an ASN1 identifier.
///
/// * `input` [Input]-wrapped string slice reference used as an input for the lexer
///
/// returns a `Result` yielding a tuple containing a reference to the remaining [Input]-wrapped string slice
/// and the parsed identifier in case of sucess, or a parsing error if unsuccessful.
///
/// #### X.680
/// _12.3 An "identifier" shall consist of an arbitrary number (one or more) of letters, digits,
/// and hyphens. The initial character shall be a lower-case letter. A hyphen shall not be the
/// last character. A hyphen shall not be immediately followed by another hyphen._
pub fn identifier(input: Input<'_>) -> ParserResult<'_, &str> {
    skip_ws_and_comments(into_inner(recognize(pair(
        alpha1,
        many0(alt((
            preceded(char('-'), into_inner(alphanumeric1)),
            into_inner(alphanumeric1),
        ))),
    ))))
    .parse(input)
}

/// Parses a valuereference lexical item.
///
/// #### X.680 12.4  Value references
/// _A "valuereference" shall consist of the sequence of characters specified for an "identifier" in
/// 12.3. In analyzing an instance of use of this notation, a "valuereference" is distinguished
/// from an "identifier" by the context in which it appears._
pub fn value_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
    into_inner(recognize(pair(
        one_of("abcdefghijklmnopqrstuvwxyz"),
        many0(alt((
            preceded(char('-'), into_inner(alphanumeric1)),
            into_inner(alphanumeric1),
        ))),
    )))
    .parse(input)
}

/// Parses a modulereference lexical item.
///
/// #### X.680 12.5 Module references
/// _A "modulereference" shall consist of the sequence of characters specified for a
/// "typereference" in 12.2. In analyzing an instance of use of this notation, a "modulereference"
/// is distinguished from a "typereference" by the context in which it appears._
pub fn module_reference(input: Input<'_>) -> ParserResult<'_, &'_ str> {
    type_reference(input)
}

pub fn into_inner<'a, F>(parser: F) -> impl Parser<Input<'a>, Output = &'a str, Error = F::Error>
where
    F: Parser<Input<'a>, Output = Input<'a>>,
{
    map(parser, Input::into_inner)
}

pub fn skip_ws<'a, F>(inner: F) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>>,
{
    preceded(multispace0, inner)
}

pub fn skip_ws_and_comments<'a, F>(
    inner: F,
) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    preceded(many0(alt((comment, into_inner(multispace1)))), inner)
}

pub fn in_parentheses<'a, F>(
    inner: F,
) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    delimited(
        skip_ws_and_comments(char(LEFT_PARENTHESIS)),
        skip_ws_and_comments(inner),
        skip_ws_and_comments(char(RIGHT_PARENTHESIS)),
    )
}

pub fn in_brackets<'a, F>(inner: F) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    delimited(
        skip_ws_and_comments(char(LEFT_BRACKET)),
        skip_ws_and_comments(inner),
        skip_ws_and_comments(char(RIGHT_BRACKET)),
    )
}

pub fn in_version_brackets<'a, F>(
    inner: F,
) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    delimited(
        skip_ws_and_comments(tag("[[")),
        skip_ws_and_comments(inner),
        skip_ws_and_comments(tag("]]")),
    )
}

pub fn opt_parentheses<'a, F>(
    inner: F,
) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    opt_delimited(
        skip_ws_and_comments(char(LEFT_BRACKET)),
        skip_ws_and_comments(inner),
        skip_ws_and_comments(char(RIGHT_BRACKET)),
    )
}

pub fn in_braces<'a, F>(inner: F) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
{
    delimited(
        skip_ws_and_comments(char(LEFT_BRACE)),
        skip_ws_and_comments(inner),
        skip_ws_and_comments(char(RIGHT_BRACE)),
    )
}

pub fn all_value(input: Input<'_>) -> ParserResult<'_, ASN1Value> {
    value(ASN1Value::All, skip_ws_and_comments(tag(ALL))).parse(input)
}

pub fn asn_tag(input: Input<'_>) -> ParserResult<'_, AsnTag> {
    into(pair(
        in_brackets(pair(
            opt(into_inner(skip_ws_and_comments(alt((
                tag(PRIVATE),
                tag(APPLICATION),
                tag(UNIVERSAL),
            ))))),
            skip_ws_and_comments(u64),
        )),
        skip_ws_and_comments(opt(alt((
            value(TaggingEnvironment::Explicit, tag(EXPLICIT)),
            value(TaggingEnvironment::Implicit, tag(IMPLICIT)),
        )))),
    ))
    .parse(input)
}

pub fn range_seperator(input: Input<'_>) -> ParserResult<'_, RangeSeperator> {
    skip_ws_and_comments(tag(RANGE))
        .map(|_| RangeSeperator())
        .parse(input)
}

pub fn extension_marker(input: Input<'_>) -> ParserResult<'_, ExtensionMarker> {
    skip_ws_and_comments(tag(ELLIPSIS))
        .map(|_| ExtensionMarker())
        .parse(input)
}

pub fn assignment(input: Input<'_>) -> ParserResult<'_, &str> {
    into_inner(skip_ws_and_comments(tag(ASSIGN))).parse(input)
}

pub fn distinguished_values(input: Input<'_>) -> ParserResult<'_, Vec<DistinguishedValue>> {
    delimited(
        skip_ws_and_comments(char(LEFT_BRACE)),
        many0(terminated(
            skip_ws_and_comments(distinguished_val),
            opt(skip_ws_and_comments(char(COMMA))),
        )),
        skip_ws_and_comments(char(RIGHT_BRACE)),
    )
    .parse(input)
}

pub fn distinguished_val(input: Input<'_>) -> ParserResult<'_, DistinguishedValue> {
    map_into(pair(skip_ws_and_comments(identifier), in_parentheses(i128))).parse(input)
}

pub fn optional_comma(input: Input<'_>) -> ParserResult<'_, Option<char>> {
    skip_ws_and_comments(opt(char(COMMA))).parse(input)
}

pub fn uppercase_identifier(input: Input<'_>) -> ParserResult<'_, &str> {
    alt((
        into_inner(recognize(pair(
            one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
            many1(alt((
                preceded(char('-'), one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")),
                one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"),
            ))),
        ))),
        terminated(
            into_inner(recognize(one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))),
            peek(is_not("abcdefghijklmnopqrstuvwxyz-")),
        ),
    ))
    .parse(input)
}

/// Parse the "optionality" of a field.
///
/// When optionality is DEFAULT, the argument `f` is used to parse the value.
///
/// # Syntax
/// ```text
/// OptionalitySpec ::= OPTIONAL | DEFAULT <f> | empty
/// ```
pub fn optionality<'a, F>(
    f: F,
) -> impl Parser<Input<'a>, Output = Optionality<F::Output>, Error = F::Error>
where
    F: Parser<Input<'a>, Error = ErrorTree<'a>>,
    F::Output: Clone,
{
    alt((
        value(Optionality::Optional, tag(OPTIONAL)),
        preceded(
            tag(DEFAULT),
            cut(skip_ws_and_comments(f.map(Optionality::Default))),
        ),
        success(Optionality::Required),
    ))
}

#[cfg(test)]
mod tests {

    use crate::lexer::asn1_value;

    use super::*;

    #[test]
    fn parses_line_comment() {
        let line = r#"-- Test, one, two, three/
"#
        .into();
        assert_eq!(" Test, one, two, three/", comment(line).unwrap().1);
    }

    #[test]
    fn parses_block_comment() {
        assert_eq!(
            r#" Test, one, two, three
and one "#,
            comment(
                r#"/* Test, one, two, three
and one */"#
                    .into()
            )
            .unwrap()
            .1
        );
        assert_eq!(
            r#"*
       * Hello
       "#,
            comment(
                r#"/**
       * Hello
       */"#
                .into()
            )
            .unwrap()
            .1
        );
        assert_eq!(
            " Very annoying! ",
            comment("-- Very annoying! --".into()).unwrap().1
        )
    }

    #[test]
    fn parses_multiline_block_comment() {
        assert_eq!(comment(r#"/**
      * This DE indicates a change of acceleration.
      *
      * The value shall be set to:
      * - 0 - `accelerate` - if the magnitude of the horizontal velocity vector increases.
      * - 1 - `decelerate` - if the magnitude of the horizontal velocity vector decreases.
      *
      * @category: Kinematic information
      * @revision: Created in V2.1.1
     */
    StartOfDeclaration"#.into()).unwrap().1,
      "*\n      * This DE indicates a change of acceleration.\n      *\n      * The value shall be set to:\n      * - 0 - `accelerate` - if the magnitude of the horizontal velocity vector increases.\n      * - 1 - `decelerate` - if the magnitude of the horizontal velocity vector decreases.\n      *\n      * @category: Kinematic information\n      * @revision: Created in V2.1.1\n     "
    )
    }

    /// 12.6.4. Whenever a "comment" begins with "/*", it shall end with a corresponding "*/",
    /// whether this "*/" is on the same line or not. If another "/*" is found before a "*/",
    /// then the comment terminates when a matching "*/" has been found for each "/*".
    #[test]
    fn parses_block_comment_with_nested_comments() {
        assert_eq!(
            comment(
                r#"/*this is a comment /*
        this is a nested comment */ this text should be parsed*/"#
                    .into()
            )
            .unwrap()
            .1,
            "this is a comment /*
        this is a nested comment */ this text should be parsed"
        )
    }

    #[test]
    fn parses_ambiguous_asn1_comment() {
        assert_eq!(
            comment(
                r#" -- This means backward
      unavailable"#
                    .into()
            )
            .map(|(i, o)| (i.into_inner(), o)),
            Ok(("\n      unavailable", " This means backward",),)
        );
        assert_eq!(
            comment(
                r#"-- This means forward
        backward    (2), -- This means backward"#
                    .into()
            )
            .map(|(i, o)| (i.into_inner(), o)),
            Ok((
                "\n        backward    (2), -- This means backward",
                " This means forward",
            ),)
        )
    }

    #[test]
    fn parses_valid_identifiers() {
        assert_eq!(identifier("EEE-DDD".into()).unwrap().1, "EEE-DDD");
        assert_eq!(identifier("GenericLane ".into()).unwrap().1, "GenericLane");
        assert_eq!(identifier("regional ".into()).unwrap().1, "regional");
        assert_eq!(identifier("NodeXY64".into()).unwrap().1, "NodeXY64");
        assert_eq!(
            identifier("Sub-Cause-Code  ".into()).unwrap().1,
            "Sub-Cause-Code"
        );
    }

    #[test]
    fn handles_invalid_identifiers() {
        assert_eq!(
            identifier("EEE--DDD".into())
                .map(|(i, o)| (i.into_inner(), o))
                .unwrap(),
            ("--DDD", "EEE")
        );
        assert!(identifier("-GenericLane".into()).is_err());
        assert!(identifier("64NodeXY".into()).is_err());
        assert!(identifier("&regional".into()).is_err());
        assert_eq!(
            identifier("Sub-Cause-Code-".into())
                .map(|(i, o)| (i.into_inner(), o))
                .unwrap(),
            ("-", "Sub-Cause-Code")
        );
    }

    #[test]
    fn discards_whitespace() {
        assert_eq!(
            skip_ws(identifier).parse(" EEE-DDD".into()).unwrap().1,
            "EEE-DDD"
        );
        assert_eq!(
            skip_ws(identifier)
                .parse("\nGenericLane ".into())
                .unwrap()
                .1,
            "GenericLane"
        );
        assert_eq!(
            skip_ws(identifier).parse("\t regional ".into()).unwrap().1,
            "regional"
        );
        assert_eq!(
            skip_ws(identifier).parse("   NodeXY64".into()).unwrap().1,
            "NodeXY64"
        );
        assert_eq!(
            skip_ws(identifier)
                .parse("\r\n\nSub-Cause-Code  ".into())
                .unwrap()
                .1,
            "Sub-Cause-Code"
        );
    }

    #[test]
    fn discards_whitespace_and_comments() {
        assert_eq!(
            skip_ws_and_comments(identifier)
                .parse(" -- comment --EEE-DDD".into())
                .unwrap()
                .1,
            "EEE-DDD"
        );
    }

    #[test]
    fn parses_distinguished_values() {
        let sample = r#"{
    positiveOutOfRange (160),
    unavailable        (161)
}"#
        .into();
        assert_eq!(
            distinguished_values(sample).unwrap().1,
            [
                DistinguishedValue {
                    name: "positiveOutOfRange".into(),
                    value: 160,
                },
                DistinguishedValue {
                    name: "unavailable".into(),
                    value: 161,
                },
            ]
        )
    }

    #[test]
    fn parses_distinguished_values_with_line_comments() {
        let sample = r#"{
    negativeOutOfRange (159), -- ignore this comment
    positiveOutOfRange (160), -- ignore this comment, too
    unavailable        (161)
}"#
        .into();
        assert_eq!(
            distinguished_values(sample).unwrap().1,
            [
                DistinguishedValue {
                    name: "negativeOutOfRange".into(),
                    value: 159,
                },
                DistinguishedValue {
                    name: "positiveOutOfRange".into(),
                    value: 160,
                },
                DistinguishedValue {
                    name: "unavailable".into(),
                    value: 161,
                },
            ]
        )
    }

    #[test]
    fn parses_eof_line_comment() {
        assert_eq!(
            line_comment(r#"-- LdapSystemSchema"#.into()).unwrap().1,
            " LdapSystemSchema"
        )
    }

    #[test]
    fn parses_default_int() {
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT\t-1".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::Integer(-1))
        );
    }

    #[test]
    fn parses_default_boolean() {
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT   TRUE".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::Boolean(true))
        );
    }

    #[test]
    fn parses_default_bitstring() {
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT '001010011'B".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::BitString(vec![
                false, false, true, false, true, false, false, true, true
            ]))
        );
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT 'F60E'H".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::BitString(vec![
                true, true, true, true, false, true, true, false, false, false, false, false, true,
                true, true, false
            ]))
        );
    }

    #[test]
    fn parses_default_enumeral() {
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT enumeral1".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::ElsewhereDeclaredValue {
                module: None,
                identifier: "enumeral1".into(),
                parent: None
            })
        );
        assert_eq!(
            optionality(asn1_value)
                .parse("DEFAULT enumeral1".into())
                .unwrap()
                .1,
            Optionality::Default(ASN1Value::ElsewhereDeclaredValue {
                module: None,
                identifier: "enumeral1".into(),
                parent: None
            })
        );
    }
}