“CSharpCodeProvider.Parse”的替代方案是

发布于 2024-09-17 12:46:32 字数 200 浏览 7 评论 0原文

我正在寻找 CSharpCodeProvider.Parse 的替代方案。该方法应该解析 [C#] 代码源并返回一个 CompileUnit 对象。但是,该方法并未在任何 .Net 框架中实现。

我的目的是能够导航 C# CodeDOM,而无需编译它。我正在编写一个进行一些代码分析的应用程序,但我不一定拥有所有外部引用,这意味着我无法编译它。

I'm looking for an alternative for CSharpCodeProvider.Parse. The method was supposed to parse a [C#] code source and return a CompileUnit object. However, the method isn't implemented in any .Net framework.

My purpose is to be able to navigate a C# CodeDOM without having to compile it. I'm writing a application that does some code analysis but I won't necessarily have all external references, which means I can't compile it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

以往的大感动 2024-09-24 12:46:33

我们的 DMS 软件再工程工具包是用于构建任意语言分析工具的工具。
DMS 提供广义解析、AST 导航和修改、从修改后的树重新生成源、符号表支持和各种分析支持,以及编写直接在表面上修改 AST 的源到源转换的能力句法。

它的 C# 前端 提供了一个完整的 C# 4.0 解析器(包括 LINQ),可以构建包含源文本的每个项目的完整抽象语法树,包括作为注释装饰的源树节点上捕获的注释。

Our DMS Software Reengineering Toolkit is a tool for building analysis tools for arbitrary languages.
DMS provides generalized parsing, AST navigation and modification, regeneration of source from a modified tree, symbol table support, and various kinds of analysis support as well as the ability to write source-to-source transformations that modify the ASTs directly in terms of surface syntax.

Its C# Front End provides a full C# 4.0 parser (including LINQ) that builds a full abstract syntax tree containing every item of the source text, including comments captured as annotations on source tree nodes that the comments decorate.

意中人 2024-09-24 12:46:32

SharpDevelop(Mono 常用的开源 IDE)有一个名为 NRefactory 的库,它允许您解析 C# 代码并将其转换为 AST:http://wiki.sharpdevelop.net/NRefactory.ashx(该链接摘录如下):

using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(sourceCode))) 
{
    parser.Parse();
    // this allows retrieving comments, preprocessor directives, etc. (stuff that isn't part of the syntax)
    specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
    // this retrieves the root node of the result AST
    result = parser.CompilationUnit;
    if (parser.Errors.Count > 0) {
        MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
    }
}

SharpDevelop (the open source IDE commonly used for Mono) has a library called NRefactory that allows you to parse C# code and convert it into an AST: http://wiki.sharpdevelop.net/NRefactory.ashx (Excerpt from that link follows):

using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(sourceCode))) 
{
    parser.Parse();
    // this allows retrieving comments, preprocessor directives, etc. (stuff that isn't part of the syntax)
    specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
    // this retrieves the root node of the result AST
    result = parser.CompilationUnit;
    if (parser.Errors.Count > 0) {
        MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
    }
}
度的依靠╰つ 2024-09-24 12:46:32

有许多免费 C# 解析器,最流行的显然是:

  • < a href="http://csparser.codeplex.com/" rel="nofollow noreferrer">CsParser (在 CodePlex 上)

There are many free C# parsers, the most popular apparently being:

淡写薰衣草的香 2024-09-24 12:46:32

实际(2017-2018)信息:

  1. 更多信息请访问https://github.com /icsharpcode/SharpDevelop/wiki/NRefactory

  2. 下载 nuget 包:“ICSharpCode.NRefactory”

  3. 这里是代码片段:

    使用ICSharpCode.NRefactory.CSharp;
    使用系统;
    使用 System.Collections.Generic;
    使用系统.IO;
    使用 System.Linq;
    使用系统文本;
    使用 System.Threading.Tasks;
    名称空间 NS1
    {
        公共课 Foo
        {
            公共无效Go()
            {       
                CSharpParser 解析器 = new CSharpParser();
                字符串文本 = File.ReadAllText("myProgram.cs");
                SyntaxTree 语法树 = parser.Parse(text);
            }
        }
    }
    

Actual (2017-2018) info:

  1. More info available at https://github.com/icsharpcode/SharpDevelop/wiki/NRefactory

  2. Download nuget package: "ICSharpCode.NRefactory"

  3. And here is code snippet:

    using ICSharpCode.NRefactory.CSharp;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Ns1
    {
        public class Foo
        {
            public void Go()
            {       
                CSharpParser parser = new CSharpParser();
                string text = File.ReadAllText("myProgram.cs");
                SyntaxTree syntaxTree = parser.Parse(text);
            }
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文