[英]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.