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 "support/BitSet.h"
9 #include "atn/PredictionContext.h"
14 /// Specialized set that can track info about the set, with support for combining similar configurations using a
15 /// graph-structured stack.
16 class ANTLR4CPP_PUBLIC ATNConfigSet {
18 /// Track the elements as they are added to the set; supports get(i)
19 std::vector<Ref<ATNConfig>> configs;
21 // TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation
22 // TODO: can we track conflicts as they are added to save scanning configs later?
25 /** Currently this is only used when we detect SLL conflict; this does
26 * not necessarily represent the ambiguous alternatives. In fact,
27 * I should also point out that this seems to include predicated alternatives
28 * that have predicates that evaluate to false. Computed in computeTargetState().
30 antlrcpp::BitSet conflictingAlts;
32 // Used in parser and lexer. In lexer, it indicates we hit a pred
33 // while computing a closure operation. Don't make a DFA state from this.
34 bool hasSemanticContext;
35 bool dipsIntoOuterContext;
37 /// Indicates that this configuration set is part of a full context
38 /// LL prediction. It will be used to determine how to merge $. With SLL
39 /// it's a wildcard whereas it is not for LL context merge.
42 ATNConfigSet(bool fullCtx = true);
43 ATNConfigSet(const Ref<ATNConfigSet> &old);
45 virtual ~ATNConfigSet();
47 virtual bool add(const Ref<ATNConfig> &config);
50 /// Adding a new config means merging contexts with existing configs for
51 /// {@code (s, i, pi, _)}, where {@code s} is the
52 /// <seealso cref="ATNConfig#state"/>, {@code i} is the <seealso cref="ATNConfig#alt"/>, and
53 /// {@code pi} is the <seealso cref="ATNConfig#semanticContext"/>. We use
54 /// {@code (s,i,pi)} as key.
56 /// This method updates <seealso cref="#dipsIntoOuterContext"/> and
57 /// <seealso cref="#hasSemanticContext"/> when necessary.
59 virtual bool add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache);
61 virtual std::vector<ATNState *> getStates();
64 * Gets the complete set of represented alternatives for the configuration
67 * @return the set of represented alternatives in this configuration set
71 antlrcpp::BitSet getAlts();
72 virtual std::vector<Ref<SemanticContext>> getPredicates();
74 virtual Ref<ATNConfig> get(size_t i) const;
76 virtual void optimizeConfigs(ATNSimulator *interpreter);
78 bool addAll(const Ref<ATNConfigSet> &other);
80 bool operator == (const ATNConfigSet &other);
81 virtual size_t hashCode();
82 virtual size_t size();
83 virtual bool isEmpty();
85 virtual bool isReadonly();
86 virtual void setReadonly(bool readonly);
87 virtual std::string toString();
90 /// Indicates that the set of configurations is read-only. Do not
91 /// allow any code to manipulate the set; DFA states will point at
92 /// the sets and they must not change. This does not protect the other
93 /// fields; in particular, conflictingAlts is set after
94 /// we've made this readonly.
97 virtual size_t getHash(ATNConfig *c); // Hash differs depending on set type.
100 size_t _cachedHashCode;
102 /// All configs but hashed by (s, i, _, pi) not including context. Wiped out
103 /// when we go readonly as this set becomes a DFA state.
104 std::unordered_map<size_t, ATNConfig *> _configLookup;
106 void InitializeInstanceFields();
110 } // namespace antlr4