abouttreesummaryrefslogcommitdiff
path: root/antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNConfigSet.h
blob: 850a07c1295da0e15c3ff23ba286f2bc41f4f62b (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* 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 "support/BitSet.h"
#include "atn/PredictionContext.h"

namespace antlr4 {
namespace atn {

  /// Specialized set that can track info about the set, with support for combining similar configurations using a
  /// graph-structured stack.
  class ANTLR4CPP_PUBLIC ATNConfigSet {
  public:
    /// Track the elements as they are added to the set; supports get(i)
    std::vector<Ref<ATNConfig>> configs;

    // TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation
    // TODO: can we track conflicts as they are added to save scanning configs later?
    size_t uniqueAlt;

    /** Currently this is only used when we detect SLL conflict; this does
     *  not necessarily represent the ambiguous alternatives. In fact,
     *  I should also point out that this seems to include predicated alternatives
     *  that have predicates that evaluate to false. Computed in computeTargetState().
     */
    antlrcpp::BitSet conflictingAlts;

    // Used in parser and lexer. In lexer, it indicates we hit a pred
    // while computing a closure operation.  Don't make a DFA state from this.
    bool hasSemanticContext;
    bool dipsIntoOuterContext;

    /// Indicates that this configuration set is part of a full context
    /// LL prediction. It will be used to determine how to merge $. With SLL
    /// it's a wildcard whereas it is not for LL context merge.
    const bool fullCtx;

    ATNConfigSet(bool fullCtx = true);
    ATNConfigSet(const Ref<ATNConfigSet> &old);

    virtual ~ATNConfigSet();

    virtual bool add(const Ref<ATNConfig> &config);

    /// <summary>
    /// Adding a new config means merging contexts with existing configs for
    /// {@code (s, i, pi, _)}, where {@code s} is the
    /// <seealso cref="ATNConfig#state"/>, {@code i} is the <seealso cref="ATNConfig#alt"/>, and
    /// {@code pi} is the <seealso cref="ATNConfig#semanticContext"/>. We use
    /// {@code (s,i,pi)} as key.
    /// <p/>
    /// This method updates <seealso cref="#dipsIntoOuterContext"/> and
    /// <seealso cref="#hasSemanticContext"/> when necessary.
    /// </summary>
    virtual bool add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache);

    virtual std::vector<ATNState *> getStates();

    /**
     * Gets the complete set of represented alternatives for the configuration
     * set.
     *
     * @return the set of represented alternatives in this configuration set
     *
     * @since 4.3
     */
    antlrcpp::BitSet getAlts();
    virtual std::vector<Ref<SemanticContext>> getPredicates();

    virtual Ref<ATNConfig> get(size_t i) const;

    virtual void optimizeConfigs(ATNSimulator *interpreter);

    bool addAll(const Ref<ATNConfigSet> &other);

    bool operator == (const ATNConfigSet &other);
    virtual size_t hashCode();
    virtual size_t size();
    virtual bool isEmpty();
    virtual void clear();
    virtual bool isReadonly();
    virtual void setReadonly(bool readonly);
    virtual std::string toString();

  protected:
    /// Indicates that the set of configurations is read-only. Do not
    /// allow any code to manipulate the set; DFA states will point at
    /// the sets and they must not change. This does not protect the other
    /// fields; in particular, conflictingAlts is set after
    /// we've made this readonly.
    bool _readonly;

    virtual size_t getHash(ATNConfig *c); // Hash differs depending on set type.

  private:
    size_t _cachedHashCode;

    /// All configs but hashed by (s, i, _, pi) not including context. Wiped out
    /// when we go readonly as this set becomes a DFA state.
    std::unordered_map<size_t, ATNConfig *> _configLookup;

    void InitializeInstanceFields();
  };

} // namespace atn
} // namespace antlr4