Comments (2)
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.
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)
- Add a grammar for Kotlin Script
- org.antlr.v4.kotlinruntime.atn.ATNConfig object has a large number of instance counts, OOM
- Need "kts" scripts support
- incorrect breakline parsing HOT 1
- AST Node Tree Refactoring / Insertion / Removal
- Consider editing repository setings to remove "Packages" section HOT 2
- I am really confused, which dependency should be used? HOT 5
- End-of-line comments not available in summary HOT 2
- Property delegate not available in summary HOT 2
- Argument with unary prefix is incomplete in summary AST HOT 2
- Best way to add new keywords like in the Spock test HOT 4
- Can I get the specified method code block HOT 3
- Record syntax error messages
- Why column is called row?
- Unsupported ast node functionType when function type used as generic type
- Publish somewhere different than jitpack.io HOT 10
- Add support for @JvmSuppressWildcards/ Annotations for types. HOT 4
- How can I get the specified method code block!Looking forward to answer
- How do I rely on this project on Maven? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ast.