/* 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. */ #pragma once #include "misc/IntervalSet.h" #include "atn/Transition.h" #include "atn/ATNStateType.h" namespace antlr4 { namespace atn { /// /// The following images show the relation of states and /// for various grammar constructs. /// /// /// ///

Basic Blocks

/// ///

Rule

/// /// /// ///

Block of 1 or more alternatives

/// /// /// ///

Greedy Loops

/// ///

Greedy Closure: {@code (...)*}

/// /// /// ///

Greedy Positive Closure: {@code (...)+}

/// /// /// ///

Greedy Optional: {@code (...)?}

/// /// /// ///

Non-Greedy Loops

/// ///

Non-Greedy Closure: {@code (...)*?}

/// /// /// ///

Non-Greedy Positive Closure: {@code (...)+?}

/// /// /// ///

Non-Greedy Optional: {@code (...)??}

/// /// ///
// GCC generates a warning here if ATN has already been declared due to the // attributes added by ANTLR4CPP_PUBLIC. // See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39159 // Only forward-declare if it hasn't already been declared. #ifndef ANTLR4CPP_ATN_DECLARED class ANTLR4CPP_PUBLIC ATN; #endif class ANTLR4CPP_PUBLIC ATNState { public: static constexpr size_t INITIAL_NUM_TRANSITIONS = 4; static constexpr size_t INVALID_STATE_NUMBER = std::numeric_limits::max(); size_t stateNumber = INVALID_STATE_NUMBER; size_t ruleIndex = 0; // at runtime, we don't have Rule objects bool epsilonOnlyTransitions = false; /// Track the transitions emanating from this ATN state. std::vector transitions; ATNState() = delete; ATNState(ATNState const&) = delete; ATNState(ATNState&&) = delete; virtual ~ATNState() = default; ATNState& operator=(ATNState const&) = delete; ATNState& operator=(ATNState&&) = delete; void addTransition(ConstTransitionPtr e); void addTransition(size_t index, ConstTransitionPtr e); ConstTransitionPtr removeTransition(size_t index); virtual size_t hashCode() const; virtual bool equals(const ATNState &other) const; virtual bool isNonGreedyExitState() const; virtual std::string toString() const; ATNStateType getStateType() const { return _stateType; } protected: explicit ATNState(ATNStateType stateType) : _stateType(stateType) {} private: /// Used to cache lookahead during parsing, not used during construction. misc::IntervalSet _nextTokenWithinRule; std::atomic _nextTokenUpdated { false }; const ATNStateType _stateType; friend class ATN; }; inline bool operator==(const ATNState &lhs, const ATNState &rhs) { return lhs.equals(rhs); } inline bool operator!=(const ATNState &lhs, const ATNState &rhs) { return !operator==(lhs, rhs); } } // namespace atn } // namespace antlr4