From cb9d45172e9cd9583fef4642d8a7426d2abe589b Mon Sep 17 00:00:00 2001 From: Beta Ziliani Date: Wed, 11 Dec 2024 15:41:49 -0300 Subject: [PATCH] Changing parse_result to return a NonTerminalNode instead of a Node --- .../runtime/compilation/internal_builder.rs | 6 +++++- .../crate/src/runtime/parser/parse_output.rs | 12 ++++++----- .../parser/parser_support/parser_function.rs | 17 ++++++---------- .../runtime/interface/generated/parser.wit | 4 ++-- .../src/runtime/interface/parser.wit.jinja2 | 4 ++-- .../wasm/src/runtime/wrappers/parser/mod.rs | 6 +++--- .../nomic-foundation-slang-parser.d.ts | 6 +++--- crates/metaslang/cst/generated/public_api.txt | 2 ++ crates/metaslang/cst/src/nodes.rs | 20 ++++++++++++------- .../cargo/crate/generated/public_api.txt | 4 ++++ .../crate/src/extensions/bindings/mod.rs | 3 ++- .../generated/compilation/internal_builder.rs | 6 +++++- .../src/generated/parser/parse_output.rs | 12 ++++++----- .../parser/parser_support/parser_function.rs | 17 ++++++---------- .../src/doc_examples/using_the_cursor.rs | 2 -- .../src/doc_examples/using_the_parser.rs | 3 +-- .../outputs/cargo/tests/src/trivia.rs | 1 - .../generated/interface/generated/parser.wit | 4 ++-- .../wasm/src/generated/wrappers/parser/mod.rs | 6 +++--- .../nomic-foundation-slang-parser.d.ts | 6 +++--- .../generated/compilation/internal_builder.rs | 6 +++++- .../src/generated/parser/parse_output.rs | 12 ++++++----- .../parser/parser_support/parser_function.rs | 17 ++++++---------- .../generated/interface/generated/parser.wit | 4 ++-- .../wasm/src/generated/wrappers/parser/mod.rs | 6 +++--- .../nomic-foundation-slang-parser.d.ts | 6 +++--- 26 files changed, 102 insertions(+), 90 deletions(-) diff --git a/crates/codegen/runtime/cargo/crate/src/runtime/compilation/internal_builder.rs b/crates/codegen/runtime/cargo/crate/src/runtime/compilation/internal_builder.rs index f35bea3920..ced9199831 100644 --- a/crates/codegen/runtime/cargo/crate/src/runtime/compilation/internal_builder.rs +++ b/crates/codegen/runtime/cargo/crate/src/runtime/compilation/internal_builder.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use std::rc::Rc; +use metaslang_cst::nodes::Node; use semver::Version; use crate::compilation::{CompilationUnit, File}; @@ -43,7 +44,10 @@ impl InternalCompilationBuilder { let import_paths = self.imports.extract(parse_output.create_tree_cursor()); - let file = File::new(id.clone(), parse_output.tree().clone()); + let file = File::new( + id.clone(), + Node::Nonterminal(Rc::clone(&parse_output.tree())), + ); self.files.insert(id, file); AddFileResponse { import_paths } diff --git a/crates/codegen/runtime/cargo/crate/src/runtime/parser/parse_output.rs b/crates/codegen/runtime/cargo/crate/src/runtime/parser/parse_output.rs index 4fbb7d45f1..38f0cf706c 100644 --- a/crates/codegen/runtime/cargo/crate/src/runtime/parser/parse_output.rs +++ b/crates/codegen/runtime/cargo/crate/src/runtime/parser/parse_output.rs @@ -1,15 +1,17 @@ -use crate::cst::{Cursor, Node, TextIndex}; +use std::rc::Rc; + +use crate::cst::{Cursor, NonterminalNode, TextIndex}; use crate::parser::ParseError; #[derive(Clone, Debug, PartialEq)] pub struct ParseOutput { - pub(crate) tree: Node, + pub(crate) tree: Rc, pub(crate) errors: Vec, } impl ParseOutput { - pub fn tree(&self) -> &Node { - &self.tree + pub fn tree(&self) -> Rc { + Rc::clone(&self.tree) } pub fn errors(&self) -> &Vec { @@ -22,6 +24,6 @@ impl ParseOutput { /// Creates a cursor that starts at the root of the parse tree. pub fn create_tree_cursor(&self) -> Cursor { - self.tree.clone().cursor_with_offset(TextIndex::ZERO) + Rc::clone(&self.tree).cursor_with_offset(TextIndex::ZERO) } } diff --git a/crates/codegen/runtime/cargo/crate/src/runtime/parser/parser_support/parser_function.rs b/crates/codegen/runtime/cargo/crate/src/runtime/parser/parser_support/parser_function.rs index e291b1df49..260aca146f 100644 --- a/crates/codegen/runtime/cargo/crate/src/runtime/parser/parser_support/parser_function.rs +++ b/crates/codegen/runtime/cargo/crate/src/runtime/parser/parser_support/parser_function.rs @@ -1,6 +1,6 @@ use std::rc::Rc; -use crate::cst::{Edge, Node, TerminalKind, TerminalKindExtensions, TextIndex}; +use crate::cst::{Edge, Node, NonterminalNode, TerminalKind, TerminalKindExtensions, TextIndex}; use crate::parser::lexer::Lexer; use crate::parser::parser_support::context::ParserContext; use crate::parser::parser_support::parser_result::{ @@ -85,14 +85,9 @@ where TerminalKind::UNRECOGNIZED }; let node = Node::terminal(kind, input.to_string()); - let tree = if no_match.kind.is_none() || start.utf8 == 0 { - node - } else { - trivia_nodes.push(Edge::anonymous(node)); - Node::nonterminal(no_match.kind.unwrap(), trivia_nodes) - }; + trivia_nodes.push(Edge::anonymous(node)); ParseOutput { - tree, + tree: Rc::new(NonterminalNode::new(no_match.kind.unwrap(), trivia_nodes)), errors: vec![ParseError::new( start..start + input.into(), no_match.expected_terminals, @@ -156,17 +151,17 @@ where )); ParseOutput { - tree: Node::nonterminal(topmost_node.kind, new_children), + tree: Rc::new(NonterminalNode::new(topmost_node.kind, new_children)), errors, } } else { - let tree = Node::Nonterminal(topmost_node); + let tree = topmost_node; let errors = stream.into_errors(); // Sanity check: Make sure that succesful parse is equivalent to not having any invalid nodes debug_assert_eq!( errors.is_empty(), - tree.clone() + Rc::clone(&tree) .cursor_with_offset(TextIndex::ZERO) .remaining_nodes() .all(|edge| edge diff --git a/crates/codegen/runtime/cargo/wasm/src/runtime/interface/generated/parser.wit b/crates/codegen/runtime/cargo/wasm/src/runtime/interface/generated/parser.wit index df41c2e0fc..6eca8e01ae 100644 --- a/crates/codegen/runtime/cargo/wasm/src/runtime/interface/generated/parser.wit +++ b/crates/codegen/runtime/cargo/wasm/src/runtime/interface/generated/parser.wit @@ -1,7 +1,7 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. interface parser { - use cst.{cursor, node, nonterminal-kind, text-range}; + use cst.{cursor, nonterminal-node, nonterminal-kind, text-range}; /// A parser instance that can parse source code into syntax trees. /// Each parser is configured for a specific language version and grammar. @@ -34,7 +34,7 @@ interface parser { resource parse-output { /// Returns the root node of the parsed syntax tree. /// Even if there are parsing errors, a partial tree will still be available. - tree: func() -> node; + tree: func() -> nonterminal-node; /// Returns a list of all parsing errors encountered. /// An empty list indicates successful parsing with no errors. diff --git a/crates/codegen/runtime/cargo/wasm/src/runtime/interface/parser.wit.jinja2 b/crates/codegen/runtime/cargo/wasm/src/runtime/interface/parser.wit.jinja2 index 3a155a6535..ebe1e15d83 100644 --- a/crates/codegen/runtime/cargo/wasm/src/runtime/interface/parser.wit.jinja2 +++ b/crates/codegen/runtime/cargo/wasm/src/runtime/interface/parser.wit.jinja2 @@ -1,5 +1,5 @@ interface parser { - use cst.{cursor, node, nonterminal-kind, text-range}; + use cst.{cursor, nonterminal-node, nonterminal-kind, text-range}; /// A parser instance that can parse source code into syntax trees. /// Each parser is configured for a specific language version and grammar. @@ -32,7 +32,7 @@ interface parser { resource parse-output { /// Returns the root node of the parsed syntax tree. /// Even if there are parsing errors, a partial tree will still be available. - tree: func() -> node; + tree: func() -> nonterminal-node; /// Returns a list of all parsing errors encountered. /// An empty list indicates successful parsing with no errors. diff --git a/crates/codegen/runtime/cargo/wasm/src/runtime/wrappers/parser/mod.rs b/crates/codegen/runtime/cargo/wasm/src/runtime/wrappers/parser/mod.rs index 679567d03e..c190cf8273 100644 --- a/crates/codegen/runtime/cargo/wasm/src/runtime/wrappers/parser/mod.rs +++ b/crates/codegen/runtime/cargo/wasm/src/runtime/wrappers/parser/mod.rs @@ -2,7 +2,7 @@ use crate::wasm_crate::utils::{define_wrapper, FromFFI, IntoFFI}; mod ffi { pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::cst::{ - Cursor, Node, TextRange, + Cursor, NonterminalNode, TextRange, }; pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::parser::{ Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError, @@ -70,8 +70,8 @@ define_wrapper! { ParseError { //================================================ define_wrapper! { ParseOutput { - fn tree(&self) -> ffi::Node { - self._borrow_ffi().tree().clone()._into_ffi() + fn tree(&self) -> ffi::NonterminalNode { + self._borrow_ffi().tree()._into_ffi() } fn errors(&self) -> Vec { diff --git a/crates/codegen/runtime/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts b/crates/codegen/runtime/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts index 6ac5bb7305..07fe4156a6 100644 --- a/crates/codegen/runtime/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts +++ b/crates/codegen/runtime/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts @@ -7,8 +7,8 @@ export namespace NomicFoundationSlangParser { } import type { Cursor } from "./nomic-foundation-slang-cst.js"; export { Cursor }; -import type { Node } from "./nomic-foundation-slang-cst.js"; -export { Node }; +import type { NonterminalNode } from "./nomic-foundation-slang-cst.js"; +export { NonterminalNode }; import type { NonterminalKind } from "./nomic-foundation-slang-cst.js"; export { NonterminalKind }; import type { TextRange } from "./nomic-foundation-slang-cst.js"; @@ -37,7 +37,7 @@ export class ParseOutput { * Returns the root node of the parsed syntax tree. * Even if there are parsing errors, a partial tree will still be available. */ - get tree(): Node; + get tree(): NonterminalNode; /** * Returns a list of all parsing errors encountered. * An empty list indicates successful parsing with no errors. diff --git a/crates/metaslang/cst/generated/public_api.txt b/crates/metaslang/cst/generated/public_api.txt index 83370fcc7c..d1f0012ef2 100644 --- a/crates/metaslang/cst/generated/public_api.txt +++ b/crates/metaslang/cst/generated/public_api.txt @@ -153,6 +153,8 @@ pub fn metaslang_cst::nodes::NonterminalNode::cursor_with_offset(self: alloc: pub fn metaslang_cst::nodes::NonterminalNode::descendants(self: alloc::rc::Rc) -> metaslang_cst::cursor::CursorIterator pub fn metaslang_cst::nodes::NonterminalNode::id(self: &alloc::rc::Rc) -> usize pub fn metaslang_cst::nodes::NonterminalNode::unparse(&self) -> alloc::string::String +impl metaslang_cst::nodes::NonterminalNode +pub fn metaslang_cst::nodes::NonterminalNode::new(kind: ::NonterminalKind, children: alloc::vec::Vec>) -> Self impl core::clone::Clone for metaslang_cst::nodes::NonterminalNode where ::NonterminalKind: core::clone::Clone pub fn metaslang_cst::nodes::NonterminalNode::clone(&self) -> metaslang_cst::nodes::NonterminalNode impl core::cmp::Eq for metaslang_cst::nodes::NonterminalNode where ::NonterminalKind: core::cmp::Eq diff --git a/crates/metaslang/cst/src/nodes.rs b/crates/metaslang/cst/src/nodes.rs index 8795b70a9e..ae8dc83f01 100644 --- a/crates/metaslang/cst/src/nodes.rs +++ b/crates/metaslang/cst/src/nodes.rs @@ -64,6 +64,18 @@ pub struct Edge { pub node: Node, } +impl NonterminalNode { + pub fn new(kind: T::NonterminalKind, children: Vec>) -> Self { + let text_len = children.iter().map(|edge| edge.text_len()).sum(); + + NonterminalNode { + kind, + text_len, + children, + } + } +} + impl Edge { /// Creates an anonymous node (without a label). pub fn anonymous(node: Node) -> Self { @@ -81,13 +93,7 @@ impl std::ops::Deref for Edge { impl Node { pub fn nonterminal(kind: T::NonterminalKind, children: Vec>) -> Self { - let text_len = children.iter().map(|edge| edge.text_len()).sum(); - - Self::Nonterminal(Rc::new(NonterminalNode { - kind, - text_len, - children, - })) + Self::Nonterminal(Rc::new(NonterminalNode::new(kind, children))) } pub fn terminal(kind: T::TerminalKind, text: String) -> Self { diff --git a/crates/solidity/outputs/cargo/crate/generated/public_api.txt b/crates/solidity/outputs/cargo/crate/generated/public_api.txt index 48a9440e5c..367836d6b1 100644 --- a/crates/solidity/outputs/cargo/crate/generated/public_api.txt +++ b/crates/solidity/outputs/cargo/crate/generated/public_api.txt @@ -953,9 +953,13 @@ impl slang_solidity::parser::ParseOutput pub fn slang_solidity::parser::ParseOutput::create_tree_cursor(&self) -> slang_solidity::cst::Cursor pub fn slang_solidity::parser::ParseOutput::errors(&self) -> &alloc::vec::Vec pub fn slang_solidity::parser::ParseOutput::is_valid(&self) -> bool +<<<<<<< HEAD pub fn slang_solidity::parser::ParseOutput::tree(&self) -> &slang_solidity::cst::Node impl core::clone::Clone for slang_solidity::parser::ParseOutput pub fn slang_solidity::parser::ParseOutput::clone(&self) -> slang_solidity::parser::ParseOutput +======= +pub fn slang_solidity::parser::ParseOutput::tree(&self) -> alloc::rc::Rc +>>>>>>> 26d5e003 (Changing parse_result to return a NonTerminalNode instead of a Node) impl core::cmp::PartialEq for slang_solidity::parser::ParseOutput pub fn slang_solidity::parser::ParseOutput::eq(&self, other: &slang_solidity::parser::ParseOutput) -> bool impl core::fmt::Debug for slang_solidity::parser::ParseOutput diff --git a/crates/solidity/outputs/cargo/crate/src/extensions/bindings/mod.rs b/crates/solidity/outputs/cargo/crate/src/extensions/bindings/mod.rs index b3ec4aa052..a6b7c2ab6c 100644 --- a/crates/solidity/outputs/cargo/crate/src/extensions/bindings/mod.rs +++ b/crates/solidity/outputs/cargo/crate/src/extensions/bindings/mod.rs @@ -16,7 +16,8 @@ pub fn add_built_ins( let parser = Parser::create(version)?; let parse_output = parser.parse(Parser::ROOT_KIND, source); - let built_ins_cursor = transform(parse_output.tree()).cursor_with_offset(TextIndex::ZERO); + let built_ins_cursor = + transform(&Node::Nonterminal(parse_output.tree())).cursor_with_offset(TextIndex::ZERO); binding_graph.add_system_file("built_ins.sol", built_ins_cursor); Ok(()) diff --git a/crates/solidity/outputs/cargo/crate/src/generated/compilation/internal_builder.rs b/crates/solidity/outputs/cargo/crate/src/generated/compilation/internal_builder.rs index a0a79d7944..cae73e0ef4 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/compilation/internal_builder.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/compilation/internal_builder.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use std::rc::Rc; +use metaslang_cst::nodes::Node; use semver::Version; use crate::compilation::{CompilationUnit, File}; @@ -45,7 +46,10 @@ impl InternalCompilationBuilder { let import_paths = self.imports.extract(parse_output.create_tree_cursor()); - let file = File::new(id.clone(), parse_output.tree().clone()); + let file = File::new( + id.clone(), + Node::Nonterminal(Rc::clone(&parse_output.tree())), + ); self.files.insert(id, file); AddFileResponse { import_paths } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/parser/parse_output.rs b/crates/solidity/outputs/cargo/crate/src/generated/parser/parse_output.rs index 83a71d08f4..765c8625f8 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/parser/parse_output.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/parser/parse_output.rs @@ -1,17 +1,19 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use crate::cst::{Cursor, Node, TextIndex}; +use std::rc::Rc; + +use crate::cst::{Cursor, NonterminalNode, TextIndex}; use crate::parser::ParseError; #[derive(Clone, Debug, PartialEq)] pub struct ParseOutput { - pub(crate) tree: Node, + pub(crate) tree: Rc, pub(crate) errors: Vec, } impl ParseOutput { - pub fn tree(&self) -> &Node { - &self.tree + pub fn tree(&self) -> Rc { + Rc::clone(&self.tree) } pub fn errors(&self) -> &Vec { @@ -24,6 +26,6 @@ impl ParseOutput { /// Creates a cursor that starts at the root of the parse tree. pub fn create_tree_cursor(&self) -> Cursor { - self.tree.clone().cursor_with_offset(TextIndex::ZERO) + Rc::clone(&self.tree).cursor_with_offset(TextIndex::ZERO) } } diff --git a/crates/solidity/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs b/crates/solidity/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs index ac5d02b724..576f5a6779 100644 --- a/crates/solidity/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs +++ b/crates/solidity/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs @@ -2,7 +2,7 @@ use std::rc::Rc; -use crate::cst::{Edge, Node, TerminalKind, TerminalKindExtensions, TextIndex}; +use crate::cst::{Edge, Node, NonterminalNode, TerminalKind, TerminalKindExtensions, TextIndex}; use crate::parser::lexer::Lexer; use crate::parser::parser_support::context::ParserContext; use crate::parser::parser_support::parser_result::{ @@ -87,14 +87,9 @@ where TerminalKind::UNRECOGNIZED }; let node = Node::terminal(kind, input.to_string()); - let tree = if no_match.kind.is_none() || start.utf8 == 0 { - node - } else { - trivia_nodes.push(Edge::anonymous(node)); - Node::nonterminal(no_match.kind.unwrap(), trivia_nodes) - }; + trivia_nodes.push(Edge::anonymous(node)); ParseOutput { - tree, + tree: Rc::new(NonterminalNode::new(no_match.kind.unwrap(), trivia_nodes)), errors: vec![ParseError::new( start..start + input.into(), no_match.expected_terminals, @@ -158,17 +153,17 @@ where )); ParseOutput { - tree: Node::nonterminal(topmost_node.kind, new_children), + tree: Rc::new(NonterminalNode::new(topmost_node.kind, new_children)), errors, } } else { - let tree = Node::Nonterminal(topmost_node); + let tree = topmost_node; let errors = stream.into_errors(); // Sanity check: Make sure that succesful parse is equivalent to not having any invalid nodes debug_assert_eq!( errors.is_empty(), - tree.clone() + Rc::clone(&tree) .cursor_with_offset(TextIndex::ZERO) .remaining_nodes() .all(|edge| edge diff --git a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_cursor.rs b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_cursor.rs index 396aa136b7..26d8a6c656 100644 --- a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_cursor.rs +++ b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_cursor.rs @@ -91,7 +91,6 @@ fn using_the_cursor() -> Result<()> { let identifiers: Vec<_> = parse_output .tree() - .clone() .descendants() .filter(|edge| edge.is_terminal_with_kind(TerminalKind::Identifier)) .map(|identifier| identifier.unparse()) @@ -106,7 +105,6 @@ fn using_the_cursor() -> Result<()> { let identifiers: Vec<_> = parse_output .tree() - .clone() .descendants() .filter(|edge| edge.label == Some(EdgeLabel::Name)) .filter(|edge| edge.is_terminal_with_kind(TerminalKind::Identifier)) diff --git a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs index db3ce9390b..f8ccc8d74a 100644 --- a/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs +++ b/crates/solidity/outputs/cargo/tests/src/doc_examples/using_the_parser.rs @@ -38,9 +38,8 @@ fn using_the_parser() -> Result<()> { // --8<-- [end:assert-is-valid] // --8<-- [start:inspect-tree] - let tree = parse_output.tree(); + let contract = parse_output.tree(); - let contract = tree.as_nonterminal().unwrap(); assert_eq!(contract.kind, NonterminalKind::ContractDefinition); assert_eq!(contract.children.len(), 7); diff --git a/crates/solidity/outputs/cargo/tests/src/trivia.rs b/crates/solidity/outputs/cargo/tests/src/trivia.rs index 85885beac3..1eadc077cc 100644 --- a/crates/solidity/outputs/cargo/tests/src/trivia.rs +++ b/crates/solidity/outputs/cargo/tests/src/trivia.rs @@ -29,7 +29,6 @@ fn compare_end_of_lines(input: &str, expected: &[&str]) -> Result<()> { let actual = output .tree() - .clone() .descendants() .filter(|edge| edge.is_terminal_with_kind(TerminalKind::EndOfLine)) .map(|eol| eol.unparse()) diff --git a/crates/solidity/outputs/cargo/wasm/src/generated/interface/generated/parser.wit b/crates/solidity/outputs/cargo/wasm/src/generated/interface/generated/parser.wit index df41c2e0fc..6eca8e01ae 100644 --- a/crates/solidity/outputs/cargo/wasm/src/generated/interface/generated/parser.wit +++ b/crates/solidity/outputs/cargo/wasm/src/generated/interface/generated/parser.wit @@ -1,7 +1,7 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. interface parser { - use cst.{cursor, node, nonterminal-kind, text-range}; + use cst.{cursor, nonterminal-node, nonterminal-kind, text-range}; /// A parser instance that can parse source code into syntax trees. /// Each parser is configured for a specific language version and grammar. @@ -34,7 +34,7 @@ interface parser { resource parse-output { /// Returns the root node of the parsed syntax tree. /// Even if there are parsing errors, a partial tree will still be available. - tree: func() -> node; + tree: func() -> nonterminal-node; /// Returns a list of all parsing errors encountered. /// An empty list indicates successful parsing with no errors. diff --git a/crates/solidity/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs b/crates/solidity/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs index 7d000977d8..daba41e616 100644 --- a/crates/solidity/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs +++ b/crates/solidity/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs @@ -4,7 +4,7 @@ use crate::wasm_crate::utils::{define_wrapper, FromFFI, IntoFFI}; mod ffi { pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::cst::{ - Cursor, Node, TextRange, + Cursor, NonterminalNode, TextRange, }; pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::parser::{ Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError, @@ -72,8 +72,8 @@ define_wrapper! { ParseError { //================================================ define_wrapper! { ParseOutput { - fn tree(&self) -> ffi::Node { - self._borrow_ffi().tree().clone()._into_ffi() + fn tree(&self) -> ffi::NonterminalNode { + self._borrow_ffi().tree()._into_ffi() } fn errors(&self) -> Vec { diff --git a/crates/solidity/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts b/crates/solidity/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts index 6ac5bb7305..07fe4156a6 100644 --- a/crates/solidity/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts +++ b/crates/solidity/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts @@ -7,8 +7,8 @@ export namespace NomicFoundationSlangParser { } import type { Cursor } from "./nomic-foundation-slang-cst.js"; export { Cursor }; -import type { Node } from "./nomic-foundation-slang-cst.js"; -export { Node }; +import type { NonterminalNode } from "./nomic-foundation-slang-cst.js"; +export { NonterminalNode }; import type { NonterminalKind } from "./nomic-foundation-slang-cst.js"; export { NonterminalKind }; import type { TextRange } from "./nomic-foundation-slang-cst.js"; @@ -37,7 +37,7 @@ export class ParseOutput { * Returns the root node of the parsed syntax tree. * Even if there are parsing errors, a partial tree will still be available. */ - get tree(): Node; + get tree(): NonterminalNode; /** * Returns a list of all parsing errors encountered. * An empty list indicates successful parsing with no errors. diff --git a/crates/testlang/outputs/cargo/crate/src/generated/compilation/internal_builder.rs b/crates/testlang/outputs/cargo/crate/src/generated/compilation/internal_builder.rs index a0a79d7944..cae73e0ef4 100644 --- a/crates/testlang/outputs/cargo/crate/src/generated/compilation/internal_builder.rs +++ b/crates/testlang/outputs/cargo/crate/src/generated/compilation/internal_builder.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use std::rc::Rc; +use metaslang_cst::nodes::Node; use semver::Version; use crate::compilation::{CompilationUnit, File}; @@ -45,7 +46,10 @@ impl InternalCompilationBuilder { let import_paths = self.imports.extract(parse_output.create_tree_cursor()); - let file = File::new(id.clone(), parse_output.tree().clone()); + let file = File::new( + id.clone(), + Node::Nonterminal(Rc::clone(&parse_output.tree())), + ); self.files.insert(id, file); AddFileResponse { import_paths } diff --git a/crates/testlang/outputs/cargo/crate/src/generated/parser/parse_output.rs b/crates/testlang/outputs/cargo/crate/src/generated/parser/parse_output.rs index 83a71d08f4..765c8625f8 100644 --- a/crates/testlang/outputs/cargo/crate/src/generated/parser/parse_output.rs +++ b/crates/testlang/outputs/cargo/crate/src/generated/parser/parse_output.rs @@ -1,17 +1,19 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. -use crate::cst::{Cursor, Node, TextIndex}; +use std::rc::Rc; + +use crate::cst::{Cursor, NonterminalNode, TextIndex}; use crate::parser::ParseError; #[derive(Clone, Debug, PartialEq)] pub struct ParseOutput { - pub(crate) tree: Node, + pub(crate) tree: Rc, pub(crate) errors: Vec, } impl ParseOutput { - pub fn tree(&self) -> &Node { - &self.tree + pub fn tree(&self) -> Rc { + Rc::clone(&self.tree) } pub fn errors(&self) -> &Vec { @@ -24,6 +26,6 @@ impl ParseOutput { /// Creates a cursor that starts at the root of the parse tree. pub fn create_tree_cursor(&self) -> Cursor { - self.tree.clone().cursor_with_offset(TextIndex::ZERO) + Rc::clone(&self.tree).cursor_with_offset(TextIndex::ZERO) } } diff --git a/crates/testlang/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs b/crates/testlang/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs index ac5d02b724..576f5a6779 100644 --- a/crates/testlang/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs +++ b/crates/testlang/outputs/cargo/crate/src/generated/parser/parser_support/parser_function.rs @@ -2,7 +2,7 @@ use std::rc::Rc; -use crate::cst::{Edge, Node, TerminalKind, TerminalKindExtensions, TextIndex}; +use crate::cst::{Edge, Node, NonterminalNode, TerminalKind, TerminalKindExtensions, TextIndex}; use crate::parser::lexer::Lexer; use crate::parser::parser_support::context::ParserContext; use crate::parser::parser_support::parser_result::{ @@ -87,14 +87,9 @@ where TerminalKind::UNRECOGNIZED }; let node = Node::terminal(kind, input.to_string()); - let tree = if no_match.kind.is_none() || start.utf8 == 0 { - node - } else { - trivia_nodes.push(Edge::anonymous(node)); - Node::nonterminal(no_match.kind.unwrap(), trivia_nodes) - }; + trivia_nodes.push(Edge::anonymous(node)); ParseOutput { - tree, + tree: Rc::new(NonterminalNode::new(no_match.kind.unwrap(), trivia_nodes)), errors: vec![ParseError::new( start..start + input.into(), no_match.expected_terminals, @@ -158,17 +153,17 @@ where )); ParseOutput { - tree: Node::nonterminal(topmost_node.kind, new_children), + tree: Rc::new(NonterminalNode::new(topmost_node.kind, new_children)), errors, } } else { - let tree = Node::Nonterminal(topmost_node); + let tree = topmost_node; let errors = stream.into_errors(); // Sanity check: Make sure that succesful parse is equivalent to not having any invalid nodes debug_assert_eq!( errors.is_empty(), - tree.clone() + Rc::clone(&tree) .cursor_with_offset(TextIndex::ZERO) .remaining_nodes() .all(|edge| edge diff --git a/crates/testlang/outputs/cargo/wasm/src/generated/interface/generated/parser.wit b/crates/testlang/outputs/cargo/wasm/src/generated/interface/generated/parser.wit index df41c2e0fc..6eca8e01ae 100644 --- a/crates/testlang/outputs/cargo/wasm/src/generated/interface/generated/parser.wit +++ b/crates/testlang/outputs/cargo/wasm/src/generated/interface/generated/parser.wit @@ -1,7 +1,7 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. interface parser { - use cst.{cursor, node, nonterminal-kind, text-range}; + use cst.{cursor, nonterminal-node, nonterminal-kind, text-range}; /// A parser instance that can parse source code into syntax trees. /// Each parser is configured for a specific language version and grammar. @@ -34,7 +34,7 @@ interface parser { resource parse-output { /// Returns the root node of the parsed syntax tree. /// Even if there are parsing errors, a partial tree will still be available. - tree: func() -> node; + tree: func() -> nonterminal-node; /// Returns a list of all parsing errors encountered. /// An empty list indicates successful parsing with no errors. diff --git a/crates/testlang/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs b/crates/testlang/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs index 7d000977d8..daba41e616 100644 --- a/crates/testlang/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs +++ b/crates/testlang/outputs/cargo/wasm/src/generated/wrappers/parser/mod.rs @@ -4,7 +4,7 @@ use crate::wasm_crate::utils::{define_wrapper, FromFFI, IntoFFI}; mod ffi { pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::cst::{ - Cursor, Node, TextRange, + Cursor, NonterminalNode, TextRange, }; pub use crate::wasm_crate::bindgen::exports::nomic_foundation::slang::parser::{ Guest, GuestParseError, GuestParseOutput, GuestParser, NonterminalKind, ParseError, @@ -72,8 +72,8 @@ define_wrapper! { ParseError { //================================================ define_wrapper! { ParseOutput { - fn tree(&self) -> ffi::Node { - self._borrow_ffi().tree().clone()._into_ffi() + fn tree(&self) -> ffi::NonterminalNode { + self._borrow_ffi().tree()._into_ffi() } fn errors(&self) -> Vec { diff --git a/crates/testlang/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts b/crates/testlang/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts index 6ac5bb7305..07fe4156a6 100644 --- a/crates/testlang/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts +++ b/crates/testlang/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-parser.d.ts @@ -7,8 +7,8 @@ export namespace NomicFoundationSlangParser { } import type { Cursor } from "./nomic-foundation-slang-cst.js"; export { Cursor }; -import type { Node } from "./nomic-foundation-slang-cst.js"; -export { Node }; +import type { NonterminalNode } from "./nomic-foundation-slang-cst.js"; +export { NonterminalNode }; import type { NonterminalKind } from "./nomic-foundation-slang-cst.js"; export { NonterminalKind }; import type { TextRange } from "./nomic-foundation-slang-cst.js"; @@ -37,7 +37,7 @@ export class ParseOutput { * Returns the root node of the parsed syntax tree. * Even if there are parsing errors, a partial tree will still be available. */ - get tree(): Node; + get tree(): NonterminalNode; /** * Returns a list of all parsing errors encountered. * An empty list indicates successful parsing with no errors.