Skip to content

Instantly share code, notes, and snippets.

@Wonicon
Created March 28, 2017 10:31
Show Gist options
  • Save Wonicon/886eaae2557085637aea2c13ce1bccf6 to your computer and use it in GitHub Desktop.
Save Wonicon/886eaae2557085637aea2c13ce1bccf6 to your computer and use it in GitHub Desktop.
Check ancestor in ANTLR
import org.antlr.v4.runtime.ParserRuleContext;
public class AntlrExt {
public static boolean underContext(ParserRuleContext curr, ParserRuleContext target, ParserRuleContext limit) {
do {
curr = curr.getParent();
} while (curr.getRuleIndex() != target.getRuleIndex() && curr.getRuleIndex() != limit.getRuleIndex());
return curr.getRuleIndex() == target.getRuleIndex();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment