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.
6 #include "tree/Trees.h"
7 #include "misc/Interval.h"
10 #include "atn/ATNState.h"
11 #include "tree/ParseTreeVisitor.h"
13 #include "RuleContext.h"
15 using namespace antlr4;
16 using namespace antlr4::atn;
18 RuleContext::RuleContext() {
19 InitializeInstanceFields();
22 RuleContext::RuleContext(RuleContext *parent_, size_t invokingState_) {
23 InitializeInstanceFields();
24 this->parent = parent_;
25 this->invokingState = invokingState_;
28 int RuleContext::depth() {
30 RuleContext *p = this;
32 if (p->parent == nullptr)
34 p = static_cast<RuleContext *>(p->parent);
40 bool RuleContext::isEmpty() {
41 return invokingState == ATNState::INVALID_STATE_NUMBER;
44 misc::Interval RuleContext::getSourceInterval() {
45 return misc::Interval::INVALID;
48 std::string RuleContext::getText() {
49 if (children.empty()) {
54 for (size_t i = 0; i < children.size(); i++) {
55 ParseTree *tree = children[i];
57 ss << tree->getText();
63 size_t RuleContext::getRuleIndex() const {
67 size_t RuleContext::getAltNumber() const {
68 return atn::ATN::INVALID_ALT_NUMBER;
71 void RuleContext::setAltNumber(size_t /*altNumber*/) {
74 antlrcpp::Any RuleContext::accept(tree::ParseTreeVisitor *visitor) {
75 return visitor->visitChildren(this);
78 std::string RuleContext::toStringTree(Parser *recog, bool pretty) {
79 return tree::Trees::toStringTree(this, recog, pretty);
82 std::string RuleContext::toStringTree(std::vector<std::string> &ruleNames, bool pretty) {
83 return tree::Trees::toStringTree(this, ruleNames, pretty);
86 std::string RuleContext::toStringTree(bool pretty) {
87 return toStringTree(nullptr, pretty);
91 std::string RuleContext::toString(const std::vector<std::string> &ruleNames) {
92 return toString(ruleNames, nullptr);
96 std::string RuleContext::toString(const std::vector<std::string> &ruleNames, RuleContext *stop) {
99 RuleContext *currentParent = this;
101 while (currentParent != stop) {
102 if (ruleNames.empty()) {
103 if (!currentParent->isEmpty()) {
104 ss << currentParent->invokingState;
107 size_t ruleIndex = currentParent->getRuleIndex();
109 std::string ruleName = (ruleIndex < ruleNames.size()) ? ruleNames[ruleIndex] : std::to_string(ruleIndex);
113 if (currentParent->parent == nullptr) // No parent anymore.
115 currentParent = static_cast<RuleContext *>(currentParent->parent);
116 if (!ruleNames.empty() || !currentParent->isEmpty()) {
126 std::string RuleContext::toString() {
127 return toString(nullptr);
130 std::string RuleContext::toString(Recognizer *recog) {
131 return toString(recog, &ParserRuleContext::EMPTY);
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);
140 void RuleContext::InitializeInstanceFields() {
141 invokingState = INVALID_INDEX;