From 787cf3cb07a22283bb8b257f5db98a9f93aa544e Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Thu, 4 Apr 2024 18:03:31 +0200 Subject: [PATCH] WIP --- .../parser/runtime/src/napi_interface/cst.rs | 49 +- .../runtime/src/napi_interface/cursor.rs | 10 +- .../src/napi_interface/parse_output.rs | 6 +- .../templates/ast_selectors.rs.jinja2 | 43 +- .../generated/napi_interface/ast_selectors.rs | 543 ++++++++++-------- .../src/generated/napi_interface/cst.rs | 49 +- .../src/generated/napi_interface/cursor.rs | 10 +- .../generated/napi_interface/parse_output.rs | 6 +- .../npm/package/src/generated/index.d.ts | 2 +- .../generated/napi_interface/ast_selectors.rs | 69 +-- .../src/generated/napi_interface/cst.rs | 49 +- .../src/generated/napi_interface/cursor.rs | 10 +- .../generated/napi_interface/parse_output.rs | 6 +- .../npm/package/src/generated/index.d.ts | 2 +- 14 files changed, 421 insertions(+), 433 deletions(-) diff --git a/crates/codegen/parser/runtime/src/napi_interface/cst.rs b/crates/codegen/parser/runtime/src/napi_interface/cst.rs index ad19bfa629..a6a4812e6e 100644 --- a/crates/codegen/parser/runtime/src/napi_interface/cst.rs +++ b/crates/codegen/parser/runtime/src/napi_interface/cst.rs @@ -1,7 +1,5 @@ use std::rc::Rc; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -16,6 +14,16 @@ pub enum NodeType { Token, } +impl RustNode { + /// Converts a Rust node into a choice of NAPI-exposed wrapper structs. + pub fn into_either(self) -> napi::Either { + match self { + RustNode::Rule(rule) => napi::Either::A(RuleNode(rule)), + RustNode::Token(token) => napi::Either::B(TokenNode(token)), + } + } +} + #[napi(namespace = "cst")] pub struct RuleNode(pub(crate) Rc); @@ -49,12 +57,12 @@ impl RuleNode { (&self.0.text_len).into() } - #[napi(ts_return_type = "Array", catch_unwind)] - pub fn children(&self, env: Env) -> Vec { + #[napi(catch_unwind)] + pub fn children(&self) -> Vec> { self.0 .children .iter() - .map(|child| child.to_js(env)) + .map(|child| child.node.clone().into_either()) .collect() } @@ -117,34 +125,3 @@ impl TokenNode { .into() } } - -pub(crate) trait ToJS { - fn to_js(&self, env: Env) -> JsObject; -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - RuleNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - TokenNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for RustNode { - fn to_js(&self, env: Env) -> JsObject { - match self { - RustNode::Rule(rust_rule_node) => rust_rule_node.to_js(env), - RustNode::Token(rust_token_node) => rust_token_node.to_js(env), - } - } -} diff --git a/crates/codegen/parser/runtime/src/napi_interface/cursor.rs b/crates/codegen/parser/runtime/src/napi_interface/cursor.rs index ee33aed25e..a9649c3fed 100644 --- a/crates/codegen/parser/runtime/src/napi_interface/cursor.rs +++ b/crates/codegen/parser/runtime/src/napi_interface/cursor.rs @@ -3,13 +3,11 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use cst::ToJS; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use text_index::{TextIndex, TextRange}; -use crate::napi_interface::{cst, text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; +use crate::napi_interface::cst::{self, RuleNode, TokenNode}; +use crate::napi_interface::{text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; #[napi(namespace = "cursor")] pub struct Cursor(pub(super) RustCursor); @@ -53,8 +51,8 @@ impl Cursor { } #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn node(&self, env: Env) -> JsObject { - self.0.node().to_js(env) + pub fn node(&self) -> napi::Either { + self.0.node().into_either() } #[napi(getter, ts_return_type = "kinds.NodeLabel", catch_unwind)] diff --git a/crates/codegen/parser/runtime/src/napi_interface/parse_output.rs b/crates/codegen/parser/runtime/src/napi_interface/parse_output.rs index d1804af45b..6eba57498b 100644 --- a/crates/codegen/parser/runtime/src/napi_interface/parse_output.rs +++ b/crates/codegen/parser/runtime/src/napi_interface/parse_output.rs @@ -1,5 +1,3 @@ -use cst::ToJS; -use napi::bindgen_prelude::Env; use napi_derive::napi; use crate::napi_interface::{cst, cursor, parse_error, RustParseOutput}; @@ -16,8 +14,8 @@ impl From for ParseOutput { #[napi(namespace = "parse_output")] impl ParseOutput { #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn tree(&self, env: Env) -> napi::JsObject { - self.0.tree().to_js(env) + pub fn tree(&self) -> napi::Either { + self.0.tree().into_either() } #[napi(ts_return_type = "Array", catch_unwind)] diff --git a/crates/codegen/parser/runtime/src/napi_interface/templates/ast_selectors.rs.jinja2 b/crates/codegen/parser/runtime/src/napi_interface/templates/ast_selectors.rs.jinja2 index 9d3e5ef8aa..819353f3cd 100644 --- a/crates/codegen/parser/runtime/src/napi_interface/templates/ast_selectors.rs.jinja2 +++ b/crates/codegen/parser/runtime/src/napi_interface/templates/ast_selectors.rs.jinja2 @@ -2,10 +2,9 @@ use std::rc::Rc; -use napi::{Env, JsObject}; use napi_derive::napi; -use crate::napi_interface::cst::{RuleNode, ToJS}; +use crate::napi_interface::cst::{RuleNode, TokenNode}; use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, TokenKind}; // @@ -19,9 +18,8 @@ use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, T )] pub fn select_sequence( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { {%- for sequence in ast_model.sequences -%} @@ -40,7 +38,7 @@ pub fn select_sequence( {% for sequence in ast_model.sequences %} impl Selector { - fn {{ sequence.name | snake_case }}(&mut self) -> Result>> { + fn {{ sequence.name | snake_case }}(&mut self) -> Result>>> { Ok(vec![ {%- for field in sequence.fields -%} {%- if field.is_optional -%} @@ -81,9 +79,8 @@ pub fn select_sequence( )] pub fn select_choice( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result { - let mut selector = Selector::new(node, env); +) -> Result> { + let mut selector = Selector::new(node); let result = match node.kind() { {%- for choice in ast_model.choices -%} @@ -102,7 +99,7 @@ pub fn select_choice( {% for choice in ast_model.choices %} impl Selector { - fn {{ choice.name | snake_case }}(&mut self) -> Result { + fn {{ choice.name | snake_case }}(&mut self) -> Result> { self.select(|node| { {%- set non_terminals_len = choice.non_terminals | length -%} {%- set terminals_len = choice.terminals | length -%} @@ -146,9 +143,8 @@ pub fn select_choice( )] pub fn select_repeated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result> { - let mut selector = Selector::new(node, env); +) -> Result>> { + let mut selector = Selector::new(node); let result = match node.kind() { {%- for repeated in ast_model.repeated -%} @@ -167,7 +163,7 @@ pub fn select_repeated( {% for repeated in ast_model.repeated %} impl Selector { - fn {{ repeated.name | snake_case }}(&mut self) -> Result> { + fn {{ repeated.name | snake_case }}(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = self.try_select(|node| { @@ -196,9 +192,8 @@ pub fn select_repeated( )] pub fn select_separated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { {%- for separated in ast_model.separated -%} @@ -217,7 +212,7 @@ pub fn select_separated( {% for separated in ast_model.separated %} impl Selector { - fn {{ separated.name | snake_case }}(&mut self) -> Result>> { + fn {{ separated.name | snake_case }}(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -253,28 +248,26 @@ pub fn select_separated( // struct Selector { - env: Env, node: Rc, index: usize, } impl Selector { - fn new(node: &RuleNode, env: Env) -> Self { + fn new(node: &RuleNode) -> Self { Self { - env, node: node.0.clone(), index: 0, } } - fn select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result { + fn select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result> { match self.try_select(filter)? { Some(node) => Ok(node), None => Error::MissingChild(self.index).into(), } } - fn try_select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result> { + fn try_select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result>> { while let Some(child) = self.node.children.get(self.index) { match child { node if node.is_trivia() => { @@ -288,9 +281,9 @@ impl Selector { } if matches!(token.kind, TokenKind::SKIPPED) => { return Error::SkippedToken(self.index).into(); } - node if filter(node) => { + labeled if filter(labeled) => { self.index += 1; - return Ok(Some(node.to_js(self.env))); + return Ok(Some(labeled.node.clone().into_either())); } _ => { break; diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/ast_selectors.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/ast_selectors.rs index 27c971e25e..51fda009a9 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/ast_selectors.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/ast_selectors.rs @@ -4,10 +4,9 @@ use std::rc::Rc; -use napi::{Env, JsObject}; use napi_derive::napi; -use crate::napi_interface::cst::{RuleNode, ToJS}; +use crate::napi_interface::cst::{RuleNode, TokenNode}; use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, TokenKind}; // @@ -21,9 +20,8 @@ use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, T )] pub fn select_sequence( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnit => selector.source_unit()?, @@ -168,7 +166,7 @@ pub fn select_sequence( } impl Selector { - fn source_unit(&mut self) -> Result>> { + fn source_unit(&mut self) -> Result>>> { Ok(vec![Some(self.select(|node| { node.is_rule_with_kind(RuleKind::SourceUnitMembers) })?)]) @@ -176,7 +174,7 @@ impl Selector { } impl Selector { - fn pragma_directive(&mut self) -> Result>> { + fn pragma_directive(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::PragmaKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Pragma))?), @@ -186,7 +184,7 @@ impl Selector { } impl Selector { - fn abi_coder_pragma(&mut self) -> Result>> { + fn abi_coder_pragma(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::AbicoderKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -195,7 +193,7 @@ impl Selector { } impl Selector { - fn experimental_pragma(&mut self) -> Result>> { + fn experimental_pragma(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ExperimentalKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ExperimentalFeature))?), @@ -204,7 +202,7 @@ impl Selector { } impl Selector { - fn version_pragma(&mut self) -> Result>> { + fn version_pragma(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::SolidityKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::VersionPragmaExpressions))?), @@ -213,7 +211,9 @@ impl Selector { } impl Selector { - fn version_pragma_or_expression(&mut self) -> Result>> { + fn version_pragma_or_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::VersionPragmaExpression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::BarBar))?), @@ -223,7 +223,9 @@ impl Selector { } impl Selector { - fn version_pragma_range_expression(&mut self) -> Result>> { + fn version_pragma_range_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::VersionPragmaExpression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Minus))?), @@ -233,7 +235,9 @@ impl Selector { } impl Selector { - fn version_pragma_prefix_expression(&mut self) -> Result>> { + fn version_pragma_prefix_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Caret))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::VersionPragmaExpression))?), @@ -242,7 +246,7 @@ impl Selector { } impl Selector { - fn import_directive(&mut self) -> Result>> { + fn import_directive(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ImportKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ImportClause))?), @@ -252,7 +256,7 @@ impl Selector { } impl Selector { - fn path_import(&mut self) -> Result>> { + fn path_import(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::StringLiteral))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::ImportAlias))?, @@ -261,7 +265,7 @@ impl Selector { } impl Selector { - fn named_import(&mut self) -> Result>> { + fn named_import(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Asterisk))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ImportAlias))?), @@ -272,7 +276,7 @@ impl Selector { } impl Selector { - fn import_deconstruction(&mut self) -> Result>> { + fn import_deconstruction(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), Some( @@ -286,7 +290,9 @@ impl Selector { } impl Selector { - fn import_deconstruction_symbol(&mut self) -> Result>> { + fn import_deconstruction_symbol( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::ImportAlias))?, @@ -295,7 +301,7 @@ impl Selector { } impl Selector { - fn import_alias(&mut self) -> Result>> { + fn import_alias(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::AsKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -304,7 +310,7 @@ impl Selector { } impl Selector { - fn using_directive(&mut self) -> Result>> { + fn using_directive(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::UsingKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::UsingClause))?), @@ -317,7 +323,7 @@ impl Selector { } impl Selector { - fn using_deconstruction(&mut self) -> Result>> { + fn using_deconstruction(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::UsingDeconstructionSymbols))?), @@ -327,7 +333,9 @@ impl Selector { } impl Selector { - fn using_deconstruction_symbol(&mut self) -> Result>> { + fn using_deconstruction_symbol( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::IdentifierPath))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::UsingAlias))?, @@ -336,7 +344,7 @@ impl Selector { } impl Selector { - fn using_alias(&mut self) -> Result>> { + fn using_alias(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::AsKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::UsingOperator))?), @@ -345,7 +353,7 @@ impl Selector { } impl Selector { - fn contract_definition(&mut self) -> Result>> { + fn contract_definition(&mut self) -> Result>>> { Ok(vec![ self.try_select(|node| node.is_token_with_kind(TokenKind::AbstractKeyword))?, Some(self.select(|node| node.is_token_with_kind(TokenKind::ContractKeyword))?), @@ -359,7 +367,7 @@ impl Selector { } impl Selector { - fn inheritance_specifier(&mut self) -> Result>> { + fn inheritance_specifier(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::IsKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::InheritanceTypes))?), @@ -368,7 +376,7 @@ impl Selector { } impl Selector { - fn inheritance_type(&mut self) -> Result>> { + fn inheritance_type(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::IdentifierPath))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::ArgumentsDeclaration))?, @@ -377,7 +385,7 @@ impl Selector { } impl Selector { - fn interface_definition(&mut self) -> Result>> { + fn interface_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::InterfaceKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -390,7 +398,7 @@ impl Selector { } impl Selector { - fn library_definition(&mut self) -> Result>> { + fn library_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::LibraryKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -402,7 +410,7 @@ impl Selector { } impl Selector { - fn struct_definition(&mut self) -> Result>> { + fn struct_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::StructKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -414,7 +422,7 @@ impl Selector { } impl Selector { - fn struct_member(&mut self) -> Result>> { + fn struct_member(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -424,7 +432,7 @@ impl Selector { } impl Selector { - fn enum_definition(&mut self) -> Result>> { + fn enum_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::EnumKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -436,7 +444,7 @@ impl Selector { } impl Selector { - fn constant_definition(&mut self) -> Result>> { + fn constant_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::ConstantKeyword))?), @@ -449,7 +457,9 @@ impl Selector { } impl Selector { - fn state_variable_definition(&mut self) -> Result>> { + fn state_variable_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::StateVariableAttributes))?), @@ -461,7 +471,9 @@ impl Selector { } impl Selector { - fn state_variable_definition_value(&mut self) -> Result>> { + fn state_variable_definition_value( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Equal))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), @@ -470,7 +482,7 @@ impl Selector { } impl Selector { - fn function_definition(&mut self) -> Result>> { + fn function_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::FunctionKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::FunctionName))?), @@ -483,7 +495,7 @@ impl Selector { } impl Selector { - fn parameters_declaration(&mut self) -> Result>> { + fn parameters_declaration(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Parameters))?), @@ -493,7 +505,7 @@ impl Selector { } impl Selector { - fn parameter(&mut self) -> Result>> { + fn parameter(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::StorageLocation))?, @@ -503,7 +515,7 @@ impl Selector { } impl Selector { - fn override_specifier(&mut self) -> Result>> { + fn override_specifier(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OverrideKeyword))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::OverridePathsDeclaration))?, @@ -512,7 +524,9 @@ impl Selector { } impl Selector { - fn override_paths_declaration(&mut self) -> Result>> { + fn override_paths_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::OverridePaths))?), @@ -522,7 +536,7 @@ impl Selector { } impl Selector { - fn returns_declaration(&mut self) -> Result>> { + fn returns_declaration(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ReturnsKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -531,7 +545,7 @@ impl Selector { } impl Selector { - fn constructor_definition(&mut self) -> Result>> { + fn constructor_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ConstructorKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -542,7 +556,9 @@ impl Selector { } impl Selector { - fn unnamed_function_definition(&mut self) -> Result>> { + fn unnamed_function_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::FunctionKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -553,7 +569,9 @@ impl Selector { } impl Selector { - fn fallback_function_definition(&mut self) -> Result>> { + fn fallback_function_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::FallbackKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -565,7 +583,9 @@ impl Selector { } impl Selector { - fn receive_function_definition(&mut self) -> Result>> { + fn receive_function_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ReceiveKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -576,7 +596,7 @@ impl Selector { } impl Selector { - fn modifier_definition(&mut self) -> Result>> { + fn modifier_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ModifierKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -588,7 +608,7 @@ impl Selector { } impl Selector { - fn modifier_invocation(&mut self) -> Result>> { + fn modifier_invocation(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::IdentifierPath))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::ArgumentsDeclaration))?, @@ -597,7 +617,7 @@ impl Selector { } impl Selector { - fn event_definition(&mut self) -> Result>> { + fn event_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::EventKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -609,7 +629,9 @@ impl Selector { } impl Selector { - fn event_parameters_declaration(&mut self) -> Result>> { + fn event_parameters_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::EventParameters))?), @@ -619,7 +641,7 @@ impl Selector { } impl Selector { - fn event_parameter(&mut self) -> Result>> { + fn event_parameter(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), self.try_select(|node| node.is_token_with_kind(TokenKind::IndexedKeyword))?, @@ -629,7 +651,9 @@ impl Selector { } impl Selector { - fn user_defined_value_type_definition(&mut self) -> Result>> { + fn user_defined_value_type_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::TypeKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -641,7 +665,7 @@ impl Selector { } impl Selector { - fn error_definition(&mut self) -> Result>> { + fn error_definition(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ErrorKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -652,7 +676,9 @@ impl Selector { } impl Selector { - fn error_parameters_declaration(&mut self) -> Result>> { + fn error_parameters_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ErrorParameters))?), @@ -662,7 +688,7 @@ impl Selector { } impl Selector { - fn error_parameter(&mut self) -> Result>> { + fn error_parameter(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), self.try_select(|node| node.is_token_with_kind(TokenKind::Identifier))?, @@ -671,7 +697,7 @@ impl Selector { } impl Selector { - fn array_type_name(&mut self) -> Result>> { + fn array_type_name(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBracket))?), @@ -682,7 +708,7 @@ impl Selector { } impl Selector { - fn function_type(&mut self) -> Result>> { + fn function_type(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::FunctionKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -693,7 +719,7 @@ impl Selector { } impl Selector { - fn mapping_type(&mut self) -> Result>> { + fn mapping_type(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::MappingKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -706,7 +732,7 @@ impl Selector { } impl Selector { - fn mapping_key(&mut self) -> Result>> { + fn mapping_key(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::MappingKeyType))?), self.try_select(|node| node.is_token_with_kind(TokenKind::Identifier))?, @@ -715,7 +741,7 @@ impl Selector { } impl Selector { - fn mapping_value(&mut self) -> Result>> { + fn mapping_value(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), self.try_select(|node| node.is_token_with_kind(TokenKind::Identifier))?, @@ -724,7 +750,7 @@ impl Selector { } impl Selector { - fn address_type(&mut self) -> Result>> { + fn address_type(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::AddressKeyword))?), self.try_select(|node| node.is_token_with_kind(TokenKind::PayableKeyword))?, @@ -733,7 +759,7 @@ impl Selector { } impl Selector { - fn block(&mut self) -> Result>> { + fn block(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Statements))?), @@ -743,7 +769,7 @@ impl Selector { } impl Selector { - fn unchecked_block(&mut self) -> Result>> { + fn unchecked_block(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::UncheckedKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Block))?), @@ -752,7 +778,7 @@ impl Selector { } impl Selector { - fn expression_statement(&mut self) -> Result>> { + fn expression_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Semicolon))?), @@ -761,7 +787,7 @@ impl Selector { } impl Selector { - fn assembly_statement(&mut self) -> Result>> { + fn assembly_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::AssemblyKeyword))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::StringLiteral))?, @@ -772,7 +798,9 @@ impl Selector { } impl Selector { - fn assembly_flags_declaration(&mut self) -> Result>> { + fn assembly_flags_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::AssemblyFlags))?), @@ -782,7 +810,9 @@ impl Selector { } impl Selector { - fn tuple_deconstruction_statement(&mut self) -> Result>> { + fn tuple_deconstruction_statement( + &mut self, + ) -> Result>>> { Ok(vec![ self.try_select(|node| node.is_token_with_kind(TokenKind::VarKeyword))?, Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -798,7 +828,9 @@ impl Selector { } impl Selector { - fn tuple_deconstruction_element(&mut self) -> Result>> { + fn tuple_deconstruction_element( + &mut self, + ) -> Result>>> { Ok(vec![self.try_select(|node| { node.is_rule_with_kind(RuleKind::TupleMember) })?]) @@ -806,7 +838,7 @@ impl Selector { } impl Selector { - fn typed_tuple_member(&mut self) -> Result>> { + fn typed_tuple_member(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::StorageLocation))?, @@ -816,7 +848,7 @@ impl Selector { } impl Selector { - fn untyped_tuple_member(&mut self) -> Result>> { + fn untyped_tuple_member(&mut self) -> Result>>> { Ok(vec![ self.try_select(|node| node.is_rule_with_kind(RuleKind::StorageLocation))?, Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), @@ -825,7 +857,9 @@ impl Selector { } impl Selector { - fn variable_declaration_statement(&mut self) -> Result>> { + fn variable_declaration_statement( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::VariableDeclarationType))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::StorageLocation))?, @@ -837,7 +871,9 @@ impl Selector { } impl Selector { - fn variable_declaration_value(&mut self) -> Result>> { + fn variable_declaration_value( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Equal))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), @@ -846,7 +882,7 @@ impl Selector { } impl Selector { - fn if_statement(&mut self) -> Result>> { + fn if_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::IfKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -859,7 +895,7 @@ impl Selector { } impl Selector { - fn else_branch(&mut self) -> Result>> { + fn else_branch(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ElseKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Statement))?), @@ -868,7 +904,7 @@ impl Selector { } impl Selector { - fn for_statement(&mut self) -> Result>> { + fn for_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ForKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -882,7 +918,7 @@ impl Selector { } impl Selector { - fn while_statement(&mut self) -> Result>> { + fn while_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::WhileKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -894,7 +930,7 @@ impl Selector { } impl Selector { - fn do_while_statement(&mut self) -> Result>> { + fn do_while_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::DoKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Statement))?), @@ -908,7 +944,7 @@ impl Selector { } impl Selector { - fn continue_statement(&mut self) -> Result>> { + fn continue_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ContinueKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Semicolon))?), @@ -917,7 +953,7 @@ impl Selector { } impl Selector { - fn break_statement(&mut self) -> Result>> { + fn break_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::BreakKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Semicolon))?), @@ -926,7 +962,7 @@ impl Selector { } impl Selector { - fn return_statement(&mut self) -> Result>> { + fn return_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ReturnKeyword))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::Expression))?, @@ -936,7 +972,7 @@ impl Selector { } impl Selector { - fn emit_statement(&mut self) -> Result>> { + fn emit_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::EmitKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::IdentifierPath))?), @@ -947,7 +983,7 @@ impl Selector { } impl Selector { - fn try_statement(&mut self) -> Result>> { + fn try_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::TryKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), @@ -959,7 +995,7 @@ impl Selector { } impl Selector { - fn catch_clause(&mut self) -> Result>> { + fn catch_clause(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::CatchKeyword))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::CatchClauseError))?, @@ -969,7 +1005,7 @@ impl Selector { } impl Selector { - fn catch_clause_error(&mut self) -> Result>> { + fn catch_clause_error(&mut self) -> Result>>> { Ok(vec![ self.try_select(|node| node.is_token_with_kind(TokenKind::Identifier))?, Some(self.select(|node| node.is_rule_with_kind(RuleKind::ParametersDeclaration))?), @@ -978,7 +1014,7 @@ impl Selector { } impl Selector { - fn revert_statement(&mut self) -> Result>> { + fn revert_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::RevertKeyword))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::IdentifierPath))?, @@ -989,7 +1025,7 @@ impl Selector { } impl Selector { - fn throw_statement(&mut self) -> Result>> { + fn throw_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::ThrowKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Semicolon))?), @@ -998,7 +1034,7 @@ impl Selector { } impl Selector { - fn assignment_expression(&mut self) -> Result>> { + fn assignment_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Equal))?), @@ -1008,7 +1044,7 @@ impl Selector { } impl Selector { - fn conditional_expression(&mut self) -> Result>> { + fn conditional_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::QuestionMark))?), @@ -1020,7 +1056,7 @@ impl Selector { } impl Selector { - fn or_expression(&mut self) -> Result>> { + fn or_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::BarBar))?), @@ -1030,7 +1066,7 @@ impl Selector { } impl Selector { - fn and_expression(&mut self) -> Result>> { + fn and_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::AmpersandAmpersand))?), @@ -1040,7 +1076,7 @@ impl Selector { } impl Selector { - fn equality_expression(&mut self) -> Result>> { + fn equality_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::EqualEqual))?), @@ -1050,7 +1086,7 @@ impl Selector { } impl Selector { - fn comparison_expression(&mut self) -> Result>> { + fn comparison_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::LessThan))?), @@ -1060,7 +1096,7 @@ impl Selector { } impl Selector { - fn bitwise_or_expression(&mut self) -> Result>> { + fn bitwise_or_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Bar))?), @@ -1070,7 +1106,7 @@ impl Selector { } impl Selector { - fn bitwise_xor_expression(&mut self) -> Result>> { + fn bitwise_xor_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Caret))?), @@ -1080,7 +1116,7 @@ impl Selector { } impl Selector { - fn bitwise_and_expression(&mut self) -> Result>> { + fn bitwise_and_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Ampersand))?), @@ -1090,7 +1126,7 @@ impl Selector { } impl Selector { - fn shift_expression(&mut self) -> Result>> { + fn shift_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::LessThanLessThan))?), @@ -1100,7 +1136,7 @@ impl Selector { } impl Selector { - fn additive_expression(&mut self) -> Result>> { + fn additive_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Plus))?), @@ -1110,7 +1146,9 @@ impl Selector { } impl Selector { - fn multiplicative_expression(&mut self) -> Result>> { + fn multiplicative_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Asterisk))?), @@ -1120,7 +1158,9 @@ impl Selector { } impl Selector { - fn exponentiation_expression(&mut self) -> Result>> { + fn exponentiation_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::AsteriskAsterisk))?), @@ -1130,7 +1170,7 @@ impl Selector { } impl Selector { - fn postfix_expression(&mut self) -> Result>> { + fn postfix_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::PlusPlus))?), @@ -1139,7 +1179,7 @@ impl Selector { } impl Selector { - fn prefix_expression(&mut self) -> Result>> { + fn prefix_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::PlusPlus))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), @@ -1148,7 +1188,9 @@ impl Selector { } impl Selector { - fn function_call_expression(&mut self) -> Result>> { + fn function_call_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ArgumentsDeclaration))?), @@ -1157,7 +1199,9 @@ impl Selector { } impl Selector { - fn call_options_expression(&mut self) -> Result>> { + fn call_options_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), @@ -1168,7 +1212,9 @@ impl Selector { } impl Selector { - fn member_access_expression(&mut self) -> Result>> { + fn member_access_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Period))?), @@ -1178,7 +1224,9 @@ impl Selector { } impl Selector { - fn index_access_expression(&mut self) -> Result>> { + fn index_access_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBracket))?), @@ -1190,7 +1238,7 @@ impl Selector { } impl Selector { - fn index_access_end(&mut self) -> Result>> { + fn index_access_end(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Colon))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::Expression))?, @@ -1199,7 +1247,9 @@ impl Selector { } impl Selector { - fn positional_arguments_declaration(&mut self) -> Result>> { + fn positional_arguments_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::PositionalArguments))?), @@ -1209,7 +1259,9 @@ impl Selector { } impl Selector { - fn named_arguments_declaration(&mut self) -> Result>> { + fn named_arguments_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::NamedArgumentGroup))?, @@ -1219,7 +1271,7 @@ impl Selector { } impl Selector { - fn named_argument_group(&mut self) -> Result>> { + fn named_argument_group(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::NamedArguments))?), @@ -1229,7 +1281,7 @@ impl Selector { } impl Selector { - fn named_argument(&mut self) -> Result>> { + fn named_argument(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Identifier))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Colon))?), @@ -1239,7 +1291,7 @@ impl Selector { } impl Selector { - fn type_expression(&mut self) -> Result>> { + fn type_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::TypeKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -1250,7 +1302,7 @@ impl Selector { } impl Selector { - fn new_expression(&mut self) -> Result>> { + fn new_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::NewKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::TypeName))?), @@ -1259,7 +1311,7 @@ impl Selector { } impl Selector { - fn tuple_expression(&mut self) -> Result>> { + fn tuple_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::TupleValues))?), @@ -1269,7 +1321,7 @@ impl Selector { } impl Selector { - fn tuple_value(&mut self) -> Result>> { + fn tuple_value(&mut self) -> Result>>> { Ok(vec![self.try_select(|node| { node.is_rule_with_kind(RuleKind::Expression) })?]) @@ -1277,7 +1329,7 @@ impl Selector { } impl Selector { - fn array_expression(&mut self) -> Result>> { + fn array_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBracket))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::ArrayValues))?), @@ -1287,7 +1339,7 @@ impl Selector { } impl Selector { - fn hex_number_expression(&mut self) -> Result>> { + fn hex_number_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::HexLiteral))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::NumberUnit))?, @@ -1296,7 +1348,9 @@ impl Selector { } impl Selector { - fn decimal_number_expression(&mut self) -> Result>> { + fn decimal_number_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::DecimalLiteral))?), self.try_select(|node| node.is_rule_with_kind(RuleKind::NumberUnit))?, @@ -1305,7 +1359,7 @@ impl Selector { } impl Selector { - fn yul_block(&mut self) -> Result>> { + fn yul_block(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBrace))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulStatements))?), @@ -1315,7 +1369,9 @@ impl Selector { } impl Selector { - fn yul_function_definition(&mut self) -> Result>> { + fn yul_function_definition( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulFunctionKeyword))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::YulIdentifier))?), @@ -1327,7 +1383,9 @@ impl Selector { } impl Selector { - fn yul_parameters_declaration(&mut self) -> Result>> { + fn yul_parameters_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulParameters))?), @@ -1337,7 +1395,9 @@ impl Selector { } impl Selector { - fn yul_returns_declaration(&mut self) -> Result>> { + fn yul_returns_declaration( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::MinusGreaterThan))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulReturnVariables))?), @@ -1346,7 +1406,9 @@ impl Selector { } impl Selector { - fn yul_variable_declaration_statement(&mut self) -> Result>> { + fn yul_variable_declaration_statement( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulLetKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulIdentifierPaths))?), @@ -1356,7 +1418,9 @@ impl Selector { } impl Selector { - fn yul_variable_declaration_value(&mut self) -> Result>> { + fn yul_variable_declaration_value( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulAssignmentOperator))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulExpression))?), @@ -1365,7 +1429,9 @@ impl Selector { } impl Selector { - fn yul_assignment_statement(&mut self) -> Result>> { + fn yul_assignment_statement( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulIdentifierPaths))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulAssignmentOperator))?), @@ -1375,7 +1441,7 @@ impl Selector { } impl Selector { - fn yul_colon_and_equal(&mut self) -> Result>> { + fn yul_colon_and_equal(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Colon))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Equal))?), @@ -1384,7 +1450,7 @@ impl Selector { } impl Selector { - fn yul_if_statement(&mut self) -> Result>> { + fn yul_if_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulIfKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulExpression))?), @@ -1394,7 +1460,7 @@ impl Selector { } impl Selector { - fn yul_for_statement(&mut self) -> Result>> { + fn yul_for_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulForKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulBlock))?), @@ -1406,7 +1472,7 @@ impl Selector { } impl Selector { - fn yul_switch_statement(&mut self) -> Result>> { + fn yul_switch_statement(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulSwitchKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulExpression))?), @@ -1416,7 +1482,7 @@ impl Selector { } impl Selector { - fn yul_default_case(&mut self) -> Result>> { + fn yul_default_case(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulDefaultKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulBlock))?), @@ -1425,7 +1491,7 @@ impl Selector { } impl Selector { - fn yul_value_case(&mut self) -> Result>> { + fn yul_value_case(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulCaseKeyword))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulLiteral))?), @@ -1435,7 +1501,7 @@ impl Selector { } impl Selector { - fn yul_leave_statement(&mut self) -> Result>> { + fn yul_leave_statement(&mut self) -> Result>>> { Ok(vec![Some(self.select(|node| { node.is_token_with_kind(TokenKind::YulLeaveKeyword) })?)]) @@ -1443,7 +1509,7 @@ impl Selector { } impl Selector { - fn yul_break_statement(&mut self) -> Result>> { + fn yul_break_statement(&mut self) -> Result>>> { Ok(vec![Some(self.select(|node| { node.is_token_with_kind(TokenKind::YulBreakKeyword) })?)]) @@ -1451,7 +1517,7 @@ impl Selector { } impl Selector { - fn yul_continue_statement(&mut self) -> Result>> { + fn yul_continue_statement(&mut self) -> Result>>> { Ok(vec![Some(self.select(|node| { node.is_token_with_kind(TokenKind::YulContinueKeyword) })?)]) @@ -1459,7 +1525,7 @@ impl Selector { } impl Selector { - fn yul_label(&mut self) -> Result>> { + fn yul_label(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::YulIdentifier))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Colon))?), @@ -1468,7 +1534,9 @@ impl Selector { } impl Selector { - fn yul_function_call_expression(&mut self) -> Result>> { + fn yul_function_call_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::YulExpression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenParen))?), @@ -1485,9 +1553,8 @@ impl Selector { #[napi(namespace = "ast_internal", ts_return_type = "cst.Node", catch_unwind)] pub fn select_choice( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result { - let mut selector = Selector::new(node, env); +) -> Result> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnitMember => selector.source_unit_member()?, @@ -1542,7 +1609,7 @@ pub fn select_choice( } impl Selector { - fn source_unit_member(&mut self) -> Result { + fn source_unit_member(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::PragmaDirective, @@ -1564,7 +1631,7 @@ impl Selector { } impl Selector { - fn pragma(&mut self) -> Result { + fn pragma(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::ABICoderPragma, @@ -1576,7 +1643,7 @@ impl Selector { } impl Selector { - fn experimental_feature(&mut self) -> Result { + fn experimental_feature(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::StringLiteral) || node.is_token_with_kind(TokenKind::Identifier) @@ -1585,7 +1652,7 @@ impl Selector { } impl Selector { - fn version_pragma_expression(&mut self) -> Result { + fn version_pragma_expression(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::VersionPragmaOrExpression, @@ -1598,7 +1665,7 @@ impl Selector { } impl Selector { - fn import_clause(&mut self) -> Result { + fn import_clause(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::PathImport, @@ -1610,7 +1677,7 @@ impl Selector { } impl Selector { - fn using_clause(&mut self) -> Result { + fn using_clause(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::IdentifierPath, RuleKind::UsingDeconstruction]) }) @@ -1618,7 +1685,7 @@ impl Selector { } impl Selector { - fn using_operator(&mut self) -> Result { + fn using_operator(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::Ampersand, @@ -1642,7 +1709,7 @@ impl Selector { } impl Selector { - fn using_target(&mut self) -> Result { + fn using_target(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::TypeName) || node.is_token_with_kind(TokenKind::Asterisk) @@ -1651,7 +1718,7 @@ impl Selector { } impl Selector { - fn contract_member(&mut self) -> Result { + fn contract_member(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::UsingDirective, @@ -1673,7 +1740,7 @@ impl Selector { } impl Selector { - fn state_variable_attribute(&mut self) -> Result { + fn state_variable_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::OverrideSpecifier) || node.is_token_with_kinds(&[ @@ -1688,7 +1755,7 @@ impl Selector { } impl Selector { - fn function_name(&mut self) -> Result { + fn function_name(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::Identifier, @@ -1700,7 +1767,7 @@ impl Selector { } impl Selector { - fn function_attribute(&mut self) -> Result { + fn function_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::ModifierInvocation, RuleKind::OverrideSpecifier]) || node.is_token_with_kinds(&[ @@ -1719,7 +1786,7 @@ impl Selector { } impl Selector { - fn function_body(&mut self) -> Result { + fn function_body(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::Block) || node.is_token_with_kind(TokenKind::Semicolon) }) @@ -1727,7 +1794,7 @@ impl Selector { } impl Selector { - fn constructor_attribute(&mut self) -> Result { + fn constructor_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::ModifierInvocation) || node.is_token_with_kinds(&[ @@ -1742,7 +1809,7 @@ impl Selector { } impl Selector { - fn unnamed_function_attribute(&mut self) -> Result { + fn unnamed_function_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::ModifierInvocation) || node.is_token_with_kinds(&[ @@ -1760,7 +1827,7 @@ impl Selector { } impl Selector { - fn fallback_function_attribute(&mut self) -> Result { + fn fallback_function_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::ModifierInvocation, RuleKind::OverrideSpecifier]) || node.is_token_with_kinds(&[ @@ -1775,7 +1842,7 @@ impl Selector { } impl Selector { - fn receive_function_attribute(&mut self) -> Result { + fn receive_function_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::ModifierInvocation, RuleKind::OverrideSpecifier]) || node.is_token_with_kinds(&[ @@ -1788,7 +1855,7 @@ impl Selector { } impl Selector { - fn modifier_attribute(&mut self) -> Result { + fn modifier_attribute(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::OverrideSpecifier) || node.is_token_with_kind(TokenKind::VirtualKeyword) @@ -1797,7 +1864,7 @@ impl Selector { } impl Selector { - fn type_name(&mut self) -> Result { + fn type_name(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::ArrayTypeName, @@ -1811,7 +1878,7 @@ impl Selector { } impl Selector { - fn function_type_attribute(&mut self) -> Result { + fn function_type_attribute(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::InternalKeyword, @@ -1828,7 +1895,7 @@ impl Selector { } impl Selector { - fn mapping_key_type(&mut self) -> Result { + fn mapping_key_type(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::ElementaryType, RuleKind::IdentifierPath]) }) @@ -1836,7 +1903,7 @@ impl Selector { } impl Selector { - fn elementary_type(&mut self) -> Result { + fn elementary_type(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::AddressType) || node.is_token_with_kinds(&[ @@ -1854,7 +1921,7 @@ impl Selector { } impl Selector { - fn statement(&mut self) -> Result { + fn statement(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::ExpressionStatement, @@ -1880,7 +1947,7 @@ impl Selector { } impl Selector { - fn tuple_member(&mut self) -> Result { + fn tuple_member(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::TypedTupleMember, RuleKind::UntypedTupleMember]) }) @@ -1888,7 +1955,7 @@ impl Selector { } impl Selector { - fn variable_declaration_type(&mut self) -> Result { + fn variable_declaration_type(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::TypeName) || node.is_token_with_kind(TokenKind::VarKeyword) @@ -1897,7 +1964,7 @@ impl Selector { } impl Selector { - fn storage_location(&mut self) -> Result { + fn storage_location(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::MemoryKeyword, @@ -1909,7 +1976,7 @@ impl Selector { } impl Selector { - fn for_statement_initialization(&mut self) -> Result { + fn for_statement_initialization(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::ExpressionStatement, @@ -1921,7 +1988,7 @@ impl Selector { } impl Selector { - fn for_statement_condition(&mut self) -> Result { + fn for_statement_condition(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::ExpressionStatement) || node.is_token_with_kind(TokenKind::Semicolon) @@ -1930,7 +1997,7 @@ impl Selector { } impl Selector { - fn expression(&mut self) -> Result { + fn expression(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::AssignmentExpression, @@ -1971,7 +2038,7 @@ impl Selector { } impl Selector { - fn member_access(&mut self) -> Result { + fn member_access(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[TokenKind::Identifier, TokenKind::AddressKeyword]) }) @@ -1979,7 +2046,7 @@ impl Selector { } impl Selector { - fn arguments_declaration(&mut self) -> Result { + fn arguments_declaration(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::PositionalArgumentsDeclaration, @@ -1990,7 +2057,7 @@ impl Selector { } impl Selector { - fn number_unit(&mut self) -> Result { + fn number_unit(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::WeiKeyword, @@ -2010,7 +2077,7 @@ impl Selector { } impl Selector { - fn string_expression(&mut self) -> Result { + fn string_expression(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::StringLiteral, @@ -2024,7 +2091,7 @@ impl Selector { } impl Selector { - fn string_literal(&mut self) -> Result { + fn string_literal(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::SingleQuotedStringLiteral, @@ -2035,7 +2102,7 @@ impl Selector { } impl Selector { - fn hex_string_literal(&mut self) -> Result { + fn hex_string_literal(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::SingleQuotedHexStringLiteral, @@ -2046,7 +2113,7 @@ impl Selector { } impl Selector { - fn unicode_string_literal(&mut self) -> Result { + fn unicode_string_literal(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::SingleQuotedUnicodeStringLiteral, @@ -2057,7 +2124,7 @@ impl Selector { } impl Selector { - fn yul_statement(&mut self) -> Result { + fn yul_statement(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::YulBlock, @@ -2078,7 +2145,7 @@ impl Selector { } impl Selector { - fn yul_assignment_operator(&mut self) -> Result { + fn yul_assignment_operator(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::YulColonAndEqual) || node.is_token_with_kind(TokenKind::ColonEqual) @@ -2087,7 +2154,7 @@ impl Selector { } impl Selector { - fn yul_switch_case(&mut self) -> Result { + fn yul_switch_case(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::YulDefaultCase, RuleKind::YulValueCase]) }) @@ -2095,7 +2162,7 @@ impl Selector { } impl Selector { - fn yul_expression(&mut self) -> Result { + fn yul_expression(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::YulFunctionCallExpression, @@ -2108,7 +2175,7 @@ impl Selector { } impl Selector { - fn yul_built_in_function(&mut self) -> Result { + fn yul_built_in_function(&mut self) -> Result> { self.select(|node| { node.is_token_with_kinds(&[ TokenKind::YulAddKeyword, @@ -2193,7 +2260,7 @@ impl Selector { } impl Selector { - fn yul_literal(&mut self) -> Result { + fn yul_literal(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[RuleKind::HexStringLiteral, RuleKind::StringLiteral]) || node.is_token_with_kinds(&[ @@ -2217,9 +2284,8 @@ impl Selector { )] pub fn select_repeated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result> { - let mut selector = Selector::new(node, env); +) -> Result>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnitMembers => selector.source_unit_members()?, @@ -2253,7 +2319,7 @@ pub fn select_repeated( } impl Selector { - fn source_unit_members(&mut self) -> Result> { + fn source_unit_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2267,7 +2333,7 @@ impl Selector { } impl Selector { - fn version_pragma_expressions(&mut self) -> Result> { + fn version_pragma_expressions(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2281,7 +2347,7 @@ impl Selector { } impl Selector { - fn contract_members(&mut self) -> Result> { + fn contract_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2295,7 +2361,7 @@ impl Selector { } impl Selector { - fn interface_members(&mut self) -> Result> { + fn interface_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2309,7 +2375,7 @@ impl Selector { } impl Selector { - fn library_members(&mut self) -> Result> { + fn library_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2323,7 +2389,7 @@ impl Selector { } impl Selector { - fn struct_members(&mut self) -> Result> { + fn struct_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2337,7 +2403,7 @@ impl Selector { } impl Selector { - fn state_variable_attributes(&mut self) -> Result> { + fn state_variable_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2351,7 +2417,7 @@ impl Selector { } impl Selector { - fn function_attributes(&mut self) -> Result> { + fn function_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2365,7 +2431,7 @@ impl Selector { } impl Selector { - fn constructor_attributes(&mut self) -> Result> { + fn constructor_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2379,7 +2445,7 @@ impl Selector { } impl Selector { - fn unnamed_function_attributes(&mut self) -> Result> { + fn unnamed_function_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2393,7 +2459,7 @@ impl Selector { } impl Selector { - fn fallback_function_attributes(&mut self) -> Result> { + fn fallback_function_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2407,7 +2473,7 @@ impl Selector { } impl Selector { - fn receive_function_attributes(&mut self) -> Result> { + fn receive_function_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2421,7 +2487,7 @@ impl Selector { } impl Selector { - fn modifier_attributes(&mut self) -> Result> { + fn modifier_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2435,7 +2501,7 @@ impl Selector { } impl Selector { - fn function_type_attributes(&mut self) -> Result> { + fn function_type_attributes(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2449,7 +2515,7 @@ impl Selector { } impl Selector { - fn statements(&mut self) -> Result> { + fn statements(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2463,7 +2529,7 @@ impl Selector { } impl Selector { - fn catch_clauses(&mut self) -> Result> { + fn catch_clauses(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2477,7 +2543,7 @@ impl Selector { } impl Selector { - fn string_literals(&mut self) -> Result> { + fn string_literals(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2491,7 +2557,7 @@ impl Selector { } impl Selector { - fn hex_string_literals(&mut self) -> Result> { + fn hex_string_literals(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2505,7 +2571,7 @@ impl Selector { } impl Selector { - fn unicode_string_literals(&mut self) -> Result> { + fn unicode_string_literals(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2519,7 +2585,7 @@ impl Selector { } impl Selector { - fn yul_statements(&mut self) -> Result> { + fn yul_statements(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2533,7 +2599,7 @@ impl Selector { } impl Selector { - fn yul_switch_cases(&mut self) -> Result> { + fn yul_switch_cases(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -2557,9 +2623,8 @@ impl Selector { )] pub fn select_separated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::VersionPragmaSpecifier => selector.version_pragma_specifier()?, @@ -2594,7 +2659,7 @@ pub fn select_separated( } impl Selector { - fn version_pragma_specifier(&mut self) -> Result>> { + fn version_pragma_specifier(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2619,7 +2684,9 @@ impl Selector { } impl Selector { - fn import_deconstruction_symbols(&mut self) -> Result>> { + fn import_deconstruction_symbols( + &mut self, + ) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2646,7 +2713,9 @@ impl Selector { } impl Selector { - fn using_deconstruction_symbols(&mut self) -> Result>> { + fn using_deconstruction_symbols( + &mut self, + ) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2673,7 +2742,7 @@ impl Selector { } impl Selector { - fn inheritance_types(&mut self) -> Result>> { + fn inheritance_types(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2697,7 +2766,7 @@ impl Selector { } impl Selector { - fn enum_members(&mut self) -> Result>> { + fn enum_members(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2720,7 +2789,7 @@ impl Selector { } impl Selector { - fn parameters(&mut self) -> Result>> { + fn parameters(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2741,7 +2810,7 @@ impl Selector { } impl Selector { - fn override_paths(&mut self) -> Result>> { + fn override_paths(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2765,7 +2834,7 @@ impl Selector { } impl Selector { - fn event_parameters(&mut self) -> Result>> { + fn event_parameters(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2789,7 +2858,7 @@ impl Selector { } impl Selector { - fn error_parameters(&mut self) -> Result>> { + fn error_parameters(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2813,7 +2882,7 @@ impl Selector { } impl Selector { - fn assembly_flags(&mut self) -> Result>> { + fn assembly_flags(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2837,7 +2906,9 @@ impl Selector { } impl Selector { - fn tuple_deconstruction_elements(&mut self) -> Result>> { + fn tuple_deconstruction_elements( + &mut self, + ) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2864,7 +2935,7 @@ impl Selector { } impl Selector { - fn positional_arguments(&mut self) -> Result>> { + fn positional_arguments(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2885,7 +2956,7 @@ impl Selector { } impl Selector { - fn named_arguments(&mut self) -> Result>> { + fn named_arguments(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2909,7 +2980,7 @@ impl Selector { } impl Selector { - fn call_options(&mut self) -> Result>> { + fn call_options(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2933,7 +3004,7 @@ impl Selector { } impl Selector { - fn tuple_values(&mut self) -> Result>> { + fn tuple_values(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2954,7 +3025,7 @@ impl Selector { } impl Selector { - fn array_values(&mut self) -> Result>> { + fn array_values(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2975,7 +3046,7 @@ impl Selector { } impl Selector { - fn identifier_path(&mut self) -> Result>> { + fn identifier_path(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -2998,7 +3069,7 @@ impl Selector { } impl Selector { - fn yul_parameters(&mut self) -> Result>> { + fn yul_parameters(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -3022,7 +3093,7 @@ impl Selector { } impl Selector { - fn yul_return_variables(&mut self) -> Result>> { + fn yul_return_variables(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -3046,7 +3117,7 @@ impl Selector { } impl Selector { - fn yul_arguments(&mut self) -> Result>> { + fn yul_arguments(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -3070,7 +3141,7 @@ impl Selector { } impl Selector { - fn yul_identifier_paths(&mut self) -> Result>> { + fn yul_identifier_paths(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -3094,7 +3165,7 @@ impl Selector { } impl Selector { - fn yul_identifier_path(&mut self) -> Result>> { + fn yul_identifier_path(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -3122,28 +3193,32 @@ impl Selector { // struct Selector { - env: Env, node: Rc, index: usize, } impl Selector { - fn new(node: &RuleNode, env: Env) -> Self { + fn new(node: &RuleNode) -> Self { Self { - env, node: node.0.clone(), index: 0, } } - fn select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result { + fn select( + &mut self, + filter: impl FnOnce(&RustNode) -> bool, + ) -> Result> { match self.try_select(filter)? { Some(node) => Ok(node), None => Error::MissingChild(self.index).into(), } } - fn try_select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result> { + fn try_select( + &mut self, + filter: impl FnOnce(&RustNode) -> bool, + ) -> Result>> { while let Some(child) = self.node.children.get(self.index) { match child { node if node.is_trivia() => { @@ -3157,9 +3232,9 @@ impl Selector { } if matches!(token.kind, TokenKind::SKIPPED) => { return Error::SkippedToken(self.index).into(); } - node if filter(node) => { + labeled if filter(labeled) => { self.index += 1; - return Ok(Some(node.to_js(self.env))); + return Ok(Some(labeled.node.clone().into_either())); } _ => { break; diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs index a942ae4a0a..8cff5d566e 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cst.rs @@ -2,8 +2,6 @@ use std::rc::Rc; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -18,6 +16,16 @@ pub enum NodeType { Token, } +impl RustNode { + /// Converts a Rust node into a choice of NAPI-exposed wrapper structs. + pub fn into_either(self) -> napi::Either { + match self { + RustNode::Rule(rule) => napi::Either::A(RuleNode(rule)), + RustNode::Token(token) => napi::Either::B(TokenNode(token)), + } + } +} + #[napi(namespace = "cst")] pub struct RuleNode(pub(crate) Rc); @@ -51,12 +59,12 @@ impl RuleNode { (&self.0.text_len).into() } - #[napi(ts_return_type = "Array", catch_unwind)] - pub fn children(&self, env: Env) -> Vec { + #[napi(catch_unwind)] + pub fn children(&self) -> Vec> { self.0 .children .iter() - .map(|child| child.to_js(env)) + .map(|child| child.node.clone().into_either()) .collect() } @@ -119,34 +127,3 @@ impl TokenNode { .into() } } - -pub(crate) trait ToJS { - fn to_js(&self, env: Env) -> JsObject; -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - RuleNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - TokenNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for RustNode { - fn to_js(&self, env: Env) -> JsObject { - match self { - RustNode::Rule(rust_rule_node) => rust_rule_node.to_js(env), - RustNode::Token(rust_token_node) => rust_token_node.to_js(env), - } - } -} diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs index 0065bafcf1..0f56de926a 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs @@ -5,13 +5,11 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use cst::ToJS; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use text_index::{TextIndex, TextRange}; -use crate::napi_interface::{cst, text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; +use crate::napi_interface::cst::{self, RuleNode, TokenNode}; +use crate::napi_interface::{text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; #[napi(namespace = "cursor")] pub struct Cursor(pub(super) RustCursor); @@ -55,8 +53,8 @@ impl Cursor { } #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn node(&self, env: Env) -> JsObject { - self.0.node().to_js(env) + pub fn node(&self) -> napi::Either { + self.0.node().into_either() } #[napi(getter, ts_return_type = "kinds.NodeLabel", catch_unwind)] diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_output.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_output.rs index 7f276a0ddc..d50d8a1fb5 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_output.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/parse_output.rs @@ -1,7 +1,5 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use cst::ToJS; -use napi::bindgen_prelude::Env; use napi_derive::napi; use crate::napi_interface::{cst, cursor, parse_error, RustParseOutput}; @@ -18,8 +16,8 @@ impl From for ParseOutput { #[napi(namespace = "parse_output")] impl ParseOutput { #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn tree(&self, env: Env) -> napi::JsObject { - self.0.tree().to_js(env) + pub fn tree(&self) -> napi::Either { + self.0.tree().into_either() } #[napi(ts_return_type = "Array", catch_unwind)] diff --git a/crates/solidity/outputs/npm/package/src/generated/index.d.ts b/crates/solidity/outputs/npm/package/src/generated/index.d.ts index 9d31b9bc2e..a1538afe4f 100644 --- a/crates/solidity/outputs/npm/package/src/generated/index.d.ts +++ b/crates/solidity/outputs/npm/package/src/generated/index.d.ts @@ -744,7 +744,7 @@ export namespace cst { get type(): NodeType.Rule; get kind(): kinds.RuleKind; get textLength(): text_index.TextIndex; - children(): Array; + children(): Array; createCursor(textOffset: text_index.TextIndex): cursor.Cursor; unparse(): string; } diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/ast_selectors.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/ast_selectors.rs index 7cea58e359..cf7f98a956 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/ast_selectors.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/ast_selectors.rs @@ -4,10 +4,9 @@ use std::rc::Rc; -use napi::{Env, JsObject}; use napi_derive::napi; -use crate::napi_interface::cst::{RuleNode, ToJS}; +use crate::napi_interface::cst::{RuleNode, TokenNode}; use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, TokenKind}; // @@ -21,9 +20,8 @@ use crate::napi_interface::{RuleKind, RustLabeledNode, RustNode, RustRuleNode, T )] pub fn select_sequence( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnit => selector.source_unit()?, @@ -42,7 +40,7 @@ pub fn select_sequence( } impl Selector { - fn source_unit(&mut self) -> Result>> { + fn source_unit(&mut self) -> Result>>> { Ok(vec![Some(self.select(|node| { node.is_rule_with_kind(RuleKind::SourceUnitMembers) })?)]) @@ -50,7 +48,7 @@ impl Selector { } impl Selector { - fn tree(&mut self) -> Result>> { + fn tree(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::TreeKeyword))?), self.try_select(|node| node.is_token_with_kind(TokenKind::Identifier))?, @@ -61,7 +59,7 @@ impl Selector { } impl Selector { - fn tree_node(&mut self) -> Result>> { + fn tree_node(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::OpenBracket))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::TreeNodeChildren))?), @@ -71,7 +69,7 @@ impl Selector { } impl Selector { - fn addition_expression(&mut self) -> Result>> { + fn addition_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Plus))?), @@ -81,7 +79,7 @@ impl Selector { } impl Selector { - fn negation_expression(&mut self) -> Result>> { + fn negation_expression(&mut self) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_token_with_kind(TokenKind::Bang))?), Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), @@ -90,7 +88,9 @@ impl Selector { } impl Selector { - fn member_access_expression(&mut self) -> Result>> { + fn member_access_expression( + &mut self, + ) -> Result>>> { Ok(vec![ Some(self.select(|node| node.is_rule_with_kind(RuleKind::Expression))?), Some(self.select(|node| node.is_token_with_kind(TokenKind::Period))?), @@ -106,9 +106,8 @@ impl Selector { #[napi(namespace = "ast_internal", ts_return_type = "cst.Node", catch_unwind)] pub fn select_choice( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result { - let mut selector = Selector::new(node, env); +) -> Result> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnitMember => selector.source_unit_member()?, @@ -125,7 +124,7 @@ pub fn select_choice( } impl Selector { - fn source_unit_member(&mut self) -> Result { + fn source_unit_member(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::Tree, @@ -138,7 +137,7 @@ impl Selector { } impl Selector { - fn tree_node_child(&mut self) -> Result { + fn tree_node_child(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kind(RuleKind::TreeNode) || node.is_token_with_kind(TokenKind::DelimitedIdentifier) @@ -147,7 +146,7 @@ impl Selector { } impl Selector { - fn expression(&mut self) -> Result { + fn expression(&mut self) -> Result> { self.select(|node| { node.is_rule_with_kinds(&[ RuleKind::AdditionExpression, @@ -159,7 +158,7 @@ impl Selector { } impl Selector { - fn literal(&mut self) -> Result { + fn literal(&mut self) -> Result> { self.select(|node| node.is_token_with_kind(TokenKind::StringLiteral)) } } @@ -175,9 +174,8 @@ impl Selector { )] pub fn select_repeated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result> { - let mut selector = Selector::new(node, env); +) -> Result>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SourceUnitMembers => selector.source_unit_members()?, @@ -192,7 +190,7 @@ pub fn select_repeated( } impl Selector { - fn source_unit_members(&mut self) -> Result> { + fn source_unit_members(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -206,7 +204,7 @@ impl Selector { } impl Selector { - fn tree_node_children(&mut self) -> Result> { + fn tree_node_children(&mut self) -> Result>> { let mut items = vec![]; while let Some(item) = @@ -230,9 +228,8 @@ impl Selector { )] pub fn select_separated( #[napi(ts_arg_type = "cst.RuleNode")] node: &RuleNode, - env: Env, -) -> Result>> { - let mut selector = Selector::new(node, env); +) -> Result>>> { + let mut selector = Selector::new(node); let result = match node.kind() { RuleKind::SeparatedIdentifiers => selector.separated_identifiers()?, @@ -246,7 +243,7 @@ pub fn select_separated( } impl Selector { - fn separated_identifiers(&mut self) -> Result>> { + fn separated_identifiers(&mut self) -> Result>>> { let mut separated = vec![]; let mut separators = vec![]; @@ -273,28 +270,32 @@ impl Selector { // struct Selector { - env: Env, node: Rc, index: usize, } impl Selector { - fn new(node: &RuleNode, env: Env) -> Self { + fn new(node: &RuleNode) -> Self { Self { - env, node: node.0.clone(), index: 0, } } - fn select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result { + fn select( + &mut self, + filter: impl FnOnce(&RustNode) -> bool, + ) -> Result> { match self.try_select(filter)? { Some(node) => Ok(node), None => Error::MissingChild(self.index).into(), } } - fn try_select(&mut self, filter: impl FnOnce(&RustNode) -> bool) -> Result> { + fn try_select( + &mut self, + filter: impl FnOnce(&RustNode) -> bool, + ) -> Result>> { while let Some(child) = self.node.children.get(self.index) { match child { node if node.is_trivia() => { @@ -308,9 +309,9 @@ impl Selector { } if matches!(token.kind, TokenKind::SKIPPED) => { return Error::SkippedToken(self.index).into(); } - node if filter(node) => { + labeled if filter(labeled) => { self.index += 1; - return Ok(Some(node.to_js(self.env))); + return Ok(Some(labeled.node.clone().into_either())); } _ => { break; diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs index a942ae4a0a..8cff5d566e 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cst.rs @@ -2,8 +2,6 @@ use std::rc::Rc; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use crate::napi_interface::cursor::Cursor; @@ -18,6 +16,16 @@ pub enum NodeType { Token, } +impl RustNode { + /// Converts a Rust node into a choice of NAPI-exposed wrapper structs. + pub fn into_either(self) -> napi::Either { + match self { + RustNode::Rule(rule) => napi::Either::A(RuleNode(rule)), + RustNode::Token(token) => napi::Either::B(TokenNode(token)), + } + } +} + #[napi(namespace = "cst")] pub struct RuleNode(pub(crate) Rc); @@ -51,12 +59,12 @@ impl RuleNode { (&self.0.text_len).into() } - #[napi(ts_return_type = "Array", catch_unwind)] - pub fn children(&self, env: Env) -> Vec { + #[napi(catch_unwind)] + pub fn children(&self) -> Vec> { self.0 .children .iter() - .map(|child| child.to_js(env)) + .map(|child| child.node.clone().into_either()) .collect() } @@ -119,34 +127,3 @@ impl TokenNode { .into() } } - -pub(crate) trait ToJS { - fn to_js(&self, env: Env) -> JsObject; -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - RuleNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for Rc { - fn to_js(&self, env: Env) -> JsObject { - TokenNode(self.clone()) - .into_instance(env) - .expect("Class constructor to be defined by #[napi]") - .as_object(env) - } -} - -impl ToJS for RustNode { - fn to_js(&self, env: Env) -> JsObject { - match self { - RustNode::Rule(rust_rule_node) => rust_rule_node.to_js(env), - RustNode::Token(rust_token_node) => rust_token_node.to_js(env), - } - } -} diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs index 0065bafcf1..0f56de926a 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/cursor.rs @@ -5,13 +5,11 @@ // The functions are meant to be definitions for export, so they're not really used #![allow(clippy::return_self_not_must_use)] -use cst::ToJS; -use napi::bindgen_prelude::Env; -use napi::JsObject; use napi_derive::napi; use text_index::{TextIndex, TextRange}; -use crate::napi_interface::{cst, text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; +use crate::napi_interface::cst::{self, RuleNode, TokenNode}; +use crate::napi_interface::{text_index, NodeLabel, RuleKind, RustCursor, TokenKind}; #[napi(namespace = "cursor")] pub struct Cursor(pub(super) RustCursor); @@ -55,8 +53,8 @@ impl Cursor { } #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn node(&self, env: Env) -> JsObject { - self.0.node().to_js(env) + pub fn node(&self) -> napi::Either { + self.0.node().into_either() } #[napi(getter, ts_return_type = "kinds.NodeLabel", catch_unwind)] diff --git a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_output.rs b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_output.rs index 7f276a0ddc..d50d8a1fb5 100644 --- a/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_output.rs +++ b/crates/testlang/outputs/cargo/slang_testlang/src/generated/napi_interface/parse_output.rs @@ -1,7 +1,5 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use cst::ToJS; -use napi::bindgen_prelude::Env; use napi_derive::napi; use crate::napi_interface::{cst, cursor, parse_error, RustParseOutput}; @@ -18,8 +16,8 @@ impl From for ParseOutput { #[napi(namespace = "parse_output")] impl ParseOutput { #[napi(ts_return_type = "cst.Node", catch_unwind)] - pub fn tree(&self, env: Env) -> napi::JsObject { - self.0.tree().to_js(env) + pub fn tree(&self) -> napi::Either { + self.0.tree().into_either() } #[napi(ts_return_type = "Array", catch_unwind)] diff --git a/crates/testlang/outputs/npm/package/src/generated/index.d.ts b/crates/testlang/outputs/npm/package/src/generated/index.d.ts index 4fa4bda6db..a7fbd92472 100644 --- a/crates/testlang/outputs/npm/package/src/generated/index.d.ts +++ b/crates/testlang/outputs/npm/package/src/generated/index.d.ts @@ -85,7 +85,7 @@ export namespace cst { get type(): NodeType.Rule; get kind(): kinds.RuleKind; get textLength(): text_index.TextIndex; - children(): Array; + children(): Array; createCursor(textOffset: text_index.TextIndex): cursor.Cursor; unparse(): string; }