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 "misc/MurmurHash.h"
7 #include "atn/PredictionContext.h"
8 #include "SemanticContext.h"
10 #include "atn/ATNConfig.h"
12 using namespace antlr4::atn;
14 ATNConfig::ATNConfig(ATNState *state_, size_t alt_, Ref<PredictionContext> const& context_)
15 : ATNConfig(state_, alt_, context_, SemanticContext::NONE) {
18 ATNConfig::ATNConfig(ATNState *state_, size_t alt_, Ref<PredictionContext> const& context_, Ref<SemanticContext> const& semanticContext_)
19 : state(state_), alt(alt_), context(context_), semanticContext(semanticContext_) {
20 reachesIntoOuterContext = 0;
23 ATNConfig::ATNConfig(Ref<ATNConfig> const& c) : ATNConfig(c, c->state, c->context, c->semanticContext) {
26 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state_) : ATNConfig(c, state_, c->context, c->semanticContext) {
29 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> const& semanticContext)
30 : ATNConfig(c, state, c->context, semanticContext) {
33 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> const& semanticContext)
34 : ATNConfig(c, c->state, c->context, semanticContext) {
37 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context)
38 : ATNConfig(c, state, context, c->semanticContext) {
41 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context,
42 Ref<SemanticContext> const& semanticContext)
43 : state(state), alt(c->alt), context(context), reachesIntoOuterContext(c->reachesIntoOuterContext),
44 semanticContext(semanticContext) {
47 ATNConfig::~ATNConfig() {
50 size_t ATNConfig::hashCode() const {
51 size_t hashCode = misc::MurmurHash::initialize(7);
52 hashCode = misc::MurmurHash::update(hashCode, state->stateNumber);
53 hashCode = misc::MurmurHash::update(hashCode, alt);
54 hashCode = misc::MurmurHash::update(hashCode, context);
55 hashCode = misc::MurmurHash::update(hashCode, semanticContext);
56 hashCode = misc::MurmurHash::finish(hashCode, 4);
60 size_t ATNConfig::getOuterContextDepth() const {
61 return reachesIntoOuterContext & ~SUPPRESS_PRECEDENCE_FILTER;
64 bool ATNConfig::isPrecedenceFilterSuppressed() const {
65 return (reachesIntoOuterContext & SUPPRESS_PRECEDENCE_FILTER) != 0;
68 void ATNConfig::setPrecedenceFilterSuppressed(bool value) {
70 reachesIntoOuterContext |= SUPPRESS_PRECEDENCE_FILTER;
72 reachesIntoOuterContext &= ~SUPPRESS_PRECEDENCE_FILTER;
76 bool ATNConfig::operator == (const ATNConfig &other) const {
77 return state->stateNumber == other.state->stateNumber && alt == other.alt &&
78 ((context == other.context) || (*context == *other.context)) &&
79 *semanticContext == *other.semanticContext &&
80 isPrecedenceFilterSuppressed() == other.isPrecedenceFilterSuppressed();
83 bool ATNConfig::operator != (const ATNConfig &other) const {
84 return !operator==(other);
87 std::string ATNConfig::toString() {
88 return toString(true);
91 std::string ATNConfig::toString(bool showAlt) {
95 ss << state->toString();
100 ss << ",[" << context->toString() << "]";
102 if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) {
103 ss << "," << semanticContext.get();
105 if (getOuterContextDepth() > 0) {
106 ss << ",up=" << getOuterContextDepth();