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

consolidate .children() APIs and iterators #1156

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

OmarTawfik
Copy link
Collaborator

@OmarTawfik OmarTawfik commented Nov 22, 2024

Consolidate redundant Node and Cursor utilities like .children() vs edges(), or Cursor vs CursorWithEdges, and expose them to WASM:

  • add node.descendants() and cursor.descendants() APIs to allow iterating over all descendants of the current node in pre-order traversal.
  • add cursor.ancestors() API to allow iterating over all ancestors of the current node, starting with the immediate parent, and moving upwards, ending with the root node.
  • add cursor.remainingNodes() API to allow iterating over all the remaining nodes. in the current tree, moving in pre-order traversal, until the tree is completed.
  • fix node.children() and parseOutput.errors() return types.

Copy link

changeset-bot bot commented Nov 22, 2024

🦋 Changeset detected

Latest commit: a2aa67d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nomicfoundation/slang Minor

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

@OmarTawfik OmarTawfik changed the title consolidate .children() APIs into iterators consolidate .children() APIs and iterators Nov 22, 2024
@OmarTawfik OmarTawfik force-pushed the fix-children branch 2 times, most recently from 9a24218 to e90c3d4 Compare November 23, 2024 02:59
@OmarTawfik OmarTawfik marked this pull request as ready for review November 23, 2024 03:28
@OmarTawfik OmarTawfik requested a review from a team as a code owner November 23, 2024 03:28
Copy link
Contributor

@ggiraldez ggiraldez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of non-blocking comments, but otherwise looks good to me!

.cloned()
})
.map(|identifier| identifier.text.clone())
.filter(|node| node.is_terminal_with_kind(TerminalKind::Identifier))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that node here is actually an Edge, which in turn consists of a label and a node. I think that's the most confusing part of this change in general: the items of both children() and CursorIterator (through descendants()) are now edges instead of nodes. Previously it was explicit with the added .with_edges() call to the cursor. It'll maybe catch users by surprise.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only had .nodes() and .edges() because edges came later, but I'm not sure it makes sense to add a specific iterator for it, given that it doesn't cost anything to do both, and they already have a Deref between them..
I updated the variable names to make this clearer. Please let me know if you have any other suggestions.

}
let current = Edge {
label: self.cursor.label(),
node: self.cursor.node().clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clone() here is redundant as the cursor's node is already cloned inside node().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

/// Returns an iterator over all descendants of the current node in pre-order traversal.
descendants: func() -> cursor-iterator;
/// Returns an iterator over all the remaining nodes in the current tree, moving in pre-order traversal, until the cursor is completed.
consume: func() -> cursor-iterator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the semantics of the generated function are different from the Rust's native function since this implementation will clone before calling to consume. Does it make sense to still call it consume in the WIT interface?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. To keep things consistent, I renamed it to remainingNodes() and modified the comment.

Consolidate redundant `Node` and `Cursor` utilities like `.children()` vs `edges()`, or `Cursor` vs `CursorWithEdges`, and expose them to WASM:

- add `node.descendants()` and `cursor.descendants()` APIs to allow iterating over all descendants of the current node in pre-order traversal.
- add `cursor.ancestors()` API to allow iterating over all ancestors of the current node, starting with the immediate parent, and moving upwards, ending with the root node.
- add `cursor.consume()` API to allow iterating over all the remaining nodes. in the current tree, moving in pre-order traversal, until the cursor is completed.
- fix `node.children()` and `parseOutput.errors()` return types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants