1
0
This commit is contained in:
Jeff 2024-01-10 15:03:52 -05:00
parent c4908dc00d
commit c75538c064
27 changed files with 176 additions and 220 deletions

View File

@ -11,32 +11,31 @@ pub struct Assignment {
type_definition: Option<TypeDefinition>, type_definition: Option<TypeDefinition>,
operator: AssignmentOperator, operator: AssignmentOperator,
statement: Statement, statement: Statement,
syntax_position: SyntaxPosition, syntax_position: SyntaxPosition,
} }
impl AbstractTree for Assignment { impl AbstractTree for Assignment {
fn from_syntax_node(source: &str, syntax_node: SyntaxNode, context: &Map) -> Result<Self> { fn from_syntax(syntax_node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "assignment", syntax_node)?; Error::expect_syntax_node(source, "assignment", syntax_node)?;
let child_count = syntax_node.child_count(); let child_count = syntax_node.child_count();
let identifier_node = syntax_node.child(0).unwrap(); let identifier_node = syntax_node.child(0).unwrap();
let identifier = Identifier::from_syntax_node(source, identifier_node, context)?; let identifier = Identifier::from_syntax(identifier_node, source, context)?;
let type_node = syntax_node.child(1).unwrap(); let type_node = syntax_node.child(1).unwrap();
let type_definition = if type_node.kind() == "type_definition" { let type_definition = if type_node.kind() == "type_definition" {
Some(TypeDefinition::from_syntax_node( Some(TypeDefinition::from_syntax(type_node, source, context)?)
source, type_node, context,
)?)
} else { } else {
None None
}; };
let operator_node = syntax_node.child(child_count - 2).unwrap(); let operator_node = syntax_node.child(child_count - 2).unwrap();
let operator = AssignmentOperator::from_syntax_node(source, operator_node, context)?; let operator = AssignmentOperator::from_syntax(operator_node, source, context)?;
let statement_node = syntax_node.child(child_count - 1).unwrap(); let statement_node = syntax_node.child(child_count - 1).unwrap();
let statement = Statement::from_syntax_node(source, statement_node, context)?; let statement = Statement::from_syntax(statement_node, source, context)?;
let statement_type = statement.expected_type(context)?; let statement_type = statement.expected_type(context)?;
let variable_key = identifier.inner().clone(); let variable_key = identifier.inner().clone();

View File

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum AssignmentOperator { pub enum AssignmentOperator {
@ -10,11 +10,7 @@ pub enum AssignmentOperator {
} }
impl AbstractTree for AssignmentOperator { impl AbstractTree for AssignmentOperator {
fn from_syntax_node( fn from_syntax(node: SyntaxNode, source: &str, _context: &crate::Map) -> Result<Self> {
source: &str,
node: tree_sitter::Node,
_context: &crate::Map,
) -> Result<Self> {
Error::expect_syntax_node(source, "assignment_operator", node)?; Error::expect_syntax_node(source, "assignment_operator", node)?;
let operator_node = node.child(0).unwrap(); let operator_node = node.child(0).unwrap();

View File

@ -2,9 +2,8 @@ use std::sync::RwLock;
use rayon::prelude::*; use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, Map, Result, Statement, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, Statement, SyntaxNode, Type, Value};
/// Abstract representation of a block. /// Abstract representation of a block.
/// ///
@ -21,7 +20,7 @@ pub struct Block {
} }
impl AbstractTree for Block { impl AbstractTree for Block {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "block", node)?; Error::expect_syntax_node(source, "block", node)?;
let first_child = node.child(0).unwrap(); let first_child = node.child(0).unwrap();
@ -38,7 +37,7 @@ impl AbstractTree for Block {
let child_node = node.child(index).unwrap(); let child_node = node.child(index).unwrap();
if child_node.kind() == "statement" { if child_node.kind() == "statement" {
let statement = Statement::from_syntax_node(source, child_node, context)?; let statement = Statement::from_syntax(child_node, source, context)?;
statements.push(statement); statements.push(statement);
} }

View File

@ -1,11 +1,10 @@
use std::{env::args, sync::OnceLock}; use std::{env::args, sync::OnceLock};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
built_in_functions::string_functions, AbstractTree, BuiltInFunction, Format, Function, List, built_in_functions::string_functions, AbstractTree, BuiltInFunction, Format, Function, List,
Map, Result, Type, Value, Map, Result, SyntaxNode, Type, Value,
}; };
static ARGS: OnceLock<Value> = OnceLock::new(); static ARGS: OnceLock<Value> = OnceLock::new();
@ -135,7 +134,7 @@ impl BuiltInValue {
} }
impl AbstractTree for BuiltInValue { impl AbstractTree for BuiltInValue {
fn from_syntax_node(_source: &str, node: Node, _context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, _source: &str, _context: &Map) -> Result<Self> {
let built_in_value = match node.kind() { let built_in_value = match node.kind() {
"args" => BuiltInValue::Args, "args" => BuiltInValue::Args,
"assert_equal" => BuiltInValue::AssertEqual, "assert_equal" => BuiltInValue::AssertEqual,

View File

@ -1,13 +1,10 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
value_node::ValueNode, AbstractTree, Error, Format, Identifier, Index, Map, Result, Type, value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic,
Value, Yield, Map, Math, Result, SyntaxNode, Type, Value, Yield,
}; };
use super::{function_call::FunctionCall, logic::Logic, math::Math};
/// Abstract representation of an expression statement. /// Abstract representation of an expression statement.
/// ///
/// Unlike statements, which can involve complex logic, an expression is /// Unlike statements, which can involve complex logic, an expression is
@ -25,7 +22,7 @@ pub enum Expression {
} }
impl AbstractTree for Expression { impl AbstractTree for Expression {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "expression", node)?; Error::expect_syntax_node(source, "expression", node)?;
let child = if node.child(0).unwrap().is_named() { let child = if node.child(0).unwrap().is_named() {
@ -35,23 +32,17 @@ impl AbstractTree for Expression {
}; };
let expression = match child.kind() { let expression = match child.kind() {
"value" => Expression::Value(ValueNode::from_syntax_node(source, child, context)?), "value" => Expression::Value(ValueNode::from_syntax(child, source, context)?),
"identifier" => { "identifier" => {
Expression::Identifier(Identifier::from_syntax_node(source, child, context)?) Expression::Identifier(Identifier::from_syntax(child, source, context)?)
} }
"index" => { "index" => Expression::Index(Box::new(Index::from_syntax(child, source, context)?)),
Expression::Index(Box::new(Index::from_syntax_node(source, child, context)?)) "math" => Expression::Math(Box::new(Math::from_syntax(child, source, context)?)),
} "logic" => Expression::Logic(Box::new(Logic::from_syntax(child, source, context)?)),
"math" => Expression::Math(Box::new(Math::from_syntax_node(source, child, context)?)), "function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax(
"logic" => { child, source, context,
Expression::Logic(Box::new(Logic::from_syntax_node(source, child, context)?))
}
"function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax_node(
source, child, context,
)?)), )?)),
"yield" => { "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, context)?)),
Expression::Yield(Box::new(Yield::from_syntax_node(source, child, context)?))
}
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "value_node, identifier, index, math, logic, function_call or yield" expected: "value_node, identifier, index, math, logic, function_call or yield"

View File

@ -1,8 +1,10 @@
use rayon::prelude::*; use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Error, Expression, Format, Identifier, Map, Result, Type, Value}; use crate::{
AbstractTree, Block, Error, Expression, Format, Identifier, Map, Result, SyntaxNode, Type,
Value,
};
/// Abstract representation of a for loop statement. /// Abstract representation of a for loop statement.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -14,7 +16,7 @@ pub struct For {
} }
impl AbstractTree for For { impl AbstractTree for For {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "for", node)?; Error::expect_syntax_node(source, "for", node)?;
let for_node = node.child(0).unwrap(); let for_node = node.child(0).unwrap();
@ -32,13 +34,13 @@ impl AbstractTree for For {
}; };
let identifier_node = node.child(1).unwrap(); let identifier_node = node.child(1).unwrap();
let identifier = Identifier::from_syntax_node(source, identifier_node, context)?; let identifier = Identifier::from_syntax(identifier_node, source, context)?;
let expression_node = node.child(3).unwrap(); let expression_node = node.child(3).unwrap();
let expression = Expression::from_syntax_node(source, expression_node, context)?; let expression = Expression::from_syntax(expression_node, source, context)?;
let item_node = node.child(4).unwrap(); let item_node = node.child(4).unwrap();
let item = Block::from_syntax_node(source, item_node, context)?; let item = Block::from_syntax(item_node, source, context)?;
Ok(For { Ok(For {
is_async, is_async,

View File

@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Error, Expression, Format, FunctionExpression, Map, Result, SyntaxPosition, Type, AbstractTree, Error, Expression, Format, FunctionExpression, Map, Result, SyntaxNode,
Value, SyntaxPosition, Type, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -28,12 +27,11 @@ impl FunctionCall {
} }
impl AbstractTree for FunctionCall { impl AbstractTree for FunctionCall {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function_call", node)?; Error::expect_syntax_node(source, "function_call", node)?;
let function_node = node.child(0).unwrap(); let function_node = node.child(0).unwrap();
let function_expression = let function_expression = FunctionExpression::from_syntax(function_node, source, context)?;
FunctionExpression::from_syntax_node(source, function_node, context)?;
let mut arguments = Vec::new(); let mut arguments = Vec::new();
@ -41,7 +39,7 @@ impl AbstractTree for FunctionCall {
let child = node.child(index).unwrap(); let child = node.child(index).unwrap();
if child.is_named() { if child.is_named() {
let expression = Expression::from_syntax_node(source, child, context)?; let expression = Expression::from_syntax(child, source, context)?;
arguments.push(expression); arguments.push(expression);
} }

View File

@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map, Result, Type, Value, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map, Result, SyntaxNode, Type,
ValueNode, Yield, Value, ValueNode, Yield,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -16,7 +15,7 @@ pub enum FunctionExpression {
} }
impl AbstractTree for FunctionExpression { impl AbstractTree for FunctionExpression {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function_expression", node)?; Error::expect_syntax_node(source, "function_expression", node)?;
let first_child = node.child(0).unwrap(); let first_child = node.child(0).unwrap();
@ -27,20 +26,18 @@ impl AbstractTree for FunctionExpression {
}; };
let function_expression = match child.kind() { let function_expression = match child.kind() {
"identifier" => FunctionExpression::Identifier(Identifier::from_syntax_node( "identifier" => {
source, child, context, FunctionExpression::Identifier(Identifier::from_syntax(child, source, context)?)
)?), }
"function_call" => FunctionExpression::FunctionCall(Box::new( "function_call" => FunctionExpression::FunctionCall(Box::new(
FunctionCall::from_syntax_node(source, child, context)?, FunctionCall::from_syntax(child, source, context)?,
)), )),
"value" => { "value" => FunctionExpression::Value(ValueNode::from_syntax(child, source, context)?),
FunctionExpression::Value(ValueNode::from_syntax_node(source, child, context)?) "index" => FunctionExpression::Index(Index::from_syntax(child, source, context)?),
"yield" => {
FunctionExpression::Yield(Box::new(Yield::from_syntax(child, source, context)?))
} }
"index" => FunctionExpression::Index(Index::from_syntax_node(source, child, context)?),
"yield" => FunctionExpression::Yield(Box::new(Yield::from_syntax_node(
source, child, context,
)?)),
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "identifier, function call, value or index".to_string(), expected: "identifier, function call, value or index".to_string(),

View File

@ -1,11 +1,10 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Block, Error, Format, Function, Identifier, Map, Result, SyntaxPosition, Type, AbstractTree, Block, Error, Format, Function, Identifier, Map, Result, SyntaxNode,
TypeDefinition, Value, SyntaxPosition, Type, TypeDefinition, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
@ -86,7 +85,7 @@ impl FunctionNode {
} }
impl AbstractTree for FunctionNode { impl AbstractTree for FunctionNode {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "function", node)?; Error::expect_syntax_node(source, "function", node)?;
let child_count = node.child_count(); let child_count = node.child_count();
@ -97,20 +96,20 @@ impl AbstractTree for FunctionNode {
let child = node.child(index).unwrap(); let child = node.child(index).unwrap();
if child.kind() == "identifier" { if child.kind() == "identifier" {
let identifier = Identifier::from_syntax_node(source, child, context)?; let identifier = Identifier::from_syntax(child, source, context)?;
parameters.push(identifier); parameters.push(identifier);
} }
if child.kind() == "type_definition" { if child.kind() == "type_definition" {
let type_definition = TypeDefinition::from_syntax_node(source, child, context)?; let type_definition = TypeDefinition::from_syntax(child, source, context)?;
parameter_types.push(type_definition.take_inner()); parameter_types.push(type_definition.take_inner());
} }
} }
let return_type_node = node.child(child_count - 2).unwrap(); let return_type_node = node.child(child_count - 2).unwrap();
let return_type = TypeDefinition::from_syntax_node(source, return_type_node, context)?; let return_type = TypeDefinition::from_syntax(return_type_node, source, context)?;
let function_context = Map::new(); let function_context = Map::new();
@ -121,7 +120,7 @@ impl AbstractTree for FunctionNode {
} }
let body_node = node.child(child_count - 1).unwrap(); let body_node = node.child(child_count - 1).unwrap();
let body = Block::from_syntax_node(source, body_node, &function_context)?; let body = Block::from_syntax(body_node, source, &function_context)?;
let r#type = Type::function(parameter_types, return_type.take_inner()); let r#type = Type::function(parameter_types, return_type.take_inner());
let syntax_position = node.range().into(); let syntax_position = node.range().into();

View File

@ -1,9 +1,8 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
/// A string by which a variable is known to a context. /// A string by which a variable is known to a context.
/// ///
@ -27,7 +26,7 @@ impl Identifier {
} }
impl AbstractTree for Identifier { impl AbstractTree for Identifier {
fn from_syntax_node(source: &str, node: Node, _context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "identifier", node)?; Error::expect_syntax_node(source, "identifier", node)?;
let text = &source[node.byte_range()]; let text = &source[node.byte_range()];

View File

@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Expression, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Block, Expression, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct IfElse { pub struct IfElse {
@ -13,12 +12,12 @@ pub struct IfElse {
} }
impl AbstractTree for IfElse { impl AbstractTree for IfElse {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
let if_expression_node = node.child(0).unwrap().child(1).unwrap(); let if_expression_node = node.child(0).unwrap().child(1).unwrap();
let if_expression = Expression::from_syntax_node(source, if_expression_node, context)?; let if_expression = Expression::from_syntax(if_expression_node, source, context)?;
let if_block_node = node.child(0).unwrap().child(2).unwrap(); let if_block_node = node.child(0).unwrap().child(2).unwrap();
let if_block = Block::from_syntax_node(source, if_block_node, context)?; let if_block = Block::from_syntax(if_block_node, source, context)?;
let child_count = node.child_count(); let child_count = node.child_count();
let mut else_if_expressions = Vec::new(); let mut else_if_expressions = Vec::new();
@ -30,19 +29,19 @@ impl AbstractTree for IfElse {
if child.kind() == "else_if" { if child.kind() == "else_if" {
let expression_node = child.child(1).unwrap(); let expression_node = child.child(1).unwrap();
let expression = Expression::from_syntax_node(source, expression_node, context)?; let expression = Expression::from_syntax(expression_node, source, context)?;
else_if_expressions.push(expression); else_if_expressions.push(expression);
let block_node = child.child(2).unwrap(); let block_node = child.child(2).unwrap();
let block = Block::from_syntax_node(source, block_node, context)?; let block = Block::from_syntax(block_node, source, context)?;
else_if_blocks.push(block); else_if_blocks.push(block);
} }
if child.kind() == "else" { if child.kind() == "else" {
let else_node = child.child(1).unwrap(); let else_node = child.child(1).unwrap();
else_block = Some(Block::from_syntax_node(source, else_node, context)?); else_block = Some(Block::from_syntax(else_node, source, context)?);
} }
} }

View File

@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, IndexExpression, List, Map, Result, Type, Value}; use crate::{
AbstractTree, Error, Format, IndexExpression, List, Map, Result, SyntaxNode, Type, Value,
};
/// Abstract representation of an index expression. /// Abstract representation of an index expression.
/// ///
@ -14,20 +15,20 @@ pub struct Index {
} }
impl AbstractTree for Index { impl AbstractTree for Index {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "index", node)?; Error::expect_syntax_node(source, "index", node)?;
let collection_node = node.child(0).unwrap(); let collection_node = node.child(0).unwrap();
let collection = IndexExpression::from_syntax_node(source, collection_node, context)?; let collection = IndexExpression::from_syntax(collection_node, source, context)?;
let index_node = node.child(2).unwrap(); let index_node = node.child(2).unwrap();
let index = IndexExpression::from_syntax_node(source, index_node, context)?; let index = IndexExpression::from_syntax(index_node, source, context)?;
let index_end_node = node.child(4); let index_end_node = node.child(4);
let index_end = if let Some(index_end_node) = index_end_node { let index_end = if let Some(index_end_node) = index_end_node {
Some(IndexExpression::from_syntax_node( Some(IndexExpression::from_syntax(
source,
index_end_node, index_end_node,
source,
context, context,
)?) )?)
} else { } else {

View File

@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, AssignmentOperator, Error, Format, Index, IndexExpression, Map, Result, AbstractTree, AssignmentOperator, Error, Format, Index, IndexExpression, Map, Result,
Statement, Type, Value, Statement, SyntaxNode, Type, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -14,17 +13,17 @@ pub struct IndexAssignment {
} }
impl AbstractTree for IndexAssignment { impl AbstractTree for IndexAssignment {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "index_assignment", node)?; Error::expect_syntax_node(source, "index_assignment", node)?;
let index_node = node.child(0).unwrap(); let index_node = node.child(0).unwrap();
let index = Index::from_syntax_node(source, index_node, context)?; let index = Index::from_syntax(index_node, source, context)?;
let operator_node = node.child(1).unwrap(); let operator_node = node.child(1).unwrap();
let operator = AssignmentOperator::from_syntax_node(source, operator_node, context)?; let operator = AssignmentOperator::from_syntax(operator_node, source, context)?;
let statement_node = node.child(2).unwrap(); let statement_node = node.child(2).unwrap();
let statement = Statement::from_syntax_node(source, statement_node, context)?; let statement = Statement::from_syntax(statement_node, source, context)?;
Ok(IndexAssignment { Ok(IndexAssignment {
index, index,

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use crate::{ use crate::{
value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map, value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map,
Result, Type, Value, Result, SyntaxNode, Type, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -14,7 +14,7 @@ pub enum IndexExpression {
} }
impl AbstractTree for IndexExpression { impl AbstractTree for IndexExpression {
fn from_syntax_node(source: &str, node: tree_sitter::Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "index_expression", node)?; Error::expect_syntax_node(source, "index_expression", node)?;
let first_child = node.child(0).unwrap(); let first_child = node.child(0).unwrap();
@ -25,16 +25,16 @@ impl AbstractTree for IndexExpression {
}; };
let abstract_node = match child.kind() { let abstract_node = match child.kind() {
"value" => IndexExpression::Value(ValueNode::from_syntax_node(source, child, context)?), "value" => IndexExpression::Value(ValueNode::from_syntax(child, source, context)?),
"identifier" => { "identifier" => {
IndexExpression::Identifier(Identifier::from_syntax_node(source, child, context)?) IndexExpression::Identifier(Identifier::from_syntax(child, source, context)?)
} }
"index" => { "index" => {
IndexExpression::Index(Box::new(Index::from_syntax_node(source, child, context)?)) IndexExpression::Index(Box::new(Index::from_syntax(child, source, context)?))
} }
"function_call" => IndexExpression::FunctionCall(Box::new( "function_call" => IndexExpression::FunctionCall(Box::new(FunctionCall::from_syntax(
FunctionCall::from_syntax_node(source, child, context)?, child, source, context,
)), )?)),
_ => { _ => {
return Err(Error::UnexpectedSyntaxNode { return Err(Error::UnexpectedSyntaxNode {
expected: "value, identifier, index or function call".to_string(), expected: "value, identifier, index or function call".to_string(),

View File

@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Format, LogicOperator, Map, Result, Type, Value}; use crate::{
AbstractTree, Error, Expression, Format, LogicOperator, Map, Result, SyntaxNode, Type, Value,
};
/// Abstract representation of a logic expression. /// Abstract representation of a logic expression.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -12,7 +13,7 @@ pub struct Logic {
} }
impl AbstractTree for Logic { impl AbstractTree for Logic {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "logic", node)?; Error::expect_syntax_node(source, "logic", node)?;
let first_node = node.child(0).unwrap(); let first_node = node.child(0).unwrap();
@ -27,9 +28,9 @@ impl AbstractTree for Logic {
) )
} }
}; };
let left = Expression::from_syntax_node(source, left_node, context)?; let left = Expression::from_syntax(left_node, source, context)?;
let operator = LogicOperator::from_syntax_node(source, operator_node, context)?; let operator = LogicOperator::from_syntax(operator_node, source, context)?;
let right = Expression::from_syntax_node(source, right_node, context)?; let right = Expression::from_syntax(right_node, source, context)?;
Ok(Logic { Ok(Logic {
left, left,

View File

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum LogicOperator { pub enum LogicOperator {
@ -15,11 +15,7 @@ pub enum LogicOperator {
} }
impl AbstractTree for LogicOperator { impl AbstractTree for LogicOperator {
fn from_syntax_node( fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> crate::Result<Self> {
source: &str,
node: tree_sitter::Node,
_context: &crate::Map,
) -> crate::Result<Self> {
Error::expect_syntax_node(source, "logic_operator", node)?; Error::expect_syntax_node(source, "logic_operator", node)?;
let operator_node = node.child(0).unwrap(); let operator_node = node.child(0).unwrap();

View File

@ -3,9 +3,10 @@
//! Note that this module is called "match" but is escaped as "r#match" because //! Note that this module is called "match" but is escaped as "r#match" because
//! "match" is a keyword in Rust. //! "match" is a keyword in Rust.
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Format, Map, Result, Statement, Type, Value}; use crate::{
AbstractTree, Error, Expression, Format, Map, Result, Statement, SyntaxNode, Type, Value,
};
/// Abstract representation of a match statement. /// Abstract representation of a match statement.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -16,11 +17,11 @@ pub struct Match {
} }
impl AbstractTree for Match { impl AbstractTree for Match {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "match", node)?; Error::expect_syntax_node(source, "match", node)?;
let matcher_node = node.child(1).unwrap(); let matcher_node = node.child(1).unwrap();
let matcher = Expression::from_syntax_node(source, matcher_node, context)?; let matcher = Expression::from_syntax(matcher_node, source, context)?;
let mut options = Vec::new(); let mut options = Vec::new();
let mut previous_expression = None; let mut previous_expression = None;
@ -35,11 +36,11 @@ impl AbstractTree for Match {
} }
if child.kind() == "expression" { if child.kind() == "expression" {
previous_expression = Some(Expression::from_syntax_node(source, child, context)?); previous_expression = Some(Expression::from_syntax(child, source, context)?);
} }
if child.kind() == "statement" { if child.kind() == "statement" {
let statement = Statement::from_syntax_node(source, child, context)?; let statement = Statement::from_syntax(child, source, context)?;
if next_statement_is_fallback { if next_statement_is_fallback {
fallback = Some(Box::new(statement)); fallback = Some(Box::new(statement));

View File

@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Expression, Format, Map, MathOperator, Result, Type, Value}; use crate::{
AbstractTree, Error, Expression, Format, Map, MathOperator, Result, SyntaxNode, Type, Value,
};
/// Abstract representation of a math operation. /// Abstract representation of a math operation.
/// ///
@ -15,17 +16,17 @@ pub struct Math {
} }
impl AbstractTree for Math { impl AbstractTree for Math {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "math", node)?; Error::expect_syntax_node(source, "math", node)?;
let left_node = node.child(0).unwrap(); let left_node = node.child(0).unwrap();
let left = Expression::from_syntax_node(source, left_node, context)?; let left = Expression::from_syntax(left_node, source, context)?;
let operator_node = node.child(1).unwrap(); let operator_node = node.child(1).unwrap();
let operator = MathOperator::from_syntax_node(source, operator_node, context)?; let operator = MathOperator::from_syntax(operator_node, source, context)?;
let right_node = node.child(2).unwrap(); let right_node = node.child(2).unwrap();
let right = Expression::from_syntax_node(source, right_node, context)?; let right = Expression::from_syntax(right_node, source, context)?;
Ok(Math { Ok(Math {
left, left,

View File

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum MathOperator { pub enum MathOperator {
@ -12,7 +12,7 @@ pub enum MathOperator {
} }
impl AbstractTree for MathOperator { impl AbstractTree for MathOperator {
fn from_syntax_node(source: &str, node: tree_sitter::Node, _context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> Result<Self> {
let operator_node = node.child(0).unwrap(); let operator_node = node.child(0).unwrap();
let operator = match operator_node.kind() { let operator = match operator_node.kind() {
"+" => MathOperator::Add, "+" => MathOperator::Add,

View File

@ -41,9 +41,8 @@ pub use {
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{Error, Map, Result, Value}; use crate::{Error, Map, Result, SyntaxNode, Value};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SyntaxPosition { pub struct SyntaxPosition {
@ -74,7 +73,7 @@ pub struct Root {
} }
impl AbstractTree for Root { impl AbstractTree for Root {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "root", node)?; Error::expect_syntax_node(source, "root", node)?;
let statement_count = node.child_count(); let statement_count = node.child_count();
@ -82,7 +81,7 @@ impl AbstractTree for Root {
for index in 0..statement_count { for index in 0..statement_count {
let statement_node = node.child(index).unwrap(); let statement_node = node.child(index).unwrap();
let statement = Statement::from_syntax_node(source, statement_node, context)?; let statement = Statement::from_syntax(statement_node, source, context)?;
statements.push(statement); statements.push(statement);
} }
@ -145,7 +144,7 @@ pub trait AbstractTree: Sized + Format {
/// ///
/// If necessary, the source code can be accessed directly by getting the /// If necessary, the source code can be accessed directly by getting the
/// node's byte range. /// node's byte range.
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self>; fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self>;
/// Verify the type integrity of the node. /// Verify the type integrity of the node.
fn check_type(&self, _source: &str, _context: &Map) -> Result<()> { fn check_type(&self, _source: &str, _context: &Map) -> Result<()> {

View File

@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Assignment, Block, Error, Expression, For, Format, IfElse, IndexAssignment, Map, AbstractTree, Assignment, Block, Error, Expression, For, Format, IfElse, IndexAssignment, Map,
Match, Result, Type, Value, While, Match, Result, SyntaxNode, Type, Value, While,
}; };
/// Abstract representation of a statement. /// Abstract representation of a statement.
@ -21,40 +20,40 @@ pub enum Statement {
} }
impl AbstractTree for Statement { impl AbstractTree for Statement {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "statement", node)?; Error::expect_syntax_node(source, "statement", node)?;
let child = node.child(0).unwrap(); let child = node.child(0).unwrap();
match child.kind() { match child.kind() {
"assignment" => Ok(Statement::Assignment(Box::new( "assignment" => Ok(Statement::Assignment(Box::new(
Assignment::from_syntax_node(source, child, context)?, Assignment::from_syntax(child, source, context)?,
))), ))),
"expression" => Ok(Statement::Expression(Expression::from_syntax_node( "expression" => Ok(Statement::Expression(Expression::from_syntax(
source, child, context, child, source, context,
)?)), )?)),
"if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node( "if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax(
source, child, context, child, source, context,
)?))), )?))),
"while" => Ok(Statement::While(Box::new(While::from_syntax_node( "while" => Ok(Statement::While(Box::new(While::from_syntax(
source, child, context, child, source, context,
)?))), )?))),
"block" => Ok(Statement::Block(Box::new(Block::from_syntax_node( "block" => Ok(Statement::Block(Box::new(Block::from_syntax(
source, child, context, child, source, context,
)?))), )?))),
"for" => Ok(Statement::For(Box::new(For::from_syntax_node( "for" => Ok(Statement::For(Box::new(For::from_syntax(
source, child, context, child, source, context,
)?))), )?))),
"index_assignment" => Ok(Statement::IndexAssignment(Box::new( "index_assignment" => Ok(Statement::IndexAssignment(Box::new(
IndexAssignment::from_syntax_node(source, child, context)?, IndexAssignment::from_syntax(child, source, context)?,
))), ))),
"match" => Ok(Statement::Match(Match::from_syntax_node( "match" => Ok(Statement::Match(Match::from_syntax(
source, child, context, child, source, context,
)?)), )?)),
"return" => { "return" => {
let statement_node = child.child(1).unwrap(); let statement_node = child.child(1).unwrap();
Ok(Statement::Return(Box::new(Statement::from_syntax_node(source, statement_node, context)?))) Ok(Statement::Return(Box::new(Statement::from_syntax(statement_node, source, context)?)))
}, },
_ => Err(Error::UnexpectedSyntaxNode { _ => Err(Error::UnexpectedSyntaxNode {
expected: expected:

View File

@ -1,9 +1,8 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, Identifier, Map, Result, Structure, Value}; use crate::{AbstractTree, Error, Format, Identifier, Map, Result, Structure, SyntaxNode, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Type { pub enum Type {
@ -139,7 +138,7 @@ impl Type {
} }
impl AbstractTree for Type { impl AbstractTree for Type {
fn from_syntax_node(_source: &str, node: Node, _context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, _source: &str, _context: &Map) -> Result<Self> {
Error::expect_syntax_node(_source, "type", node)?; Error::expect_syntax_node(_source, "type", node)?;
let type_node = node.child(0).unwrap(); let type_node = node.child(0).unwrap();
@ -147,7 +146,7 @@ impl AbstractTree for Type {
let r#type = match type_node.kind() { let r#type = match type_node.kind() {
"[" => { "[" => {
let item_type_node = node.child(1).unwrap(); let item_type_node = node.child(1).unwrap();
let item_type = Type::from_syntax_node(_source, item_type_node, _context)?; let item_type = Type::from_syntax(item_type_node, _source, _context)?;
Type::List(Box::new(item_type)) Type::List(Box::new(item_type))
} }
@ -163,7 +162,7 @@ impl AbstractTree for Type {
let child = node.child(index).unwrap(); let child = node.child(index).unwrap();
if child.is_named() { if child.is_named() {
let parameter_type = Type::from_syntax_node(_source, child, _context)?; let parameter_type = Type::from_syntax(child, _source, _context)?;
parameter_types.push(parameter_type); parameter_types.push(parameter_type);
} }
@ -171,7 +170,7 @@ impl AbstractTree for Type {
let final_node = node.child(child_count - 1).unwrap(); let final_node = node.child(child_count - 1).unwrap();
let return_type = if final_node.is_named() { let return_type = if final_node.is_named() {
Type::from_syntax_node(_source, final_node, _context)? Type::from_syntax(final_node, _source, _context)?
} else { } else {
Type::None Type::None
}; };
@ -188,7 +187,7 @@ impl AbstractTree for Type {
"str" => Type::String, "str" => Type::String,
"option" => { "option" => {
let inner_type_node = node.child(2).unwrap(); let inner_type_node = node.child(2).unwrap();
let inner_type = Type::from_syntax_node(_source, inner_type_node, _context)?; let inner_type = Type::from_syntax(inner_type_node, _source, _context)?;
Type::Option(Box::new(inner_type)) Type::Option(Box::new(inner_type))
} }

View File

@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Error, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Error, Format, Map, Result, SyntaxNode, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct TypeDefinition { pub struct TypeDefinition {
@ -23,11 +22,11 @@ impl TypeDefinition {
} }
impl AbstractTree for TypeDefinition { impl AbstractTree for TypeDefinition {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "type_definition", node)?; Error::expect_syntax_node(source, "type_definition", node)?;
let type_node = node.child(1).unwrap(); let type_node = node.child(1).unwrap();
let r#type = Type::from_syntax_node(source, type_node, context)?; let r#type = Type::from_syntax(type_node, source, context)?;
Ok(TypeDefinition { r#type }) Ok(TypeDefinition { r#type })
} }

View File

@ -1,11 +1,10 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier, AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
List, Map, Result, Statement, Structure, Type, TypeDefinition, Value, List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefinition, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
@ -23,7 +22,7 @@ pub enum ValueNode {
} }
impl AbstractTree for ValueNode { impl AbstractTree for ValueNode {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "value", node)?; Error::expect_syntax_node(source, "value", node)?;
let child = node.child(0).unwrap(); let child = node.child(0).unwrap();
@ -31,7 +30,7 @@ impl AbstractTree for ValueNode {
"boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()), "boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()),
"float" => ValueNode::Float(source[child.byte_range()].to_string()), "float" => ValueNode::Float(source[child.byte_range()].to_string()),
"function" => { "function" => {
let function_node = FunctionNode::from_syntax_node(source, child, context)?; let function_node = FunctionNode::from_syntax(child, source, context)?;
ValueNode::Function(Function::ContextDefined(function_node)) ValueNode::Function(Function::ContextDefined(function_node))
} }
@ -48,8 +47,7 @@ impl AbstractTree for ValueNode {
let current_node = child.child(index).unwrap(); let current_node = child.child(index).unwrap();
if current_node.is_named() { if current_node.is_named() {
let expression = let expression = Expression::from_syntax(current_node, source, context)?;
Expression::from_syntax_node(source, current_node, context)?;
expressions.push(expression); expressions.push(expression);
} }
} }
@ -62,25 +60,20 @@ impl AbstractTree for ValueNode {
let mut current_type = None; let mut current_type = None;
for index in 0..child.child_count() - 1 { for index in 0..child.child_count() - 1 {
let child_syntax_node = child.child(index).unwrap(); let child = child.child(index).unwrap();
if child_syntax_node.kind() == "identifier" { if child.kind() == "identifier" {
current_key = current_key = Identifier::from_syntax(child, source, context)?.take_inner();
Identifier::from_syntax_node(source, child_syntax_node, context)?
.take_inner();
current_type = None; current_type = None;
} }
if child_syntax_node.kind() == "type_definition" { if child.kind() == "type_definition" {
current_type = Some( current_type =
TypeDefinition::from_syntax_node(source, child_syntax_node, context)? Some(TypeDefinition::from_syntax(child, source, context)?.take_inner());
.take_inner(),
);
} }
if child_syntax_node.kind() == "statement" { if child.kind() == "statement" {
let statement = let statement = Statement::from_syntax(child, source, context)?;
Statement::from_syntax_node(source, child_syntax_node, context)?;
if let Some(type_definition) = &current_type { if let Some(type_definition) = &current_type {
type_definition.check(&statement.expected_type(context)?)?; type_definition.check(&statement.expected_type(context)?)?;
@ -99,8 +92,7 @@ impl AbstractTree for ValueNode {
ValueNode::Option(None) ValueNode::Option(None)
} else { } else {
let expression_node = child.child(2).unwrap(); let expression_node = child.child(2).unwrap();
let expression = let expression = Expression::from_syntax(expression_node, source, context)?;
Expression::from_syntax_node(source, expression_node, context)?;
ValueNode::Option(Some(Box::new(expression))) ValueNode::Option(Some(Box::new(expression)))
} }
@ -108,9 +100,9 @@ impl AbstractTree for ValueNode {
"built_in_value" => { "built_in_value" => {
let built_in_value_node = child.child(0).unwrap(); let built_in_value_node = child.child(0).unwrap();
ValueNode::BuiltInValue(BuiltInValue::from_syntax_node( ValueNode::BuiltInValue(BuiltInValue::from_syntax(
source,
built_in_value_node, built_in_value_node,
source,
context, context,
)?) )?)
} }
@ -134,26 +126,20 @@ impl AbstractTree for ValueNode {
} }
current_type = None; current_type = None;
current_identifier = Some(Identifier::from_syntax_node( current_identifier =
source, Some(Identifier::from_syntax(child_syntax_node, source, context)?);
child_syntax_node,
context,
)?);
} }
if child_syntax_node.kind() == "type_definition" { if child_syntax_node.kind() == "type_definition" {
current_type = Some( current_type = Some(
TypeDefinition::from_syntax_node(source, child_syntax_node, context)? TypeDefinition::from_syntax(child_syntax_node, source, context)?
.take_inner(), .take_inner(),
); );
} }
if child_syntax_node.kind() == "statement" { if child_syntax_node.kind() == "statement" {
current_statement = Some(Statement::from_syntax_node( current_statement =
source, Some(Statement::from_syntax(child_syntax_node, source, context)?);
child_syntax_node,
context,
)?);
if let Some(identifier) = &current_identifier { if let Some(identifier) = &current_identifier {
let r#type = if let Some(r#type) = &current_type { let r#type = if let Some(r#type) = &current_type {

View File

@ -1,7 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{AbstractTree, Block, Error, Expression, Format, Map, Result, Type, Value}; use crate::{AbstractTree, Block, Error, Expression, Format, Map, Result, SyntaxNode, Type, Value};
/// Abstract representation of a while loop. /// Abstract representation of a while loop.
/// ///
@ -13,14 +12,14 @@ pub struct While {
} }
impl AbstractTree for While { impl AbstractTree for While {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> crate::Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> crate::Result<Self> {
Error::expect_syntax_node(source, "while", node)?; Error::expect_syntax_node(source, "while", node)?;
let expression_node = node.child(1).unwrap(); let expression_node = node.child(1).unwrap();
let expression = Expression::from_syntax_node(source, expression_node, context)?; let expression = Expression::from_syntax(expression_node, source, context)?;
let block_node = node.child(2).unwrap(); let block_node = node.child(2).unwrap();
let block = Block::from_syntax_node(source, block_node, context)?; let block = Block::from_syntax(block_node, source, context)?;
Ok(While { expression, block }) Ok(While { expression, block })
} }

View File

@ -1,9 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tree_sitter::Node;
use crate::{ use crate::{
function_expression::FunctionExpression, AbstractTree, Error, Expression, Format, FunctionCall, function_expression::FunctionExpression, AbstractTree, Error, Expression, Format, FunctionCall,
Map, Result, Type, Value, Map, Result, SyntaxNode, Type, Value,
}; };
/// Abstract representation of a yield expression. /// Abstract representation of a yield expression.
@ -15,15 +14,14 @@ pub struct Yield {
} }
impl AbstractTree for Yield { impl AbstractTree for Yield {
fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> { fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result<Self> {
Error::expect_syntax_node(source, "yield", node)?; Error::expect_syntax_node(source, "yield", node)?;
let input_node = node.child(0).unwrap(); let input_node = node.child(0).unwrap();
let input = Expression::from_syntax_node(source, input_node, context)?; let input = Expression::from_syntax(input_node, source, context)?;
let function_node = node.child(2).unwrap(); let function_node = node.child(2).unwrap();
let function_expression = let function_expression = FunctionExpression::from_syntax(function_node, source, context)?;
FunctionExpression::from_syntax_node(source, function_node, context)?;
let mut arguments = Vec::new(); let mut arguments = Vec::new();
@ -33,7 +31,7 @@ impl AbstractTree for Yield {
let child = node.child(index).unwrap(); let child = node.child(index).unwrap();
if child.is_named() { if child.is_named() {
let expression = Expression::from_syntax_node(source, child, context)?; let expression = Expression::from_syntax(child, source, context)?;
arguments.push(expression); arguments.push(expression);
} }

View File

@ -2,9 +2,9 @@
//! //!
//! You can use this library externally by calling either of the "eval" //! You can use this library externally by calling either of the "eval"
//! functions or by constructing your own Evaluator. //! functions or by constructing your own Evaluator.
use tree_sitter::{Node, Parser, Tree as TSTree, TreeCursor}; use tree_sitter::{Parser, Tree as TSTree, TreeCursor};
use crate::{language, AbstractTree, Error, Format, Map, Result, Root, Value}; use crate::{language, AbstractTree, Error, Format, Map, Result, Root, SyntaxNode, Value};
/// Interpret the given source code. /// Interpret the given source code.
/// ///
@ -72,7 +72,7 @@ impl Interpreter {
} }
pub fn parse(&mut self, source: &str) -> Result<()> { pub fn parse(&mut self, source: &str) -> Result<()> {
fn check_for_error(source: &str, node: Node, cursor: &mut TreeCursor) -> Result<()> { fn check_for_error(node: SyntaxNode, source: &str, cursor: &mut TreeCursor) -> Result<()> {
if node.is_error() { if node.is_error() {
Err(Error::Syntax { Err(Error::Syntax {
source: source[node.byte_range()].to_string(), source: source[node.byte_range()].to_string(),
@ -80,7 +80,7 @@ impl Interpreter {
}) })
} else { } else {
for child in node.children(&mut cursor.clone()) { for child in node.children(&mut cursor.clone()) {
check_for_error(source, child, cursor)?; check_for_error(child, source, cursor)?;
} }
Ok(()) Ok(())
@ -93,7 +93,7 @@ impl Interpreter {
let root = tree.root_node(); let root = tree.root_node();
let mut cursor = root.walk(); let mut cursor = root.walk();
check_for_error(source, root, &mut cursor)?; check_for_error(root, source, &mut cursor)?;
} }
self.syntax_tree = syntax_tree; self.syntax_tree = syntax_tree;
@ -105,9 +105,9 @@ impl Interpreter {
self.parse(source)?; self.parse(source)?;
self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree { self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree {
Some(Root::from_syntax_node( Some(Root::from_syntax(
source,
syntax_tree.root_node(), syntax_tree.root_node(),
source,
&self.context, &self.context,
)?) )?)
} else { } else {