dust/src/abstract_tree/value_node.rs

207 lines
7.2 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
2023-10-10 18:12:07 +00:00
use serde::{Deserialize, Serialize};
use tree_sitter::Node;
2023-10-10 21:12:38 +00:00
use crate::{
2023-11-27 20:02:08 +00:00
AbstractTree, Error, Expression, Function, Identifier, List, Map, Result, Statement, Table,
2023-11-30 01:59:58 +00:00
Type, Value,
2023-10-10 21:12:38 +00:00
};
2023-10-10 18:12:07 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
2023-11-27 22:53:12 +00:00
pub enum ValueNode {
Boolean(String),
Float(String),
Integer(String),
String(String),
List(Vec<Expression>),
Empty,
Map(BTreeMap<String, Statement>),
Table {
column_names: Vec<Identifier>,
rows: Box<Expression>,
},
Function(Function),
2023-10-10 18:12:07 +00:00
}
impl AbstractTree for ValueNode {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
debug_assert_eq!("value", node.kind());
let child = node.child(0).unwrap();
2023-11-27 22:53:12 +00:00
let value_node = match child.kind() {
"boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()),
"float" => ValueNode::Float(source[child.byte_range()].to_string()),
"integer" => ValueNode::Integer(source[child.byte_range()].to_string()),
"string" => {
let without_quotes = child.start_byte() + 1..child.end_byte() - 1;
ValueNode::String(source[without_quotes].to_string())
}
2023-10-10 18:12:07 +00:00
"list" => {
2023-10-28 14:28:43 +00:00
let mut expressions = Vec::new();
2023-10-10 18:12:07 +00:00
for index in 1..child.child_count() - 1 {
2023-10-28 14:28:43 +00:00
let current_node = child.child(index).unwrap();
2023-10-10 18:12:07 +00:00
2023-10-28 14:28:43 +00:00
if current_node.is_named() {
let expression = Expression::from_syntax_node(source, current_node)?;
expressions.push(expression);
2023-10-10 18:12:07 +00:00
}
}
2023-11-27 22:53:12 +00:00
ValueNode::List(expressions)
2023-10-10 18:12:07 +00:00
}
2023-10-18 23:26:49 +00:00
"table" => {
2023-10-31 22:18:39 +00:00
let identifier_list_node = child.child(1).unwrap();
let identifier_count = identifier_list_node.child_count();
let mut column_names = Vec::with_capacity(identifier_count);
2023-10-18 23:26:49 +00:00
2023-10-31 22:18:39 +00:00
for index in 0..identifier_count {
let identifier_node = identifier_list_node.child(index).unwrap();
2023-10-18 23:26:49 +00:00
2023-10-31 22:18:39 +00:00
if identifier_node.is_named() {
let identifier = Identifier::from_syntax_node(source, identifier_node)?;
2023-10-18 23:26:49 +00:00
column_names.push(identifier)
}
}
2023-10-31 22:18:39 +00:00
let expression_node = child.child(2).unwrap();
let expression = Expression::from_syntax_node(source, expression_node)?;
2023-11-27 22:53:12 +00:00
ValueNode::Table {
2023-10-18 23:26:49 +00:00
column_names,
rows: Box::new(expression),
}
}
2023-10-10 18:12:07 +00:00
"map" => {
let mut child_nodes = BTreeMap::new();
let mut current_key = "".to_string();
for index in 0..child.child_count() - 1 {
let child_syntax_node = child.child(index).unwrap();
if child_syntax_node.kind() == "identifier" {
current_key =
Identifier::from_syntax_node(source, child_syntax_node)?.take_inner();
}
2023-10-31 19:21:13 +00:00
if child_syntax_node.kind() == "statement" {
2023-10-10 18:12:07 +00:00
let key = current_key.clone();
2023-10-31 19:21:13 +00:00
let statement = Statement::from_syntax_node(source, child_syntax_node)?;
2023-10-10 18:12:07 +00:00
2023-10-31 19:21:13 +00:00
child_nodes.insert(key, statement);
2023-10-10 18:12:07 +00:00
}
}
2023-11-27 22:53:12 +00:00
ValueNode::Map(child_nodes)
2023-10-10 18:12:07 +00:00
}
2023-11-27 22:53:12 +00:00
"function" => ValueNode::Function(Function::from_syntax_node(source, child)?),
2023-10-10 18:12:07 +00:00
_ => {
return Err(Error::UnexpectedSyntaxNode {
expected:
"string, integer, float, boolean, list, table, map, function or empty",
actual: child.kind(),
location: child.start_position(),
relevant_source: source[child.byte_range()].to_string(),
})
}
};
2023-11-27 22:53:12 +00:00
Ok(value_node)
2023-10-10 18:12:07 +00:00
}
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value> {
2023-11-27 22:53:12 +00:00
let value = match self {
ValueNode::Boolean(value_source) => Value::Boolean(value_source.parse().unwrap()),
ValueNode::Float(value_source) => Value::Float(value_source.parse().unwrap()),
ValueNode::Integer(value_source) => Value::Integer(value_source.parse().unwrap()),
ValueNode::String(value_source) => Value::String(value_source.parse().unwrap()),
ValueNode::List(expressions) => {
let mut values = Vec::with_capacity(expressions.len());
for node in expressions {
2023-10-10 18:12:07 +00:00
let value = node.run(source, context)?;
values.push(value);
}
2023-10-26 22:03:59 +00:00
Value::List(List::with_items(values))
2023-10-10 18:12:07 +00:00
}
2023-11-27 22:53:12 +00:00
ValueNode::Empty => Value::Empty,
ValueNode::Map(key_statement_pairs) => {
2023-10-29 23:31:06 +00:00
let map = Map::new();
2023-10-10 18:12:07 +00:00
2023-11-05 18:54:29 +00:00
{
let mut variables = map.variables_mut()?;
2023-11-27 22:53:12 +00:00
for (key, statement) in key_statement_pairs {
let value = statement.run(source, context)?;
2023-10-10 18:12:07 +00:00
2023-11-05 18:54:29 +00:00
variables.insert(key.clone(), value);
}
2023-10-10 18:12:07 +00:00
}
2023-10-29 23:31:06 +00:00
Value::Map(map)
2023-10-10 18:12:07 +00:00
}
2023-11-27 22:53:12 +00:00
ValueNode::Table {
2023-10-18 23:26:49 +00:00
column_names,
rows: row_expression,
} => {
let mut headers = Vec::with_capacity(column_names.len());
let mut rows = Vec::new();
for identifier in column_names {
let name = identifier.inner().clone();
headers.push(name)
}
let _row_values = row_expression.run(source, context)?;
2023-10-26 22:03:59 +00:00
let row_values = _row_values.as_list()?.items();
2023-10-18 23:26:49 +00:00
2023-10-26 22:03:59 +00:00
for value in row_values.iter() {
let row = value.as_list()?.items().clone();
2023-10-18 23:26:49 +00:00
rows.push(row)
}
let table = Table::from_raw_parts(headers, rows);
Value::Table(table)
}
2023-11-27 22:53:12 +00:00
ValueNode::Function(function) => Value::Function(function.clone()),
2023-10-10 18:12:07 +00:00
};
Ok(value)
}
2023-11-30 00:23:42 +00:00
2023-11-30 01:59:58 +00:00
fn expected_type(&self, context: &Map) -> Result<Type> {
2023-11-30 00:23:42 +00:00
let r#type = match self {
2023-11-30 01:59:58 +00:00
ValueNode::Boolean(_) => Type::Boolean,
ValueNode::Float(_) => Type::Float,
ValueNode::Integer(_) => Type::Integer,
ValueNode::String(_) => Type::String,
2023-11-30 00:23:42 +00:00
ValueNode::List(expressions) => {
let first_expression_type = if let Some(first) = expressions.first() {
first.expected_type(context)?
} else {
2023-11-30 01:59:58 +00:00
Type::Empty
2023-11-30 00:23:42 +00:00
};
2023-11-30 01:59:58 +00:00
Type::List(Box::new(first_expression_type))
2023-11-30 00:23:42 +00:00
}
2023-11-30 01:59:58 +00:00
ValueNode::Empty => Type::Any,
ValueNode::Map(_) => Type::Map,
2023-11-30 00:23:42 +00:00
ValueNode::Table {
column_names: _,
rows: _,
2023-11-30 01:59:58 +00:00
} => Type::Table,
2023-11-30 00:23:42 +00:00
ValueNode::Function(function) => function.expected_type(context)?,
};
Ok(r#type)
}
2023-10-10 18:12:07 +00:00
}