syn_grammar_model/lib.rs
1//! # syn-grammar-model
2//!
3//! This library contains the shared logic for parsing, validating, and analyzing
4//! `syn-grammar` definitions. It is intended to be used by procedural macros
5//! that generate parsers or documentation from the grammar DSL.
6//!
7//! ## Pipeline
8//!
9//! 1. **[parser]**: Parse input tokens into a syntactic AST.
10//! 2. **[model]**: Convert the AST into a semantic model (via `Into`).
11//! 3. **[validator]**: Validate the model for semantic correctness.
12//! 4. **[analysis]**: Extract information (keywords, recursion) for code generation.
13
14use proc_macro2::TokenStream;
15use syn::Result;
16
17pub mod analysis;
18pub mod model;
19pub mod parser;
20pub mod validator;
21
22pub use model::{Backend, BuiltIn};
23pub use proc_macro2::Span;
24
25/// Reusable pipeline: Parses, transforms, and validates the grammar.
26///
27/// This encapsulates the standard 3-step process used by all backends.
28///
29/// This function uses the provided `Backend` to validate built-ins.
30pub fn parse_grammar<B: Backend>(input: TokenStream) -> Result<model::GrammarDefinition> {
31 // 1. Parsing: From TokenStream to syntactic AST
32 let p_ast: parser::GrammarDefinition = syn::parse2(input)?;
33
34 // 2. Transformation: From syntactic AST to semantic model
35 let m_ast: model::GrammarDefinition = p_ast.into();
36
37 // 3. Validation: Check for semantic errors
38 validator::validate::<B>(&m_ast)?;
39
40 Ok(m_ast)
41}