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/DecisionState.h"
8 #include "atn/PredictionContext.h"
9 #include "SemanticContext.h"
10 #include "atn/LexerActionExecutor.h"
12 #include "support/CPPUtils.h"
14 #include "atn/LexerATNConfig.h"
16 using namespace antlr4::atn;
17 using namespace antlrcpp;
19 LexerATNConfig::LexerATNConfig(ATNState *state, int alt, Ref<PredictionContext> const& context)
20 : ATNConfig(state, alt, context, SemanticContext::NONE), _passedThroughNonGreedyDecision(false) {
23 LexerATNConfig::LexerATNConfig(ATNState *state, int alt, Ref<PredictionContext> const& context,
24 Ref<LexerActionExecutor> const& lexerActionExecutor)
25 : ATNConfig(state, alt, context, SemanticContext::NONE), _lexerActionExecutor(lexerActionExecutor),
26 _passedThroughNonGreedyDecision(false) {
29 LexerATNConfig::LexerATNConfig(Ref<LexerATNConfig> const& c, ATNState *state)
30 : ATNConfig(c, state, c->context, c->semanticContext), _lexerActionExecutor(c->_lexerActionExecutor),
31 _passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) {
34 LexerATNConfig::LexerATNConfig(Ref<LexerATNConfig> const& c, ATNState *state, Ref<LexerActionExecutor> const& lexerActionExecutor)
35 : ATNConfig(c, state, c->context, c->semanticContext), _lexerActionExecutor(lexerActionExecutor),
36 _passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) {
39 LexerATNConfig::LexerATNConfig(Ref<LexerATNConfig> const& c, ATNState *state, Ref<PredictionContext> const& context)
40 : ATNConfig(c, state, context, c->semanticContext), _lexerActionExecutor(c->_lexerActionExecutor),
41 _passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) {
44 Ref<LexerActionExecutor> LexerATNConfig::getLexerActionExecutor() const {
45 return _lexerActionExecutor;
48 bool LexerATNConfig::hasPassedThroughNonGreedyDecision() {
49 return _passedThroughNonGreedyDecision;
52 size_t LexerATNConfig::hashCode() const {
53 size_t hashCode = misc::MurmurHash::initialize(7);
54 hashCode = misc::MurmurHash::update(hashCode, state->stateNumber);
55 hashCode = misc::MurmurHash::update(hashCode, alt);
56 hashCode = misc::MurmurHash::update(hashCode, context);
57 hashCode = misc::MurmurHash::update(hashCode, semanticContext);
58 hashCode = misc::MurmurHash::update(hashCode, _passedThroughNonGreedyDecision ? 1 : 0);
59 hashCode = misc::MurmurHash::update(hashCode, _lexerActionExecutor);
60 hashCode = misc::MurmurHash::finish(hashCode, 6);
64 bool LexerATNConfig::operator == (const LexerATNConfig& other) const
69 if (_passedThroughNonGreedyDecision != other._passedThroughNonGreedyDecision)
72 if (_lexerActionExecutor == nullptr)
73 return other._lexerActionExecutor == nullptr;
74 if (*_lexerActionExecutor != *(other._lexerActionExecutor)) {
78 return ATNConfig::operator == (other);
81 bool LexerATNConfig::checkNonGreedyDecision(Ref<LexerATNConfig> const& source, ATNState *target) {
82 return source->_passedThroughNonGreedyDecision ||
83 (is<DecisionState*>(target) && (static_cast<DecisionState*>(target))->nonGreedy);