繁体   English   中英

给定一个 ANTLR ParserRuleContext,我如何找到给定类型的第一个父级?

[英]Given an ANTLR ParserRuleContext, how do I find the first parent of a given type?

我正在使用 ANTLR 的XPath来获取数据库视图的表依赖项:

//create_view//from_clause//tableview_name

现在我需要获取每个表名所属的视图名称。 例如,如果有FirstParentOfType方法,则代码可能如下所示:

context.FirstParentOfType<Create_viewContext>().tableview_name().GetText();

但据我所知,这种方法不存在。 这是怎么做的?

等待更好的方法,这是一个扩展方法:

public static class RuleContextExtensions
{
    public static T FirstParentOfType<T>(this RuleContext context) where T : RuleContext => context as T ?? (context.Parent != null ? FirstParentOfType<T>(context.Parent) : null);
}

暂无
暂无

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

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