JuliaLang / JuliaSyntax.jl

The Julia compiler frontend
Other
274 stars 33 forks source link

Location of TypedSyntaxNode #192

Closed timholy closed 1 year ago

timholy commented 1 year ago

I now think that the best way to handle https://github.com/JuliaDebug/Cthulhu.jl/pull/345 is to create

mutable struct TypedSyntaxNode <: AbstractSyntaxNode
    node::SyntaxNode
    typ::Any
    # These duplicate info in SyntaxNode but are needed so we can build trees of TypedSyntaxNodes
    parent::Union{Nothing,SyntaxNode}
    val::Any
end

Do you want that type, possibly together with its printing functions, to live here? Or should it be a separate package?

A more invasive but extensible approach would be to factor out the "data" of nodes from the tree structure. Then assembly and traversal become orthogonal from content. So we'd have mutable struct TreeNode{NodeData} with parent, children, and val::Union{NodeData,Nothing}, where val (for any node that isn't just a container of other nodes) might be

struct SyntaxData
    source::SourceFile
    raw::GreenNode{SyntaxHead}
    position::Int
end

Then we'd only need TypedSyntaxData with an extra typ field; everything else would be reusable.

c42f commented 1 year ago

Do you want that type, possibly together with its printing functions, to live here?

I think maybe we don't? It's obviously useful, but there might be more fields needed for specific purposes.

Generally I feel there's may possible fields which might be needed in various analyses or transformations and we can't hope to cover them all. (JuliaFormatter, for example, has its own entire set of fields for formatting purposes.)

I'm super happy to cover the common cases if we know what they are. Or somehow just make it really easy to build custom trees with "extra" fields. mutable struct TreeNode{NodeData} seems like a good start on that.