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