Skip to content

Commit b1dced3

Browse files
authored
public API overhaul (#647)
- rename `with_colour` to `with_color` as we are using a single `en-US` locale in the entire project. - use `SUPPORTED_VERSIONS.binary_search()` in `Language::new()` - use functions instead of properties for expensive operations -> #628 - require specifying an initial offset when creating a CST cursor -> #628
1 parent 35dff1e commit b1dced3

61 files changed

Lines changed: 409 additions & 366 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/weak-turkeys-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/slang": minor
3+
---
4+
5+
Require specifying an initial offset when creating a CST cursor.

crates/codegen/parser/runtime/src/cst.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Node {
4646
}
4747
}
4848

49-
pub fn cursor(&self) -> Cursor {
50-
Cursor::new(self.clone())
49+
pub fn create_cursor(&self, text_offset: TextIndex) -> Cursor {
50+
Cursor::new(self.clone(), text_offset)
5151
}
5252

5353
pub fn as_rule(&self) -> Option<&Rc<RuleNode>> {

crates/codegen/parser/runtime/src/cursor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ impl Iterator for Cursor {
8787
}
8888

8989
impl Cursor {
90-
pub(crate) fn new(node: Node) -> Self {
90+
pub(crate) fn new(node: Node, text_offset: TextIndex) -> Self {
9191
Self {
9292
path: vec![],
9393
current: PathNode {
9494
node,
9595
child_number: 0,
96-
text_offset: Default::default(),
96+
text_offset,
9797
},
9898
is_completed: false,
9999
}

crates/codegen/parser/runtime/src/napi/napi_cst.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RuleNode {
4242
(&self.0.text_len).into()
4343
}
4444

45-
#[napi(getter, ts_return_type = "Array<cst.RuleNode | cst.TokenNode>")]
45+
#[napi(ts_return_type = "Array<cst.RuleNode | cst.TokenNode>")]
4646
pub fn children(&self, env: Env) -> Vec<JsObject> {
4747
self.0
4848
.children
@@ -51,9 +51,11 @@ impl RuleNode {
5151
.collect()
5252
}
5353

54-
#[napi(getter, ts_return_type = "cursor.Cursor")]
55-
pub fn cursor(&self) -> Cursor {
56-
Cursor::new(RustNode::Rule(self.0.clone()).cursor())
54+
#[napi(ts_return_type = "cursor.Cursor")]
55+
pub fn create_cursor(&self, text_offset: TextIndex) -> Cursor {
56+
RustNode::Rule(self.0.clone())
57+
.create_cursor((&text_offset).into())
58+
.into()
5759
}
5860
}
5961

@@ -83,9 +85,11 @@ impl TokenNode {
8385
self.0.text.clone()
8486
}
8587

86-
#[napi(getter, ts_return_type = "cursor.Cursor")]
87-
pub fn cursor(&self) -> Cursor {
88-
Cursor::new(RustNode::Token(self.0.clone()).cursor())
88+
#[napi(ts_return_type = "cursor.Cursor")]
89+
pub fn create_cursor(&self, text_offset: TextIndex) -> Cursor {
90+
RustNode::Token(self.0.clone())
91+
.create_cursor((&text_offset).into())
92+
.into()
8993
}
9094
}
9195

crates/codegen/parser/runtime/src/napi/napi_cursor.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ use napi_text_index::*;
1010
#[napi(namespace = "cursor")]
1111
pub struct Cursor(Box<RustCursor>);
1212

13+
impl From<RustCursor> for Cursor {
14+
fn from(value: RustCursor) -> Self {
15+
Self(value.into())
16+
}
17+
}
18+
1319
#[napi(namespace = "cursor")]
1420
impl Cursor {
1521
pub(crate) fn new(cursor: RustCursor) -> Self {
@@ -41,7 +47,7 @@ impl Cursor {
4147
self.0.is_completed()
4248
}
4349

44-
#[napi(getter, ts_return_type = "cst.RuleNode | cst.TokenNode")]
50+
#[napi(ts_return_type = "cst.RuleNode | cst.TokenNode")]
4551
pub fn node(&self, env: Env) -> JsObject {
4652
self.0.node().to_js(&env)
4753
}
@@ -56,7 +62,7 @@ impl Cursor {
5662
(&self.0.text_range()).into()
5763
}
5864

59-
#[napi(getter, ts_return_type = "Array<cst.RuleNode>")]
65+
#[napi(ts_return_type = "Array<cst.RuleNode>")]
6066
pub fn path_rule_nodes(&self, env: Env) -> Vec<JsObject> {
6167
self.0
6268
.path_rule_nodes()

crates/codegen/parser/runtime/src/napi/napi_parse_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl ParseError {
2929
}
3030

3131
#[napi(namespace = "parse_error")]
32-
pub fn to_error_report(&self, source_id: String, source: String, with_colour: bool) -> String {
33-
self.0.to_error_report(&source_id, &source, with_colour)
32+
pub fn to_error_report(&self, source_id: String, source: String, with_color: bool) -> String {
33+
self.0.to_error_report(&source_id, &source, with_color)
3434
}
3535
}

crates/codegen/parser/runtime/src/napi/napi_parse_output.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ impl From<RustParseOutput> for ParseOutput {
1414

1515
#[napi(namespace = "parse_output")]
1616
impl ParseOutput {
17-
#[napi(getter, ts_return_type = "cst.RuleNode | cst.TokenNode")]
18-
pub fn parse_tree(&self, env: Env) -> napi::JsObject {
19-
return self.0.parse_tree().to_js(&env);
17+
#[napi(ts_return_type = "cst.RuleNode | cst.TokenNode")]
18+
pub fn tree(&self, env: Env) -> napi::JsObject {
19+
return self.0.tree().to_js(&env);
2020
}
2121

22-
#[napi(getter, ts_return_type = "Array<parse_error.ParseError>")]
22+
#[napi(ts_return_type = "Array<parse_error.ParseError>")]
2323
pub fn errors(&self) -> Vec<napi_parse_error::ParseError> {
2424
self.0.errors().iter().map(|x| x.clone().into()).collect()
2525
}
@@ -28,4 +28,10 @@ impl ParseOutput {
2828
pub fn is_valid(&self) -> bool {
2929
self.0.is_valid()
3030
}
31+
32+
/// Creates a cursor that starts at the root of the parse tree.
33+
#[napi(ts_return_type = "cursor.Cursor")]
34+
pub fn create_tree_cursor(&self) -> napi_cursor::Cursor {
35+
return self.0.create_tree_cursor().into();
36+
}
3137
}

crates/codegen/parser/runtime/src/napi/napi_text_index.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ impl From<&RustTextIndex> for TextIndex {
2020
}
2121
}
2222

23+
impl From<&TextIndex> for RustTextIndex {
24+
fn from(value: &TextIndex) -> Self {
25+
Self {
26+
utf8: value.utf8 as usize,
27+
utf16: value.utf16 as usize,
28+
char: value.char as usize,
29+
}
30+
}
31+
}
32+
2333
#[napi(object, namespace = "text_index")]
2434
#[derive(Copy, Clone)]
2535
pub struct TextRange {

crates/codegen/parser/runtime/src/parse_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl ParseError {
2828
.collect();
2929
}
3030

31-
pub fn to_error_report(&self, source_id: &str, source: &str, with_colour: bool) -> String {
32-
return render_error_report(self, source_id, source, with_colour);
31+
pub fn to_error_report(&self, source_id: &str, source: &str, with_color: bool) -> String {
32+
return render_error_report(self, source_id, source, with_color);
3333
}
3434
}
3535

crates/codegen/parser/runtime/src/parse_output.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{cst, parse_error::ParseError};
1+
use crate::{cst, cursor::Cursor, parse_error::ParseError};
22

33
#[derive(Debug, PartialEq)]
44
pub struct ParseOutput {
@@ -7,7 +7,7 @@ pub struct ParseOutput {
77
}
88

99
impl ParseOutput {
10-
pub fn parse_tree(&self) -> cst::Node {
10+
pub fn tree(&self) -> cst::Node {
1111
return self.parse_tree.clone();
1212
}
1313

@@ -18,4 +18,9 @@ impl ParseOutput {
1818
pub fn is_valid(&self) -> bool {
1919
return self.errors.is_empty();
2020
}
21+
22+
/// Creates a cursor that starts at the root of the parse tree.
23+
pub fn create_tree_cursor(&self) -> Cursor {
24+
return self.parse_tree.create_cursor(Default::default());
25+
}
2126
}

0 commit comments

Comments
 (0)