I try to use Rascal to compute metrics out of an AST that is created with Clang out of Objective C code. Since the structure of the Clang AST differs from the one that is used in Rascal, i think it needs some refactoring or workarounds to work with it.
One way could be to write a parser that translate the AST. An other way could be to use regular expressions with recursion. But there are a lot of elements in the Clang AST and it would take some time to write a parser or a function.
Is there a less time consuming way to traverse a Clang AST in Rascal?
I believe it would be best to write some code against the Clang AST API to print the AST in a Rascal readable form. We use this strategy for other front-ends as well.
I.e. use the information in http://clang.llvm.org/docs/RAVFrontendAction.html to make a recursive AST visitor that prints things like:
ifStatement(intConstant(1),[])
After this you can read it in the AST using ValueIO
: readTextValueFile(#node, file)
, or you could use the ShellExec
library and readTextValueString
function.
This would give you a representation of the AST typed node
. If you want a typed representation, then also data declarations need to be generated, as in:
data Statement = ifStatement(Expression cond, list[Statement] body);
By the way, if anybody has written a JSON or XML exporter for Clang ASTs already, you would be king because there are libraries for Rascal to directly read in these formats.