eclipse-langium / langium

Next-gen language engineering / DSL framework
https://langium.org/
MIT License
740 stars 66 forks source link

Node path information in the AST #329

Closed spoenemann closed 2 years ago

spoenemann commented 2 years ago

In https://github.com/langium/langium/pull/324 I added a $path property to AST nodes, mainly for enabling programmatic construction of ASTs. An example where this is necessary is when you provide a built-in library in memory. The AstNodeLocator currently depends on the node model to compute paths, which does not work for programmatic ASTs.

An alternative would be to add $containerProperty and maybe also $containerIndex to AST nodes instead of $path. This might also be useful for user code when you want to inspect where a node is contained. And in addition this approach would make the AstNodeContent type redundant, so we could simplify the streamContents and streamAllContents utilities.

What do you think?

dhuebner commented 2 years ago

An alternative would be to add $containerProperty and maybe also $containerIndex

This sounds like EMF.

instead of $path.

Path is a precomputed and points to a node inside a document independent on how deep it is nested. Without a path you will need to iterate over the whole container hierarchy to find a node or to say from which document it comes from.

Would it be instead possible to have a utility that creates a path for a programmatically created AstNodes which than can be used to locate them? Imagine a path #//foo/bar means a path from the root object

spoenemann commented 2 years ago

This sounds like EMF.

It's just basic tree traversal. We already have $container, which is the same as EMF's eContainer.

Would it be instead possible to have a utility that creates a path for a programmatically created AstNodes which than can be used to locate them?

Yes, but we would still need to keep the $path property that I created. My point is that by replacing it with $containerProperty, we could benefit from multiple sides: