]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNSimulator.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATNSimulator.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 "atn/ATN.h"
9 #include "misc/IntervalSet.h"
10 #include "support/CPPUtils.h"
11 #include "atn/PredictionContext.h"
12
13 namespace antlr4 {
14 namespace atn {
15
16   class ANTLR4CPP_PUBLIC ATNSimulator {
17   public:
18     /// Must distinguish between missing edge and edge we know leads nowhere.
19     static const Ref<dfa::DFAState> ERROR;
20     const ATN &atn;
21
22     ATNSimulator(const ATN &atn, PredictionContextCache &sharedContextCache);
23     virtual ~ATNSimulator();
24
25     virtual void reset() = 0;
26
27     /**
28      * Clear the DFA cache used by the current instance. Since the DFA cache may
29      * be shared by multiple ATN simulators, this method may affect the
30      * performance (but not accuracy) of other parsers which are being used
31      * concurrently.
32      *
33      * @throws UnsupportedOperationException if the current instance does not
34      * support clearing the DFA.
35      *
36      * @since 4.3
37      */
38     virtual void clearDFA();
39     virtual PredictionContextCache& getSharedContextCache();
40     virtual Ref<PredictionContext> getCachedContext(Ref<PredictionContext> const& context);
41
42     /// @deprecated Use <seealso cref="ATNDeserializer#deserialize"/> instead.
43     static ATN deserialize(const std::vector<uint16_t> &data);
44
45     /// @deprecated Use <seealso cref="ATNDeserializer#checkCondition(boolean)"/> instead.
46     static void checkCondition(bool condition);
47
48     /// @deprecated Use <seealso cref="ATNDeserializer#checkCondition(boolean, String)"/> instead.
49     static void checkCondition(bool condition, const std::string &message);
50
51     /// @deprecated Use <seealso cref="ATNDeserializer#edgeFactory"/> instead.
52     static Transition *edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3,
53                                    const std::vector<misc::IntervalSet> &sets);
54
55     /// @deprecated Use <seealso cref="ATNDeserializer#stateFactory"/> instead.
56     static ATNState *stateFactory(int type, int ruleIndex);
57
58   protected:
59     static antlrcpp::SingleWriteMultipleReadLock _stateLock; // Lock for DFA states.
60     static antlrcpp::SingleWriteMultipleReadLock _edgeLock; // Lock for the sparse edge map in DFA states.
61
62     /// <summary>
63     /// The context cache maps all PredictionContext objects that are equals()
64     ///  to a single cached copy. This cache is shared across all contexts
65     ///  in all ATNConfigs in all DFA states.  We rebuild each ATNConfigSet
66     ///  to use only cached nodes/graphs in addDFAState(). We don't want to
67     ///  fill this during closure() since there are lots of contexts that
68     ///  pop up but are not used ever again. It also greatly slows down closure().
69     ///  <p/>
70     ///  This cache makes a huge difference in memory and a little bit in speed.
71     ///  For the Java grammar on java.*, it dropped the memory requirements
72     ///  at the end from 25M to 16M. We don't store any of the full context
73     ///  graphs in the DFA because they are limited to local context only,
74     ///  but apparently there's a lot of repetition there as well. We optimize
75     ///  the config contexts before storing the config set in the DFA states
76     ///  by literally rebuilding them with cached subgraphs only.
77     ///  <p/>
78     ///  I tried a cache for use during closure operations, that was
79     ///  whacked after each adaptivePredict(). It cost a little bit
80     ///  more time I think and doesn't save on the overall footprint
81     ///  so it's not worth the complexity.
82     /// </summary>
83     PredictionContextCache &_sharedContextCache;
84   };
85
86 } // namespace atn
87 } // namespace antlr4