如何获得Kotlin AST?

我有一个与Kotlin源的字符串。 如何在运行时编译它并获取抽象语法树和类型信息来分析?

2 Solutions collect form web for “如何获得Kotlin AST?”

我有一些Kotlin编译器的调查。 在我的GitHub仓库中可以看到一些AST的概念证明。

这只是一个草图,但可以帮助:

 class KotlinScriptParser { companion object { private val LOG = Logger.getLogger(KotlinScriptParser.javaClass.name) private val messageCollector = object : MessageCollector { override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { val path = location.path val position = if (path == null) "" else "$path: (${location.line}, ${location.column}) " val text = position + message if (CompilerMessageSeverity.VERBOSE.contains(severity)) { LOG.finest(text) } else if (CompilerMessageSeverity.ERRORS.contains(severity)) { LOG.severe(text) } else if (severity == CompilerMessageSeverity.INFO) { LOG.info(text) } else { LOG.warning(text) } } } private val classPath: ArrayList<File> by lazy { val classpath = arrayListOf<File>() classpath += PathUtil.getResourcePathForClass(AnnotationTarget.CLASS.javaClass) classpath } } fun parse(vararg files: String): TopDownAnalysisContext { // The Kotlin compiler configuration val configuration = CompilerConfiguration() val groupingCollector = GroupingMessageCollector(messageCollector) val severityCollector = MessageSeverityCollector(groupingCollector) configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, severityCollector) configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots()) // The path to .kt files sources files.forEach { configuration.addKotlinSourceRoot(it) } // Configuring Kotlin class path configuration.addJvmClasspathRoots(classPath) configuration.put(JVMConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME) configuration.put<List<AnalyzerScriptParameter>>(JVMConfigurationKeys.SCRIPT_PARAMETERS, CommandLineScriptUtils.scriptParameters()) val rootDisposable = Disposer.newDisposable() try { val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) val ktFiles = environment.getSourceFiles() val sharedTrace = CliLightClassGenerationSupport.NoScopeRecordCliBindingTrace() val moduleContext = TopDownAnalyzerFacadeForJVM.createContextWithSealedModule(environment.project, environment.getModuleName()) val project = moduleContext.project val allFiles = JvmAnalyzerFacade.getAllFilesToAnalyze(project, null, ktFiles) val providerFactory = FileBasedDeclarationProviderFactory(moduleContext.storageManager, allFiles) val lookupTracker = LookupTracker.DO_NOTHING val packagePartProvider = JvmPackagePartProvider(environment) val container = createContainerForTopDownAnalyzerForJvm( moduleContext, sharedTrace, providerFactory, GlobalSearchScope.allScope(project), lookupTracker, packagePartProvider) val additionalProviders = ArrayList<PackageFragmentProvider>() additionalProviders.add(container.javaDescriptorResolver.packageFragmentProvider) return container.lazyTopDownAnalyzerForTopLevel.analyzeFiles(TopDownAnalysisMode.LocalDeclarations, allFiles, additionalProviders) } finally { rootDisposable.dispose() if (severityCollector.anyReported(CompilerMessageSeverity.ERROR)) { throw RuntimeException("Compilation error") } } } } fun main(args: Array<String>) { val scriptFile = "/media/data/java/blackfern/kotlin-compile-test/test.kt" val parser = KotlinScriptParser() // Getting a root element of the AST val analyzeContext = parser.parse(scriptFile) // Sample AST investigation val function = analyzeContext.functions.keys.first() val body = function.bodyExpression as KtBlockExpression } 

目前没有标准的API来做到这一点。 您可以使用Kotlin编译器和REPL 源代码来尝试实现这一点。

  • 有没有办法自动填充参数名称?
  • kotlin限定符注释被忽略
  • Android Studio(Kotlin)如何将StringArray转换为MutableList
  • Kotlin:消除List中的空值(或其他功能转换)
  • 有问题从fragmentManager / MapFragment移动到supportfragmentManager / SupportMapFragment
  • 使用函数引用重写Kotlin中的Java代码会发生SAM类型冲突
  • 嘲笑与Kotlin和Mockito的接口
  • 当我在Kotlin中使用委托时,MutableMap <String,Any>如何将值传递给var?
  • kotlin - 数字类型的自动转换
  • 检查计算的输出是否是整数
  • Google Guava Invokedynamic要求--min-sdk-version> = 26
  • Kotlin language will be the best programming language for Android.