/proc/self/cwd/external/antlr4-cpp-runtime~/runtime/src/ParserRuleContext.cpp
Line | Count | Source |
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/TerminalNode.h" |
7 | | #include "tree/ErrorNode.h" |
8 | | #include "misc/Interval.h" |
9 | | #include "Parser.h" |
10 | | #include "Token.h" |
11 | | |
12 | | #include "support/Casts.h" |
13 | | #include "support/CPPUtils.h" |
14 | | |
15 | | #include "ParserRuleContext.h" |
16 | | |
17 | | using namespace antlr4; |
18 | | using namespace antlr4::tree; |
19 | | |
20 | | using namespace antlrcpp; |
21 | | |
22 | | ParserRuleContext ParserRuleContext::EMPTY; |
23 | | |
24 | | ParserRuleContext::ParserRuleContext() |
25 | 9.52M | : start(nullptr), stop(nullptr) { |
26 | 9.52M | } |
27 | | |
28 | | ParserRuleContext::ParserRuleContext(ParserRuleContext *parent, size_t invokingStateNumber) |
29 | 18.9M | : RuleContext(parent, invokingStateNumber), start(nullptr), stop(nullptr) { |
30 | 18.9M | } |
31 | | |
32 | 9.52M | void ParserRuleContext::copyFrom(ParserRuleContext *ctx) { |
33 | | // from RuleContext |
34 | 9.52M | this->parent = ctx->parent; |
35 | 9.52M | this->invokingState = ctx->invokingState; |
36 | | |
37 | 9.52M | this->start = ctx->start; |
38 | 9.52M | this->stop = ctx->stop; |
39 | | |
40 | | // copy any error nodes to alt label node |
41 | 9.52M | if (!ctx->children.empty()) { |
42 | 3.38k | for (auto *child : ctx->children) { |
43 | 3.38k | if (ErrorNode::is(child)) { |
44 | 3.38k | downCast<ErrorNode*>(child)->setParent(this); |
45 | 3.38k | children.push_back(child); |
46 | 3.38k | } |
47 | 3.38k | } |
48 | | |
49 | | // Remove the just reparented error nodes from the source context. |
50 | 3.38k | ctx->children.erase(std::remove_if(ctx->children.begin(), ctx->children.end(), [this](tree::ParseTree *e) -> bool { |
51 | 3.38k | return std::find(children.begin(), children.end(), e) != children.end(); |
52 | 3.38k | }), ctx->children.end()); |
53 | 3.38k | } |
54 | 9.52M | } |
55 | | |
56 | 18.9M | void ParserRuleContext::enterRule(tree::ParseTreeListener * /*listener*/) { |
57 | 18.9M | } |
58 | | |
59 | 18.9M | void ParserRuleContext::exitRule(tree::ParseTreeListener * /*listener*/) { |
60 | 18.9M | } |
61 | | |
62 | 6.53M | tree::TerminalNode* ParserRuleContext::addChild(tree::TerminalNode *t) { |
63 | 6.53M | t->setParent(this); |
64 | 6.53M | children.push_back(t); |
65 | 6.53M | return t; |
66 | 6.53M | } |
67 | | |
68 | 25.8M | RuleContext* ParserRuleContext::addChild(RuleContext *ruleInvocation) { |
69 | 25.8M | children.push_back(ruleInvocation); |
70 | 25.8M | return ruleInvocation; |
71 | 25.8M | } |
72 | | |
73 | 6.87M | void ParserRuleContext::removeLastChild() { |
74 | 6.87M | if (!children.empty()) { |
75 | 6.87M | children.pop_back(); |
76 | 6.87M | } |
77 | 6.87M | } |
78 | | |
79 | 0 | tree::TerminalNode* ParserRuleContext::getToken(size_t ttype, size_t i) const { |
80 | 0 | if (i >= children.size()) { |
81 | 0 | return nullptr; |
82 | 0 | } |
83 | 0 | size_t j = 0; // what token with ttype have we found? |
84 | 0 | for (auto *child : children) { |
85 | 0 | if (TerminalNode::is(child)) { |
86 | 0 | tree::TerminalNode *typedChild = downCast<tree::TerminalNode*>(child); |
87 | 0 | Token *symbol = typedChild->getSymbol(); |
88 | 0 | if (symbol->getType() == ttype) { |
89 | 0 | if (j++ == i) { |
90 | 0 | return typedChild; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | } |
94 | 0 | } |
95 | 0 | return nullptr; |
96 | 0 | } |
97 | | |
98 | 0 | std::vector<tree::TerminalNode *> ParserRuleContext::getTokens(size_t ttype) const { |
99 | 0 | std::vector<tree::TerminalNode*> tokens; |
100 | 0 | for (auto *child : children) { |
101 | 0 | if (TerminalNode::is(child)) { |
102 | 0 | tree::TerminalNode *typedChild = downCast<tree::TerminalNode*>(child); |
103 | 0 | Token *symbol = typedChild->getSymbol(); |
104 | 0 | if (symbol->getType() == ttype) { |
105 | 0 | tokens.push_back(typedChild); |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | 0 | return tokens; |
110 | 0 | } |
111 | | |
112 | 0 | misc::Interval ParserRuleContext::getSourceInterval() { |
113 | 0 | if (start == nullptr) { |
114 | 0 | return misc::Interval::INVALID; |
115 | 0 | } |
116 | | |
117 | 0 | if (stop == nullptr || stop->getTokenIndex() < start->getTokenIndex()) { |
118 | 0 | return misc::Interval(start->getTokenIndex(), start->getTokenIndex() - 1); // empty |
119 | 0 | } |
120 | 0 | return misc::Interval(start->getTokenIndex(), stop->getTokenIndex()); |
121 | 0 | } |
122 | | |
123 | 2.22M | Token* ParserRuleContext::getStart() const { |
124 | 2.22M | return start; |
125 | 2.22M | } |
126 | | |
127 | 2.22M | Token* ParserRuleContext::getStop() const { |
128 | 2.22M | return stop; |
129 | 2.22M | } |
130 | | |
131 | 0 | std::string ParserRuleContext::toInfoString(Parser *recognizer) { |
132 | 0 | std::vector<std::string> rules = recognizer->getRuleInvocationStack(this); |
133 | 0 | std::reverse(rules.begin(), rules.end()); |
134 | 0 | std::string rulesStr = antlrcpp::arrayToString(rules); |
135 | 0 | return "ParserRuleContext" + rulesStr + "{start=" + std::to_string(start->getTokenIndex()) + ", stop=" + |
136 | 0 | std::to_string(stop->getTokenIndex()) + '}'; |
137 | 0 | } |
138 | | |