2023-10-06 17:32:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
|
|
|
use crate::{
|
2023-11-15 01:41:57 +00:00
|
|
|
AbstractTree, Assignment, Block, Error, Expression, Filter, Find, For, IfElse, IndexAssignment,
|
|
|
|
Insert, Map, Match, Remove, Result, Select, Transform, Value, While,
|
2023-10-06 17:32:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Abstract representation of a statement.
|
|
|
|
///
|
2023-10-09 19:54:47 +00:00
|
|
|
/// A statement may evaluate to an Empty value when run. If a Statement is an
|
|
|
|
/// Expression, it will always return a non-empty value when run.
|
2023-10-06 17:32:58 +00:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub enum Statement {
|
|
|
|
Assignment(Box<Assignment>),
|
|
|
|
Expression(Expression),
|
|
|
|
IfElse(Box<IfElse>),
|
|
|
|
Match(Match),
|
2023-10-07 02:45:36 +00:00
|
|
|
While(Box<While>),
|
2023-11-11 01:44:03 +00:00
|
|
|
Block(Box<Block>),
|
2023-10-17 18:06:02 +00:00
|
|
|
For(Box<For>),
|
2023-10-17 20:40:07 +00:00
|
|
|
Transform(Box<Transform>),
|
2023-10-17 21:52:41 +00:00
|
|
|
Filter(Box<Filter>),
|
2023-10-18 22:26:45 +00:00
|
|
|
Find(Box<Find>),
|
2023-10-19 01:50:45 +00:00
|
|
|
Remove(Box<Remove>),
|
2023-10-21 17:04:17 +00:00
|
|
|
Select(Box<Select>),
|
2023-10-22 17:55:56 +00:00
|
|
|
Insert(Box<Insert>),
|
2023-11-15 01:41:57 +00:00
|
|
|
IndexAssignment(Box<IndexAssignment>),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for Statement {
|
2023-10-10 17:29:11 +00:00
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
2023-10-06 17:32:58 +00:00
|
|
|
let child = node.child(0).unwrap();
|
|
|
|
|
|
|
|
match child.kind() {
|
|
|
|
"assignment" => Ok(Statement::Assignment(Box::new(
|
2023-10-10 17:29:11 +00:00
|
|
|
Assignment::from_syntax_node(source, child)?,
|
2023-10-06 17:32:58 +00:00
|
|
|
))),
|
|
|
|
"expression" => Ok(Self::Expression(Expression::from_syntax_node(
|
2023-10-10 17:29:11 +00:00
|
|
|
source, child,
|
2023-10-06 17:32:58 +00:00
|
|
|
)?)),
|
|
|
|
"if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node(
|
2023-10-10 17:29:11 +00:00
|
|
|
source, child,
|
2023-10-06 17:32:58 +00:00
|
|
|
)?))),
|
|
|
|
"tool" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node(
|
2023-10-10 17:29:11 +00:00
|
|
|
source, child,
|
2023-10-06 17:32:58 +00:00
|
|
|
)?))),
|
2023-10-07 02:45:36 +00:00
|
|
|
"while" => Ok(Statement::While(Box::new(While::from_syntax_node(
|
2023-10-10 17:29:11 +00:00
|
|
|
source, child,
|
2023-10-07 02:45:36 +00:00
|
|
|
)?))),
|
2023-11-11 01:44:03 +00:00
|
|
|
"block" => Ok(Statement::Block(Box::new(Block::from_syntax_node(
|
2023-10-17 18:06:02 +00:00
|
|
|
source, child,
|
|
|
|
)?))),
|
|
|
|
"for" => Ok(Statement::For(Box::new(For::from_syntax_node(
|
2023-10-16 20:48:02 +00:00
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-17 20:40:07 +00:00
|
|
|
"transform" => Ok(Statement::Transform(Box::new(Transform::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-17 21:52:41 +00:00
|
|
|
"filter" => Ok(Statement::Filter(Box::new(Filter::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-18 22:26:45 +00:00
|
|
|
"find" => Ok(Statement::Find(Box::new(Find::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-19 01:50:45 +00:00
|
|
|
"remove" => Ok(Statement::Remove(Box::new(Remove::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-21 17:04:17 +00:00
|
|
|
"select" => Ok(Statement::Select(Box::new(Select::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-10-22 17:55:56 +00:00
|
|
|
"insert" => Ok(Statement::Insert(Box::new(Insert::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
2023-11-15 01:41:57 +00:00
|
|
|
"index_assignment" => Ok(Statement::IndexAssignment(Box::new(IndexAssignment::from_syntax_node(
|
|
|
|
source, child,
|
|
|
|
)?))),
|
|
|
|
_ => Err(Error::expect_syntax_node(
|
|
|
|
source,
|
|
|
|
"assignment, expression, if...else, while, for, transform, filter, tool, async, find, remove, select, insert or index_assignment",
|
|
|
|
child
|
|
|
|
).unwrap_err())
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 20:44:50 +00:00
|
|
|
fn run(&self, source: &str, context: &mut Map) -> Result<Value> {
|
2023-10-06 17:32:58 +00:00
|
|
|
match self {
|
2023-10-10 17:29:11 +00:00
|
|
|
Statement::Assignment(assignment) => assignment.run(source, context),
|
|
|
|
Statement::Expression(expression) => expression.run(source, context),
|
|
|
|
Statement::IfElse(if_else) => if_else.run(source, context),
|
|
|
|
Statement::Match(r#match) => r#match.run(source, context),
|
|
|
|
Statement::While(r#while) => r#while.run(source, context),
|
2023-11-11 01:44:03 +00:00
|
|
|
Statement::Block(block) => block.run(source, context),
|
2023-10-17 18:06:02 +00:00
|
|
|
Statement::For(r#for) => r#for.run(source, context),
|
2023-10-17 20:40:07 +00:00
|
|
|
Statement::Transform(transform) => transform.run(source, context),
|
2023-10-17 21:52:41 +00:00
|
|
|
Statement::Filter(filter) => filter.run(source, context),
|
2023-10-18 22:26:45 +00:00
|
|
|
Statement::Find(find) => find.run(source, context),
|
2023-10-19 01:50:45 +00:00
|
|
|
Statement::Remove(remove) => remove.run(source, context),
|
2023-10-21 17:04:17 +00:00
|
|
|
Statement::Select(select) => select.run(source, context),
|
2023-10-22 17:55:56 +00:00
|
|
|
Statement::Insert(insert) => insert.run(source, context),
|
2023-11-15 01:41:57 +00:00
|
|
|
Statement::IndexAssignment(index_assignment) => index_assignment.run(source, context),
|
2023-10-06 17:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|