]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNConfig.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATNConfig.cpp
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 "misc/MurmurHash.h"
7 #include "atn/PredictionContext.h"
8 #include "SemanticContext.h"
9
10 #include "atn/ATNConfig.h"
11
12 using namespace antlr4::atn;
13
14 ATNConfig::ATNConfig(ATNState *state_, size_t alt_, Ref<PredictionContext> const& context_)
15   : ATNConfig(state_, alt_, context_, SemanticContext::NONE) {
16 }
17
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;
21 }
22
23 ATNConfig::ATNConfig(Ref<ATNConfig> const& c) : ATNConfig(c, c->state, c->context, c->semanticContext) {
24 }
25
26 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state_) : ATNConfig(c, state_, c->context, c->semanticContext) {
27 }
28
29 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<SemanticContext> const& semanticContext)
30   : ATNConfig(c, state, c->context, semanticContext) {
31 }
32
33 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, Ref<SemanticContext> const& semanticContext)
34   : ATNConfig(c, c->state, c->context, semanticContext) {
35 }
36
37 ATNConfig::ATNConfig(Ref<ATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context)
38   : ATNConfig(c, state, context, c->semanticContext) {
39 }
40
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) {
45 }
46
47 ATNConfig::~ATNConfig() {
48 }
49
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);
57   return hashCode;
58 }
59
60 size_t ATNConfig::getOuterContextDepth() const {
61   return reachesIntoOuterContext & ~SUPPRESS_PRECEDENCE_FILTER;
62 }
63
64 bool ATNConfig::isPrecedenceFilterSuppressed() const {
65   return (reachesIntoOuterContext & SUPPRESS_PRECEDENCE_FILTER) != 0;
66 }
67
68 void ATNConfig::setPrecedenceFilterSuppressed(bool value) {
69   if (value) {
70     reachesIntoOuterContext |= SUPPRESS_PRECEDENCE_FILTER;
71   } else {
72     reachesIntoOuterContext &= ~SUPPRESS_PRECEDENCE_FILTER;
73   }
74 }
75
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();
81 }
82
83 bool ATNConfig::operator != (const ATNConfig &other) const {
84   return !operator==(other);
85 }
86
87 std::string ATNConfig::toString() {
88   return toString(true);
89 }
90
91 std::string ATNConfig::toString(bool showAlt) {
92   std::stringstream ss;
93   ss << "(";
94
95   ss << state->toString();
96   if (showAlt) {
97     ss << "," << alt;
98   }
99   if (context) {
100     ss << ",[" << context->toString() << "]";
101   }
102   if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) {
103     ss << "," << semanticContext.get();
104   }
105   if (getOuterContextDepth() > 0) {
106     ss << ",up=" << getOuterContextDepth();
107   }
108   ss << ')';
109
110   return ss.str();
111 }