I's starting a new project at my university for design pattern detection. I'd
write a VS2005 Add-In but i need to access the abstract syntax tree used in
visual studio (if avaible). Do you know if it is possible?
thanks
Carlos J. Quintero [VB MVP] - 27 Apr 2006 15:12 GMT
Hi,
I am not sure about what abstract syntax is, but the extensibility model of
VS only allows you to parse the source code and get its code elements. See
my articles:
HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or
add-in
http://www.mztools.com/articles/2006/MZ004.htm
HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro
or add-in
http://www.mztools.com/articles/2006/MZ008.htm

Signature
Best regards,
Carlos J. Quintero
MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
> I's starting a new project at my university for design pattern detection.
> I'd
[quoted text clipped - 3 lines]
>
> thanks
Don Caton - 27 Apr 2006 15:59 GMT
> I's starting a new project at my university for design pattern detection. I'd
> write a VS2005 Add-In but i need to access the abstract syntax tree used in
> visual studio (if avaible). Do you know if it is possible?
>
> thanks
There is no such thing as "the abstract syntax tree used in Visual
Studio".
Each language's compiler creates its own AST, but that is an internal
implementation detail and I don't know of any compilers that expose
their AST's to the outside world.
Every language service package in Visual Studio (the thing that handles
syntax highlighting, intellisense, etc.) also implements an AST (or uses
some service exposed by the language's compiler to do it) but this too
is an internal implementation detail of the particular VS language
service.
Visual Studio itself knows nothing about the particulars of any given
language service or project package it hosts. If you want an AST,
you'll have to generate it yourself. About the only thing VS can help
you with is giving you access to the text buffer in a code editor
window.
I suggest you take a look at Antlr (www.antlr.org). Writing a parser
for a non-trivial language is a lot of work, but there are existing
grammar files for many languages on the Antlr web site which would give
you a good starting point.
--
Don
ali@hodroj.net - 27 Apr 2006 19:56 GMT
Hi,
I assume you meant the AST for a C# source code? or VB.NET? In
either way you cannot get that AST through visual studio because it's
internal to the compiler. However, you do need to write any parser,
what you can do is use Code Domain Object Model classes provided by
.NET, where every terminal and non-terminal of the C# grammar is coded
as a class (all subclasses of CodeObject class). In this case, all you
need to do is declare a CSharpCodeProvider class, use the Parse method
to parse the text buffer (source code), and then walk through the
CodeCompileUnit object returned from the Parse() method to detect the
design patterns (which shouldn't be hard depending on the pattern
you're looking for).
Code Domain Object Model:
http://msdn2.microsoft.com/en-US/library/system.codedom(VS.80).aspx
CSharpCodeProvider:
http://msdn2.microsoft.com/en-US/library/microsoft.csharp.csharpcodeprovider(VS.
80).aspx
Regards,
Ali M Hodroj
ali@hodroj.net - 27 Apr 2006 19:57 GMT
oops ....
"However, you do need to write any parser,"
should've been "However, you do NOT need to write any parser,"