X-Git-Url: https://gitweb.ps.run/toc/blobdiff_plain/9f94b672a5dc32da5ad01742bd4e976315a30d9c..c6ad2948bb98d42f8e0883ef82cd14cd2d5eda60:/antlr4-cpp-runtime-4.9.2-source/runtime/src/dfa/DFAState.cpp diff --git a/antlr4-cpp-runtime-4.9.2-source/runtime/src/dfa/DFAState.cpp b/antlr4-cpp-runtime-4.9.2-source/runtime/src/dfa/DFAState.cpp new file mode 100644 index 0000000..998fed3 --- /dev/null +++ b/antlr4-cpp-runtime-4.9.2-source/runtime/src/dfa/DFAState.cpp @@ -0,0 +1,100 @@ +/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. + * Use of this file is governed by the BSD 3-clause license that + * can be found in the LICENSE.txt file in the project root. + */ + +#include "atn/ATNConfigSet.h" +#include "atn/SemanticContext.h" +#include "atn/ATNConfig.h" +#include "misc/MurmurHash.h" + +#include "dfa/DFAState.h" + +using namespace antlr4::dfa; +using namespace antlr4::atn; + +DFAState::PredPrediction::PredPrediction(const Ref &pred, int alt) : pred(pred) { + InitializeInstanceFields(); + this->alt = alt; +} + +DFAState::PredPrediction::~PredPrediction() { +} + +std::string DFAState::PredPrediction::toString() { + return std::string("(") + pred->toString() + ", " + std::to_string(alt) + ")"; +} + +void DFAState::PredPrediction::InitializeInstanceFields() { + alt = 0; +} + +DFAState::DFAState() { + InitializeInstanceFields(); +} + +DFAState::DFAState(int state) : DFAState() { + stateNumber = state; +} + +DFAState::DFAState(std::unique_ptr configs_) : DFAState() { + configs = std::move(configs_); +} + +DFAState::~DFAState() { + for (auto *predicate : predicates) { + delete predicate; + } +} + +std::set DFAState::getAltSet() { + std::set alts; + if (configs != nullptr) { + for (size_t i = 0; i < configs->size(); i++) { + alts.insert(configs->get(i)->alt); + } + } + return alts; +} + +size_t DFAState::hashCode() const { + size_t hash = misc::MurmurHash::initialize(7); + hash = misc::MurmurHash::update(hash, configs->hashCode()); + hash = misc::MurmurHash::finish(hash, 1); + return hash; +} + +bool DFAState::operator == (const DFAState &o) const { + // compare set of ATN configurations in this set with other + if (this == &o) { + return true; + } + + return *configs == *o.configs; +} + +std::string DFAState::toString() { + std::stringstream ss; + ss << stateNumber; + if (configs) { + ss << ":" << configs->toString(); + } + if (isAcceptState) { + ss << " => "; + if (!predicates.empty()) { + for (size_t i = 0; i < predicates.size(); i++) { + ss << predicates[i]->toString(); + } + } else { + ss << prediction; + } + } + return ss.str(); +} + +void DFAState::InitializeInstanceFields() { + stateNumber = -1; + isAcceptState = false; + prediction = 0; + requiresFullContext = false; +}