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/Trees.h"
8 
9 #include "XPathRuleElement.h"
10 
11 using namespace antlr4::tree;
12 using namespace antlr4::tree::xpath;
13 
XPathRuleElement(const std::string & ruleName,size_t ruleIndex)14 XPathRuleElement::XPathRuleElement(const std::string& ruleName,
15                                    size_t ruleIndex)
16     : XPathElement(ruleName) {
17   _ruleIndex = ruleIndex;
18 }
19 
evaluate(ParseTree * t)20 std::vector<ParseTree*> XPathRuleElement::evaluate(ParseTree* t) {
21   // return all children of t that match nodeName
22   std::vector<ParseTree*> nodes;
23   for (auto c : t->children) {
24     if (antlrcpp::is<ParserRuleContext*>(c)) {
25       ParserRuleContext* ctx = dynamic_cast<ParserRuleContext*>(c);
26       if ((ctx->getRuleIndex() == _ruleIndex && !_invert) ||
27           (ctx->getRuleIndex() != _ruleIndex && _invert)) {
28         nodes.push_back(ctx);
29       }
30     }
31   }
32   return nodes;
33 }
34