granit-parser 1.2.1

A YAML parser with comment and style support, written in pure Rust
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
use granit_parser::{
    options, BufferedInput, ErrorKind, Event, Options, Parser, Placement, ScanError, Scanner,
    StrInput, Token, TokenType,
};

/// Drive the parser to completion and return the first error, if any.
fn first_error(yaml: &str, options: Options) -> Option<ScanError> {
    Parser::new_from_str_with_options(yaml, options).find_map(Result::err)
}

/// Drive an iterator-backed parser to completion and return the first error, if any.
fn first_iter_error(yaml: &str, options: Options) -> Option<ScanError> {
    Parser::new_from_iter_with_options(yaml.chars(), options).find_map(Result::err)
}

fn scanner_tokens(yaml: &str, options: Options) -> Vec<Token<'_>> {
    let tokens = Scanner::with_options(StrInput::new(yaml), options.clone())
        .collect::<Result<Vec<_>, _>>()
        .unwrap();
    let iter_tokens = Scanner::with_options(BufferedInput::new(yaml.chars()), options)
        .collect::<Result<Vec<_>, _>>()
        .unwrap();
    assert_eq!(tokens, iter_tokens, "input: {yaml:?}");
    tokens
}

#[test]
fn reserved_directive_separated_comments_are_emitted_after_parameters() {
    for (yaml, name, param, comment_text) in [
        (
            "%FUTURE option # keep this comment\n---\nvalue\n",
            "FUTURE",
            "option",
            " keep this comment",
        ),
        (
            "%FUTURE\toption\t# café 漢字\r\n---\r\nvalue\r\n",
            "FUTURE",
            "option",
            " café 漢字",
        ),
        (
            "%FUTURE#name option#value # actual comment\n---\nvalue\n",
            "FUTURE#name",
            "option#value",
            " actual comment",
        ),
    ] {
        let tokens = scanner_tokens(yaml, Options::default());
        assert_eq!(
            tokens[1].token_type(),
            &TokenType::ReservedDirective(name.into(), vec![param.into()]),
        );
        let TokenType::Comment(comment) = tokens[2].token_type() else {
            panic!("expected a comment after the directive: {yaml:?}");
        };
        assert_eq!(comment.text(), comment_text);
        assert_eq!(comment.placement(), Placement::Right);
        assert_eq!(tokens[1].span().end, tokens[2].span().start);
        let comment_source = format!("#{comment_text}");
        assert_eq!(tokens[2].span().slice(yaml), Some(comment_source.as_str()));

        let events = Parser::new_from_str(yaml)
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(
            events,
            Parser::new_from_iter(yaml.chars())
                .collect::<Result<Vec<_>, _>>()
                .unwrap(),
        );
        let comments = events
            .iter()
            .filter_map(|(event, span)| match event {
                Event::Comment(text, _) => Some((text.as_ref(), *span)),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(comments, [(comment_text, tokens[2].span())]);
        assert!(events.iter().any(|(event, span)| {
            matches!(event, Event::Scalar(value, ..) if value == "value")
                && span.slice(yaml) == Some("value")
        }));
    }
}

#[test]
fn reserved_directive_comments_do_not_consume_parameter_or_byte_limits() {
    for (directive, param_count) in [("FUTURE", 0), ("FUTURE option", 1)] {
        let yaml = format!("%{directive} #{}\n---\nvalue\n", " word".repeat(1000));
        for limits in [
            Options::default(),
            options! {
                max_reserved_directive_params: param_count,
                max_directive_bytes: directive.len(),
            },
        ] {
            for emit_comments in [true, false] {
                let mut options = limits.clone();
                options.emit_comments = emit_comments;
                let tokens = scanner_tokens(&yaml, options.clone());
                let TokenType::ReservedDirective(_, params) = tokens[1].token_type() else {
                    panic!("expected a reserved directive");
                };
                assert_eq!(params.len(), param_count);
                assert_eq!(
                    tokens
                        .iter()
                        .filter(|token| { matches!(token.token_type(), TokenType::Comment(_)) })
                        .count(),
                    usize::from(emit_comments)
                );
                assert!(first_error(&yaml, options.clone()).is_none());
                assert!(first_iter_error(&yaml, options).is_none());
            }
        }
    }
}

#[test]
fn reserved_directive_comments_do_not_hide_excess_parameters() {
    for yaml in [
        "%FUTURE option # comment\n---\nvalue\n",
        "%FUTURE option#value # comment\n---\nvalue\n",
    ] {
        for emit_comments in [true, false] {
            let options = options! {
                max_reserved_directive_params: 0,
                emit_comments: emit_comments,
            };
            for error in [
                first_error(yaml, options.clone()),
                first_iter_error(yaml, options),
            ] {
                assert_eq!(
                    error.unwrap().kind(),
                    &ErrorKind::TooManyReservedDirectiveParams { limit: 0 },
                    "input: {yaml:?}, emit_comments: {emit_comments}",
                );
            }
        }
    }
}

#[test]
fn reserved_directive_comments_at_eof_do_not_supply_a_document() {
    for (directive, params) in [("FUTURE", vec![]), ("FUTURE option", vec!["option".into()])] {
        for comment_text in ["", " café 漢字"] {
            let yaml = format!("%{directive} #{comment_text}");
            for emit_comments in [true, false] {
                let options = options! { emit_comments: emit_comments };
                let tokens = scanner_tokens(&yaml, options.clone());
                assert_eq!(
                    tokens[1].token_type(),
                    &TokenType::ReservedDirective("FUTURE".into(), params.clone()),
                );
                assert_eq!(tokens.len(), 3 + usize::from(emit_comments));
                assert_eq!(tokens.last().unwrap().token_type(), &TokenType::StreamEnd);
                if emit_comments {
                    let TokenType::Comment(comment) = tokens[2].token_type() else {
                        panic!("expected EOF comment: {yaml:?}");
                    };
                    assert_eq!(comment.text(), comment_text);
                    assert_eq!(comment.placement(), Placement::Right);
                    assert_eq!(
                        tokens[2].span().slice(&yaml),
                        Some(&yaml[1 + directive.len() + 1..])
                    );
                }
                for error in [
                    first_error(&yaml, options.clone()),
                    first_iter_error(&yaml, options),
                ] {
                    assert_eq!(error.unwrap().kind(), &ErrorKind::ExpectedDocumentStart);
                }
            }
        }
    }
}

#[test]
fn reserved_directive_comments_still_validate_control_characters_when_suppressed() {
    for character in ['\u{1}', '\u{7f}'] {
        let yaml = format!("%FUTURE option # café {character}\n---\nvalue\n");
        for emit_comments in [true, false] {
            let options = options! { emit_comments: emit_comments };
            for error in [
                first_error(&yaml, options.clone()),
                first_iter_error(&yaml, options),
            ] {
                let error = error.expect("invalid comment content must be rejected");
                assert_eq!(error.kind(), &ErrorKind::UnexpectedCharacter { character });
                assert_eq!(error.marker().line(), 1);
                assert_eq!(
                    error.marker().col(),
                    "%FUTURE option # café ".chars().count()
                );
            }
        }
    }
}

#[test]
fn reserved_directive_comment_separator_is_excluded_from_utf8_byte_limit() {
    for directive in ["FUTURE\toption#value", "FÜTURE é"] {
        let yaml = format!("%{directive}{}# comment\n---\nvalue\n", " \t".repeat(1024));
        for emit_comments in [true, false] {
            let exact = options! {
                max_directive_bytes: directive.len(),
                max_reserved_directive_params: 1,
                emit_comments: emit_comments,
            };
            let tokens = scanner_tokens(&yaml, exact.clone());
            assert!(
                matches!(tokens[1].token_type(), TokenType::ReservedDirective(_, params) if params.len() == 1)
            );
            assert!(first_error(&yaml, exact.clone()).is_none());
            assert!(first_iter_error(&yaml, exact.clone()).is_none());

            let mut too_small = exact;
            too_small.max_directive_bytes -= 1;
            for error in [
                first_error(&yaml, too_small.clone()),
                first_iter_error(&yaml, too_small),
            ] {
                assert_eq!(
                    error.unwrap().kind(),
                    &ErrorKind::DirectiveByteLimitExceeded {
                        limit: directive.len() - 1
                    },
                    "input directive: {directive:?}, emit_comments: {emit_comments}",
                );
            }
        }
    }
}

// ZYU8: Directive variants
// In YAML 1.2, a directive name is any non\u{2011}space, non\u{2011}line\u{2011}break sequence of characters
// Saphyr expects only alphabetic characters in a directive name, dot . triggers the error.
#[test]
fn yaml_zyu8_directive_variant_yaml11_null_document() {
    let yaml = "%YAML1.1\n---\n";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        match item {
            Ok((_event, _span)) => {}
            Err(e) => {
                got_err = Some(e);
                break;
            }
        }
    }
    assert!(got_err.is_none(), "Error: {}", got_err.unwrap().info());
}

#[test]
fn yaml_reserved_directive_stars() {
    let yaml = "%***\n---\n";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        if let Err(e) = item {
            got_err = Some(e);
            break;
        }
    }
    assert!(got_err.is_none(), "Error: {}", got_err.unwrap().info());
}

#[test]
fn yaml_bad_yaml_directive() {
    let yaml = "%YAML 1.1 1.2\n---\n";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        if let Err(e) = item {
            got_err = Some(e);
            break;
        }
    }
    // This should fail because "YAML" is a defined directive and it has too many parameters.
    assert!(got_err.is_some());
    assert!(got_err
        .unwrap()
        .info()
        .contains("did not find expected comment or line break"));
}

#[test]
fn yaml_reserved_directive_with_params() {
    let yaml = "%FOO bar baz\n---\n";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        if let Err(e) = item {
            got_err = Some(e);
            break;
        }
    }
    assert!(got_err.is_none(), "Error: {}", got_err.unwrap().info());
}

#[test]
fn yaml_reserved_directive_at_eof() {
    let yaml = "%FOO";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        if let Err(e) = item {
            got_err = Some(e);
            break;
        }
    }
    assert!(got_err.is_some(), "Expected an error");
    assert!(got_err
        .unwrap()
        .info()
        .contains("did not find expected <document start>"));
}

#[test]
fn yaml_reserved_directive_with_param_at_eof() {
    let yaml = "%FOO bar";
    let mut got_err: Option<ScanError> = None;

    for item in Parser::new_from_str(yaml) {
        if let Err(e) = item {
            got_err = Some(e);
            break;
        }
    }
    assert!(got_err.is_some(), "Expected an error");
    assert!(got_err
        .unwrap()
        .info()
        .contains("did not find expected <document start>"));
}

// The parser ignores reserved directives, but the scanner still has to materialize one `String`
// per parameter to build the token. Without a limit, `%X` followed by two-byte ` a` parameters
// buys an allocation and a vector slot for every two input bytes, all inside a single token and
// therefore before any event-level budget can see it.
#[test]
fn reserved_directive_params_are_capped_by_default() {
    let mut yaml = String::from("%X");
    for _ in 0..100_000 {
        yaml.push_str(" a");
    }
    yaml.push_str("\n---\n");

    let err = first_error(&yaml, Options::default()).expect("expected a limit error");
    assert!(
        err.info().contains("reserved directive exceeds"),
        "unexpected error: {}",
        err.info()
    );
}

#[test]
fn reserved_directive_param_count_limit_is_configurable() {
    let options = options! { max_reserved_directive_params: 2 };

    assert!(first_error("%X a b\n---\n", options.clone()).is_none());

    let err = first_error("%X a b c\n---\n", options).expect("expected a limit error");
    assert!(
        err.info()
            .contains("reserved directive exceeds the configured limit of 2 parameters"),
        "unexpected error: {}",
        err.info()
    );
}

// A single unbounded parameter is a weaker amplification than many small ones, but the scanner
// would still retain a `String` the size of the input for a token the parser discards.
#[test]
fn oversized_reserved_directive_param_is_rejected() {
    let mut yaml = String::from("%X ");
    yaml.push_str(&"a".repeat(100_000));
    yaml.push_str("\n---\n");

    let err = first_error(&yaml, Options::default()).expect("expected a limit error");
    assert!(
        err.info()
            .contains("directive exceeds the configured limit of 1024 bytes"),
        "unexpected error: {}",
        err.info()
    );
}

#[test]
fn oversized_directive_name_is_rejected() {
    let yaml = format!("%{}\n---\n", "A".repeat(100_000));

    let err = first_error(&yaml, Options::default()).expect("expected a limit error");
    assert!(
        err.info()
            .contains("directive exceeds the configured limit of 1024 bytes"),
        "unexpected error: {}",
        err.info()
    );
}

// The byte limit spans the whole directive, so parameters that individually fit still cannot add
// up to an unbounded token.
#[test]
fn reserved_directive_bytes_are_capped_across_params() {
    let options = options! { max_reserved_directive_params: usize::MAX };
    let mut yaml = String::from("%X");
    for _ in 0..100_000 {
        yaml.push_str(" aaaaaaaa");
    }
    yaml.push_str("\n---\n");

    let err = first_error(&yaml, options).expect("expected a limit error");
    assert!(
        err.info()
            .contains("directive exceeds the configured limit of 1024 bytes"),
        "unexpected error: {}",
        err.info()
    );
}

#[test]
fn directive_byte_limit_is_configurable_and_counts_multibyte_chars() {
    let options = options! { max_directive_bytes: 8 };

    // The name, the separating blank and three two-byte characters exactly fill the budget.
    assert!(first_error("%X ÿÿÿ\n---\n", options.clone()).is_none());

    let err = first_error("%X ÿÿÿÿ\n---\n", options).expect("expected a limit error");
    assert!(
        err.info()
            .contains("directive exceeds the configured limit of 8 bytes"),
        "unexpected error: {}",
        err.info()
    );
}

// Real directives are short; the defaults must not disturb them.
#[test]
fn ordinary_directives_are_unaffected_by_the_limits() {
    let yaml = "%YAML 1.2\n%TAG !e! tag:example.com,2000:app/\n%FOO bar baz\n---\nkey: value\n";

    assert!(first_error(yaml, Options::default()).is_none());
}

#[test]
fn tag_directive_handle_and_prefix_are_capped_for_borrowed_and_streaming_inputs() {
    let oversized = [
        format!("%TAG !{}! x\n---\n", "a".repeat(100_000)),
        format!("%TAG !e! tag:{}\n---\n", "a".repeat(100_000)),
    ];

    for yaml in &oversized {
        for error in [
            first_error(yaml, Options::default()),
            first_iter_error(yaml, Options::default()),
        ] {
            let error = error.expect("expected a directive byte-limit error");
            assert_eq!(
                error.kind(),
                &ErrorKind::DirectiveByteLimitExceeded { limit: 1024 }
            );
        }
    }
}

#[test]
fn tag_directive_byte_limit_counts_separators_and_raw_escape_bytes() {
    let yaml = "%TAG !e! %C3%BF\n---\n";
    let exact = options! { max_directive_bytes: 14 };

    assert!(first_error(yaml, exact.clone()).is_none());
    assert!(first_iter_error(yaml, exact).is_none());

    let too_small = options! { max_directive_bytes: 13 };
    for error in [
        first_error(yaml, too_small.clone()),
        first_iter_error(yaml, too_small),
    ] {
        let error = error.expect("expected a directive byte-limit error");
        assert_eq!(
            error.kind(),
            &ErrorKind::DirectiveByteLimitExceeded { limit: 13 }
        );
    }
}