简体   繁体   English

AST Walker似乎工作不正常

[英]AST walker doesn't seem to work fine

I am using Roslyn and I have created an AST walker in order to detect when certain nodes are traversed. 我正在使用Roslyn,并且创建了一个AST Walker来检测何时遍历某些节点。

The source I am trying to parse is the following: 我尝试解析的来源如下:

var source = string.Format(@"
    using System;
    using System.Collections;
    using System.Linq;
    using System.Text;

    namespace HelloWorld
      {{
         class {0} : {1}
         {{
            static void Main(string[] args)
            {{
               Console.WriteLine(""Hello, World!"");
            }}
         }}
      }}", "MyClass", "MyBaseClass");

And this is the walker: 这是沃克:

namespace MyStuff
{
    using System;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;

    public class MyWalker : CSharpSyntaxWalker
    {
        public MyWalker(SyntaxNode node) :
          base(SyntaxWalkerDepth.StructuredTrivia)
        {
            this.Root = node;
        }

        public SyntaxNode Root { get; private set; }

        public void Start()
        {
            this.Visit(this.Root);
        }

        sealed public override void VisitAccessorDeclaration(AccessorDeclarationSyntax node)
        {
            this.DoSomething(node, this.type.IsInstanceOfType(node));
        }

        ...

        sealed public override void VisitYieldStatement(YieldStatementSyntax node)
        {
            this.DoSomething(node, this.type.IsInstanceOfType(node));
        }

        // BREAKPOINT IN THE BODY OF THIS FUNCTION!
        private void DoSomething(SyntaxNode node)
        {
            ...
        }       
    }
}

I am overriding all visit methods, alphabetically from VisitAccessorDeclaration to VisitYieldStatement . 我将覆盖所有访问方法,按字母顺序从VisitAccessorDeclarationVisitYieldStatement I am doing this just to try stuff, not for real applications, I know it is quite nonsense. 我这样做只是为了尝试一些东西,而不是针对实际应用程序,我知道这完全是胡说八道。

Visiting 参观

I run it: 我运行它:

SyntaxNode root = CSharpSyntaxTree.ParseText(source).GetRoot();

And then run the walker: 然后运行助行器:

var astWalker = new MyWalker(this.Root);
astExecutor.Start();
// NEXT OPERATION

What happens? 怎么了? I seta breakpoint on function DoSomething and I expect it to be it a lot of times. 我在函数DoSomething上设置了一个断点,并且我希望它会出现很多次。 Actually it does not happen. 实际上这不会发生。 It happens only for one node: VisitCompilationUnit . 它仅在一个节点上发生: VisitCompilationUnit After the control flow passes on on // NEXT OPERATION and that's it. 在控制流程继续// NEXT OPERATION ,就是这样。

What am I missing? 我想念什么? Thanks 谢谢

In your overrides, you need to call the base version of the method you are overriding. 在覆盖中,您需要调用要覆盖的方法的基本版本。 We allow you to stop the walk. 我们允许您停止步行。 By not calling base so that if you are trying to visit only specific nodes, you can control it. 通过不调用base,以便如果您只尝试访问特定节点,则可以对其进行控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM