在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Kotlin/grammar-tools开源软件地址(OpenSource Url):https://github.com/Kotlin/grammar-tools开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):kotlin-grammar-toolsDescriptionThis library allows tokenize and parse Kotlin code in your program using the Kotlin grammar. Simple example: fun main() {
val tokens = tokenizeKotlinCode("val x = foo() + 10;")
val parseTree = parseKotlinCode(tokens)
// or just `val parseTree = parseKotlinCode("val x = foo() + 10;")`
println(parseTree)
} Tokens or parse tree can be used for various Kotlin code analysis. Note that the parse tree may not match exactly to PSI (parse tree generated by Kotlin compiler). This is due to the fact that some errors for the user convenience are not generated at the parser level, but later; the grammar, in turn, takes into account such cases and may not allow the code that could be parsed by the Kotlin compiler parser. Kotlin grammarThe grammar is located in the Kotlin specification repository. StatusThe library is develped only for internal purposes of the Kotlin team, and actual state of the library isn't guaranteed. UsingTo use the library you can run the Also, you can just download jar from Releases or TeamCity ( ExceptionsLexer and parser are throwing exceptions if it has been inputted the invalid code (in terms of lexer or parser): Example of handling this exceptions: fun foo(): ParseTree? {
val tokens = try {
tokenizeKotlinCode("val x = foo() + 10;")
} catch (e: KotlinLexerException) {
println("Tokenization the code fails")
return null
}
val parseTree = try {
parseKotlinCode(tokens)
} catch (e: KotlinParserException) {
println("Parsing the code fails")
return null
}
return parseTree
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论