]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNState.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATNState.h
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.
4  */
5
6 #pragma once
7
8 #include "misc/IntervalSet.h"
9
10 namespace antlr4 {
11 namespace atn {
12
13   /// <summary>
14   /// The following images show the relation of states and
15   /// <seealso cref="ATNState#transitions"/> for various grammar constructs.
16   ///
17   /// <ul>
18   ///
19   /// <li>Solid edges marked with an &#0949; indicate a required
20   /// <seealso cref="EpsilonTransition"/>.</li>
21   ///
22   /// <li>Dashed edges indicate locations where any transition derived from
23   /// <seealso cref="Transition"/> might appear.</li>
24   ///
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>
28   ///
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>
32   ///
33   /// </ul>
34   ///
35   /// <h2>Basic Blocks</h2>
36   ///
37   /// <h3>Rule</h3>
38   ///
39   /// <embed src="images/Rule.svg" type="image/svg+xml"/>
40   ///
41   /// <h3>Block of 1 or more alternatives</h3>
42   ///
43   /// <embed src="images/Block.svg" type="image/svg+xml"/>
44   ///
45   /// <h2>Greedy Loops</h2>
46   ///
47   /// <h3>Greedy Closure: {@code (...)*}</h3>
48   ///
49   /// <embed src="images/ClosureGreedy.svg" type="image/svg+xml"/>
50   ///
51   /// <h3>Greedy Positive Closure: {@code (...)+}</h3>
52   ///
53   /// <embed src="images/PositiveClosureGreedy.svg" type="image/svg+xml"/>
54   ///
55   /// <h3>Greedy Optional: {@code (...)?}</h3>
56   ///
57   /// <embed src="images/OptionalGreedy.svg" type="image/svg+xml"/>
58   ///
59   /// <h2>Non-Greedy Loops</h2>
60   ///
61   /// <h3>Non-Greedy Closure: {@code (...)*?}</h3>
62   ///
63   /// <embed src="images/ClosureNonGreedy.svg" type="image/svg+xml"/>
64   ///
65   /// <h3>Non-Greedy Positive Closure: {@code (...)+?}</h3>
66   ///
67   /// <embed src="images/PositiveClosureNonGreedy.svg" type="image/svg+xml"/>
68   ///
69   /// <h3>Non-Greedy Optional: {@code (...)??}</h3>
70   ///
71   /// <embed src="images/OptionalNonGreedy.svg" type="image/svg+xml"/>
72   /// </summary>
73   class ANTLR4CPP_PUBLIC ATN;
74
75   class ANTLR4CPP_PUBLIC ATNState {
76   public:
77     ATNState();
78     ATNState(ATNState const&) = delete;
79
80     virtual ~ATNState();
81
82     ATNState& operator=(ATNState const&) = delete;
83
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();
87 #else
88     enum : size_t {
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
91     };
92 #endif
93
94     enum {
95       ATN_INVALID_TYPE = 0,
96       BASIC = 1,
97       RULE_START = 2,
98       BLOCK_START = 3,
99       PLUS_BLOCK_START = 4,
100       STAR_BLOCK_START = 5,
101       TOKEN_START = 6,
102       RULE_STOP = 7,
103       BLOCK_END = 8,
104       STAR_LOOP_BACK = 9,
105       STAR_LOOP_ENTRY = 10,
106       PLUS_LOOP_BACK = 11,
107       LOOP_END = 12
108     };
109
110     static const std::vector<std::string> serializationNames;
111
112     size_t stateNumber = INVALID_STATE_NUMBER;
113     size_t ruleIndex = 0; // at runtime, we don't have Rule objects
114     bool epsilonOnlyTransitions = false;
115
116   public:
117     virtual size_t hashCode();
118     bool operator == (const ATNState &other);
119
120     /// Track the transitions emanating from this ATN state.
121     std::vector<Transition*> transitions;
122
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;
129
130   private:
131     /// Used to cache lookahead during parsing, not used during construction.
132
133     misc::IntervalSet _nextTokenWithinRule;
134     std::atomic<bool> _nextTokenUpdated { false };
135
136     friend class ATN;
137   };
138
139 } // namespace atn
140 } // namespace antlr4