aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr4_cpp_runtime/src/dfa/DFA.h
blob: 360eda8ba76116303eebc7617d96674340933733 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

#pragma once

#include "dfa/DFAState.h"

namespace antlr4 {
namespace dfa {

  class ANTLR4CPP_PUBLIC DFA final {
  private:
    struct DFAStateHasher final {
      size_t operator()(const DFAState *dfaState) const {
        return dfaState->hashCode();
      }
    };

    struct DFAStateComparer final {
      bool operator()(const DFAState *lhs, const DFAState *rhs) const {
        return lhs == rhs || *lhs == *rhs;
      }
    };

  public:
    /// A set of all DFA states. Use a map so we can get old state back.
    /// Set only allows you to see if it's there.

    /// From which ATN state did we create this DFA?
    atn::DecisionState *atnStartState;
    std::unordered_set<DFAState*, DFAStateHasher, DFAStateComparer> states; // States are owned by this class.
    DFAState *s0;
    size_t decision;

    explicit DFA(atn::DecisionState *atnStartState);
    DFA(atn::DecisionState *atnStartState, size_t decision);
    DFA(const DFA &other) = delete;
    DFA(DFA &&other);
    ~DFA();

    /**
     * Gets whether this DFA is a precedence DFA. Precedence DFAs use a special
     * start state {@link #s0} which is not stored in {@link #states}. The
     * {@link DFAState#edges} array for this start state contains outgoing edges
     * supplying individual start states corresponding to specific precedence
     * values.
     *
     * @return {@code true} if this is a precedence DFA; otherwise,
     * {@code false}.
     * @see Parser#getPrecedence()
     */
    bool isPrecedenceDfa() const;

    /**
     * Get the start state for a specific precedence value.
     *
     * @param precedence The current precedence.
     * @return The start state corresponding to the specified precedence, or
     * {@code null} if no start state exists for the specified precedence.
     *
     * @throws IllegalStateException if this is not a precedence DFA.
     * @see #isPrecedenceDfa()
     */
    DFAState* getPrecedenceStartState(int precedence) const;

    /**
     * Set the start state for a specific precedence value.
     *
     * @param precedence The current precedence.
     * @param startState The start state corresponding to the specified
     * precedence.
     *
     * @throws IllegalStateException if this is not a precedence DFA.
     * @see #isPrecedenceDfa()
     */
    void setPrecedenceStartState(int precedence, DFAState *startState);

    /// Return a list of all states in this DFA, ordered by state number.
    std::vector<DFAState *> getStates() const;

    std::string toString(const Vocabulary &vocabulary) const;

    std::string toLexerString() const;

  private:
    /**
     * {@code true} if this DFA is for a precedence decision; otherwise,
     * {@code false}. This is the backing field for {@link #isPrecedenceDfa}.
     */
    bool _precedenceDfa;
  };

} // namespace atn
} // namespace antlr4