diff --git a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp index a8c18a3527..a351a53d68 100755 --- a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp @@ -61,59 +61,125 @@ using namespace antlrcpp; namespace { -uint32_t deserializeInt32(const std::vector& data, size_t offset) { - return static_cast(data[offset]) | (static_cast(data[offset + 1]) << 16); -} + void checkCondition(bool condition, std::string_view message) { + if (!condition) { + throw IllegalStateException(std::string(message)); + } + } -ssize_t readUnicodeInt(const std::vector& data, int& p) { - return static_cast(data[p++]); -} + void checkCondition(bool condition) { + checkCondition(condition, ""); + } -ssize_t readUnicodeInt32(const std::vector& data, int& p) { - auto result = deserializeInt32(data, p); - p += 2; - return static_cast(result); -} + /** + * Analyze the {@link StarLoopEntryState} states in the specified ATN to set + * the {@link StarLoopEntryState#isPrecedenceDecision} field to the + * correct value. + * + * @param atn The ATN. + */ + void markPrecedenceDecisions(const ATN &atn) { + for (ATNState *state : atn.states) { + if (!is(state)) { + continue; + } -// We templatize this on the function type so the optimizer can inline -// the 16- or 32-bit readUnicodeInt/readUnicodeInt32 as needed. -template -void deserializeSets( - const std::vector& data, - int& p, - std::vector& sets, - F readUnicode) { - size_t nsets = data[p++]; - sets.reserve(sets.size() + nsets); - for (size_t i = 0; i < nsets; i++) { - size_t nintervals = data[p++]; - misc::IntervalSet set; - - bool containsEof = data[p++] != 0; - if (containsEof) { - set.add(-1); + /* We analyze the ATN to determine if this ATN decision state is the + * decision for the closure block that determines whether a + * precedence rule should continue or complete. + */ + if (atn.ruleToStartState[state->ruleIndex]->isLeftRecursiveRule) { + ATNState *maybeLoopEndState = state->transitions[state->transitions.size() - 1]->target; + if (is(maybeLoopEndState)) { + if (maybeLoopEndState->epsilonOnlyTransitions && is(maybeLoopEndState->transitions[0]->target)) { + downCast(state)->isPrecedenceDecision = true; + } + } + } } + } + + Ref lexerActionFactory(LexerActionType type, int data1, int data2) { + switch (type) { + case LexerActionType::CHANNEL: + return std::make_shared(data1); + + case LexerActionType::CUSTOM: + return std::make_shared(data1, data2); + + case LexerActionType::MODE: + return std::make_shared< LexerModeAction>(data1); - for (size_t j = 0; j < nintervals; j++) { - auto a = readUnicode(data, p); - auto b = readUnicode(data, p); - set.add(a, b); + case LexerActionType::MORE: + return LexerMoreAction::getInstance(); + + case LexerActionType::POP_MODE: + return LexerPopModeAction::getInstance(); + + case LexerActionType::PUSH_MODE: + return std::make_shared(data1); + + case LexerActionType::SKIP: + return LexerSkipAction::getInstance(); + + case LexerActionType::TYPE: + return std::make_shared(data1); + + default: + throw IllegalArgumentException("The specified lexer action type " + std::to_string(static_cast(type)) + + " is not valid."); } - sets.push_back(set); } -} -} + uint32_t deserializeInt32(const std::vector& data, size_t offset) { + return static_cast(data[offset]) | (static_cast(data[offset + 1]) << 16); + } -ATNDeserializer::ATNDeserializer(): ATNDeserializer(ATNDeserializationOptions::getDefaultOptions()) { -} + ssize_t readUnicodeInt(const std::vector& data, int& p) { + return static_cast(data[p++]); + } -ATNDeserializer::ATNDeserializer(const ATNDeserializationOptions& dso): _deserializationOptions(dso) { -} + ssize_t readUnicodeInt32(const std::vector& data, int& p) { + auto result = deserializeInt32(data, p); + p += 2; + return static_cast(result); + } + + // We templatize this on the function type so the optimizer can inline + // the 16- or 32-bit readUnicodeInt/readUnicodeInt32 as needed. + template + void deserializeSets( + const std::vector& data, + int& p, + std::vector& sets, + F readUnicode) { + size_t nsets = data[p++]; + sets.reserve(sets.size() + nsets); + for (size_t i = 0; i < nsets; i++) { + size_t nintervals = data[p++]; + misc::IntervalSet set; + + bool containsEof = data[p++] != 0; + if (containsEof) { + set.add(-1); + } + + for (size_t j = 0; j < nintervals; j++) { + auto a = readUnicode(data, p); + auto b = readUnicode(data, p); + set.add(a, b); + } + sets.push_back(set); + } + } -ATNDeserializer::~ATNDeserializer() { } -ATN ATNDeserializer::deserialize(const std::vector& data) { + +ATNDeserializer::ATNDeserializer() : ATNDeserializer(ATNDeserializationOptions::getDefaultOptions()) {} + +ATNDeserializer::ATNDeserializer(ATNDeserializationOptions deserializationOptions) : _deserializationOptions(std::move(deserializationOptions)) {} + +ATN ATNDeserializer::deserialize(const std::vector& data) const { int p = 0; int version = data[p++]; if (version != SERIALIZED_VERSION) { @@ -448,35 +514,7 @@ ATN ATNDeserializer::deserialize(const std::vector& data) { return atn; } -/** - * Analyze the {@link StarLoopEntryState} states in the specified ATN to set - * the {@link StarLoopEntryState#isPrecedenceDecision} field to the - * correct value. - * - * @param atn The ATN. - */ -void ATNDeserializer::markPrecedenceDecisions(const ATN &atn) const { - for (ATNState *state : atn.states) { - if (!is(state)) { - continue; - } - - /* We analyze the ATN to determine if this ATN decision state is the - * decision for the closure block that determines whether a - * precedence rule should continue or complete. - */ - if (atn.ruleToStartState[state->ruleIndex]->isLeftRecursiveRule) { - ATNState *maybeLoopEndState = state->transitions[state->transitions.size() - 1]->target; - if (is(maybeLoopEndState)) { - if (maybeLoopEndState->epsilonOnlyTransitions && is(maybeLoopEndState->transitions[0]->target)) { - downCast(state)->isPrecedenceDecision = true; - } - } - } - } -} - -void ATNDeserializer::verifyATN(const ATN &atn) { +void ATNDeserializer::verifyATN(const ATN &atn) const { // verify assumptions for (ATNState *state : atn.states) { if (state == nullptr) { @@ -535,16 +573,6 @@ void ATNDeserializer::verifyATN(const ATN &atn) { } } -void ATNDeserializer::checkCondition(bool condition) { - checkCondition(condition, ""); -} - -void ATNDeserializer::checkCondition(bool condition, const std::string &message) { - if (!condition) { - throw IllegalStateException(message); - } -} - ConstTransitionPtr ATNDeserializer::edgeFactory(const ATN &atn, size_t type, size_t /*src*/, size_t trg, size_t arg1, size_t arg2, size_t arg3, const std::vector &sets) { @@ -635,34 +663,4 @@ ATNState* ATNDeserializer::stateFactory(size_t type, size_t ruleIndex) { return s; } -Ref ATNDeserializer::lexerActionFactory(LexerActionType type, int data1, int data2) const { - switch (type) { - case LexerActionType::CHANNEL: - return std::make_shared(data1); - - case LexerActionType::CUSTOM: - return std::make_shared(data1, data2); - - case LexerActionType::MODE: - return std::make_shared< LexerModeAction>(data1); - case LexerActionType::MORE: - return LexerMoreAction::getInstance(); - - case LexerActionType::POP_MODE: - return LexerPopModeAction::getInstance(); - - case LexerActionType::PUSH_MODE: - return std::make_shared(data1); - - case LexerActionType::SKIP: - return LexerSkipAction::getInstance(); - - case LexerActionType::TYPE: - return std::make_shared(data1); - - default: - throw IllegalArgumentException("The specified lexer action type " + std::to_string(static_cast(type)) + - " is not valid."); - } -} diff --git a/runtime/Cpp/runtime/src/atn/ATNDeserializer.h b/runtime/Cpp/runtime/src/atn/ATNDeserializer.h index dfe51a2bc6..17ae0275c8 100755 --- a/runtime/Cpp/runtime/src/atn/ATNDeserializer.h +++ b/runtime/Cpp/runtime/src/atn/ATNDeserializer.h @@ -5,41 +5,32 @@ #pragma once -#include "atn/LexerAction.h" #include "atn/ATNDeserializationOptions.h" +#include "atn/LexerAction.h" #include "atn/Transition.h" namespace antlr4 { namespace atn { -class ANTLR4CPP_PUBLIC ATNDeserializer { -public: - static constexpr size_t SERIALIZED_VERSION = 4; - - ATNDeserializer(); - - explicit ATNDeserializer(const ATNDeserializationOptions& dso); - - virtual ~ATNDeserializer(); + class ANTLR4CPP_PUBLIC ATNDeserializer final { + public: + static constexpr size_t SERIALIZED_VERSION = 4; - virtual ATN deserialize(const std::vector &input); - virtual void verifyATN(const ATN &atn); + ATNDeserializer(); - static void checkCondition(bool condition); - static void checkCondition(bool condition, const std::string &message); + explicit ATNDeserializer(ATNDeserializationOptions deserializationOptions); - static ConstTransitionPtr edgeFactory(const ATN &atn, size_t type, size_t src, size_t trg, size_t arg1, size_t arg2, - size_t arg3, const std::vector &sets); + ATN deserialize(const std::vector &input) const; + void verifyATN(const ATN &atn) const; - static ATNState *stateFactory(size_t type, size_t ruleIndex); + static ConstTransitionPtr edgeFactory(const ATN &atn, size_t type, size_t src, size_t trg, size_t arg1, size_t arg2, + size_t arg3, const std::vector &sets); -protected: - void markPrecedenceDecisions(const ATN &atn) const; - Ref lexerActionFactory(LexerActionType type, int data1, int data2) const; + static ATNState* stateFactory(size_t type, size_t ruleIndex); -private: - const ATNDeserializationOptions _deserializationOptions; -}; + private: + const ATNDeserializationOptions _deserializationOptions; + }; } // namespace atn } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp index c1a7f2d6db..00ce9ec190 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNSimulator.cpp @@ -3,28 +3,24 @@ * can be found in the LICENSE.txt file in the project root. */ -#include "atn/ATNType.h" +#include "atn/ATNSimulator.h" + #include "atn/ATNConfigSet.h" -#include "dfa/DFAState.h" #include "atn/ATNDeserializer.h" +#include "atn/ATNType.h" #include "atn/EmptyPredictionContext.h" - -#include "atn/ATNSimulator.h" +#include "dfa/DFAState.h" using namespace antlr4; using namespace antlr4::dfa; using namespace antlr4::atn; -const Ref ATNSimulator::ERROR = std::make_shared(INT32_MAX); +const Ref ATNSimulator::ERROR = std::make_shared(std::numeric_limits::max()); std::shared_mutex ATNSimulator::_stateLock; std::shared_mutex ATNSimulator::_edgeLock; ATNSimulator::ATNSimulator(const ATN &atn, PredictionContextCache &sharedContextCache) -: atn(atn), _sharedContextCache(sharedContextCache) { -} - -ATNSimulator::~ATNSimulator() { -} + : atn(atn), _sharedContextCache(sharedContextCache) {} void ATNSimulator::clearDFA() { throw UnsupportedOperationException("This ATN simulator does not support clearing the DFA."); @@ -39,25 +35,3 @@ Ref ATNSimulator::getCachedContext(Ref, Ref> visited; return PredictionContext::getCachedContext(context, _sharedContextCache, visited); } - -ATN ATNSimulator::deserialize(const std::vector &data) { - ATNDeserializer deserializer; - return deserializer.deserialize(data); -} - -void ATNSimulator::checkCondition(bool condition) { - ATNDeserializer::checkCondition(condition); -} - -void ATNSimulator::checkCondition(bool condition, const std::string &message) { - ATNDeserializer::checkCondition(condition, message); -} - -ConstTransitionPtr ATNSimulator::edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3, - const std::vector &sets) { - return ATNDeserializer::edgeFactory(atn, type, src, trg, arg1, arg2, arg3, sets); -} - -ATNState *ATNSimulator::stateFactory(int type, int ruleIndex) { - return ATNDeserializer::stateFactory(type, ruleIndex); -} diff --git a/runtime/Cpp/runtime/src/atn/ATNSimulator.h b/runtime/Cpp/runtime/src/atn/ATNSimulator.h index 2fe5c5e04c..b0de2cbfeb 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/ATNSimulator.h @@ -6,9 +6,9 @@ #pragma once #include "atn/ATN.h" +#include "atn/PredictionContext.h" #include "misc/IntervalSet.h" #include "support/CPPUtils.h" -#include "atn/PredictionContext.h" namespace antlr4 { namespace atn { @@ -20,7 +20,8 @@ namespace atn { const ATN &atn; ATNSimulator(const ATN &atn, PredictionContextCache &sharedContextCache); - virtual ~ATNSimulator(); + + virtual ~ATNSimulator() = default; virtual void reset() = 0; @@ -39,22 +40,6 @@ namespace atn { virtual PredictionContextCache& getSharedContextCache(); virtual Ref getCachedContext(Ref const& context); - /// @deprecated Use instead. - static ATN deserialize(const std::vector &data); - - /// @deprecated Use instead. - static void checkCondition(bool condition); - - /// @deprecated Use instead. - static void checkCondition(bool condition, const std::string &message); - - /// @deprecated Use instead. - static ConstTransitionPtr edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3, - const std::vector &sets); - - /// @deprecated Use instead. - static ATNState *stateFactory(int type, int ruleIndex); - protected: static std::shared_mutex _stateLock; // Lock for DFA states. static std::shared_mutex _edgeLock; // Lock for the sparse edge map in DFA states.