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
|
/* 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/TerminalNode.h"
#include "tree/ErrorNode.h"
#include "misc/Interval.h"
#include "Parser.h"
#include "Token.h"
#include "support/Casts.h"
#include "support/CPPUtils.h"
#include "ParserRuleContext.h"
using namespace antlr4;
using namespace antlr4::tree;
using namespace antlrcpp;
ParserRuleContext ParserRuleContext::EMPTY;
ParserRuleContext::ParserRuleContext()
: start(nullptr), stop(nullptr) {
}
ParserRuleContext::ParserRuleContext(ParserRuleContext *parent, size_t invokingStateNumber)
: RuleContext(parent, invokingStateNumber), start(nullptr), stop(nullptr) {
}
void ParserRuleContext::copyFrom(ParserRuleContext *ctx) {
// from RuleContext
this->parent = ctx->parent;
this->invokingState = ctx->invokingState;
this->start = ctx->start;
this->stop = ctx->stop;
// copy any error nodes to alt label node
if (!ctx->children.empty()) {
for (auto *child : ctx->children) {
if (ErrorNode::is(child)) {
downCast<ErrorNode*>(child)->setParent(this);
children.push_back(child);
}
}
// Remove the just reparented error nodes from the source context.
ctx->children.erase(std::remove_if(ctx->children.begin(), ctx->children.end(), [this](tree::ParseTree *e) -> bool {
return std::find(children.begin(), children.end(), e) != children.end();
}), ctx->children.end());
}
}
void ParserRuleContext::enterRule(tree::ParseTreeListener * /*listener*/) {
}
void ParserRuleContext::exitRule(tree::ParseTreeListener * /*listener*/) {
}
tree::TerminalNode* ParserRuleContext::addChild(tree::TerminalNode *t) {
t->setParent(this);
children.push_back(t);
return t;
}
RuleContext* ParserRuleContext::addChild(RuleContext *ruleInvocation) {
children.push_back(ruleInvocation);
return ruleInvocation;
}
void ParserRuleContext::removeLastChild() {
if (!children.empty()) {
children.pop_back();
}
}
tree::TerminalNode* ParserRuleContext::getToken(size_t ttype, size_t i) const {
if (i >= children.size()) {
return nullptr;
}
size_t j = 0; // what token with ttype have we found?
for (auto *child : children) {
if (TerminalNode::is(child)) {
tree::TerminalNode *typedChild = downCast<tree::TerminalNode*>(child);
Token *symbol = typedChild->getSymbol();
if (symbol->getType() == ttype) {
if (j++ == i) {
return typedChild;
}
}
}
}
return nullptr;
}
std::vector<tree::TerminalNode *> ParserRuleContext::getTokens(size_t ttype) const {
std::vector<tree::TerminalNode*> tokens;
for (auto *child : children) {
if (TerminalNode::is(child)) {
tree::TerminalNode *typedChild = downCast<tree::TerminalNode*>(child);
Token *symbol = typedChild->getSymbol();
if (symbol->getType() == ttype) {
tokens.push_back(typedChild);
}
}
}
return tokens;
}
misc::Interval ParserRuleContext::getSourceInterval() {
if (start == nullptr) {
return misc::Interval::INVALID;
}
if (stop == nullptr || stop->getTokenIndex() < start->getTokenIndex()) {
return misc::Interval(start->getTokenIndex(), start->getTokenIndex() - 1); // empty
}
return misc::Interval(start->getTokenIndex(), stop->getTokenIndex());
}
Token* ParserRuleContext::getStart() const {
return start;
}
Token* ParserRuleContext::getStop() const {
return stop;
}
std::string ParserRuleContext::toInfoString(Parser *recognizer) {
std::vector<std::string> rules = recognizer->getRuleInvocationStack(this);
std::reverse(rules.begin(), rules.end());
std::string rulesStr = antlrcpp::arrayToString(rules);
return "ParserRuleContext" + rulesStr + "{start=" + std::to_string(start->getTokenIndex()) + ", stop=" +
std::to_string(stop->getTokenIndex()) + '}';
}
|