这就是我和我的团队选择为我们的学校项目做的事情。嗯,实际上我们还没有决定如何解析 C# 源文件。
我们的目标是对 C# 源文件进行全面分析,并生成报告。
其中报告将包含代码中发生的事情。
该报告只需包含:
- 字符串文字、
- 方法名称、
- 变量名称、
- 字段名称
- 等,
我负责研究这个 Irony 库。老实说,我不知道将数据整理成干净可读的报告的最佳方法。我正在使用用 zip 打包的 C# 语法类。
有什么步骤可以正确识别每个节点子节点吗? (例如:using 指令、命名空间声明、类声明等、方法体)
任何帮助或建议将非常感激。谢谢。
编辑:抱歉,我忘了说我们也需要分析方法调用。
This is what my team and I chose to do for our school project. Well, actually we haven't decided on how to parse the C# source files yet.
What we are aiming to achieve is, perform a full analysis on a C# source file, and produce up a report.
In which the report is going to contain stuff that happening in the codes.
The report only has to contain:
- string literals
- method names
- variable names
- field names
- etc
I'm in charge of looking into this Irony library. To be honest, I don't know the best way to sort the data out into a clean readable report. I am using the C# grammar class packed with the zip.
Is there any step where I can properly identify each node children? (eg: using directives, namespace declaration, class declaration etc, method body)
Any help or advice would be very much appreciated. Thanks.
EDIT: Sorry I forgot to say we need to analysis the method calls too.
发布评论
评论(2)
您的主要目标是掌握形式语言的基础知识。 这里可能会找到一个好的初创公司。本文介绍了如何在一个简单的数字计算器的语法示例中使用 Irony。
假设您想要解析包含 C# 代码的某个文件以及您知道的路径:
这里是遍历方法本身,并计算节点中的一些信息。实际上,这段代码计算了类的每个方法中的语句数量。如果您有任何疑问,欢迎随时来问我
Your main goal is to master the basics of formal languages. A good start-up might be found here. This article describes the way to use Irony on the sample of a grammar of a simple numeric calculator.
Suppose you want to parse a certain file containing C# code the path to which you know:
And here is the traversal method itself with counting some info in the nodes. Actually this code counts the number of statements in every method of the class. If you have any question you are always welcome to ask me
我不确定这是否是您所需要的,但您可以使用 CodeDom 和 CodeDom.Compiler 命名空间来编译 C# 代码,然后使用反射分析结果,例如:
更新: 在 VS2015 中,您可以使用新的 C# 编译器(又名 Roslyn)来执行相同的操作,对于 示例:
I'm not sure this is what you need but you could use the CodeDom and CodeDom.Compiler namespaces to compile the C# code, and than analyze the results using Reflection, something like:
Update: In VS2015 you could use the new C# compiler (AKA Roslyn) to do the same, for example: