Using Microsoft.Cci.Ast to build C# AST?

May 15, 2009 at 4:51 PM

Hi,

Do I understand it correctly that I can use the Microsoft.Cci.Ast namespace to implement a C# AST?

Can I use it "as-is", or do I have to extend it for this purpose? I mean I can see a lot of classes that reflect concepts that I need for a C# AST but still I guess some concepts are missing, eg. query expressions. Is that right?

Does the Ast model support preserving source comments? What about preprocessor directives? If not, then is there a recommended way to implement such concepts?

Does CCI support semantic analysis steps like resolving type references, expression classifications, member references, etc.?

Thanks,

Ferenc

Coordinator
May 15, 2009 at 5:34 PM

You can pretty much use the CCI AST base classes as is for a C# AST. However, as you observe, there is currently not much support for C# v3 and none for C# v4.

The base classes support semantic analysis steps like resolving type references and so on. These are needed in order to project the AST nodes onto CodeModel nodes.

The AST model does not support preserving source comments and preprocessor directives. These are best modeled in a language specific manner. Such models exist in the prototype Spec# compiler based on this version of CCI. (The released Spec# compiler uses an earlier version of CCI.) The prototype will hopefully be released in early July under a non commercial license. The basic idea is to have a model for tokens (i.e. given a source file, get from it a list of tokens, where each token tells you what it is and where it can be found). Tokens include comments and their source locations can be related to source locations in the AST nodes (or even the Code Model nodes). Likewise there is a model that gives you a list of preprocessor directives associated with a source file, as well as the regions that have been included and excluded by the preprocessor. You can think of the preprocessor as a separate component that produces a preprocessor model, which is then consumed by the scanner to produce a token model, which is then consumed by the parser to produce an AST, which is the projected onto the Code Model.

Please note that the AST classes are somewhat incomplete and not well tested. They represent a lot of work, which you can save by reusing them, but you'll have to contribute quite a bit of additional work to get to a functional C# v3 compiler.