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/Trees.h"
7 #include "misc/Interval.h"
8 #include "Parser.h"
9 #include "atn/ATN.h"
10 #include "atn/ATNState.h"
11 #include "tree/ParseTreeVisitor.h"
12
13 #include "RuleContext.h"
14
15 using namespace antlr4;
16 using namespace antlr4::atn;
17
RuleContext()18 RuleContext::RuleContext() {
19 InitializeInstanceFields();
20 }
21
RuleContext(RuleContext * parent_,size_t invokingState_)22 RuleContext::RuleContext(RuleContext *parent_, size_t invokingState_) {
23 InitializeInstanceFields();
24 this->parent = parent_;
25 this->invokingState = invokingState_;
26 }
27
depth()28 int RuleContext::depth() {
29 int n = 1;
30 RuleContext *p = this;
31 while (true) {
32 if (p->parent == nullptr)
33 break;
34 p = static_cast<RuleContext *>(p->parent);
35 n++;
36 }
37 return n;
38 }
39
isEmpty()40 bool RuleContext::isEmpty() {
41 return invokingState == ATNState::INVALID_STATE_NUMBER;
42 }
43
getSourceInterval()44 misc::Interval RuleContext::getSourceInterval() {
45 return misc::Interval::INVALID;
46 }
47
getText()48 std::string RuleContext::getText() {
49 if (children.empty()) {
50 return "";
51 }
52
53 std::stringstream ss;
54 for (size_t i = 0; i < children.size(); i++) {
55 ParseTree *tree = children[i];
56 if (tree != nullptr)
57 ss << tree->getText();
58 }
59
60 return ss.str();
61 }
62
getRuleIndex() const63 size_t RuleContext::getRuleIndex() const {
64 return INVALID_INDEX;
65 }
66
getAltNumber() const67 size_t RuleContext::getAltNumber() const {
68 return atn::ATN::INVALID_ALT_NUMBER;
69 }
70
setAltNumber(size_t)71 void RuleContext::setAltNumber(size_t /*altNumber*/) {
72 }
73
accept(tree::ParseTreeVisitor * visitor)74 antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) {
75 return visitor->visitChildren(this);
76 }
77
toStringTree(Parser * recog,bool pretty)78 std::string RuleContext::toStringTree(Parser *recog, bool pretty) {
79 return tree::Trees::toStringTree(this, recog, pretty);
80 }
81
toStringTree(std::vector<std::string> & ruleNames,bool pretty)82 std::string RuleContext::toStringTree(std::vector<std::string> &ruleNames, bool pretty) {
83 return tree::Trees::toStringTree(this, ruleNames, pretty);
84 }
85
toStringTree(bool pretty)86 std::string RuleContext::toStringTree(bool pretty) {
87 return toStringTree(nullptr, pretty);
88 }
89
90
toString(const std::vector<std::string> & ruleNames)91 std::string RuleContext::toString(const std::vector<std::string> &ruleNames) {
92 return toString(ruleNames, nullptr);
93 }
94
95
toString(const std::vector<std::string> & ruleNames,RuleContext * stop)96 std::string RuleContext::toString(const std::vector<std::string> &ruleNames, RuleContext *stop) {
97 std::stringstream ss;
98
99 RuleContext *currentParent = this;
100 ss << "[";
101 while (currentParent != stop) {
102 if (ruleNames.empty()) {
103 if (!currentParent->isEmpty()) {
104 ss << currentParent->invokingState;
105 }
106 } else {
107 size_t ruleIndex = currentParent->getRuleIndex();
108
109 std::string ruleName = (ruleIndex < ruleNames.size()) ? ruleNames[ruleIndex] : std::to_string(ruleIndex);
110 ss << ruleName;
111 }
112
113 if (currentParent->parent == nullptr) // No parent anymore.
114 break;
115 currentParent = static_cast<RuleContext *>(currentParent->parent);
116 if (!ruleNames.empty() || !currentParent->isEmpty()) {
117 ss << " ";
118 }
119 }
120
121 ss << "]";
122
123 return ss.str();
124 }
125
toString()126 std::string RuleContext::toString() {
127 return toString(nullptr);
128 }
129
toString(Recognizer * recog)130 std::string RuleContext::toString(Recognizer *recog) {
131 return toString(recog, &ParserRuleContext::EMPTY);
132 }
133
toString(Recognizer * recog,RuleContext * stop)134 std::string RuleContext::toString(Recognizer *recog, RuleContext *stop) {
135 if (recog == nullptr)
136 return toString(std::vector<std::string>(), stop); // Don't use an initializer {} here or we end up calling ourselve recursivly.
137 return toString(recog->getRuleNames(), stop);
138 }
139
InitializeInstanceFields()140 void RuleContext::InitializeInstanceFields() {
141 invokingState = INVALID_INDEX;
142 }
143
144