在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):cretz/kastree开源软件地址(OpenSource Url):https://github.com/cretz/kastree开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):KastreeKastree is a simple library to manipulate Kotlin source code as a set of AST objects. Features:
The project is a simple one and probably will not support a lot of features. It was created to facilitate advanced Kotlin code generation beyond the string-based versions that exist. NoteKastree is currently not being actively developed. Another kotlin AST parsing library is kotlinx.ast UsageGettingThis project has three libraries that are deployed to Maven Central. To simply build the AST from a Kotlin JVM or Java project, add the following dependency in Gradle:
That does not include the parser. To include the parser (which transitively includes the entire Kotlin compiler), instead use:
While the parser only works from JVM projects, the AST itself (and writers/visitors) can be used from other multiplatform projects. In the shared/common project, include the common lib:
ExamplesExamples below are simple Kotlin scripts. Parse codeIn this example, we use the wrapper around the Kotlin compiler's parser: import kastree.ast.psi.Parser
val code = """
package foo
fun bar() {
// Print hello
println("Hello, World!")
}
fun baz() = println("Hello, again!")
""".trimIndent()
// Call the parser with the code
val file = Parser.parseFile(code)
// The file var is now a kastree.ast.Node.File that is used in future examples... The // ...
val extrasMap = Converter.WithExtras()
val file = Parser(extrasMap).parseFile(code)
// extrasMap is an instance of kastree.ast.ExtrasMap Write codeTo write the code created above, simply use the writer import kastree.ast.Writer
// ...
println(Writer.write(file)) This outputs a string of the written code from the AST // ...
println(Writer.write(file, extrasMap)) This outputs the code with the comments. View nodes of a typeThis will get all strings: import kastree.ast.Node
import kastree.ast.Visitor
// ...
var strings = emptyList<String>()
Visitor.visit(file) { v, _ ->
if (v is Node.Expr.StringTmpl.Elem.Regular) strings += v.str
}
// Prints [Hello, World!, Hello, again!]
println(strings) The first parameter of the lambda is the nullable node and the second parameter is the parent. There is a Modify nodesThis will change "Hello, World!" and "Hello, again!" to "Howdy, World!" and "Howdy, again": import kastree.ast.MutableVisitor
// ...
val newFile = MutableVisitor.preVisit(file) { v, _ ->
if (v !is Node.Expr.StringTmpl.Elem.Regular) v
else v.copy(str = v.str.replace("Hello", "Howdy"))
} Now Note, since Running testsThe tests rely on a checked out version of the Kotlin source repository since it
uses the test data there to build a corpus to test against. The path to the base of the repo needs to be set as the
This will ignore all Kotlin files with expected parse errors and only test against the ones that are valid (178 as of this writing). The test parses the Kotlin code into this AST, then re-writes this AST, then re-parses what was just written and confirms it matches the original AST field for field. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论