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