-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I get the AST representing statements inside the method? #21
Comments
And how can I get the AST representing statements inside the method? |
Hi @rajatpundir, Parsing the body of a function is currently not possible, but maybe easy to add. I will try to figure out how complicated this is. |
Hi @rajatpundir, I added support for line/row information and buffer index offset. The Kotlin source can be found in The number in the first row is the index in the token stream, so only tokens (leafs of the tree) have such an id. the first range the start and end in the buffer (starting from zero), the second range is line:col, both starting at 1. You can use
|
Sadly, there are only some summary nodes, but the full kotlin language is currently not covered. I'm using the library to build an outline of the file. you can access the raw ast of the function, for example this function:
will be represented in the summay ast as:
When you use the extension function It is possible to add the required ast nodes for a summary here: for every possible ast node type, a convert call can be added there to convert the given type of raw ast to the summay ast. all existing ast nodes are already added as comments. But sadly, I have currently no time to implement this. If anybody can help implementing this, please feel free to send me any questions! |
Hi, @drieks |
Hi @lululu016, This will run all tests found here: When this is working, try to change one of files, for example in ´Annotation1.kt.txt´, you can change Annotation1 to AnnotationX. When you rerun the tests, this test should be marked as failed. Then, create a new file called ´.override-test-data´ in the root folder of the repository (where README.md is located) and rerun the test again. Now, the test will update the data in the unit test folder and will abort the test with the message The same applies when you add a new file ´FunctionReturn.kt.txt´ with some kotlin function you want to parse. The first test data should be small, so you can start implementing a simple ast node. Maybe Run the unittest to let the unittest write its actual data as the expected data. Now you can open the new file ´MyExample.raw.ast.txt´ in your editor to see the raw (unparsed) AST. Here, you can see the return ast node in line 48: ´´´ The Code to simplify the ast is implemented in the file ´grammar-kotlin-parser-common/src/commonMain/kotlin/kotlinx/ast/grammar/kotlin/common/summary/KotlinTreeMapBuilder.kt´ ´´´ This comment is taken from the antlr grammar for the kotlin language, here after this comment you can add the new code. But before you can start, you have to "fix" the existing converter for "functionBody": This means 'convert all ast nodes matching the filter "byDescription("functionBody")' using this lambda:
|
Hi, @drieks |
Hi, @drieks |
Hi @lululu016, this is great! I will try to write some more examples. |
Hi, @@drieks Recently I try to extract statement inside the method, and I would like to ask for help for some quetions I faced and look forwards to some examples from you. For example, if I want to represent a fun main(arg: Array<String>) {
while(num>=5){
println("Loop: $num")
num--
}
} I want to represent it in ast.summary like this:
How should I do?I would be really appreciate if you could give me some examples, so that I can complete other statement, like |
Hi @lululu016, The first step is adding the transformation target. The "raw" AST is always a generic tree structure and therefore not typesafe. Currently, all supported kotlin ast nodes are transformed into instances of ´Klass´ (see Klass.kt line 9). Because there is currently no support for blocks, a generic statement should be added and maybe a loop statement (like in the kotlin grammar, see KotlinParser.g4 line 323 and 340ff). ´KlassLoopStatement´ should contain a type describing the loop type (for, while...), maybe ´KlassLoopType´. I created a new branch, feel free to change anything, the code is not tested. |
Hi @lululu016 , I added a transformation from functionBody to KlassBlock. The Implementation is not finished, currently, only a dummy comment is added as an example:
You can see this comments in the updated unit test files. The next step is to handle "block" and "expression" child nodes of functionBody recursively. |
Hi @lululu016 , As you can see in line 443 of file |
In the next commit (drieks@7c79d0a) I added the "real" implementation for the "functionBody" transformation.
The "ASSIGNMENT" and "NL" tokens are not matched by the filter and therefore dropped. See the grammar as a reference, added as a comment before the convert function:
All |
When parsing AST, how can I find out on which line a particular Class or Function was declared?
The text was updated successfully, but these errors were encountered: