c# – 可以停下步行者吗?

前端之家收集整理的这篇文章主要介绍了c# – 可以停下步行者吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个ParseTree监听器实现,我用来在标准VBA模块中获​​取全局范围声明:
public class DeclarationSectionListener : DeclarationListener
{
    private bool _insideProcedure;

    public override void EnterVariableStmt(VisualBasic6Parser.VariableStmtContext context)
    {
        var visibility = context.visibility();
        if (!_insideProcedure && visibility == null 
            || visibility.GetText() == Tokens.Public
            || visibility.GetText() == Tokens.Global)
        {
            base.EnterVariableStmt(context);
        }
    }

    public override void EnterConstStmt(VisualBasic6Parser.ConstStmtContext context)
    {
        var visibility = context.visibility();
        if (!_insideProcedure && visibility == null
            || visibility.GetText() == Tokens.Public
            || visibility.GetText() == Tokens.Global)
        {
            base.EnterConstStmt(context);
        }
    }

    public override void EnterArg(VisualBasic6Parser.ArgContext context)
    {
        return;
    }

    public override void EnterSubStmt(VisualBasic6Parser.SubStmtContext context)
    {
        _insideProcedure = true;
    }

    public override void EnterFunctionStmt(VisualBasic6Parser.FunctionStmtContext context)
    {
        _insideProcedure = true;
    }

    public override void EnterPropertyGetStmt(VisualBasic6Parser.PropertyGetStmtContext context)
    {
        _insideProcedure = true;
    }

    public override void EnterPropertyLetStmt(VisualBasic6Parser.PropertyLetStmtContext context)
    {
        _insideProcedure = true;
    }

    public override void ExitPropertySetStmt(VisualBasic6Parser.PropertySetStmtContext context)
    {
        _insideProcedure = true;
    }
}

有没有办法告诉树步行者停止走路?说我有一个这样的VBA模块:

Public Const foo = 123
Public bar As String

Public Sub DoSomething()
    ' some code
End Sub

' ...
' 10K more lines of code
' ...

Private Function GetSomething() As String
     ' some code 
End Function

一旦进入Public Sub DoSomething(),我希望树木随便停止走解析树,因为我对下面的任何东西都不感兴趣.目前,我正在走整个解析树,只是在任何程序范围内,我都不用做任何事情.

有没有办法,从解析树监听器实现中,告诉步行者停止走树?

解决方法

是的,您可以执行以下操作之一:

>抛出异常,如CancellationException
>创建一个扩展ParseTreeWalker的新课程,并提供您自己的实现walk,了解您需要的取消概念.从current implementation of walk开始,修改它以满足您的需求是有意义的.

猜你在找的C#相关文章