Skip to content

Commit

Permalink
use borrow<> for AST selectors
Browse files Browse the repository at this point in the history
fixes #1128
  • Loading branch information
OmarTawfik committed Oct 22, 2024
1 parent 685de31 commit c477142
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
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);
});

0 comments on commit c477142

Please sign in to comment.