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

Expose cursor method in napi #615

Merged
merged 2 commits into from
Oct 23, 2023
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/short-wombats-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

`cursor` method is now exposed in Typescript API
2 changes: 2 additions & 0 deletions crates/codegen/parser/runtime/src/napi/napi_cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl RuleNode {
.collect()
}

#[napi(getter)]
pub fn cursor(&self) -> Cursor {
Cursor::new(RustNode::Rule(self.0.clone()).cursor())
}
Expand Down Expand Up @@ -74,6 +75,7 @@ impl TokenNode {
self.0.text.clone()
}

#[napi(getter)]
pub fn cursor(&self) -> Cursor {
Cursor::new(RustNode::Token(self.0.clone()).cursor())
}
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.

17 changes: 17 additions & 0 deletions crates/solidity/outputs/npm/tests/src/cst-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ test("parse rule", () => {
}
});

test("trivial cursor access", () => {
const source = "int256 constant z = 1**2**3;";
const language = new Language("0.8.1");

const { parseTree } = language.parse(ProductionKind.SourceUnit, source);

let cursor = parseTree.cursor;
let node = cursor.node;
if (node instanceof RuleNode) {
expect(node.type).toEqual(NodeType.Rule);
expect(node.kind).toEqual(RuleKind.SourceUnit);
expect(node.children).toHaveLength(1);
} else {
fail("Expected RuleNode");
}
});

test("calculate unicode characters text length", () => {
const source = `unicode"some 😁 emoji"`;
const language = new Language("0.8.1");
Expand Down