Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use borrow<> for AST selectors #1130

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-drinks-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

fix a bug where CST nodes are invalidated after using AST types
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ interface ast {
use cst.{node, nonterminal-node};

resource selectors {
sequence: static func(node: nonterminal-node) -> result<list<option<node>>, string>;
choice: static func(node: nonterminal-node) -> result<node, string>;
repeated: static func(node: nonterminal-node) -> result<list<node>, string>;
separated: static func(node: nonterminal-node) -> result<list<list<node>>, string>;
sequence: static func(node: borrow<nonterminal-node>) -> result<list<option<node>>, string>;
choice: static func(node: borrow<nonterminal-node>) -> result<node, string>;
repeated: static func(node: borrow<nonterminal-node>) -> result<list<node>, string>;
separated: static func(node: borrow<nonterminal-node>) -> result<list<list<node>>, string>;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod ffi {
Guest, GuestSelectors,
};
pub use crate::wasm_crate::bindings::exports::nomic_foundation::slang::cst::{
Node, NonterminalNode,
Node, NonterminalNodeBorrow,
};
}

Expand All @@ -27,25 +27,25 @@ impl ffi::Guest for crate::wasm_crate::World {
pub struct SelectorsWrapper;

impl ffi::GuestSelectors for SelectorsWrapper {
fn sequence(node: ffi::NonterminalNode) -> Result<Vec<Option<ffi::Node>>, String> {
fn sequence(node: ffi::NonterminalNodeBorrow<'_>) -> Result<Vec<Option<ffi::Node>>, String> {
Ok(selectors::select_sequence(node._borrow_ffi())?
.into_iter()
.map(|opt| opt.map(|node| node._into_ffi()))
.collect())
}

fn choice(node: ffi::NonterminalNode) -> Result<ffi::Node, String> {
fn choice(node: ffi::NonterminalNodeBorrow<'_>) -> Result<ffi::Node, String> {
Ok(selectors::select_choice(node._borrow_ffi())?._into_ffi())
}

fn repeated(node: ffi::NonterminalNode) -> Result<Vec<ffi::Node>, String> {
fn repeated(node: ffi::NonterminalNodeBorrow<'_>) -> Result<Vec<ffi::Node>, String> {
Ok(selectors::select_repeated(node._borrow_ffi())?
.into_iter()
.map(|node| node._into_ffi())
.collect())
}

fn separated(node: ffi::NonterminalNode) -> Result<Vec<Vec<ffi::Node>>, String> {
fn separated(node: ffi::NonterminalNodeBorrow<'_>) -> Result<Vec<Vec<ffi::Node>>, String> {
Ok(selectors::select_separated(node._borrow_ffi())?
.into_iter()
.map(|vec| vec.into_iter().map(|node| node._into_ffi()).collect())
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions crates/testlang/outputs/npm/tests/src/tests/ast/ast.mts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,22 @@ test("create and use binary expressions", () => {
assertIsTerminalNode(operator, TerminalKind.Plus, "+");
assertIsTerminalNode(rightOperand.variant, TerminalKind.Identifier, "bar");
});

it("can reuse the same CST nodes after selectors", () => {
// Bug: https://github.com/NomicFoundation/slang/issues/1128

const source = `foo + bar`;

const parser = Parser.create("1.0.0");
const parseOutput = parser.parse(NonterminalKind.SourceUnit, source);
parseOutput.isValid(); // true

const cst = parseOutput.tree.asNonterminalNode()!;
const ast = new SourceUnit(cst);

expect(ast.cst.kind).toBe(NonterminalKind.SourceUnit);

expect(ast.members.cst.kind).toBe(NonterminalKind.SourceUnitMembers);

expect(ast.cst.kind).toBe(NonterminalKind.SourceUnit);
});
Loading