kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
325 stars 23 forks source link

Is it possible to insert an element? #92

Open zeroarst opened 1 year ago

zeroarst commented 1 year ago

I am just coming from https://github.com/cretz/kastree as it points me to here.

Bascially what I want to do is to insert a new element. kastree can modify the node, but does not seem to be able to insert a node. So I came here and take a look. Loos like this library can only parse but cannot insert an element? https://github.com/kotlinx/ast/issues/43

sdfgsdfgd commented 8 months ago

inserting an element, removing an element, refactoring an existing element... I'm interested in these use cases as well...

it should be possible to grab the exact same tree, copy it, with a node inserted, or removed, or copy()d

Could the maintainers confirm this is a possible path forward ? Are we able to unwrap() the AST tree to original kotlin code, and then back ? Are we able to modify the AST node tree as I described ? This is a very valuable capability.

If it's not possible with this library, would it be possible to do with ANTLR-kotlin ? Looking forward to any ideas

drieks commented 8 months ago

Hi @sdfgsdfgd,

this library is using different ANTLR versions, you for example use ANTLR-Kotlin for parsing. This library is mainly a wrapper around the "raw" tree parsed by ANTLR, adding for example classes and a list of imports, to make it easier to analyze the tree.

you can use copy to change the kotlinx.ast tree, because it is immutable. But all existing notes are pointing to the initally parsed tree. To put it simply, you can think of the ANTLR tree as being built from an array of tokens (or bytes). if you insert or delete anything, you will invalidate all following tokens. The simplest problem is calucating a new byte offset / row / col for each token.

But you normally can't modify the tree itself, but even if you can, it's very complicated to now that kind of tokens are allowed.

If you think about "I want to insert a new top level class", you can write this in many ways. you can write "class Class" or "class Class {}" or class Class() {}" with almost all combinations of whitespace/comment tokens. All this details are encoded in the ANTLR Tree, but because this is not important for understanding the structure, it is hidden by kotlinx.ast.

The best way to implement this in this library is adding a function, that will drop all ANTLR-related stuff from the kotlinx.ast tree (because modifying it will make it invalid). then adding a new function that will create a new source file out of the AST classes. at this stage you will loose all details like comments and formatting, but it is easy to implement and more or less fast at runtime.

aother idea is reparsing everything for each change. you can copy everything from beginning up to the position you want to change, then you can skip some tokens (for delete) or insert a plain string (containging meybe a new function), then copying all tokens from the curent position to the end. after this, you can reparse the resulting string to get a new valid kotlinx.ast tree (and a valid underlaying antlr tree).

I think this is very slow at runtime, but maybe its fine for you. but you cant't solve stuff like indentation in this way. In summary it is simply in kotlinx.ast, but there is no provision for changing an ANLR tree.

I would recommend you to parse the code with kotlinx.ast and write new code to a new file with kotlinpoet or a template language.

what is your usecase?