Skip to content

Conversation

OmarTawfik
Copy link
Contributor

@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: ae456f9

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
@OmarTawfik OmarTawfik enabled auto-merge November 23, 2024 03:28
@ggiraldez ggiraldez disabled auto-merge November 25, 2024 22:25
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
Contributor 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
Contributor 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
Contributor 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.
@OmarTawfik OmarTawfik requested a review from a team as a code owner November 28, 2024 11:47
@OmarTawfik OmarTawfik added this pull request to the merge queue Nov 28, 2024
Merged via the queue into NomicFoundation:main with commit 3a82f06 Nov 28, 2024
1 check passed
@OmarTawfik OmarTawfik deleted the fix-children branch November 28, 2024 12:30
@github-actions github-actions bot mentioned this pull request Nov 27, 2024
github-merge-queue bot pushed a commit that referenced this pull request Jan 24, 2025
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.19.0

### Minor Changes

- [#1156](#1156)
[`3a82f06`](3a82f06)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - add
`node.descendants()` and `cursor.descendants()` APIs to allow iterating
over all descendants of the current node in pre-order traversal.

- [#1156](#1156)
[`3a82f06`](3a82f06)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - fix
`node.children()` and `parseOutput.errors()` return types

- [#1194](#1194)
[`7a25d63`](7a25d63)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - split
`parser/Parser.supportedVersions()` into a new `utils/LanguageFacts`
API, with `allVersions()`, `earliestVersion()`, and `latestVersion()`
methods.

- [#1194](#1194)
[`7a25d63`](7a25d63)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - expose the
`BingingGraph` API to allow querying definitions/references between
source files.

- [#1156](#1156)
[`3a82f06`](3a82f06)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - 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.

- [#1156](#1156)
[`3a82f06`](3a82f06)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - 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.

- [#1223](#1223)
[`3e85a14`](3e85a14)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - split
`Parser.parse()` API into `parse_file_contents()` and
`parse_nonterminal()`.

- [#1194](#1194)
[`7a25d63`](7a25d63)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - add a
`CompilationBuilder` API to incrementally load and resolve source files
and their imports.

- [#1223](#1223)
[`3e85a14`](3e85a14)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - rename
`Query.parse()` to `Query.create()`, and provide exact `TextRange` for
any errors it returns.

- [#1172](#1172)
[`6102886`](6102886)
Thanks [@beta-ziliani](https://github.com/beta-ziliani)! - Improved
error recovery, where leading trivia are always parsed and included
before an erroneous terminal.

- [#1223](#1223)
[`3e85a14`](3e85a14)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - add
`TerminalKindExtensions.is_identifier()` API to distinguish terminals
like Solidity's `Identifier` and Yul's `YulIdentifier`.

### Patch Changes

- [#1134](#1134)
[`cfc62f2`](cfc62f2)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - remove
`YulPathComponent` and just use `YulIdentifier` instead.

- [#1138](#1138)
[`44a706f`](44a706f)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - add `ThisKeyword`
and `SuperKeyword` to the grammar, instead of parsing them as
identifiers.

- [#1134](#1134)
[`cfc62f2`](cfc62f2)
Thanks [@OmarTawfik](https://github.com/OmarTawfik)! - unreserve
`AddressKeyword`, and let it be used for `MemberAccessExpression`,
`StructMember`, etc...

- [#1154](#1154)
[`7b9b478`](7b9b478)
Thanks [@beta-ziliani](https://github.com/beta-ziliani)! - Adding
support for deprecated keywords `jump` and `jumpi`

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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