Skip to content

Commit

Permalink
Expose cursor method in napi (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyBlakey authored Oct 23, 2023
1 parent 1d992d4 commit 06cbbe8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
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.

2 changes: 2 additions & 0 deletions crates/solidity/outputs/npm/package/src/generated/index.d.ts

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

0 comments on commit 06cbbe8

Please sign in to comment.