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//! # Features
58//! **Note:** This crate's MSRV is `1.81.0`.
59//!
60//! #### `debug_prints`
61//! Enables the `debug` module and usage of debug prints in the scanner and the parser. Do not
62//! enable if you are consuming the crate rather than working on it as this can significantly
63//! decrease performance. Output remains opt-in behind a local compile-time toggle in
64//! `src/debug.rs`.
65//!
66//! This feature does not raise the MSRV further.
67//!
68//! This feature is _not_ `no_std` compatible.
69
70#![forbid(unsafe_code)]
71#![warn(missing_docs, clippy::pedantic)]
72#![no_std]
73
74#[macro_use]
75extern crate alloc;
76
77#[cfg(feature = "debug_prints")]
78extern crate std;
79
80mod char_traits;
81#[macro_use]
82mod debug;
83pub mod input;
84mod parser;
85/// A stack-based parser implementation.
86pub mod parser_stack;
87mod scanner;
88
89pub use crate::input::{str::StrInput, BorrowedInput, BufferedInput, Input};
90pub use crate::parser::{
91 Event, EventReceiver, Parser, ParserTrait, SpannedEventReceiver, StructureStyle, Tag,
92 TryEventReceiver, TryLoadError, TrySpannedEventReceiver,
93};
94pub use crate::scanner::{
95 Comment, Marker, Placement, ScalarStyle, ScanError, Scanner, Span, Token, TokenType,
96};