]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/tree/xpath/XPathRuleElement.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / tree / xpath / XPathRuleElement.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/Trees.h"
8
9 #include "XPathRuleElement.h"
10
11 using namespace antlr4::tree;
12 using namespace antlr4::tree::xpath;
13
14 XPathRuleElement::XPathRuleElement(const std::string &ruleName, size_t ruleIndex) : XPathElement(ruleName) {
15   _ruleIndex = ruleIndex;
16 }
17
18 std::vector<ParseTree *> XPathRuleElement::evaluate(ParseTree *t) {
19   // return all children of t that match nodeName
20   std::vector<ParseTree *> nodes;
21   for (auto *c : t->children) {
22     if (antlrcpp::is<ParserRuleContext *>(c)) {
23       ParserRuleContext *ctx = dynamic_cast<ParserRuleContext *>(c);
24       if ((ctx->getRuleIndex() == _ruleIndex && !_invert) || (ctx->getRuleIndex() != _ruleIndex && _invert)) {
25         nodes.push_back(ctx);
26       }
27     }
28   }
29   return nodes;
30 }