Parser for C#

2018-12-31 23:37发布

Which parsers are available for parsing C# code?

I'm looking for a C# parser that can be used in C# and give me access to line and file informations about each artefact of the analysed code.

标签: c# parsing
15条回答
素衣白纱
2楼-- · 2019-01-01 00:13

If you are going to compile C# v3.5 to .net assemblies:

var cp = new Microsoft.CSharp.CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });

http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx

查看更多
琉璃瓶的回忆
3楼-- · 2019-01-01 00:13

You should definitely check out Roslyn since MS just opened (or will soon open) the code with an Apache 2 license here. You can also check out a way to parse this info with this code from GitHub.

查看更多
不再属于我。
4楼-- · 2019-01-01 00:15

Something that is gaining momentum and very appropriate for the job is Nemerle

you can see how it could solve it in these videos from NDC :

查看更多
长期被迫恋爱
5楼-- · 2019-01-01 00:15

Not in C#, but a full C# 2/3/4 parser that builds full ASTs is available with our DMS Software Reengineering Toolkit.

DMS provides a vast infrastructure for parsing, tree building, construction of symbol tables and flow analyses, source-to-source transformation, and regeneration of source code from the (modified) ASTs. (It also handles many other languages than just C#.)

EDIT (September) 2013: This answer hasn't been updated recently. DMS has long handled C# 5.0

查看更多
看淡一切
6楼-- · 2019-01-01 00:17

Mono (open source) includes C# compiler (and of course parser)

查看更多
几人难应
7楼-- · 2019-01-01 00:21

I've implemented just what you are asking (AST Parsing of C# code) at the OWASP O2 Platform project using SharpDevelop AST APIs.

In order to make it easier to consume I wrote a quick API that exposes a number of key source code elements (using statements, types, methods, properties, fields, comments) and is able to rewrite the original C# code into C# and into VBNET.

You can see this API in action on this O2 XRule script file: ascx_View_SourceCode_AST.cs.o2 .

For example this is how you process a C# source code text and populate a number of TreeViews & TextBoxes:

    public void updateView(string sourceCode)
    {   
        var ast = new Ast_CSharp(sourceCode);
        ast_TreeView.show_Ast(ast);
        types_TreeView.show_List(ast.astDetails.Types, "Text");
        usingDeclarations_TreeView.show_List(ast.astDetails.UsingDeclarations,"Text");
        methods_TreeView.show_List(ast.astDetails.Methods,"Text");
        fields_TreeView.show_List(ast.astDetails.Fields,"Text");
        properties_TreeView.show_List(ast.astDetails.Properties,"Text");
        comments_TreeView.show_List(ast.astDetails.Comments,"Text");

        rewritenCSharpCode_SourceCodeEditor.setDocumentContents(ast.astDetails.CSharpCode, ".cs");
        rewritenVBNet_SourceCodeEditor.setDocumentContents(ast.astDetails.VBNetCode, ".vb");                                
    }

The example on ascx_View_SourceCode_AST.cs.o2 also shows how you can then use the information gathered from the AST to select on the source code a type, method, comment, etc..

For reference here is the API code that wrote (note that this is my first pass at using SharpDevelop's C# AST parser, and I am still getting my head around how it works):

查看更多
登录 后发表回答