]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/pattern/ParseTreePattern.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / tree / pattern / ParseTreePattern.cpp
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.
4  */
5
6 #include "tree/ParseTree.h"
7 #include "tree/pattern/ParseTreePatternMatcher.h"
8 #include "tree/pattern/ParseTreeMatch.h"
9
10 #include "tree/xpath/XPath.h"
11 #include "tree/xpath/XPathElement.h"
12
13 #include "tree/pattern/ParseTreePattern.h"
14
15 using namespace antlr4::tree;
16 using namespace antlr4::tree::pattern;
17
18 using namespace antlrcpp;
19
20 ParseTreePattern::ParseTreePattern(ParseTreePatternMatcher *matcher, const std::string &pattern, int patternRuleIndex_,
21                                    ParseTree *patternTree)
22   : patternRuleIndex(patternRuleIndex_), _pattern(pattern), _patternTree(patternTree), _matcher(matcher) {
23 }
24
25 ParseTreePattern::~ParseTreePattern() {
26 }
27
28 ParseTreeMatch ParseTreePattern::match(ParseTree *tree) {
29   return _matcher->match(tree, *this);
30 }
31
32 bool ParseTreePattern::matches(ParseTree *tree) {
33   return _matcher->match(tree, *this).succeeded();
34 }
35
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);
44     }
45   }
46   return matches;
47 }
48
49
50 ParseTreePatternMatcher *ParseTreePattern::getMatcher() const {
51   return _matcher;
52 }
53
54 std::string ParseTreePattern::getPattern() const {
55   return _pattern;
56 }
57
58 int ParseTreePattern::getPatternRuleIndex() const {
59   return patternRuleIndex;
60 }
61
62 ParseTree* ParseTreePattern::getPatternTree() const {
63   return _patternTree;
64 }