nu_protocol/ast/
match_pattern.rs1use super::Expression;
2use crate::{Span, Value, VarId};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct MatchPattern {
8 pub pattern: Pattern,
9 pub guard: Option<Box<Expression>>,
10 pub span: Span,
11}
12
13impl MatchPattern {
14 pub fn variables(&self) -> Vec<VarId> {
15 self.pattern.variables()
16 }
17
18 pub fn is_wildcard(&self) -> bool {
19 self.guard.is_none() && self.pattern.is_wildcard()
20 }
21}
22
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
25pub enum Pattern {
26 Record(Vec<(String, MatchPattern)>),
28 List(Vec<MatchPattern>),
30 Expression(Box<Expression>),
34 Value(Value),
37 Variable(VarId),
39 Or(Vec<MatchPattern>),
41 Rest(VarId),
43 IgnoreRest,
45 IgnoreValue,
47 Garbage,
49}
50
51impl Pattern {
52 pub fn variables(&self) -> Vec<VarId> {
53 let mut output = vec![];
54 match self {
55 Pattern::Record(items) => {
56 for item in items {
57 output.append(&mut item.1.variables());
58 }
59 }
60 Pattern::List(items) => {
61 for item in items {
62 output.append(&mut item.variables());
63 }
64 }
65 Pattern::Variable(var_id) => output.push(*var_id),
66 Pattern::Or(patterns) => {
67 for pattern in patterns {
68 output.append(&mut pattern.variables());
69 }
70 }
71 Pattern::Rest(var_id) => output.push(*var_id),
72 Pattern::Expression(_)
73 | Pattern::Value(_)
74 | Pattern::IgnoreValue
75 | Pattern::Garbage
76 | Pattern::IgnoreRest => {}
77 }
78
79 output
80 }
81
82 pub fn is_wildcard(&self) -> bool {
83 match self {
84 Self::Variable(_) | Self::IgnoreValue => true,
85 Self::Or(match_patterns) => match_patterns.iter().any(|x| x.is_wildcard()),
86 _ => false,
87 }
88 }
89}