The Ceylon Typechecker: How to obtain the typed syntax tree?

爷,独闯天下 提交于 2019-12-22 08:27:49

问题


Im trying to programmatically use / embed the Ceylon Typechecker to analyse Ceylon source code. In this use case I want to access all the information that normally the compiler would consume. But Im not going to compile and Im not going to add dependency on the compiler.

It seem to me that the main.Main entry point in ceylon/typechecker/src/main/Main.java is not the appropriate entry point for this use case (obtaining the typed tree and the attached models), because this information, which was collected by the visitors in the three checker passes is discarded, and only errors are printed.

So, my question is:

How can I parse and typecheck a compilation unit and then obtain 1. the typed syntax tree, and 2. the associated model objects of the types the analysis visitors encounter in the tree, which are linked to from the tree.

edited:

There was (and is) some confusion on my side about the 3 different ASTs there are.

In the README in ceylon /ceylon.ast it is said:

¨ ... ceylon.ast.core – the Ceylon classes that represent a Ceylon AST. Pure Ceylon (backend-independent). ... ceylon.ast.redhat – transforms a ceylon.ast.core AST from + to a RedHat compiler (ceylon-spec AST, and also contains functions to compile a ceylon.ast.core AST from a code string (using the RedHat compiler) ... ¨.

So there are 3 ASTs: 1. The one generated by antlr, 2. ceylon.ast.core, and 3. ceylon.ast.redhat. Why?


回答1:


In short, you'll want to:

  • Configure a TypeCheckerBuilder with the source files you want to typecheck,
  • Obtain a TypeChecker from the builder (builder.typechecker),
  • Invoke the typechecker (typeChecker.process()),
  • Process the results available from typeChecker.phasedUnits. Specifically, typeChecker.getPhasedUnits().getPhasedUnits() will give you a List<PhasedUnit>, and for each PhasedUnit, you can call getCompilationUnit() to obtain its Tree.CompilationUnit, which is the root of the AST. AST nodes also include getters for model objects.

For a detailed example, you can review the code for the Dart backend, working forwards and backwards from the call to process() in the compileDart() function.

See testCompile for example code that calls compileDart().



来源:https://stackoverflow.com/questions/35274781/the-ceylon-typechecker-how-to-obtain-the-typed-syntax-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!