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 "RuleContext.h"
13 class ANTLR4CPP_PUBLIC ATN {
15 #if __cplusplus >= 201703L
16 static constexpr size_t INVALID_ALT_NUMBER = 0;
19 INVALID_ALT_NUMBER = 0,
23 /// Used for runtime deserialization of ATNs from strings.
26 ATN(ATNType grammarType, size_t maxTokenType);
29 std::vector<ATNState *> states;
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;
36 /// Maps from rule index to starting state number.
37 std::vector<RuleStartState *> ruleToStartState;
39 /// Maps from rule index to stop state number.
40 std::vector<RuleStopState *> ruleToStopState;
42 /// The type of the ATN.
45 /// The maximum value for any symbol recognized by a transition in the ATN.
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
52 /// <seealso cref="ATNDeserializationOptions#isGenerateRuleBypassTransitions"/>
53 /// deserialization option was specified; otherwise, this is {@code null}.
55 std::vector<size_t> ruleToTokenType;
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;
61 std::vector<TokensStartState *> modeToStartState;
63 ATN& operator = (ATN &other) NOEXCEPT;
64 ATN& operator = (ATN &&other) NOEXCEPT;
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.
72 virtual misc::IntervalSet nextTokens(ATNState *s, RuleContext *ctx) const;
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
79 virtual misc::IntervalSet const& nextTokens(ATNState *s) const;
81 virtual void addState(ATNState *state);
83 virtual void removeState(ATNState *state);
85 virtual int defineDecisionState(DecisionState *s);
87 virtual DecisionState *getDecisionState(size_t decision) const;
89 virtual size_t getNumberOfDecisions() const;
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.
100 /// If {@code context} is {@code null}, it is treated as
101 /// <seealso cref="ParserRuleContext#EMPTY"/>.
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;
111 std::string toString() const;
114 mutable std::mutex _mutex;
118 } // namespace antlr4