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/ATNConfigSet.h"
7 #include "atn/SemanticContext.h"
8 #include "atn/ATNConfig.h"
9 #include "misc/MurmurHash.h"
11 #include "dfa/DFAState.h"
13 using namespace antlr4::dfa;
14 using namespace antlr4::atn;
16 DFAState::PredPrediction::PredPrediction(const Ref<SemanticContext> &pred, int alt) : pred(pred) {
17 InitializeInstanceFields();
21 DFAState::PredPrediction::~PredPrediction() {
24 std::string DFAState::PredPrediction::toString() {
25 return std::string("(") + pred->toString() + ", " + std::to_string(alt) + ")";
28 void DFAState::PredPrediction::InitializeInstanceFields() {
32 DFAState::DFAState() {
33 InitializeInstanceFields();
36 DFAState::DFAState(int state) : DFAState() {
40 DFAState::DFAState(std::unique_ptr<ATNConfigSet> configs_) : DFAState() {
41 configs = std::move(configs_);
44 DFAState::~DFAState() {
45 for (auto *predicate : predicates) {
50 std::set<size_t> DFAState::getAltSet() {
51 std::set<size_t> alts;
52 if (configs != nullptr) {
53 for (size_t i = 0; i < configs->size(); i++) {
54 alts.insert(configs->get(i)->alt);
60 size_t DFAState::hashCode() const {
61 size_t hash = misc::MurmurHash::initialize(7);
62 hash = misc::MurmurHash::update(hash, configs->hashCode());
63 hash = misc::MurmurHash::finish(hash, 1);
67 bool DFAState::operator == (const DFAState &o) const {
68 // compare set of ATN configurations in this set with other
73 return *configs == *o.configs;
76 std::string DFAState::toString() {
80 ss << ":" << configs->toString();
84 if (!predicates.empty()) {
85 for (size_t i = 0; i < predicates.size(); i++) {
86 ss << predicates[i]->toString();
95 void DFAState::InitializeInstanceFields() {
97 isAcceptState = false;
99 requiresFullContext = false;