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 "atn/EmptyPredictionContext.h"
8 #include "atn/SingletonPredictionContext.h"
10 using namespace antlr4::atn;
12 SingletonPredictionContext::SingletonPredictionContext(Ref<PredictionContext> const& parent, size_t returnState)
13 : PredictionContext(parent ? calculateHashCode(parent, returnState) : calculateEmptyHashCode()),
14 parent(parent), returnState(returnState) {
15 assert(returnState != ATNState::INVALID_STATE_NUMBER);
18 SingletonPredictionContext::~SingletonPredictionContext() {
21 Ref<SingletonPredictionContext> SingletonPredictionContext::create(Ref<PredictionContext> const& parent, size_t returnState) {
23 if (returnState == EMPTY_RETURN_STATE && parent) {
24 // someone can pass in the bits of an array ctx that mean $
25 return std::dynamic_pointer_cast<SingletonPredictionContext>(EMPTY);
27 return std::make_shared<SingletonPredictionContext>(parent, returnState);
30 size_t SingletonPredictionContext::size() const {
34 Ref<PredictionContext> SingletonPredictionContext::getParent(size_t index) const {
36 ((void)(index)); // Make Release build happy.
40 size_t SingletonPredictionContext::getReturnState(size_t index) const {
42 ((void)(index)); // Make Release build happy.
46 bool SingletonPredictionContext::operator == (const PredictionContext &o) const {
51 const SingletonPredictionContext *other = dynamic_cast<const SingletonPredictionContext*>(&o);
52 if (other == nullptr) {
56 if (this->hashCode() != other->hashCode()) {
57 return false; // can't be same if hash is different
60 if (returnState != other->returnState)
63 if (!parent && !other->parent)
65 if (!parent || !other->parent)
68 return *parent == *other->parent;
71 std::string SingletonPredictionContext::toString() const {
72 //std::string up = !parent.expired() ? parent.lock()->toString() : "";
73 std::string up = parent != nullptr ? parent->toString() : "";
74 if (up.length() == 0) {
75 if (returnState == EMPTY_RETURN_STATE) {
78 return std::to_string(returnState);
80 return std::to_string(returnState) + " " + up;