fix NAPI cursor types and expose cursor.depth#676
fix NAPI cursor types and expose cursor.depth#676OmarTawfik merged 7 commits intoNomicFoundation:mainfrom
cursor types and expose cursor.depth#676Conversation
🦋 Changeset detectedLatest commit: 82b9683 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
3d679ce to
428e9ce
Compare
Cursor::find_matching() APIcursor types and expose cursor.depth
|
|
||
| let mut cursor = output.create_tree_cursor(); | ||
| let mut pragmas = vec![]; | ||
| while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) { |
There was a problem hiding this comment.
Can't we adapt the old code to directly use the new while cursor.go_to_next_rule_with_kinds(..) followed by extract_pragma(cursor.node())?
There was a problem hiding this comment.
Unfortunately this is recursive, which for something like X || Y would match all three of X || Y, X, and Y. We need to skip children when found.
There was a problem hiding this comment.
Since this is DFS pre-order, I imagine the parent should be matched first and in the loop body we already call cursor.go_to_next_non_descendent(), which should skip the subtree elements (i.e. individual versions) in a subsequent while find_rule_with_kind loop call. Otherwise, how the previous code handled it? Or was it wrong/matched everything recursively?
There was a problem hiding this comment.
This would miss two consecutive versions like X Y which is also valid syntax. go_to_next_non_descendent() would go to Y, and then find_rule_with_kind() would skip it before we get to it.
65dfb5c to
18164d5
Compare
Xanewok
left a comment
There was a problem hiding this comment.
Left a question but not blocking; thanks!
|
|
||
| let mut cursor = output.create_tree_cursor(); | ||
| let mut pragmas = vec![]; | ||
| while let Some(rule_node) = cursor.find_rule_with_kind(&[RuleKind::VersionPragmaExpression]) { |
There was a problem hiding this comment.
Since this is DFS pre-order, I imagine the parent should be matched first and in the loop body we already call cursor.go_to_next_non_descendent(), which should skip the subtree elements (i.e. individual versions) in a subsequent while find_rule_with_kind loop call. Otherwise, how the previous code handled it? Or was it wrong/matched everything recursively?
| if self.current.child_number > 0 { | ||
| if let Some(parent_path_element) = self.path.last() { | ||
| let new_child_number = self.current.child_number + 1; | ||
| let new_child_number = self.current.child_number - 1; |
There was a problem hiding this comment.
Ooh, good catch! It'd be great to add some simple tests to catch stuff like this at some point.
| } | ||
|
|
||
| true | ||
| self.go_to_first_child() || self.go_to_next_non_descendent() |
There was a problem hiding this comment.
I was gonna mumble about it but on the second thought it really is a lot clearer now, good thinking!
|
It looks like CI needs fixing. |
|
@OmarTawfik I've edited the OP to close the #636 when merged, hope you don't mind! I think that further improvements not strictly related to exposing the existing Rust logic to TS (like tracking the line/column from #683) are probably better tracked in #334. |
18164d5 to
82b9683
Compare
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @nomicfoundation/slang@0.12.0 ### Minor Changes - [#699](#699) [`ddfebfe9`](ddfebfe) Thanks [@Xanewok](https://github.com/Xanewok)! - Remove `ProductionKind` in favor of `RuleKind` - [#699](#699) [`ddfebfe9`](ddfebfe) Thanks [@Xanewok](https://github.com/Xanewok)! - Allow parsing individual precedence expressions, like `ShiftExpression` - [#665](#665) [`4b5f8b46`](4b5f8b4) Thanks [@Xanewok](https://github.com/Xanewok)! - Remove the CST Visitor API in favor of the Cursor API - [#666](#666) [`0434b68c`](0434b68) Thanks [@Xanewok](https://github.com/Xanewok)! - Add `Node::unparse()` that allows to reconstruct the source code from the CST node - [#675](#675) [`daea4b7f`](daea4b7) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - rename `Cursor`'s `pathRuleNodes()` to `ancestors()` in the NodeJS API. - [#676](#676) [`b496d361`](b496d36) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - Fix NAPI `cursor` types and expose `cursor.depth`. ### Patch Changes - [#685](#685) [`b5fca94a`](b5fca94) Thanks [@Xanewok](https://github.com/Xanewok)! - `bytes` is now properly recognized as a reserved word - [#660](#660) [`97028991`](9702899) Thanks [@Xanewok](https://github.com/Xanewok)! - Drop List suffix from collection grammar rule names Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing). If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @nomicfoundation/slang@0.12.0 ### Minor Changes - [#699](NomicFoundation/slang#699) [`ddfebfe9`](NomicFoundation/slang@ddfebfe) Thanks [@Xanewok](https://github.com/Xanewok)! - Remove `ProductionKind` in favor of `RuleKind` - [#699](NomicFoundation/slang#699) [`ddfebfe9`](NomicFoundation/slang@ddfebfe) Thanks [@Xanewok](https://github.com/Xanewok)! - Allow parsing individual precedence expressions, like `ShiftExpression` - [#665](NomicFoundation/slang#665) [`4b5f8b46`](NomicFoundation/slang@4b5f8b4) Thanks [@Xanewok](https://github.com/Xanewok)! - Remove the CST Visitor API in favor of the Cursor API - [#666](NomicFoundation/slang#666) [`0434b68c`](NomicFoundation/slang@0434b68) Thanks [@Xanewok](https://github.com/Xanewok)! - Add `Node::unparse()` that allows to reconstruct the source code from the CST node - [#675](NomicFoundation/slang#675) [`daea4b7f`](NomicFoundation/slang@daea4b7) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - rename `Cursor`'s `pathRuleNodes()` to `ancestors()` in the NodeJS API. - [#676](NomicFoundation/slang#676) [`b496d361`](NomicFoundation/slang@b496d36) Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - Fix NAPI `cursor` types and expose `cursor.depth`. ### Patch Changes - [#685](NomicFoundation/slang#685) [`b5fca94a`](NomicFoundation/slang@b5fca94) Thanks [@Xanewok](https://github.com/Xanewok)! - `bytes` is now properly recognized as a reserved word - [#660](NomicFoundation/slang#660) [`97028991`](NomicFoundation/slang@9702899) Thanks [@Xanewok](https://github.com/Xanewok)! - Drop List suffix from collection grammar rule names Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
cursor.depthAPI.cst.Nodeunion type.index.d.ts.Cursor::find_*()APIs withgo_to_*()alternatives.Fixes #636