1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2 * Use of this file is governed by the BSD 3-clause license that
3 * can be found in the LICENSE.txt file in the project root.
6 #include "tree/ParseTree.h"
7 #include "tree/pattern/ParseTreePatternMatcher.h"
8 #include "tree/pattern/ParseTreeMatch.h"
10 #include "tree/xpath/XPath.h"
11 #include "tree/xpath/XPathElement.h"
13 #include "tree/pattern/ParseTreePattern.h"
15 using namespace antlr4::tree;
16 using namespace antlr4::tree::pattern;
18 using namespace antlrcpp;
20 ParseTreePattern::ParseTreePattern(ParseTreePatternMatcher *matcher, const std::string &pattern, int patternRuleIndex_,
21 ParseTree *patternTree)
22 : patternRuleIndex(patternRuleIndex_), _pattern(pattern), _patternTree(patternTree), _matcher(matcher) {
25 ParseTreePattern::~ParseTreePattern() {
28 ParseTreeMatch ParseTreePattern::match(ParseTree *tree) {
29 return _matcher->match(tree, *this);
32 bool ParseTreePattern::matches(ParseTree *tree) {
33 return _matcher->match(tree, *this).succeeded();
36 std::vector<ParseTreeMatch> ParseTreePattern::findAll(ParseTree *tree, const std::string &xpath) {
37 xpath::XPath finder(_matcher->getParser(), xpath);
38 std::vector<ParseTree *> subtrees = finder.evaluate(tree);
39 std::vector<ParseTreeMatch> matches;
40 for (auto *t : subtrees) {
41 ParseTreeMatch aMatch = match(t);
42 if (aMatch.succeeded()) {
43 matches.push_back(aMatch);
50 ParseTreePatternMatcher *ParseTreePattern::getMatcher() const {
54 std::string ParseTreePattern::getPattern() const {
58 int ParseTreePattern::getPatternRuleIndex() const {
59 return patternRuleIndex;
62 ParseTree* ParseTreePattern::getPatternTree() const {