Skip to main content

oxilean_parse/pattern/
types.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4
5use crate::{Pattern, SurfaceExpr};
6
7/// A pattern matrix row (for exhaustiveness checking).
8#[allow(dead_code)]
9#[allow(missing_docs)]
10#[derive(Debug, Clone)]
11pub struct PatternMatrixRow {
12    /// Patterns in this row (one per column)
13    pub patterns: Vec<String>,
14    /// Body of this arm
15    pub body: String,
16}
17impl PatternMatrixRow {
18    /// Create a new row.
19    #[allow(dead_code)]
20    pub fn new(patterns: Vec<&str>, body: &str) -> Self {
21        PatternMatrixRow {
22            patterns: patterns.into_iter().map(|s| s.to_string()).collect(),
23            body: body.to_string(),
24        }
25    }
26    /// Returns true if this row is a wildcard row (all patterns are wildcards).
27    #[allow(dead_code)]
28    pub fn is_wildcard_row(&self) -> bool {
29        self.patterns.iter().all(|p| p == "_")
30    }
31}
32/// A row in the pattern matrix.
33#[derive(Debug, Clone)]
34pub struct PatternRow {
35    /// The patterns for each column
36    pub patterns: Vec<Pattern>,
37    /// The body expression for this row
38    pub body: SurfaceExpr,
39    /// Optional guard expression
40    pub guard: Option<SurfaceExpr>,
41}
42/// Known constructors for exhaustiveness checking.
43#[derive(Debug, Clone)]
44pub struct TypeConstructors {
45    /// Name of the type
46    pub type_name: String,
47    /// Constructor information
48    pub constructors: Vec<ConstructorInfo>,
49}
50/// A pattern type tag for classification.
51#[allow(dead_code)]
52#[allow(missing_docs)]
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub enum PatternTagExt {
55    /// Wildcard
56    Wild,
57    /// Variable binding
58    Var,
59    /// Constructor
60    Ctor,
61    /// Literal
62    Lit,
63    /// Or-pattern
64    Or,
65    /// As-pattern
66    As,
67    /// Guard pattern
68    Guard,
69}
70/// A pattern match arm.
71#[allow(dead_code)]
72#[allow(missing_docs)]
73#[derive(Debug, Clone)]
74pub struct MatchArmExt {
75    /// The pattern string
76    pub pattern: String,
77    /// The body string
78    pub body: String,
79    /// Optional guard
80    pub guard: Option<String>,
81}
82impl MatchArmExt {
83    /// Create a new match arm.
84    #[allow(dead_code)]
85    pub fn new(pattern: &str, body: &str) -> Self {
86        MatchArmExt {
87            pattern: pattern.to_string(),
88            body: body.to_string(),
89            guard: None,
90        }
91    }
92    /// Add a guard.
93    #[allow(dead_code)]
94    pub fn with_guard(mut self, guard: &str) -> Self {
95        self.guard = Some(guard.to_string());
96        self
97    }
98}
99/// A branch in a case tree switch.
100#[derive(Debug, Clone, PartialEq)]
101pub struct CaseBranch {
102    /// Constructor name
103    pub ctor: String,
104    /// Number of fields for this constructor
105    pub num_fields: usize,
106    /// Subtree for this branch
107    pub subtree: CaseTree,
108}
109/// Inaccessible pattern: `.(_)` - pattern that can't be matched
110#[derive(Debug, Clone, PartialEq)]
111pub struct InaccessiblePattern {
112    /// The inaccessible pattern content
113    pub inner: Box<Pattern>,
114}
115/// As-pattern: `pat @ name`
116#[derive(Debug, Clone, PartialEq)]
117pub struct AsPattern {
118    /// The underlying pattern
119    pub pattern: Box<Pattern>,
120    /// The binding name
121    pub name: String,
122}
123/// Pattern specialization result
124#[derive(Debug, Clone, PartialEq)]
125pub struct SpecializedPattern {
126    /// The resulting patterns after specialization
127    pub patterns: Vec<Pattern>,
128    /// Whether this specialization applies
129    pub applies: bool,
130}
131/// A match clause with a pattern and a body.
132#[derive(Debug, Clone)]
133pub struct MatchClause {
134    /// The pattern to match
135    pub pattern: Pattern,
136    /// The body expression
137    pub body: SurfaceExpr,
138}
139/// A variable binding in a pattern match.
140#[allow(dead_code)]
141#[allow(missing_docs)]
142#[derive(Debug, Clone)]
143pub struct PatternBinding {
144    /// Variable name
145    pub name: String,
146    /// Position in the constructor (0-indexed)
147    pub position: usize,
148    /// Type annotation if present
149    pub ty: Option<String>,
150}
151impl PatternBinding {
152    /// Create a new binding.
153    #[allow(dead_code)]
154    pub fn new(name: &str, position: usize) -> Self {
155        PatternBinding {
156            name: name.to_string(),
157            position,
158            ty: None,
159        }
160    }
161    /// Set the type annotation.
162    #[allow(dead_code)]
163    pub fn with_type(mut self, ty: &str) -> Self {
164        self.ty = Some(ty.to_string());
165        self
166    }
167}
168/// Result of pattern compilation.
169#[derive(Debug, Clone, PartialEq)]
170pub enum CaseTree {
171    /// A leaf node: execute the body at the given index
172    Leaf {
173        /// Index into the original clause list
174        body_idx: usize,
175    },
176    /// A switch on a scrutinee column
177    Switch {
178        /// Column index of the scrutinee
179        scrutinee: usize,
180        /// Constructor branches
181        branches: Vec<CaseBranch>,
182        /// Default branch if no constructor matches
183        default: Option<Box<CaseTree>>,
184    },
185    /// Pattern match failure (non-exhaustive)
186    Failure,
187}
188/// Array/list pattern: `[x, y, z]` or `[x, ..., z]`
189#[derive(Debug, Clone, PartialEq)]
190pub struct ArrayPattern {
191    /// Elements before the rest pattern (if any)
192    pub prefix: Vec<crate::Located<Pattern>>,
193    /// Rest pattern like `..` (true if present)
194    pub rest: bool,
195    /// Elements after the rest pattern (if any)
196    pub suffix: Vec<crate::Located<Pattern>>,
197}
198/// A pattern coverage checker (simplified).
199#[allow(dead_code)]
200#[allow(missing_docs)]
201pub struct PatternCoverageExt {
202    /// Number of arms
203    pub arm_count: usize,
204    /// Whether a wildcard arm is present
205    pub has_wildcard: bool,
206}
207impl PatternCoverageExt {
208    /// Create a new coverage checker.
209    #[allow(dead_code)]
210    pub fn new() -> Self {
211        PatternCoverageExt {
212            arm_count: 0,
213            has_wildcard: false,
214        }
215    }
216    /// Add an arm.
217    #[allow(dead_code)]
218    pub fn add_arm(&mut self, tag: PatternTagExt) {
219        self.arm_count += 1;
220        if tag == PatternTagExt::Wild || tag == PatternTagExt::Var {
221            self.has_wildcard = true;
222        }
223    }
224    /// Returns true if coverage is trivially complete (has a wildcard).
225    #[allow(dead_code)]
226    pub fn is_complete(&self) -> bool {
227        self.has_wildcard
228    }
229}
230/// Information about a single constructor.
231#[derive(Debug, Clone)]
232pub struct ConstructorInfo {
233    /// Constructor name
234    pub name: String,
235    /// Number of arguments (arity)
236    pub arity: usize,
237}
238/// Pattern coverage information
239#[derive(Debug, Clone)]
240pub struct PatternCoverage {
241    /// Patterns that are covered
242    pub covered: Vec<Pattern>,
243    /// Patterns that are not covered
244    pub uncovered: Vec<Pattern>,
245}
246/// A pattern transformer that applies renaming to variable patterns.
247#[allow(dead_code)]
248#[allow(missing_docs)]
249pub struct PatternRenamer {
250    /// Map from old name to new name
251    pub renaming: std::collections::HashMap<String, String>,
252}
253impl PatternRenamer {
254    /// Create a new renamer.
255    #[allow(dead_code)]
256    pub fn new() -> Self {
257        PatternRenamer {
258            renaming: std::collections::HashMap::new(),
259        }
260    }
261    /// Add a renaming.
262    #[allow(dead_code)]
263    pub fn add(&mut self, from: &str, to: &str) {
264        self.renaming.insert(from.to_string(), to.to_string());
265    }
266    /// Rename a pattern string.
267    #[allow(dead_code)]
268    pub fn rename(&self, pattern: &str) -> String {
269        self.renaming
270            .get(pattern)
271            .cloned()
272            .unwrap_or_else(|| pattern.to_string())
273    }
274}