/** * Determine whether or not a parse tree matches this tree pattern. * * @param tree The parse tree to match against this tree pattern. * @return {@code true} if {@code tree} is a match for the current tree * pattern; otherwise, {@code false}. */ public boolean matches(ParseTree tree) { return matcher.match(tree, this).succeeded(); }
/** * Compare {@code pattern} matched as rule {@code patternRuleIndex} against * {@code tree} and return a {@link ParseTreeMatch} object that contains the * matched elements, or the node at which the match failed. */ public ParseTreeMatch match(ParseTree tree, String pattern, int patternRuleIndex) { ParseTreePattern p = compile(pattern, patternRuleIndex); return match(tree, p); }
public List<? extends Token> tokenize(String pattern) { List<Chunk> chunks = split(pattern); TagChunk tagChunk = (TagChunk)chunk; if ( Character.isUpperCase(tagChunk.getTag().charAt(0)) ) { Integer ttype = parser.getTokenType(tagChunk.getTag()); if ( ttype==Token.INVALID_TYPE ) { throw new IllegalArgumentException("Unknown token "+tagChunk.getTag()+" in pattern: "+pattern); TokenTagToken t = new TokenTagToken(tagChunk.getTag(), ttype, tagChunk.getLabel()); tokens.add(t); else if ( Character.isLowerCase(tagChunk.getTag().charAt(0)) ) { int ruleIndex = parser.getRuleIndex(tagChunk.getTag()); if ( ruleIndex==-1 ) { throw new IllegalArgumentException("Unknown rule "+tagChunk.getTag()+" in pattern: "+pattern); tokens.add(new RuleTagToken(tagChunk.getTag(), ruleImaginaryTokenType, tagChunk.getLabel())); throw new IllegalArgumentException("invalid tag: "+tagChunk.getTag()+" in pattern: "+pattern); ANTLRInputStream in = new ANTLRInputStream(textChunk.getText()); lexer.setInputStream(in); Token t = lexer.nextToken();
TokenTagToken tokenTagToken = (TokenTagToken)t2.getSymbol(); labels.map(tokenTagToken.getTokenName(), tree); if ( tokenTagToken.getLabel()!=null ) { labels.map(tokenTagToken.getLabel(), tree); ParseTree mismatchedNode = null; RuleTagToken ruleTagToken = getRuleTagToken(r2); if ( ruleTagToken!=null ) { ParseTreeMatch m = null; if ( r1.getRuleContext().getRuleIndex() == r2.getRuleContext().getRuleIndex() ) { labels.map(ruleTagToken.getRuleName(), tree); if ( ruleTagToken.getLabel()!=null ) { labels.map(ruleTagToken.getLabel(), tree); ParseTree childMatch = matchImpl(r1.getChild(i), patternTree.getChild(i), labels); if ( childMatch != null ) { return childMatch;
/** * Compare {@code pattern} matched against {@code tree} and return a * {@link ParseTreeMatch} object that contains the matched elements, or the * node at which the match failed. Pass in a compiled pattern instead of a * string representation of a tree pattern. */ public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern) { MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>(); ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); return new ParseTreeMatch(tree, pattern, labels, mismatchedNode); }
List<? extends Token> tokenList = tokenize(pattern); ListTokenSource tokenSrc = new ListTokenSource(tokenList); CommonTokenStream tokens = new CommonTokenStream(tokenSrc); throw new CannotInvokeStartRule(e); throw new StartRuleDoesNotConsumeFullPattern(); return new ParseTreePattern(this, pattern, patternRuleIndex, tree);
/** * 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; }
chunks.add(new TextChunk(text)); chunks.add(new TextChunk(text)); ruleOrToken = tag.substring(colon+1, tag.length()); chunks.add(new TagChunk(label, ruleOrToken)); if ( i+1 < ntags ) { chunks.add(new TextChunk(text)); if ( afterLastTag < n ) { // copy text from end of last tag to end String text = pattern.substring(afterLastTag, n); chunks.add(new TextChunk(text)); if ( c instanceof TextChunk ) { TextChunk tc = (TextChunk)c; String unescaped = tc.getText().replace(escape, ""); if (unescaped.length() < tc.getText().length()) { chunks.set(i, new TextChunk(unescaped));
/** * The same as {@link #compileParseTreePattern(String, int)} but specify a * {@link Lexer} rather than trying to deduce it from this parser. */ public ParseTreePattern compileParseTreePattern(String pattern, int patternRuleIndex, Lexer lexer) { ParseTreePatternMatcher m = new ParseTreePatternMatcher(lexer, this); return m.compile(pattern, patternRuleIndex); }
/** Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a * compiled pattern instead of a string representation of a tree pattern. */ public boolean matches(ParseTree tree, ParseTreePattern pattern) { MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>(); ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); return mismatchedNode == null; }
/** * Match a specific parse tree against this tree pattern. * * @param tree The parse tree to match against this tree pattern. * @return A {@link ParseTreeMatch} object describing the result of the * match operation. The {@link ParseTreeMatch#succeeded()} method can be * used to determine whether or not the match was successful. */ public ParseTreeMatch match(ParseTree tree) { return matcher.match(tree, this); }
/** * Compare {@code pattern} matched against {@code tree} and return a * {@link ParseTreeMatch} object that contains the matched elements, or the * node at which the match failed. Pass in a compiled pattern instead of a * string representation of a tree pattern. */ public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern) { MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>(); ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); return new ParseTreeMatch(tree, pattern, labels, mismatchedNode); }
/** * Determine whether or not a parse tree matches this tree pattern. * * @param tree The parse tree to match against this tree pattern. * @return {@code true} if {@code tree} is a match for the current tree * pattern; otherwise, {@code false}. */ public boolean matches(ParseTree tree) { return matcher.match(tree, this).succeeded(); }
/** * Compare {@code pattern} matched as rule {@code patternRuleIndex} against * {@code tree} and return a {@link ParseTreeMatch} object that contains the * matched elements, or the node at which the match failed. */ public ParseTreeMatch match(ParseTree tree, String pattern, int patternRuleIndex) { ParseTreePattern p = compile(pattern, patternRuleIndex); return match(tree, p); }
/** * Compare {@code pattern} matched against {@code tree} and return a * {@link ParseTreeMatch} object that contains the matched elements, or the * node at which the match failed. Pass in a compiled pattern instead of a * string representation of a tree pattern. */ public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern) { MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>(); ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); return new ParseTreeMatch(tree, pattern, labels, mismatchedNode); }
/** * Determine whether or not a parse tree matches this tree pattern. * * @param tree The parse tree to match against this tree pattern. * @return {@code true} if {@code tree} is a match for the current tree * pattern; otherwise, {@code false}. */ public boolean matches(ParseTree tree) { return matcher.match(tree, this).succeeded(); }
/** * Compare {@code pattern} matched against {@code tree} and return a * {@link ParseTreeMatch} object that contains the matched elements, or the * node at which the match failed. Pass in a compiled pattern instead of a * string representation of a tree pattern. */ public ParseTreeMatch match(ParseTree tree, ParseTreePattern pattern) { MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>(); ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels); return new ParseTreeMatch(tree, pattern, labels, mismatchedNode); }
/** * Determine whether or not a parse tree matches this tree pattern. * * @param tree The parse tree to match against this tree pattern. * @return {@code true} if {@code tree} is a match for the current tree * pattern; otherwise, {@code false}. */ public boolean matches(ParseTree tree) { return matcher.match(tree, this).succeeded(); }