1
0
dust/src/abstract_tree/match.rs

106 lines
3.2 KiB
Rust
Raw Normal View History

2023-10-06 17:32:58 +00:00
//! Pattern matching.
//!
//! Note that this module is called "match" but is escaped as "r#match" because
//! "match" is a keyword in Rust.
use serde::{Deserialize, Serialize};
2024-01-10 20:03:52 +00:00
use crate::{
AbstractTree, Error, Expression, Format, Map, Result, Statement, SyntaxNode, Type, Value,
};
2023-10-06 17:32:58 +00:00
/// Abstract representation of a match statement.
2023-10-06 17:32:58 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Match {
matcher: Expression,
options: Vec<(Expression, Statement)>,
2023-12-11 15:18:46 +00:00
fallback: Option<Box<Statement>>,
}
2023-10-06 17:32:58 +00:00
impl AbstractTree for Match {
2024-01-10 20:03:52 +00:00
fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "match", node)?;
let matcher_node = node.child(1).unwrap();
2024-01-10 20:03:52 +00:00
let matcher = Expression::from_syntax(matcher_node, source, context)?;
let mut options = Vec::new();
let mut previous_expression = None;
2023-12-11 15:18:46 +00:00
let mut next_statement_is_fallback = false;
let mut fallback = None;
for index in 2..node.child_count() {
let child = node.child(index).unwrap();
2023-12-11 15:18:46 +00:00
if child.kind() == "*" {
next_statement_is_fallback = true;
}
if child.kind() == "expression" {
2024-01-10 20:03:52 +00:00
previous_expression = Some(Expression::from_syntax(child, source, context)?);
}
if child.kind() == "statement" {
2024-01-10 20:03:52 +00:00
let statement = Statement::from_syntax(child, source, context)?;
2023-12-11 15:18:46 +00:00
if next_statement_is_fallback {
fallback = Some(Box::new(statement));
next_statement_is_fallback = false;
} else if let Some(expression) = &previous_expression {
options.push((expression.clone(), statement));
}
}
}
2023-12-11 15:18:46 +00:00
Ok(Match {
matcher,
options,
fallback,
})
2023-10-06 17:32:58 +00:00
}
fn run(&self, source: &str, context: &Map) -> Result<Value> {
let matcher_value = self.matcher.run(source, context)?;
for (expression, statement) in &self.options {
let option_value = expression.run(source, context)?;
if matcher_value == option_value {
return statement.run(source, context);
}
}
2023-12-11 15:18:46 +00:00
if let Some(fallback) = &self.fallback {
fallback.run(source, context)
} else {
2023-12-31 14:14:43 +00:00
Ok(Value::none())
2023-12-11 15:18:46 +00:00
}
2023-11-30 00:23:42 +00:00
}
fn expected_type(&self, context: &Map) -> Result<Type> {
let (_, first_statement) = self.options.first().unwrap();
first_statement.expected_type(context)
2023-10-06 17:32:58 +00:00
}
}
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
impl Format for Match {
fn format(&self, output: &mut String, indent_level: u8) {
output.push_str("match ");
self.matcher.format(output, indent_level);
output.push_str(" {");
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
for (expression, statement) in &self.options {
expression.format(output, indent_level);
output.push_str(" => ");
statement.format(output, indent_level);
2024-01-06 10:00:36 +00:00
}
2024-01-06 13:11:09 +00:00
if let Some(statement) = &self.fallback {
output.push_str("* => ");
statement.format(output, indent_level);
2024-01-06 10:00:36 +00:00
}
2024-01-06 13:11:09 +00:00
output.push('}');
2024-01-06 10:00:36 +00:00
}
}