2023-11-05 18:54:29 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tree_sitter::Node;
|
|
|
|
|
2023-11-18 01:10:07 +00:00
|
|
|
use crate::{AbstractTree, BuiltInFunction, Expression, FunctionCall, Result, Value};
|
2023-11-05 18:54:29 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
|
|
|
|
pub struct Yield {
|
|
|
|
call: FunctionCall,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AbstractTree for Yield {
|
|
|
|
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
|
|
|
|
let input_node = node.child(0).unwrap();
|
|
|
|
let input = Expression::from_syntax_node(source, input_node)?;
|
|
|
|
|
2023-11-16 02:13:14 +00:00
|
|
|
let function_node = node.child(3).unwrap();
|
|
|
|
let mut arguments = Vec::new();
|
2023-11-05 18:54:29 +00:00
|
|
|
|
2023-11-16 02:13:14 +00:00
|
|
|
arguments.push(input);
|
|
|
|
|
|
|
|
for index in 4..node.child_count() - 1 {
|
|
|
|
let node = node.child(index).unwrap();
|
|
|
|
|
|
|
|
if node.is_named() {
|
|
|
|
let expression = Expression::from_syntax_node(source, node)?;
|
|
|
|
|
|
|
|
arguments.push(expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let call = if function_node.kind() == "built_in_function" {
|
|
|
|
let function = BuiltInFunction::from_syntax_node(source, function_node)?;
|
|
|
|
|
|
|
|
FunctionCall::BuiltIn(Box::new(function))
|
|
|
|
} else {
|
2023-11-18 01:10:07 +00:00
|
|
|
let name = Expression::from_syntax_node(source, function_node)?;
|
2023-11-16 02:13:14 +00:00
|
|
|
|
2023-11-18 01:10:07 +00:00
|
|
|
FunctionCall::ContextDefined { name, arguments }
|
2023-11-16 02:13:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Yield { call })
|
2023-11-05 18:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&self, source: &str, context: &mut crate::Map) -> Result<Value> {
|
2023-11-16 02:13:14 +00:00
|
|
|
self.call.run(source, context)
|
2023-11-05 18:54:29 +00:00
|
|
|
}
|
|
|
|
}
|