]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/dfa/DFA.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / dfa / DFA.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 "dfa/DFAState.h"
9
10 namespace antlrcpp {
11   class SingleWriteMultipleReadLock;
12 }
13
14 namespace antlr4 {
15 namespace dfa {
16
17   class ANTLR4CPP_PUBLIC DFA {
18   public:
19     /// A set of all DFA states. Use a map so we can get old state back.
20     /// Set only allows you to see if it's there.
21
22     /// From which ATN state did we create this DFA?
23     atn::DecisionState *atnStartState;
24     std::unordered_set<DFAState *, DFAState::Hasher, DFAState::Comparer> states; // States are owned by this class.
25     DFAState *s0;
26     size_t decision;
27
28     DFA(atn::DecisionState *atnStartState);
29     DFA(atn::DecisionState *atnStartState, size_t decision);
30     DFA(const DFA &other) = delete;
31     DFA(DFA &&other);
32     virtual ~DFA();
33
34     /**
35      * Gets whether this DFA is a precedence DFA. Precedence DFAs use a special
36      * start state {@link #s0} which is not stored in {@link #states}. The
37      * {@link DFAState#edges} array for this start state contains outgoing edges
38      * supplying individual start states corresponding to specific precedence
39      * values.
40      *
41      * @return {@code true} if this is a precedence DFA; otherwise,
42      * {@code false}.
43      * @see Parser#getPrecedence()
44      */
45     bool isPrecedenceDfa() const;
46
47     /**
48      * Get the start state for a specific precedence value.
49      *
50      * @param precedence The current precedence.
51      * @return The start state corresponding to the specified precedence, or
52      * {@code null} if no start state exists for the specified precedence.
53      *
54      * @throws IllegalStateException if this is not a precedence DFA.
55      * @see #isPrecedenceDfa()
56      */
57     DFAState* getPrecedenceStartState(int precedence) const;
58
59     /**
60      * Set the start state for a specific precedence value.
61      *
62      * @param precedence The current precedence.
63      * @param startState The start state corresponding to the specified
64      * precedence.
65      *
66      * @throws IllegalStateException if this is not a precedence DFA.
67      * @see #isPrecedenceDfa()
68      */
69     void setPrecedenceStartState(int precedence, DFAState *startState, antlrcpp::SingleWriteMultipleReadLock &lock);
70
71     /// Return a list of all states in this DFA, ordered by state number.
72     virtual std::vector<DFAState *> getStates() const;
73
74     /**
75      * @deprecated Use {@link #toString(Vocabulary)} instead.
76      */
77     virtual std::string toString(const std::vector<std::string>& tokenNames);
78     std::string toString(const Vocabulary &vocabulary) const;
79
80     virtual std::string toLexerString();
81
82   private:
83     /**
84      * {@code true} if this DFA is for a precedence decision; otherwise,
85      * {@code false}. This is the backing field for {@link #isPrecedenceDfa}.
86      */
87     bool _precedenceDfa;
88   };
89
90 } // namespace atn
91 } // namespace antlr4