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//! #### `error_messages` (enabled by default)
67//! Provides human-readable text through [`ErrorKind`]'s `Display` implementation and
68//! [`ScanError::info`]. Disabling this feature makes both render an empty string while retaining
69//! machine-readable error kinds and source markers.
70//!
71//! #### `std`
72//! Retains the original `std::io::Error` inside [`InputIoError`] when constructed through
73//! `InputIoError::from_io` or its `From<std::io::Error>` implementation. Without this feature,
74//! [`InputIoError::from_message`] remains available for portable `no_std` error reporting.
75//!
76//! #### `debug_prints`
77//! Enables the `debug` module and usage of debug prints in the scanner and the parser. Do not
78//! enable if you are consuming the crate rather than working on it as this can significantly
79//! decrease performance. Output remains opt-in behind a local compile-time toggle in
80//! `src/debug.rs`.
81//!
82//! This feature does not raise the MSRV further.
83//!
84//! This feature enables `std` and is _not_ `no_std` compatible.
85
86#![forbid(unsafe_code)]
87#![warn(missing_docs, clippy::pedantic)]
88#![no_std]
89
90extern crate alloc;
91
92#[cfg(feature = "std")]
93extern crate std;
94
95mod char_traits;
96#[macro_use]
97mod debug;
98mod error;
99pub mod input;
100mod parser;
101mod parser_stack;
102mod scanner;
103
104pub use crate::error::{ErrorKind, InputIoError, ScanError};
105pub use crate::input::{str::StrInput, BorrowedInput, BufferedInput, FallibleBufferedInput, Input};
106pub use crate::parser::{
107    Event, EventReceiver, ParseResult, Parser, ParserTrait, SpannedEventReceiver, StructureStyle,
108    Tag, TryEventReceiver, TryLoadError, TrySpannedEventReceiver, YamlVersion,
109};
110pub use crate::parser_stack::{ParserStack, ReplayParser};
111pub use crate::scanner::{
112    Comment, Marker, Placement, ScalarStyle, Scanner, Span, Token, TokenType,
113};