dust/src/abstract_tree/index_expression.rs

96 lines
3.8 KiB
Rust
Raw Normal View History

use serde::{Deserialize, Serialize};
use crate::{
2024-01-31 18:51:48 +00:00
error::{RuntimeError, SyntaxError, ValidationError},
value_node::ValueNode,
2024-02-11 00:31:47 +00:00
AbstractTree, Context, Format, FunctionCall, Identifier, Index, SyntaxNode, Type, Value,
};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum IndexExpression {
Value(ValueNode),
Identifier(Identifier),
Index(Box<Index>),
FunctionCall(Box<FunctionCall>),
}
impl AbstractTree for IndexExpression {
2024-02-11 00:31:47 +00:00
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
2024-02-01 00:35:27 +00:00
SyntaxError::expect_syntax_node(source, "index_expression", node)?;
let first_child = node.child(0).unwrap();
let child = if first_child.is_named() {
first_child
} else {
node.child(1).unwrap()
};
let abstract_node = match child.kind() {
2024-01-10 20:03:52 +00:00
"value" => IndexExpression::Value(ValueNode::from_syntax(child, source, context)?),
"identifier" => {
2024-01-10 20:03:52 +00:00
IndexExpression::Identifier(Identifier::from_syntax(child, source, context)?)
}
"index" => {
2024-01-10 20:03:52 +00:00
IndexExpression::Index(Box::new(Index::from_syntax(child, source, context)?))
}
2024-01-10 20:03:52 +00:00
"function_call" => IndexExpression::FunctionCall(Box::new(FunctionCall::from_syntax(
child, source, context,
)?)),
_ => {
2024-02-01 00:07:18 +00:00
return Err(SyntaxError::UnexpectedSyntaxNode {
expected: "value, identifier, index or function call".to_string(),
actual: child.kind().to_string(),
location: child.start_position(),
relevant_source: source[child.byte_range()].to_string(),
})
}
};
Ok(abstract_node)
}
2024-02-11 00:31:47 +00:00
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
match self {
IndexExpression::Value(value_node) => value_node.expected_type(context),
IndexExpression::Identifier(identifier) => identifier.expected_type(context),
IndexExpression::Index(index) => index.expected_type(context),
IndexExpression::FunctionCall(function_call) => function_call.expected_type(context),
}
}
2024-01-31 18:51:48 +00:00
2024-02-11 00:31:47 +00:00
fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> {
2024-01-31 18:51:48 +00:00
match self {
2024-02-01 01:52:34 +00:00
IndexExpression::Value(value_node) => value_node.validate(_source, _context),
IndexExpression::Identifier(identifier) => identifier.validate(_source, _context),
IndexExpression::Index(index) => index.validate(_source, _context),
2024-01-31 18:51:48 +00:00
IndexExpression::FunctionCall(function_call) => {
2024-02-01 01:52:34 +00:00
function_call.validate(_source, _context)
2024-01-31 18:51:48 +00:00
}
}
}
2024-02-11 00:31:47 +00:00
fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
2024-01-31 18:51:48 +00:00
match self {
IndexExpression::Value(value_node) => value_node.run(source, context),
IndexExpression::Identifier(identifier) => identifier.run(source, context),
IndexExpression::Index(index) => index.run(source, context),
IndexExpression::FunctionCall(function_call) => function_call.run(source, context),
}
}
}
2024-01-06 10:00:36 +00:00
2024-01-06 13:11:09 +00:00
impl Format for IndexExpression {
fn format(&self, output: &mut String, indent_level: u8) {
2024-01-06 10:00:36 +00:00
match self {
2024-01-06 13:11:09 +00:00
IndexExpression::Value(value_node) => {
value_node.format(output, indent_level);
2024-01-06 13:11:09 +00:00
}
IndexExpression::Identifier(identifier) => identifier.format(output, indent_level),
IndexExpression::FunctionCall(function_call) => {
function_call.format(output, indent_level)
}
IndexExpression::Index(index) => index.format(output, indent_level),
2024-01-06 10:00:36 +00:00
}
}
}