Skip to main content

granit_parser/
lib.rs

1// Copyright 2015, Yuheng Chen.
2// Copyright 2023, Ethiraric.
3// See the LICENSE file at the top-level directory of this distribution.
4
5//! YAML 1.2 parser implementation in pure Rust.
6//!
7//! `granit-parser` is a low-level event parser. It reads YAML input and yields a stream of
8//! [`Event`] values paired with their source [`Span`].
9//! Comments are emitted as [`Event::Comment`]. They are presentation metadata, not YAML data
10//! nodes, so consumers building YAML value trees should ignore them.
11//!
12//! Add it to your project:
13//!
14//! ```sh
15//! cargo add granit-parser
16//! ```
17//!
18//! # Usage
19//!
20//! ```rust
21//! use granit_parser::{Event, Parser, Placement};
22//!
23//! # fn main() -> Result<(), granit_parser::ScanError> {
24//! let yaml = r#"# header
25//! items: # inline
26//!   - milk
27//!   - bread
28//! "#;
29//! let mut comments = Vec::new();
30//!
31//! for next in Parser::new_from_str(yaml) {
32//!     let (event, span) = next?;
33//!     if let Event::Comment(text, placement) = event {
34//!         comments.push((
35//!             text.into_owned(),
36//!             placement,
37//!             span.slice(yaml).unwrap().to_owned(),
38//!         ));
39//!     }
40//! }
41//!
42//! assert_eq!(
43//!     comments,
44//!     [
45//!         (" header".to_owned(), Placement::Above, "# header".to_owned()),
46//!         (" inline".to_owned(), Placement::Right, "# inline".to_owned()),
47//!     ]
48//! );
49//! # Ok(())
50//! # }
51//! ```
52//!
53//! For comment events, the companion [`Span`] covers the whole source comment, including `#` and
54//! excluding the line break. With [`Parser::new_from_str`], [`Span::slice`] returns that source
55//! comment text.
56//!
57//! # Limits
58//!
59//! To keep streaming parsing memory bounded, syntactically ambiguous collection-entry positions
60//! that require comment lookahead accept at most 32 consecutive comments before the following node
61//! is resolved. Longer runs return a [`ScanError`] instead of being buffered without bound.
62//!
63//! # Features
64//! **Note:** This crate's MSRV is `1.81.0`.
65//!
66//! #### `debug_prints`
67//! Enables the `debug` module and usage of debug prints in the scanner and the parser. Do not
68//! enable if you are consuming the crate rather than working on it as this can significantly
69//! decrease performance. Output remains opt-in behind a local compile-time toggle in
70//! `src/debug.rs`.
71//!
72//! This feature does not raise the MSRV further.
73//!
74//! This feature is _not_ `no_std` compatible.
75
76#![forbid(unsafe_code)]
77#![warn(missing_docs, clippy::pedantic)]
78#![no_std]
79
80#[macro_use]
81extern crate alloc;
82
83#[cfg(feature = "debug_prints")]
84extern crate std;
85
86mod char_traits;
87#[macro_use]
88mod debug;
89pub mod input;
90mod parser;
91/// A stack-based parser implementation.
92pub mod parser_stack;
93mod scanner;
94
95pub use crate::input::{str::StrInput, BorrowedInput, BufferedInput, Input};
96pub use crate::parser::{
97    Event, EventReceiver, Parser, ParserTrait, SpannedEventReceiver, StructureStyle, Tag,
98    TryEventReceiver, TryLoadError, TrySpannedEventReceiver,
99};
100pub use crate::scanner::{
101    Comment, Marker, Placement, ScalarStyle, ScanError, Scanner, Span, Token, TokenType,
102};