Skip to main content

oxilean_parse/pattern/
patterncompiler_predicates.rs

1//! # PatternCompiler - predicates Methods
2//!
3//! This module contains method implementations for `PatternCompiler`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use crate::Pattern;
8
9use super::types::PatternRow;
10
11use super::patterncompiler_type::PatternCompiler;
12
13impl PatternCompiler {
14    /// Check if a pattern is irrefutable (always matches).
15    ///
16    /// Wildcards and variable bindings are irrefutable. Constructor and
17    /// literal patterns are not. An or-pattern is irrefutable if either
18    /// branch is irrefutable.
19    pub fn is_irrefutable(&self, pattern: &Pattern) -> bool {
20        match pattern {
21            Pattern::Wild | Pattern::Var(_) => true,
22            Pattern::Ctor(_, _) | Pattern::Lit(_) => false,
23            Pattern::Or(left, right) => {
24                self.is_irrefutable(&left.value) || self.is_irrefutable(&right.value)
25            }
26        }
27    }
28    /// Check if all patterns in a list are irrefutable.
29    pub(super) fn all_irrefutable(&self, patterns: &[PatternRow]) -> bool {
30        patterns
31            .iter()
32            .all(|row| row.patterns.iter().all(|p| self.is_irrefutable(p)))
33    }
34    /// Check if a set of patterns subsumes a new pattern.
35    pub(super) fn subsumes_all(&self, rows: &[PatternRow], new_pattern: &[Pattern]) -> bool {
36        for row in rows {
37            if row.patterns.len() == new_pattern.len()
38                && row
39                    .patterns
40                    .iter()
41                    .zip(new_pattern.iter())
42                    .all(|(p1, p2)| self.is_irrefutable(p1) || self.patterns_equivalent(p1, p2))
43            {
44                return true;
45            }
46        }
47        false
48    }
49}