badness_parser/lib.rs
1//! badness-parser — the lossless CST parser, semantic model, and
2//! command-signature database behind [badness](https://badness.dev/), for
3//! LaTeX (`.tex`, `.sty`/`.cls`, `.dtx`, `.ins`) and BibTeX (`.bib`).
4//!
5//! The parser treats input as generic TeX surface syntax and always produces a
6//! lossless rowan tree: `reconstruct(text) == text`, byte for byte. Semantics
7//! (arity, verbatim-ness, sectioning) are layered on top in [`semantic`],
8//! never inside the grammar.
9//!
10//! The optional `schema` feature derives `schemars::JsonSchema` for the
11//! project-declaration wire types used by configuration front ends.
12
13#![deny(clippy::debug_assert_with_mut_call)]
14
15macro_rules! impl_rowan_lang {
16 ($language:ty, $kind:ty, $name:literal) => {
17 const _: () = assert!(
18 <$kind>::ROOT as u16 + 1 == <$kind>::__LAST as u16,
19 "ROOT must be the final syntax kind"
20 );
21
22 impl From<$kind> for rowan::SyntaxKind {
23 fn from(kind: $kind) -> Self {
24 Self(kind as u16)
25 }
26 }
27
28 impl rowan::Language for $language {
29 type Kind = $kind;
30
31 fn kind_from_raw(raw: rowan::SyntaxKind) -> $kind {
32 assert!(
33 raw.0 <= <$kind>::ROOT as u16,
34 "invalid {} SyntaxKind discriminant: {}",
35 $name,
36 raw.0
37 );
38 // SAFETY: the kind is a contiguous `#[repr(u16)]` enum from zero
39 // through `ROOT`, and the assertion bounds the raw value to it.
40 unsafe { std::mem::transmute::<u16, $kind>(raw.0) }
41 }
42
43 fn kind_to_raw(kind: $kind) -> rowan::SyntaxKind {
44 kind.into()
45 }
46 }
47 };
48}
49
50pub mod ast;
51pub mod bib;
52pub mod declarations;
53pub mod directives;
54mod error;
55pub mod parser;
56pub mod semantic;
57pub mod syntax;
58
59pub use error::SyntaxError;
60
61// Re-export rowan so embedders can name the exact tree types this crate is
62// built against without pinning a matching rowan version themselves.
63pub use rowan;