From c75538c064727e70da2c62b5b581aa583c3a95f0 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 10 Jan 2024 15:03:52 -0500 Subject: [PATCH] Clean up --- src/abstract_tree/assignment.rs | 13 +++--- src/abstract_tree/assignment_operator.rs | 8 +--- src/abstract_tree/block.rs | 7 ++- src/abstract_tree/built_in_value.rs | 5 +-- src/abstract_tree/expression.rs | 31 +++++--------- src/abstract_tree/for.rs | 14 +++--- src/abstract_tree/function_call.rs | 12 +++--- src/abstract_tree/function_expression.rs | 25 +++++------ src/abstract_tree/function_node.rs | 15 +++---- src/abstract_tree/identifier.rs | 5 +-- src/abstract_tree/if_else.rs | 15 +++---- src/abstract_tree/index.rs | 15 ++++--- src/abstract_tree/index_assignment.rs | 11 +++-- src/abstract_tree/index_expression.rs | 16 +++---- src/abstract_tree/logic.rs | 13 +++--- src/abstract_tree/logic_operator.rs | 8 +--- src/abstract_tree/match.rs | 13 +++--- src/abstract_tree/math.rs | 13 +++--- src/abstract_tree/math_operator.rs | 4 +- src/abstract_tree/mod.rs | 9 ++-- src/abstract_tree/statement.rs | 35 ++++++++------- src/abstract_tree/type.rs | 13 +++--- src/abstract_tree/type_definition.rs | 7 ++- src/abstract_tree/value_node.rs | 54 +++++++++--------------- src/abstract_tree/while.rs | 9 ++-- src/abstract_tree/yield.rs | 12 +++--- src/interpret.rs | 14 +++--- 27 files changed, 176 insertions(+), 220 deletions(-) diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index 5ec96d9..158e17c 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -11,32 +11,31 @@ pub struct Assignment { type_definition: Option, operator: AssignmentOperator, statement: Statement, + syntax_position: SyntaxPosition, } impl AbstractTree for Assignment { - fn from_syntax_node(source: &str, syntax_node: SyntaxNode, context: &Map) -> Result { + fn from_syntax(syntax_node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "assignment", syntax_node)?; let child_count = syntax_node.child_count(); 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_definition = if type_node.kind() == "type_definition" { - Some(TypeDefinition::from_syntax_node( - source, type_node, context, - )?) + Some(TypeDefinition::from_syntax(type_node, source, context)?) } else { None }; 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 = 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 variable_key = identifier.inner().clone(); diff --git a/src/abstract_tree/assignment_operator.rs b/src/abstract_tree/assignment_operator.rs index ca60427..69ef328 100644 --- a/src/abstract_tree/assignment_operator.rs +++ b/src/abstract_tree/assignment_operator.rs @@ -1,6 +1,6 @@ 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)] pub enum AssignmentOperator { @@ -10,11 +10,7 @@ pub enum AssignmentOperator { } impl AbstractTree for AssignmentOperator { - fn from_syntax_node( - source: &str, - node: tree_sitter::Node, - _context: &crate::Map, - ) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, _context: &crate::Map) -> Result { Error::expect_syntax_node(source, "assignment_operator", node)?; let operator_node = node.child(0).unwrap(); diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 5d16484..9a87881 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -2,9 +2,8 @@ use std::sync::RwLock; use rayon::prelude::*; 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. /// @@ -21,7 +20,7 @@ pub struct Block { } impl AbstractTree for Block { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "block", node)?; let first_child = node.child(0).unwrap(); @@ -38,7 +37,7 @@ impl AbstractTree for Block { let child_node = node.child(index).unwrap(); 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); } diff --git a/src/abstract_tree/built_in_value.rs b/src/abstract_tree/built_in_value.rs index dd46fc5..e23a5e9 100644 --- a/src/abstract_tree/built_in_value.rs +++ b/src/abstract_tree/built_in_value.rs @@ -1,11 +1,10 @@ use std::{env::args, sync::OnceLock}; use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ built_in_functions::string_functions, AbstractTree, BuiltInFunction, Format, Function, List, - Map, Result, Type, Value, + Map, Result, SyntaxNode, Type, Value, }; static ARGS: OnceLock = OnceLock::new(); @@ -135,7 +134,7 @@ impl BuiltInValue { } impl AbstractTree for BuiltInValue { - fn from_syntax_node(_source: &str, node: Node, _context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, _source: &str, _context: &Map) -> Result { let built_in_value = match node.kind() { "args" => BuiltInValue::Args, "assert_equal" => BuiltInValue::AssertEqual, diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index e28d366..12ee2ba 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -1,13 +1,10 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ - value_node::ValueNode, AbstractTree, Error, Format, Identifier, Index, Map, Result, Type, - Value, Yield, + value_node::ValueNode, AbstractTree, Error, Format, FunctionCall, Identifier, Index, Logic, + Map, Math, Result, SyntaxNode, Type, Value, Yield, }; -use super::{function_call::FunctionCall, logic::Logic, math::Math}; - /// Abstract representation of an expression statement. /// /// Unlike statements, which can involve complex logic, an expression is @@ -25,7 +22,7 @@ pub enum Expression { } impl AbstractTree for Expression { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "expression", node)?; let child = if node.child(0).unwrap().is_named() { @@ -35,23 +32,17 @@ impl AbstractTree for Expression { }; 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" => { - Expression::Identifier(Identifier::from_syntax_node(source, child, context)?) + Expression::Identifier(Identifier::from_syntax(child, source, context)?) } - "index" => { - Expression::Index(Box::new(Index::from_syntax_node(source, child, context)?)) - } - "math" => Expression::Math(Box::new(Math::from_syntax_node(source, child, context)?)), - "logic" => { - Expression::Logic(Box::new(Logic::from_syntax_node(source, child, context)?)) - } - "function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax_node( - source, child, context, + "index" => Expression::Index(Box::new(Index::from_syntax(child, source, context)?)), + "math" => Expression::Math(Box::new(Math::from_syntax(child, source, context)?)), + "logic" => Expression::Logic(Box::new(Logic::from_syntax(child, source, context)?)), + "function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax( + child, source, context, )?)), - "yield" => { - Expression::Yield(Box::new(Yield::from_syntax_node(source, child, context)?)) - } + "yield" => Expression::Yield(Box::new(Yield::from_syntax(child, source, context)?)), _ => { return Err(Error::UnexpectedSyntaxNode { expected: "value_node, identifier, index, math, logic, function_call or yield" diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index cfa8eb1..560a7d7 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -1,8 +1,10 @@ use rayon::prelude::*; 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. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -14,7 +16,7 @@ pub struct For { } impl AbstractTree for For { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "for", node)?; let for_node = node.child(0).unwrap(); @@ -32,13 +34,13 @@ impl AbstractTree for For { }; 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 = 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 = Block::from_syntax_node(source, item_node, context)?; + let item = Block::from_syntax(item_node, source, context)?; Ok(For { is_async, diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index ea2917d..e55778b 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -1,9 +1,8 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ - AbstractTree, Error, Expression, Format, FunctionExpression, Map, Result, SyntaxPosition, Type, - Value, + AbstractTree, Error, Expression, Format, FunctionExpression, Map, Result, SyntaxNode, + SyntaxPosition, Type, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -28,12 +27,11 @@ impl FunctionCall { } impl AbstractTree for FunctionCall { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "function_call", node)?; let function_node = node.child(0).unwrap(); - let function_expression = - FunctionExpression::from_syntax_node(source, function_node, context)?; + let function_expression = FunctionExpression::from_syntax(function_node, source, context)?; let mut arguments = Vec::new(); @@ -41,7 +39,7 @@ impl AbstractTree for FunctionCall { let child = node.child(index).unwrap(); if child.is_named() { - let expression = Expression::from_syntax_node(source, child, context)?; + let expression = Expression::from_syntax(child, source, context)?; arguments.push(expression); } diff --git a/src/abstract_tree/function_expression.rs b/src/abstract_tree/function_expression.rs index ccf6421..3d01da5 100644 --- a/src/abstract_tree/function_expression.rs +++ b/src/abstract_tree/function_expression.rs @@ -1,9 +1,8 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ - AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map, Result, Type, Value, - ValueNode, Yield, + AbstractTree, Error, Format, FunctionCall, Identifier, Index, Map, Result, SyntaxNode, Type, + Value, ValueNode, Yield, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -16,7 +15,7 @@ pub enum FunctionExpression { } impl AbstractTree for FunctionExpression { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "function_expression", node)?; let first_child = node.child(0).unwrap(); @@ -27,20 +26,18 @@ impl AbstractTree for FunctionExpression { }; let function_expression = match child.kind() { - "identifier" => FunctionExpression::Identifier(Identifier::from_syntax_node( - source, child, context, - )?), + "identifier" => { + FunctionExpression::Identifier(Identifier::from_syntax(child, source, context)?) + } "function_call" => FunctionExpression::FunctionCall(Box::new( - FunctionCall::from_syntax_node(source, child, context)?, + FunctionCall::from_syntax(child, source, context)?, )), - "value" => { - FunctionExpression::Value(ValueNode::from_syntax_node(source, child, context)?) + "value" => FunctionExpression::Value(ValueNode::from_syntax(child, source, 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 { expected: "identifier, function call, value or index".to_string(), diff --git a/src/abstract_tree/function_node.rs b/src/abstract_tree/function_node.rs index 51d24e0..04d9e33 100644 --- a/src/abstract_tree/function_node.rs +++ b/src/abstract_tree/function_node.rs @@ -1,11 +1,10 @@ use std::fmt::{self, Display, Formatter}; use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ - AbstractTree, Block, Error, Format, Function, Identifier, Map, Result, SyntaxPosition, Type, - TypeDefinition, Value, + AbstractTree, Block, Error, Format, Function, Identifier, Map, Result, SyntaxNode, + SyntaxPosition, Type, TypeDefinition, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] @@ -86,7 +85,7 @@ impl FunctionNode { } impl AbstractTree for FunctionNode { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "function", node)?; let child_count = node.child_count(); @@ -97,20 +96,20 @@ impl AbstractTree for FunctionNode { let child = node.child(index).unwrap(); if child.kind() == "identifier" { - let identifier = Identifier::from_syntax_node(source, child, context)?; + let identifier = Identifier::from_syntax(child, source, context)?; parameters.push(identifier); } 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()); } } 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(); @@ -121,7 +120,7 @@ impl AbstractTree for FunctionNode { } 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 syntax_position = node.range().into(); diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 90919b6..a4a58ef 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -1,9 +1,8 @@ use std::fmt::{self, Display, Formatter}; 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. /// @@ -27,7 +26,7 @@ impl Identifier { } impl AbstractTree for Identifier { - fn from_syntax_node(source: &str, node: Node, _context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> Result { Error::expect_syntax_node(source, "identifier", node)?; let text = &source[node.byte_range()]; diff --git a/src/abstract_tree/if_else.rs b/src/abstract_tree/if_else.rs index e53e580..37d0d69 100644 --- a/src/abstract_tree/if_else.rs +++ b/src/abstract_tree/if_else.rs @@ -1,7 +1,6 @@ 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)] pub struct IfElse { @@ -13,12 +12,12 @@ pub struct IfElse { } impl AbstractTree for IfElse { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { 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 = 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 mut else_if_expressions = Vec::new(); @@ -30,19 +29,19 @@ impl AbstractTree for IfElse { if child.kind() == "else_if" { 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); 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); } if child.kind() == "else" { 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)?); } } diff --git a/src/abstract_tree/index.rs b/src/abstract_tree/index.rs index df1f5fb..5f33b0c 100644 --- a/src/abstract_tree/index.rs +++ b/src/abstract_tree/index.rs @@ -1,7 +1,8 @@ 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. /// @@ -14,20 +15,20 @@ pub struct Index { } impl AbstractTree for Index { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "index", node)?; 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 = 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 = if let Some(index_end_node) = index_end_node { - Some(IndexExpression::from_syntax_node( - source, + Some(IndexExpression::from_syntax( index_end_node, + source, context, )?) } else { diff --git a/src/abstract_tree/index_assignment.rs b/src/abstract_tree/index_assignment.rs index 436fe09..1caa054 100644 --- a/src/abstract_tree/index_assignment.rs +++ b/src/abstract_tree/index_assignment.rs @@ -1,9 +1,8 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ AbstractTree, AssignmentOperator, Error, Format, Index, IndexExpression, Map, Result, - Statement, Type, Value, + Statement, SyntaxNode, Type, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -14,17 +13,17 @@ pub struct IndexAssignment { } impl AbstractTree for IndexAssignment { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "index_assignment", node)?; 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 = 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 = Statement::from_syntax_node(source, statement_node, context)?; + let statement = Statement::from_syntax(statement_node, source, context)?; Ok(IndexAssignment { index, diff --git a/src/abstract_tree/index_expression.rs b/src/abstract_tree/index_expression.rs index 7e8d5ef..2778da2 100644 --- a/src/abstract_tree/index_expression.rs +++ b/src/abstract_tree/index_expression.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::{ 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)] @@ -14,7 +14,7 @@ pub enum IndexExpression { } impl AbstractTree for IndexExpression { - fn from_syntax_node(source: &str, node: tree_sitter::Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "index_expression", node)?; let first_child = node.child(0).unwrap(); @@ -25,16 +25,16 @@ impl AbstractTree for IndexExpression { }; 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" => { - IndexExpression::Identifier(Identifier::from_syntax_node(source, child, context)?) + IndexExpression::Identifier(Identifier::from_syntax(child, source, context)?) } "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( - FunctionCall::from_syntax_node(source, child, context)?, - )), + "function_call" => IndexExpression::FunctionCall(Box::new(FunctionCall::from_syntax( + child, source, context, + )?)), _ => { return Err(Error::UnexpectedSyntaxNode { expected: "value, identifier, index or function call".to_string(), diff --git a/src/abstract_tree/logic.rs b/src/abstract_tree/logic.rs index 6fab60c..542c9d8 100644 --- a/src/abstract_tree/logic.rs +++ b/src/abstract_tree/logic.rs @@ -1,7 +1,8 @@ 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. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -12,7 +13,7 @@ pub struct Logic { } impl AbstractTree for Logic { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "logic", node)?; 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 operator = LogicOperator::from_syntax_node(source, operator_node, context)?; - let right = Expression::from_syntax_node(source, right_node, context)?; + let left = Expression::from_syntax(left_node, source, context)?; + let operator = LogicOperator::from_syntax(operator_node, source, context)?; + let right = Expression::from_syntax(right_node, source, context)?; Ok(Logic { left, diff --git a/src/abstract_tree/logic_operator.rs b/src/abstract_tree/logic_operator.rs index 19ed7f9..4ec9035 100644 --- a/src/abstract_tree/logic_operator.rs +++ b/src/abstract_tree/logic_operator.rs @@ -1,6 +1,6 @@ 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)] pub enum LogicOperator { @@ -15,11 +15,7 @@ pub enum LogicOperator { } impl AbstractTree for LogicOperator { - fn from_syntax_node( - source: &str, - node: tree_sitter::Node, - _context: &crate::Map, - ) -> crate::Result { + fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> crate::Result { Error::expect_syntax_node(source, "logic_operator", node)?; let operator_node = node.child(0).unwrap(); diff --git a/src/abstract_tree/match.rs b/src/abstract_tree/match.rs index 279ee78..e86c584 100644 --- a/src/abstract_tree/match.rs +++ b/src/abstract_tree/match.rs @@ -3,9 +3,10 @@ //! Note that this module is called "match" but is escaped as "r#match" because //! "match" is a keyword in Rust. 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. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -16,11 +17,11 @@ pub struct Match { } impl AbstractTree for Match { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "match", node)?; 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 previous_expression = None; @@ -35,11 +36,11 @@ impl AbstractTree for Match { } 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" { - let statement = Statement::from_syntax_node(source, child, context)?; + let statement = Statement::from_syntax(child, source, context)?; if next_statement_is_fallback { fallback = Some(Box::new(statement)); diff --git a/src/abstract_tree/math.rs b/src/abstract_tree/math.rs index 63ad7ab..2a37bf0 100644 --- a/src/abstract_tree/math.rs +++ b/src/abstract_tree/math.rs @@ -1,7 +1,8 @@ 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. /// @@ -15,17 +16,17 @@ pub struct Math { } impl AbstractTree for Math { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "math", node)?; 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 = 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 = Expression::from_syntax_node(source, right_node, context)?; + let right = Expression::from_syntax(right_node, source, context)?; Ok(Math { left, diff --git a/src/abstract_tree/math_operator.rs b/src/abstract_tree/math_operator.rs index 0fa1d1b..8b74fbd 100644 --- a/src/abstract_tree/math_operator.rs +++ b/src/abstract_tree/math_operator.rs @@ -1,6 +1,6 @@ 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)] pub enum MathOperator { @@ -12,7 +12,7 @@ pub enum MathOperator { } impl AbstractTree for MathOperator { - fn from_syntax_node(source: &str, node: tree_sitter::Node, _context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, _context: &Map) -> Result { let operator_node = node.child(0).unwrap(); let operator = match operator_node.kind() { "+" => MathOperator::Add, diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 61c8ace..e55d7f7 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -41,9 +41,8 @@ pub use { }; 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)] pub struct SyntaxPosition { @@ -74,7 +73,7 @@ pub struct Root { } impl AbstractTree for Root { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "root", node)?; let statement_count = node.child_count(); @@ -82,7 +81,7 @@ impl AbstractTree for Root { for index in 0..statement_count { 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); } @@ -145,7 +144,7 @@ pub trait AbstractTree: Sized + Format { /// /// If necessary, the source code can be accessed directly by getting the /// node's byte range. - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result; + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result; /// Verify the type integrity of the node. fn check_type(&self, _source: &str, _context: &Map) -> Result<()> { diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index 8ed43e5..4054ad2 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -1,9 +1,8 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ 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. @@ -21,40 +20,40 @@ pub enum Statement { } impl AbstractTree for Statement { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "statement", node)?; let child = node.child(0).unwrap(); match child.kind() { "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( - source, child, context, + "expression" => Ok(Statement::Expression(Expression::from_syntax( + child, source, context, )?)), - "if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax_node( - source, child, context, + "if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax( + child, source, context, )?))), - "while" => Ok(Statement::While(Box::new(While::from_syntax_node( - source, child, context, + "while" => Ok(Statement::While(Box::new(While::from_syntax( + child, source, context, )?))), - "block" => Ok(Statement::Block(Box::new(Block::from_syntax_node( - source, child, context, + "block" => Ok(Statement::Block(Box::new(Block::from_syntax( + child, source, context, )?))), - "for" => Ok(Statement::For(Box::new(For::from_syntax_node( - source, child, context, + "for" => Ok(Statement::For(Box::new(For::from_syntax( + child, source, context, )?))), "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( - source, child, context, + "match" => Ok(Statement::Match(Match::from_syntax( + child, source, context, )?)), "return" => { 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 { expected: diff --git a/src/abstract_tree/type.rs b/src/abstract_tree/type.rs index ec6054e..b504384 100644 --- a/src/abstract_tree/type.rs +++ b/src/abstract_tree/type.rs @@ -1,9 +1,8 @@ use std::fmt::{self, Display, Formatter}; 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)] pub enum Type { @@ -139,7 +138,7 @@ impl Type { } impl AbstractTree for Type { - fn from_syntax_node(_source: &str, node: Node, _context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, _source: &str, _context: &Map) -> Result { Error::expect_syntax_node(_source, "type", node)?; let type_node = node.child(0).unwrap(); @@ -147,7 +146,7 @@ impl AbstractTree for Type { let r#type = match type_node.kind() { "[" => { 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)) } @@ -163,7 +162,7 @@ impl AbstractTree for Type { let child = node.child(index).unwrap(); 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); } @@ -171,7 +170,7 @@ impl AbstractTree for Type { let final_node = node.child(child_count - 1).unwrap(); let return_type = if final_node.is_named() { - Type::from_syntax_node(_source, final_node, _context)? + Type::from_syntax(final_node, _source, _context)? } else { Type::None }; @@ -188,7 +187,7 @@ impl AbstractTree for Type { "str" => Type::String, "option" => { 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)) } diff --git a/src/abstract_tree/type_definition.rs b/src/abstract_tree/type_definition.rs index 78f3dbd..49c4377 100644 --- a/src/abstract_tree/type_definition.rs +++ b/src/abstract_tree/type_definition.rs @@ -1,7 +1,6 @@ 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)] pub struct TypeDefinition { @@ -23,11 +22,11 @@ impl TypeDefinition { } impl AbstractTree for TypeDefinition { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "type_definition", node)?; 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 }) } diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index aedab27..ee5a86c 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -1,11 +1,10 @@ use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ 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)] @@ -23,7 +22,7 @@ pub enum ValueNode { } impl AbstractTree for ValueNode { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "value", node)?; let child = node.child(0).unwrap(); @@ -31,7 +30,7 @@ impl AbstractTree for ValueNode { "boolean" => ValueNode::Boolean(source[child.byte_range()].to_string()), "float" => ValueNode::Float(source[child.byte_range()].to_string()), "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)) } @@ -48,8 +47,7 @@ impl AbstractTree for ValueNode { let current_node = child.child(index).unwrap(); if current_node.is_named() { - let expression = - Expression::from_syntax_node(source, current_node, context)?; + let expression = Expression::from_syntax(current_node, source, context)?; expressions.push(expression); } } @@ -62,25 +60,20 @@ impl AbstractTree for ValueNode { let mut current_type = None; 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" { - current_key = - Identifier::from_syntax_node(source, child_syntax_node, context)? - .take_inner(); + if child.kind() == "identifier" { + current_key = Identifier::from_syntax(child, source, context)?.take_inner(); current_type = None; } - if child_syntax_node.kind() == "type_definition" { - current_type = Some( - TypeDefinition::from_syntax_node(source, child_syntax_node, context)? - .take_inner(), - ); + if child.kind() == "type_definition" { + current_type = + Some(TypeDefinition::from_syntax(child, source, context)?.take_inner()); } - if child_syntax_node.kind() == "statement" { - let statement = - Statement::from_syntax_node(source, child_syntax_node, context)?; + if child.kind() == "statement" { + let statement = Statement::from_syntax(child, source, context)?; if let Some(type_definition) = ¤t_type { type_definition.check(&statement.expected_type(context)?)?; @@ -99,8 +92,7 @@ impl AbstractTree for ValueNode { ValueNode::Option(None) } else { let expression_node = child.child(2).unwrap(); - let expression = - Expression::from_syntax_node(source, expression_node, context)?; + let expression = Expression::from_syntax(expression_node, source, context)?; ValueNode::Option(Some(Box::new(expression))) } @@ -108,9 +100,9 @@ impl AbstractTree for ValueNode { "built_in_value" => { let built_in_value_node = child.child(0).unwrap(); - ValueNode::BuiltInValue(BuiltInValue::from_syntax_node( - source, + ValueNode::BuiltInValue(BuiltInValue::from_syntax( built_in_value_node, + source, context, )?) } @@ -134,26 +126,20 @@ impl AbstractTree for ValueNode { } current_type = None; - current_identifier = Some(Identifier::from_syntax_node( - source, - child_syntax_node, - context, - )?); + current_identifier = + Some(Identifier::from_syntax(child_syntax_node, source, context)?); } if child_syntax_node.kind() == "type_definition" { current_type = Some( - TypeDefinition::from_syntax_node(source, child_syntax_node, context)? + TypeDefinition::from_syntax(child_syntax_node, source, context)? .take_inner(), ); } if child_syntax_node.kind() == "statement" { - current_statement = Some(Statement::from_syntax_node( - source, - child_syntax_node, - context, - )?); + current_statement = + Some(Statement::from_syntax(child_syntax_node, source, context)?); if let Some(identifier) = ¤t_identifier { let r#type = if let Some(r#type) = ¤t_type { diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index 21de6e9..664f154 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -1,7 +1,6 @@ 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. /// @@ -13,14 +12,14 @@ pub struct While { } impl AbstractTree for While { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> crate::Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> crate::Result { Error::expect_syntax_node(source, "while", node)?; 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 = Block::from_syntax_node(source, block_node, context)?; + let block = Block::from_syntax(block_node, source, context)?; Ok(While { expression, block }) } diff --git a/src/abstract_tree/yield.rs b/src/abstract_tree/yield.rs index ba8b85b..2400828 100644 --- a/src/abstract_tree/yield.rs +++ b/src/abstract_tree/yield.rs @@ -1,9 +1,8 @@ use serde::{Deserialize, Serialize}; -use tree_sitter::Node; use crate::{ function_expression::FunctionExpression, AbstractTree, Error, Expression, Format, FunctionCall, - Map, Result, Type, Value, + Map, Result, SyntaxNode, Type, Value, }; /// Abstract representation of a yield expression. @@ -15,15 +14,14 @@ pub struct Yield { } impl AbstractTree for Yield { - fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result { + fn from_syntax(node: SyntaxNode, source: &str, context: &Map) -> Result { Error::expect_syntax_node(source, "yield", node)?; 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_expression = - FunctionExpression::from_syntax_node(source, function_node, context)?; + let function_expression = FunctionExpression::from_syntax(function_node, source, context)?; let mut arguments = Vec::new(); @@ -33,7 +31,7 @@ impl AbstractTree for Yield { let child = node.child(index).unwrap(); if child.is_named() { - let expression = Expression::from_syntax_node(source, child, context)?; + let expression = Expression::from_syntax(child, source, context)?; arguments.push(expression); } diff --git a/src/interpret.rs b/src/interpret.rs index b6875b2..8c564fd 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -2,9 +2,9 @@ //! //! You can use this library externally by calling either of the "eval" //! 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. /// @@ -72,7 +72,7 @@ impl Interpreter { } 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() { Err(Error::Syntax { source: source[node.byte_range()].to_string(), @@ -80,7 +80,7 @@ impl Interpreter { }) } else { for child in node.children(&mut cursor.clone()) { - check_for_error(source, child, cursor)?; + check_for_error(child, source, cursor)?; } Ok(()) @@ -93,7 +93,7 @@ impl Interpreter { let root = tree.root_node(); let mut cursor = root.walk(); - check_for_error(source, root, &mut cursor)?; + check_for_error(root, source, &mut cursor)?; } self.syntax_tree = syntax_tree; @@ -105,9 +105,9 @@ impl Interpreter { self.parse(source)?; self.abstract_tree = if let Some(syntax_tree) = &self.syntax_tree { - Some(Root::from_syntax_node( - source, + Some(Root::from_syntax( syntax_tree.root_node(), + source, &self.context, )?) } else {