]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNConfigSet.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATNConfigSet.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 "support/BitSet.h"
9 #include "atn/PredictionContext.h"
10
11 namespace antlr4 {
12 namespace atn {
13
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 {
17   public:
18     /// Track the elements as they are added to the set; supports get(i)
19     std::vector<Ref<ATNConfig>> configs;
20
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?
23     size_t uniqueAlt;
24
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().
29      */
30     antlrcpp::BitSet conflictingAlts;
31
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;
36
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.
40     const bool fullCtx;
41
42     ATNConfigSet(bool fullCtx = true);
43     ATNConfigSet(const Ref<ATNConfigSet> &old);
44
45     virtual ~ATNConfigSet();
46
47     virtual bool add(const Ref<ATNConfig> &config);
48
49     /// <summary>
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.
55     /// <p/>
56     /// This method updates <seealso cref="#dipsIntoOuterContext"/> and
57     /// <seealso cref="#hasSemanticContext"/> when necessary.
58     /// </summary>
59     virtual bool add(const Ref<ATNConfig> &config, PredictionContextMergeCache *mergeCache);
60
61     virtual std::vector<ATNState *> getStates();
62
63     /**
64      * Gets the complete set of represented alternatives for the configuration
65      * set.
66      *
67      * @return the set of represented alternatives in this configuration set
68      *
69      * @since 4.3
70      */
71     antlrcpp::BitSet getAlts();
72     virtual std::vector<Ref<SemanticContext>> getPredicates();
73
74     virtual Ref<ATNConfig> get(size_t i) const;
75
76     virtual void optimizeConfigs(ATNSimulator *interpreter);
77
78     bool addAll(const Ref<ATNConfigSet> &other);
79
80     bool operator == (const ATNConfigSet &other);
81     virtual size_t hashCode();
82     virtual size_t size();
83     virtual bool isEmpty();
84     virtual void clear();
85     virtual bool isReadonly();
86     virtual void setReadonly(bool readonly);
87     virtual std::string toString();
88
89   protected:
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.
95     bool _readonly;
96
97     virtual size_t getHash(ATNConfig *c); // Hash differs depending on set type.
98
99   private:
100     size_t _cachedHashCode;
101
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;
105
106     void InitializeInstanceFields();
107   };
108
109 } // namespace atn
110 } // namespace antlr4