/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
public static boolean ruleHasMultipleOutermostAlts(Parser parser, ParseTree ruleTree) { Collection<ParseTree> ors = XPath.findAll(ruleTree, "/parserRuleSpec/ruleBlock/ruleAltList/OR", parser); if ( ors.size()>=1 ) return true; ors = XPath.findAll(ruleTree, "/lexerRule/lexerRuleBlock/lexerAltList/OR", parser); return ors.size()>=1; }
public static TerminalNode getRuleDefNameNode(Parser parser, ParseTree tree, String ruleName) { Collection<ParseTree> ruleDefRuleNodes; if ( Grammar.isTokenName(ruleName) ) { ruleDefRuleNodes = XPath.findAll(tree, "//lexerRule/TOKEN_REF", parser); } else { ruleDefRuleNodes = XPath.findAll(tree, "//parserRuleSpec/RULE_REF", parser); } for (ParseTree node : ruleDefRuleNodes) { String r = node.getText(); // always a TerminalNode; just get rule name of this def if ( r.equals(ruleName) ) { return (TerminalNode)node; } } return null; }
public static List<TerminalNode> getAllRuleRefNodes(Parser parser, ParseTree tree, String ruleName) { List<TerminalNode> nodes = new ArrayList<TerminalNode>(); Collection<ParseTree> ruleRefs; if ( Grammar.isTokenName(ruleName) ) { ruleRefs = XPath.findAll(tree, "//lexerRuleBlock//TOKEN_REF", parser); } else { ruleRefs = XPath.findAll(tree, "//ruleBlock//RULE_REF", parser); } for (ParseTree node : ruleRefs) { TerminalNode terminal = (TerminalNode)node; Token rrefToken = terminal.getSymbol(); String r = rrefToken.getText(); if ( r.equals(ruleName) ) { nodes.add(terminal); } } if ( nodes.size()==0 ) return null; return nodes; }
/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
/** * Find all nodes using XPath and then try to match those subtrees against * this tree pattern. * * @param tree The {@link ParseTree} to match against this pattern. * @param xpath An expression matching the nodes * * @return A collection of {@link ParseTreeMatch} objects describing the * successful matches. Unsuccessful matches are omitted from the result, * regardless of the reason for the failure. */ @NotNull public List<ParseTreeMatch> findAll(@NotNull ParseTree tree, @NotNull String xpath) { Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser()); List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>(); for (ParseTree t : subtrees) { ParseTreeMatch match = match(t); if ( match.succeeded() ) { matches.add(match); } } return matches; }
Collection<ParseTree> literalNodes = XPath.findAll(tree, "//ruleBlock//STRING_LITERAL", parser); LinkedHashMap<String, String> lexerRules = new LinkedHashMap<String, String>(); for (ParseTree node : literalNodes) { Collection<ParseTree> allRuleNodes = XPath.findAll(tree, "//ruleSpec", parser); for (ParseTree r : allRuleNodes) {