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 const SYN_BUILTINS: &[&str] = &[
23 "ident",
24 "integer",
25 "string",
26 "rust_type",
27 "rust_block",
28 "lit_str",
29 "lit_int",
30 "lit_char",
31 "lit_bool",
32 "lit_float",
33 "spanned_int_lit",
34 "spanned_string_lit",
35 "spanned_float_lit",
36 "spanned_bool_lit",
37 "spanned_char_lit",
38 "outer_attrs",
39];
40
41/// Reusable pipeline: Parses, transforms, and validates the grammar.
42///
43/// This encapsulates the standard 3-step process used by all backends.
44pub fn parse_grammar(input: TokenStream) -> Result<model::GrammarDefinition> {
45 // 1. Parsing: From TokenStream to syntactic AST
46 let p_ast: parser::GrammarDefinition = syn::parse2(input)?;
47
48 // 2. Transformation: From syntactic AST to semantic model
49 let m_ast: model::GrammarDefinition = p_ast.into();
50
51 // 3. Validation: Check for semantic errors
52 validator::validate(&m_ast, SYN_BUILTINS)?;
53
54 Ok(m_ast)
55}