C#:创建方法头解析器

发布于 2024-11-03 02:54:15 字数 299 浏览 0 评论 0原文

我想编写一个解析器来告诉我字符串的哪一部分是方法头。在 C# 中执行此操作的最佳方法是什么?

语言语法规范可以在此处找到。我不认为这是正确的 BNF/EBNF,所以也许有一种方法可以将其转换为这样的(就像将其放入正确的 BNF 的 html 解析器)。

我应该以某种方式使用正则表达式还是自定义构建的解析器?我受到限制,因为我需要自己构建它,而无需外部工具的帮助。

I would like to write a parser to tell me what part of a string is a methodheader. What is the best way to do this in C#?

The language grammar specification can be found here. I don't think this is proper BNF/EBNF, so perhaps there is a way to transform it into such (like an html parser that puts it into proper BNF.)

Should I use regular expressions or a custom built parser somehow? I am restricted in that I need to build it myself without the help of outside tools.

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

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

发布评论

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

评论(1

始终不够 2024-11-10 02:54:15

我发现 NRefactory 库(开源 SharpDevelop 工具的一部分)非常擅长解析将 C# 模块放入抽象语法树中。一旦有了抽象语法树,您就可以非常轻松地浏览以查找方法头、位置等。

虽然它的主要用途是在 SharpDevelop(一个 GUI 工具)中使用,但它是一个独立的 DLL,并且可以在任何 .NET 应用程序中使用。据我所知,该文档不是很详尽,但 Reflector 让我可以很容易地检查它并找出答案。

一些代码:

    internal static string CreateAstSexpression(string filename)
    {
        using (var fs = File.OpenRead(filename))
        {
            using (var parser = ParserFactory.CreateParser(SupportedLanguage.CSharp,
                                                           new StreamReader(fs)))
            {
                parser.Parse();

                // RetrieveSpecials() returns an IList<ISpecial>
                // parser.Lexer.SpecialTracker.RetrieveSpecials()...
                // "specials" == comments, preprocessor directives, etc.

                // parser.CompilationUnit retrieves the root node of the result AST
                return SexpressionGenerator.Generate(parser.CompilationUnit).ToString();
            }
        }
    }

ParserFactory 类是 NRefactory 的一部分。
就我而言,我想要一个描述 C# 缓冲区的 lisp s 表达式,因此我编写了一个遍历“CompilationUnit”的 S 表达式生成器。它只是一棵节点树,从命名空间开始,然后是类/结构/枚举。在类/结构节点内,有方法节点(以及字段、属性等)。


如果对完成的 DLL 不感兴趣,那么也许这个是。

在发现并采用 NRefactory 之前,我尝试为 C# 生成明智的语法。这是在 emacs 中使用的,它有一个wisent引擎。

我永远无法让它正常工作。
也许它对你有用。


你说你不想使用“外部工具”。不确定该限制的动机;如果它是家庭作业,那么我想这是有道理的,但出于其他目的,如果不使用已经存在的经过充分测试和充分理解的工具,那确实是一种耻辱。

如果您采纳我在这里提出的任何建议,那么您正在构建某种外部工具。但有些选项比其他选项要好一些。

I found the NRefactory library, part of the open-source SharpDevelop tool, to be very good at parsing C# modules into an abstract syntax tree. Once you have that you can scan through very easily to find the method headers, the locations, and so on.

Though its primary use is for within SharpDevelop (A GUI tool), it is a standalone DLL, and it can be used within any .NET app. The documentation isn't very thorough, as far as I could tell, but Reflector let me examine it and figure things out pretty easily.

some code:

    internal static string CreateAstSexpression(string filename)
    {
        using (var fs = File.OpenRead(filename))
        {
            using (var parser = ParserFactory.CreateParser(SupportedLanguage.CSharp,
                                                           new StreamReader(fs)))
            {
                parser.Parse();

                // RetrieveSpecials() returns an IList<ISpecial>
                // parser.Lexer.SpecialTracker.RetrieveSpecials()...
                // "specials" == comments, preprocessor directives, etc.

                // parser.CompilationUnit retrieves the root node of the result AST
                return SexpressionGenerator.Generate(parser.CompilationUnit).ToString();
            }
        }
    }

The ParserFactory class is part of NRefactory.
In my case I wanted a lisp s-expression describing the C# buffer, so I wrote an S-expression generator that walked through the "CompilationUnit". It's just a tree of nodes, starting with namespace, then class/struct/enum. Within the class/struct node, there are method nodes (as well as field, property, etc).


If that finished DLL is not of interest, then maybe this is.

Before finding and embracing NRefactory, I tried to produce a wisent grammar for c#. This was for use within emacs, which has a wisent engine.

I never could get it to work properly.
Maybe it's of use to you.


you said that you didn't want to use "outside tools". Not sure of the motivation for that restriction; if it is homework, then I guess it makes sense, but for other purposes, it really would be a shame to not use the well-tested and well-understood tools that are already out there.

If you take either of the suggestions I've made here, you're building on something that is an outside tool. But some of the options are a little better than others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文