Giter VIP home page Giter VIP logo

Comments (2)

sdfgsdfgd avatar sdfgsdfgd commented on August 21, 2024 1

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

from ast.

drieks avatar drieks commented on August 21, 2024

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?

from ast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.