]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/RuleContext.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / RuleContext.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/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
18 RuleContext::RuleContext() {
19   InitializeInstanceFields();
20 }
21
22 RuleContext::RuleContext(RuleContext *parent_, size_t invokingState_) {
23   InitializeInstanceFields();
24   this->parent = parent_;
25   this->invokingState = invokingState_;
26 }
27
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
40 bool RuleContext::isEmpty() {
41   return invokingState == ATNState::INVALID_STATE_NUMBER;
42 }
43
44 misc::Interval RuleContext::getSourceInterval() {
45   return misc::Interval::INVALID;
46 }
47
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
63 size_t RuleContext::getRuleIndex() const {
64   return INVALID_INDEX;
65 }
66
67 size_t RuleContext::getAltNumber() const {
68   return atn::ATN::INVALID_ALT_NUMBER;
69 }
70
71 void RuleContext::setAltNumber(size_t /*altNumber*/) {
72 }
73
74 antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) {
75   return visitor->visitChildren(this);
76 }
77
78 std::string RuleContext::toStringTree(Parser *recog, bool pretty) {
79   return tree::Trees::toStringTree(this, recog, pretty);
80 }
81
82 std::string RuleContext::toStringTree(std::vector<std::string> &ruleNames, bool pretty) {
83   return tree::Trees::toStringTree(this, ruleNames, pretty);
84 }
85
86 std::string RuleContext::toStringTree(bool pretty) {
87   return toStringTree(nullptr, pretty);
88 }
89
90
91 std::string RuleContext::toString(const std::vector<std::string> &ruleNames) {
92   return toString(ruleNames, nullptr);
93 }
94
95
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
126 std::string RuleContext::toString() {
127   return toString(nullptr);
128 }
129
130 std::string RuleContext::toString(Recognizer *recog) {
131   return toString(recog, &ParserRuleContext::EMPTY);
132 }
133
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
140 void RuleContext::InitializeInstanceFields() {
141   invokingState = INVALID_INDEX;
142 }
143