Skip to content

Conversation

@c42f
Copy link
Member

@c42f c42f commented Aug 7, 2024

Here I commit to a more consistent but simpler child access API for syntax trees, as informed by the JuliaLowering work so far:

  • is_leaf(node) is given a precise definition (previously !haschildren() - but that had issues - see Rename haschildren() to is_leaf() #483)
  • children(node) returns the child list, or nothing if there are no children. The nothing might be seen as inconvenient, but mapping across the children of a leaf node is probably an error and one should probably branch on is_leaf first.
  • numchildren(node) is documented
  • node[i], node[i:j] are documented to index into the child list

We distinguish GreenNode and its implementation of span from SyntaxNode and its implementation of byte_range and sourcetext - these seem to just have very different APIs, at least as of now.

I've deleted the questionable overloads of multidimensional getindex and the child function in favor of single dimensional getindex. I don't know whether anyone ever ended up using these. But I didn't and they didn't seem useful+consistent enough to keep the complexity.

I've kept setindex! for now, to set a child of a SyntaxNode. Though I'm not sure this is a good idea to support by default and I didn't mark it public yet.

Also I've added more docs!

Also mark the public parts of the API as public in Julia 1.11+

@c42f c42f requested review from LilithHafner and timholy August 7, 2024 07:31
@c42f c42f force-pushed the caf/tree-API-cleanup branch 2 times, most recently from 6cd1b85 to 57c8c3c Compare August 7, 2024 10:34
Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

This looks good. I think TypedSyntax's tests do exploit multidimensional child, but that's an easy change. I presume this will become 0.5.0?

@c42f
Copy link
Member Author

c42f commented Aug 9, 2024

I presume this will become 0.5.0?

We need to get to 1.0 so we can use semver properly. So the next release will be 1.0.0 :-) I've been working really hard on a lot of 1.0.0 changes in the last few weeks, trying to get the documentation and APIs in better shape. See also #472, but I keep finding more to do - it turns out there's a lot of changes I want to squeeze in which are informed by the JuliaLowering work.

@c42f
Copy link
Member Author

c42f commented Aug 9, 2024

I think TypedSyntax's tests do exploit multidimensional child, but that's an easy change.

We could keep child() if you think it's useful? In that case I'd document what it does and mark it public. It's more the multidimensional getindex which is dubious and should probably be deleted.

@c42f c42f force-pushed the caf/tree-API-cleanup branch from bca9487 to 5aa4740 Compare August 9, 2024 02:00
@timholy
Copy link
Member

timholy commented Aug 9, 2024

Either way is fine; it's not such a hard change to make to TypedSyntax's tests. A regex search suggests there are 74 uses of child(arg, i, j...) and that's not too bad to change if you think it would be cleaner without.

c42f added 3 commits August 10, 2024 05:53
Here I commit to a more consistent but simpler child access API for
syntax trees, as informed by the JuliaLowering work so far:

* `is_leaf(node)` is given a precise definition (previously
  `!haschildren()` - but that had issues - see #483)
* `children(node)` returns the child list, or `nothing` if there are no
  children. The `nothing` might be seen as inconvenient, but mapping
  across the children of a leaf node is probably an error and one should
  probably branch on `is_leaf` first.
* `numchildren(node)` is documented
* `node[i]`, `node[i:j]` are documented to index into the child list

We distinguish `GreenNode` and its implementation of `span` from
`SyntaxNode` and its implementation of `byte_range` and `sourcetext` -
these seem to just have very different APIs, at least as of now.

I've deleted the questionable overloads of multidimensional `getindex`
and the `child` function in favor of single dimensional getindex. I
don't know whether anyone ever ended up using these. But I didn't and
they didn't seem useful+consistent enough to keep the complexity.

I've kept setindex! for now, to set a child of a `SyntaxNode`. Though
I'm not sure this is a good idea to support by default.
@c42f c42f force-pushed the caf/tree-API-cleanup branch from 5aa4740 to b644c87 Compare August 9, 2024 19:54
@c42f c42f merged commit 0a0aa04 into main Aug 9, 2024
@c42f c42f deleted the caf/tree-API-cleanup branch August 9, 2024 20:09
@c42f
Copy link
Member Author

c42f commented Aug 9, 2024

Ok thanks! Let's go with the more minimal API then :)

c42f added a commit to JuliaLang/julia that referenced this pull request Oct 17, 2025
…ee-API-cleanup

Clean up and document syntax tree child access API + mark public API
topolarity pushed a commit to JuliaLang/julia that referenced this pull request Nov 14, 2025
…ee-API-cleanup

Clean up and document syntax tree child access API + mark public API
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