Skip to main content

xsd_schema/compiler/
mod.rs

1//! NFA compilation for XSD content models
2//!
3//! This module compiles XSD content model particles (sequences, choices, all-groups)
4//! into NFAs that can be used for efficient content validation.
5//!
6//! # Architecture
7//!
8//! The compiler uses Thompson's construction with composable fragments:
9//! - Each element or wildcard becomes a single-state fragment
10//! - Sequences concatenate fragments with epsilon transitions
11//! - Choices add new start/end states with epsilon branches
12//! - Repetition adds epsilon loops based on occurrence constraints
13//!
14//! # Example
15//!
16//! ```
17//! use xsd_schema::compiler::{CompileContext, FragmentBuilder, NfaTerm, fragment_to_table};
18//! use xsd_schema::{SchemaSet, NameId};
19//!
20//! // Build a simple NFA for a single element
21//! let builder = FragmentBuilder::new();
22//! let term = NfaTerm::element(NameId(1), None, None);
23//! let fragment = builder.single_term(term, None);
24//! let nfa = fragment_to_table(fragment);
25//!
26//! assert_eq!(nfa.state_count(), 2); // term state + exit state
27//! ```
28
29mod all_group;
30mod compile;
31mod error;
32mod fragment;
33mod nfa;
34#[cfg(feature = "xsd11")]
35mod open_content;
36mod particle;
37pub(crate) mod substitution;
38mod upa;
39
40pub use all_group::{
41    term_matches, term_matches_with_substitution, validate_all_group_constraints, AllGroupModel,
42    AllGroupState, AllParticle, OpenContentMode, OpenContentWildcard, TermMatchResult,
43};
44pub use compile::{
45    compile_content_model_for_upa, compile_content_model_matcher, compile_model_group,
46    compile_particle, CompileContext,
47};
48pub(crate) use compile::{
49    is_top_level_all_group, resolve_top_level_all_group_ref, validate_outer_all_group_occurs,
50};
51pub use error::{NfaCompileError, NfaCompileResult};
52pub use fragment::{fragment_to_table, FragmentBuilder, NfaFragment};
53pub use nfa::{
54    advance_states, advance_with_priority, epsilon_closure, term_matches as nfa_term_matches,
55    ActiveConfig, ActiveStates, CounterDef, CounterId, MatchInfo, NfaState, NfaTable, NfaTerm,
56    NfaTransition, StateId, TransitionKind,
57};
58pub use particle::{apply_occurs, MaxOccurs};
59pub use substitution::{
60    build_substitution_group_map, build_substitution_group_map_with_abstract,
61    validate_all_substitution_groups, SubstitutionGroupMap,
62};
63pub use upa::{check_all_group_upa, check_upa};
64
65use crate::types::complex::{OpenContentMode as TypesOpenContentMode, WildcardRef};
66
67/// Strategy for matching compiled content models.
68#[derive(Debug, Clone)]
69pub enum ContentModelMatcher {
70    /// Standard NFA-based content model.
71    Nfa(NfaTable),
72    /// All-group content model.
73    AllGroup(AllGroupModel),
74    /// NFA content model with open content wildcard.
75    WithOpenContent {
76        nfa: NfaTable,
77        mode: TypesOpenContentMode,
78        wildcard: Option<WildcardRef>,
79    },
80    /// All-group base + NFA extension (XSD 1.1 complex type extension).
81    #[cfg(feature = "xsd11")]
82    AllGroupExtension {
83        base_model: AllGroupModel,
84        extension_nfa: NfaTable,
85    },
86}
87
88#[cfg(feature = "xsd11")]
89pub use open_content::validate_all_default_open_content;