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.
8 #include "misc/IntervalSet.h"
14 /// The following images show the relation of states and
15 /// <seealso cref="ATNState#transitions"/> for various grammar constructs.
19 /// <li>Solid edges marked with an ε indicate a required
20 /// <seealso cref="EpsilonTransition"/>.</li>
22 /// <li>Dashed edges indicate locations where any transition derived from
23 /// <seealso cref="Transition"/> might appear.</li>
25 /// <li>Dashed nodes are place holders for either a sequence of linked
26 /// <seealso cref="BasicState"/> states or the inclusion of a block representing a nested
27 /// construct in one of the forms below.</li>
29 /// <li>Nodes showing multiple outgoing alternatives with a {@code ...} support
30 /// any number of alternatives (one or more). Nodes without the {@code ...} only
31 /// support the exact number of alternatives shown in the diagram.</li>
35 /// <h2>Basic Blocks</h2>
39 /// <embed src="images/Rule.svg" type="image/svg+xml"/>
41 /// <h3>Block of 1 or more alternatives</h3>
43 /// <embed src="images/Block.svg" type="image/svg+xml"/>
45 /// <h2>Greedy Loops</h2>
47 /// <h3>Greedy Closure: {@code (...)*}</h3>
49 /// <embed src="images/ClosureGreedy.svg" type="image/svg+xml"/>
51 /// <h3>Greedy Positive Closure: {@code (...)+}</h3>
53 /// <embed src="images/PositiveClosureGreedy.svg" type="image/svg+xml"/>
55 /// <h3>Greedy Optional: {@code (...)?}</h3>
57 /// <embed src="images/OptionalGreedy.svg" type="image/svg+xml"/>
59 /// <h2>Non-Greedy Loops</h2>
61 /// <h3>Non-Greedy Closure: {@code (...)*?}</h3>
63 /// <embed src="images/ClosureNonGreedy.svg" type="image/svg+xml"/>
65 /// <h3>Non-Greedy Positive Closure: {@code (...)+?}</h3>
67 /// <embed src="images/PositiveClosureNonGreedy.svg" type="image/svg+xml"/>
69 /// <h3>Non-Greedy Optional: {@code (...)??}</h3>
71 /// <embed src="images/OptionalNonGreedy.svg" type="image/svg+xml"/>
73 class ANTLR4CPP_PUBLIC ATN;
75 class ANTLR4CPP_PUBLIC ATNState {
78 ATNState(ATNState const&) = delete;
82 ATNState& operator=(ATNState const&) = delete;
84 #if __cplusplus >= 201703L
85 static constexpr size_t INITIAL_NUM_TRANSITIONS = 4;
86 static constexpr size_t INVALID_STATE_NUMBER = std::numeric_limits<size_t>::max();
89 INITIAL_NUM_TRANSITIONS = 4,
90 INVALID_STATE_NUMBER = static_cast<size_t>(-1), // std::numeric_limits<size_t>::max(); doesn't work in VS 2013
100 STAR_BLOCK_START = 5,
105 STAR_LOOP_ENTRY = 10,
110 static const std::vector<std::string> serializationNames;
112 size_t stateNumber = INVALID_STATE_NUMBER;
113 size_t ruleIndex = 0; // at runtime, we don't have Rule objects
114 bool epsilonOnlyTransitions = false;
117 virtual size_t hashCode();
118 bool operator == (const ATNState &other);
120 /// Track the transitions emanating from this ATN state.
121 std::vector<Transition*> transitions;
123 virtual bool isNonGreedyExitState();
124 virtual std::string toString() const;
125 virtual void addTransition(Transition *e);
126 virtual void addTransition(size_t index, Transition *e);
127 virtual Transition* removeTransition(size_t index);
128 virtual size_t getStateType() = 0;
131 /// Used to cache lookahead during parsing, not used during construction.
133 misc::IntervalSet _nextTokenWithinRule;
134 std::atomic<bool> _nextTokenUpdated { false };
140 } // namespace antlr4