]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATN.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATN.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 "RuleContext.h"
9
10 namespace antlr4 {
11 namespace atn {
12
13   class ANTLR4CPP_PUBLIC ATN {
14   public:
15 #if __cplusplus >= 201703L
16     static constexpr size_t INVALID_ALT_NUMBER = 0;
17 #else
18     enum : size_t {
19       INVALID_ALT_NUMBER = 0,
20     };
21 #endif
22
23     /// Used for runtime deserialization of ATNs from strings.
24     ATN();
25     ATN(ATN &&other);
26     ATN(ATNType grammarType, size_t maxTokenType);
27     virtual ~ATN();
28
29     std::vector<ATNState *> states;
30
31     /// Each subrule/rule is a decision point and we must track them so we
32     /// can go back later and build DFA predictors for them.  This includes
33     /// all the rules, subrules, optional blocks, ()+, ()* etc...
34     std::vector<DecisionState *> decisionToState;
35
36     /// Maps from rule index to starting state number.
37     std::vector<RuleStartState *> ruleToStartState;
38
39     /// Maps from rule index to stop state number.
40     std::vector<RuleStopState *> ruleToStopState;
41
42     /// The type of the ATN.
43     ATNType grammarType;
44
45     /// The maximum value for any symbol recognized by a transition in the ATN.
46     size_t maxTokenType;
47
48     /// <summary>
49     /// For lexer ATNs, this maps the rule index to the resulting token type.
50     /// For parser ATNs, this maps the rule index to the generated bypass token
51     /// type if the
52     /// <seealso cref="ATNDeserializationOptions#isGenerateRuleBypassTransitions"/>
53     /// deserialization option was specified; otherwise, this is {@code null}.
54     /// </summary>
55     std::vector<size_t> ruleToTokenType;
56
57     /// For lexer ATNs, this is an array of {@link LexerAction} objects which may
58     /// be referenced by action transitions in the ATN.
59     std::vector<Ref<LexerAction>> lexerActions;
60
61     std::vector<TokensStartState *> modeToStartState;
62
63     ATN& operator = (ATN &other) NOEXCEPT;
64     ATN& operator = (ATN &&other) NOEXCEPT;
65
66     /// <summary>
67     /// Compute the set of valid tokens that can occur starting in state {@code s}.
68     ///  If {@code ctx} is null, the set of tokens will not include what can follow
69     ///  the rule surrounding {@code s}. In other words, the set will be
70     ///  restricted to tokens reachable staying within {@code s}'s rule.
71     /// </summary>
72     virtual misc::IntervalSet nextTokens(ATNState *s, RuleContext *ctx) const;
73
74     /// <summary>
75     /// Compute the set of valid tokens that can occur starting in {@code s} and
76     /// staying in same rule. <seealso cref="Token#EPSILON"/> is in set if we reach end of
77     /// rule.
78     /// </summary>
79     virtual misc::IntervalSet const& nextTokens(ATNState *s) const;
80
81     virtual void addState(ATNState *state);
82
83     virtual void removeState(ATNState *state);
84
85     virtual int defineDecisionState(DecisionState *s);
86
87     virtual DecisionState *getDecisionState(size_t decision) const;
88
89     virtual size_t getNumberOfDecisions() const;
90
91     /// <summary>
92     /// Computes the set of input symbols which could follow ATN state number
93     /// {@code stateNumber} in the specified full {@code context}. This method
94     /// considers the complete parser context, but does not evaluate semantic
95     /// predicates (i.e. all predicates encountered during the calculation are
96     /// assumed true). If a path in the ATN exists from the starting state to the
97     /// <seealso cref="RuleStopState"/> of the outermost context without matching any
98     /// symbols, <seealso cref="Token#EOF"/> is added to the returned set.
99     /// <p/>
100     /// If {@code context} is {@code null}, it is treated as
101     /// <seealso cref="ParserRuleContext#EMPTY"/>.
102     /// </summary>
103     /// <param name="stateNumber"> the ATN state number </param>
104     /// <param name="context"> the full parse context </param>
105     /// <returns> The set of potentially valid input symbols which could follow the
106     /// specified state in the specified context. </returns>
107     /// <exception cref="IllegalArgumentException"> if the ATN does not contain a state with
108     /// number {@code stateNumber} </exception>
109     virtual misc::IntervalSet getExpectedTokens(size_t stateNumber, RuleContext *context) const;
110
111     std::string toString() const;
112
113   private:
114     mutable std::mutex _mutex;
115   };
116
117 } // namespace atn
118 } // namespace antlr4