use crate::{Pattern, SurfaceExpr};
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct PatternMatrixRow {
pub patterns: Vec<String>,
pub body: String,
}
impl PatternMatrixRow {
#[allow(dead_code)]
pub fn new(patterns: Vec<&str>, body: &str) -> Self {
PatternMatrixRow {
patterns: patterns.into_iter().map(|s| s.to_string()).collect(),
body: body.to_string(),
}
}
#[allow(dead_code)]
pub fn is_wildcard_row(&self) -> bool {
self.patterns.iter().all(|p| p == "_")
}
}
#[derive(Debug, Clone)]
pub struct PatternRow {
pub patterns: Vec<Pattern>,
pub body: SurfaceExpr,
pub guard: Option<SurfaceExpr>,
}
#[derive(Debug, Clone)]
pub struct TypeConstructors {
pub type_name: String,
pub constructors: Vec<ConstructorInfo>,
}
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PatternTagExt {
Wild,
Var,
Ctor,
Lit,
Or,
As,
Guard,
}
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct MatchArmExt {
pub pattern: String,
pub body: String,
pub guard: Option<String>,
}
impl MatchArmExt {
#[allow(dead_code)]
pub fn new(pattern: &str, body: &str) -> Self {
MatchArmExt {
pattern: pattern.to_string(),
body: body.to_string(),
guard: None,
}
}
#[allow(dead_code)]
pub fn with_guard(mut self, guard: &str) -> Self {
self.guard = Some(guard.to_string());
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CaseBranch {
pub ctor: String,
pub num_fields: usize,
pub subtree: CaseTree,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InaccessiblePattern {
pub inner: Box<Pattern>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AsPattern {
pub pattern: Box<Pattern>,
pub name: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SpecializedPattern {
pub patterns: Vec<Pattern>,
pub applies: bool,
}
#[derive(Debug, Clone)]
pub struct MatchClause {
pub pattern: Pattern,
pub body: SurfaceExpr,
}
#[allow(dead_code)]
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct PatternBinding {
pub name: String,
pub position: usize,
pub ty: Option<String>,
}
impl PatternBinding {
#[allow(dead_code)]
pub fn new(name: &str, position: usize) -> Self {
PatternBinding {
name: name.to_string(),
position,
ty: None,
}
}
#[allow(dead_code)]
pub fn with_type(mut self, ty: &str) -> Self {
self.ty = Some(ty.to_string());
self
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum CaseTree {
Leaf {
body_idx: usize,
},
Switch {
scrutinee: usize,
branches: Vec<CaseBranch>,
default: Option<Box<CaseTree>>,
},
Failure,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ArrayPattern {
pub prefix: Vec<crate::Located<Pattern>>,
pub rest: bool,
pub suffix: Vec<crate::Located<Pattern>>,
}
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct PatternCoverageExt {
pub arm_count: usize,
pub has_wildcard: bool,
}
impl PatternCoverageExt {
#[allow(dead_code)]
pub fn new() -> Self {
PatternCoverageExt {
arm_count: 0,
has_wildcard: false,
}
}
#[allow(dead_code)]
pub fn add_arm(&mut self, tag: PatternTagExt) {
self.arm_count += 1;
if tag == PatternTagExt::Wild || tag == PatternTagExt::Var {
self.has_wildcard = true;
}
}
#[allow(dead_code)]
pub fn is_complete(&self) -> bool {
self.has_wildcard
}
}
#[derive(Debug, Clone)]
pub struct ConstructorInfo {
pub name: String,
pub arity: usize,
}
#[derive(Debug, Clone)]
pub struct PatternCoverage {
pub covered: Vec<Pattern>,
pub uncovered: Vec<Pattern>,
}
#[allow(dead_code)]
#[allow(missing_docs)]
pub struct PatternRenamer {
pub renaming: std::collections::HashMap<String, String>,
}
impl PatternRenamer {
#[allow(dead_code)]
pub fn new() -> Self {
PatternRenamer {
renaming: std::collections::HashMap::new(),
}
}
#[allow(dead_code)]
pub fn add(&mut self, from: &str, to: &str) {
self.renaming.insert(from.to_string(), to.to_string());
}
#[allow(dead_code)]
pub fn rename(&self, pattern: &str) -> String {
self.renaming
.get(pattern)
.cloned()
.unwrap_or_else(|| pattern.to_string())
}
}