C# 解析器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/81406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:19:24  来源:igfitidea点击:

Parser for C#

提问by Julien Hoarau

Which parsers are available for parsing C# code?

哪些解析器可用于解析 C# 代码?

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# 中使用的 C# 解析器,让我可以访问有关所分析代码的每个人工制品的行和文件信息。

采纳答案by Julien Hoarau

Works on source code:

适用于源代码:

Works on assembly:

装配工作:

The problem with assembly "parsing" is that we have less informations about line and file (the informations is based on .pdb file, and Pdb contains lines informations only for methods)

汇编“解析”的问题是我们关于行和文件的信息较少(信息基于.pdb文件,而Pdb只包含方法的行信息)

I personnaly recommend Mono.Ceciland NRefactory.

我个人推荐Mono.CecilNRefactory

回答by aku

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

Mono(开源)包括 C# 编译器(当然还有解析器)

回答by Akselsson

SharpDevelop, an open source IDE, comes with a visitor-based code parser which works really well. It can be used independently of the IDE.

SharpDevelop是一个开源 IDE,带有一个基于访问者的代码解析器,效果非常好。它可以独立于 IDE 使用。

回答by Hallgrim

Consider to use reflection on a built binary instead of parsing the C# code directly. The reflection API is really easy to use and perhaps you can get all the information you need?

考虑对构建的二进制文件使用反射,而不是直接解析 C# 代码。反射 API 真的很容易使用,也许您可​​以获得所需的所有信息?

回答by leppie

GPPGmight be of use, if you are willing to write your own parser (which is fun).

如果您愿意编写自己的解析器(这很有趣),GPPG可能会有用。

回答by sbeskur

Have a look at Gold Parser. It has a very intuitive IU that lets you interactively test your grammar and generate C# code. There are plenty of examples available with it and it is completely free.

看看黄金解析器。它有一个非常直观的 IU,可让您以交互方式测试语法并生成 C# 代码。它有很多可用的示例,而且它是完全免费的。

回答by Dinis Cruz

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

我已经使用 SharpDevelop AST API在OWASP O2 平台项目中实现了您所要求的(C# 代码的 AST 解析)。

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.

为了使其更易于使用,我编写了一个快速 API,该 API 公开了许多关键源代码元素(使用语句、类型、方法、属性、字段、注释),并且能够将原始 C# 代码重写为 C# 和 VBNET .

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

您可以在此 O2 XRule 脚本文件中看到此 API 的运行情况: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:

例如,这是处理 C# 源代码文本并填充许多 TreeView 和 TextBox 的方式:

    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.o2also shows how you can then use the information gathered from the AST to select on the source code a type, method, comment, etc..

ascx_View_SourceCode_AST.cs.o2上的示例还展示了如何使用从 AST 收集的信息在源代码上选择类型、方法、注释等。

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):

作为参考,这里是编写的 API 代码(请注意,这是我第一次使用 SharpDevelop 的 C# AST 解析器,我仍在了解它的工作原理):

回答by Ira Baxter

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

不是在 C# 中,而是在我们的DMS 软件再工程工具包中提供了构建完整 AST 的完整 C# 2/3/4 解析器。

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#.)

DMS 为解析、树构建、符号表的构建和流分析、源到源转换以及从(修改后的)AST 重新生成源代码提供了大量基础设施。(它还处理许多其他语言,而不仅仅是 C#。)

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

编辑(九月)2013:这个答案最近没有更新。DMS 长期以来一直在处理 C# 5.0

回答by zproxy

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

如果要将 C# v3.5 编译为 .net 程序集:

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

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

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