]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/LexerATNConfig.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / LexerATNConfig.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/DecisionState.h"
8 #include "atn/PredictionContext.h"
9 #include "SemanticContext.h"
10 #include "atn/LexerActionExecutor.h"
11
12 #include "support/CPPUtils.h"
13
14 #include "atn/LexerATNConfig.h"
15
16 using namespace antlr4::atn;
17 using namespace antlrcpp;
18
19 LexerATNConfig::LexerATNConfig(ATNState *state, int alt, Ref<PredictionContext> const& context)
20   : ATNConfig(state, alt, context, SemanticContext::NONE), _passedThroughNonGreedyDecision(false) {
21 }
22
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) {
27 }
28
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)) {
32 }
33
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)) {
37 }
38
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)) {
42 }
43
44 Ref<LexerActionExecutor> LexerATNConfig::getLexerActionExecutor() const {
45   return _lexerActionExecutor;
46 }
47
48 bool LexerATNConfig::hasPassedThroughNonGreedyDecision() {
49   return _passedThroughNonGreedyDecision;
50 }
51
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);
61   return hashCode;
62 }
63
64 bool LexerATNConfig::operator == (const LexerATNConfig& other) const
65 {
66   if (this == &other)
67     return true;
68
69   if (_passedThroughNonGreedyDecision != other._passedThroughNonGreedyDecision)
70     return false;
71
72   if (_lexerActionExecutor == nullptr)
73     return other._lexerActionExecutor == nullptr;
74   if (*_lexerActionExecutor != *(other._lexerActionExecutor)) {
75     return false;
76   }
77
78   return ATNConfig::operator == (other);
79 }
80
81 bool LexerATNConfig::checkNonGreedyDecision(Ref<LexerATNConfig> const& source, ATNState *target) {
82   return source->_passedThroughNonGreedyDecision ||
83     (is<DecisionState*>(target) && (static_cast<DecisionState*>(target))->nonGreedy);
84 }