Skip to main content

parse

Function parse 

Source
pub fn parse(source: &str) -> EventList<'_>
Expand description

Parses source into the lossless event stream with the default Settings (including Limits::default()).

Collects events into an EventList: the same parser, the same events. Never fails and never panics: problems are reported in-band as EventKind::Error and EventKind::Warning events, and the stream always satisfies the event contract (C1–C6). Runs in O(n) time with no recursion.

Examples found in repository?
examples/events.rs (line 8)
5fn main() {
6    let path = std::env::args().nth(1).expect("usage: events <file>");
7    let src = std::fs::read_to_string(path).expect("readable UTF-8 file");
8    let events = oneyaml_syntax::parse(&src);
9    for e in events.sourced() {
10        let text = e.text().unwrap_or("<bad span>");
11        let span = e.span();
12        match (e.diagnostic(), e.value()) {
13            (Some(d), _) => println!("{:>5}..{:<5} {d}  {text:?}", span.start, span.end),
14            (None, Some(value)) => println!(
15                "{:>5}..{:<5} {:?} = {value:?}  {text:?}",
16                span.start,
17                span.end,
18                e.kind()
19            ),
20            (None, None) => println!(
21                "{:>5}..{:<5} {:?}  {text:?}",
22                span.start,
23                span.end,
24                e.kind()
25            ),
26        }
27    }
28    match oneyaml_syntax::check_contract(&src, &events) {
29        Ok(()) => println!("contract: ok"),
30        Err(err) => println!("contract: {err}"),
31    }
32}