]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/SemanticContext.h
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / SemanticContext.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 "Recognizer.h"
9 #include "support/CPPUtils.h"
10
11 namespace antlr4 {
12 namespace atn {
13
14   /// A tree structure used to record the semantic context in which
15   ///  an ATN configuration is valid.  It's either a single predicate,
16   ///  a conjunction "p1 && p2", or a sum of products "p1||p2".
17   ///
18   ///  I have scoped the AND, OR, and Predicate subclasses of
19   ///  SemanticContext within the scope of this outer class.
20   class ANTLR4CPP_PUBLIC SemanticContext : public std::enable_shared_from_this<SemanticContext> {
21   public:
22     struct Hasher
23     {
24       size_t operator()(Ref<SemanticContext> const& k) const {
25         return k->hashCode();
26       }
27     };
28
29     struct Comparer {
30       bool operator()(Ref<SemanticContext> const& lhs, Ref<SemanticContext> const& rhs) const {
31         if (lhs == rhs)
32           return true;
33         return (lhs->hashCode() == rhs->hashCode()) && (*lhs == *rhs);
34       }
35     };
36
37
38     using Set = std::unordered_set<Ref<SemanticContext>, Hasher, Comparer>;
39
40     /**
41      * The default {@link SemanticContext}, which is semantically equivalent to
42      * a predicate of the form {@code {true}?}.
43      */
44     static const Ref<SemanticContext> NONE;
45
46     virtual ~SemanticContext();
47
48     virtual size_t hashCode() const = 0;
49     virtual std::string toString() const = 0;
50     virtual bool operator == (const SemanticContext &other) const = 0;
51     virtual bool operator != (const SemanticContext &other) const;
52
53     /// <summary>
54     /// For context independent predicates, we evaluate them without a local
55     /// context (i.e., null context). That way, we can evaluate them without
56     /// having to create proper rule-specific context during prediction (as
57     /// opposed to the parser, which creates them naturally). In a practical
58     /// sense, this avoids a cast exception from RuleContext to myruleContext.
59     /// <p/>
60     /// For context dependent predicates, we must pass in a local context so that
61     /// references such as $arg evaluate properly as _localctx.arg. We only
62     /// capture context dependent predicates in the context in which we begin
63     /// prediction, so we passed in the outer context here in case of context
64     /// dependent predicate evaluation.
65     /// </summary>
66     virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) = 0;
67
68     /**
69      * Evaluate the precedence predicates for the context and reduce the result.
70      *
71      * @param parser The parser instance.
72      * @param parserCallStack
73      * @return The simplified semantic context after precedence predicates are
74      * evaluated, which will be one of the following values.
75      * <ul>
76      * <li>{@link #NONE}: if the predicate simplifies to {@code true} after
77      * precedence predicates are evaluated.</li>
78      * <li>{@code null}: if the predicate simplifies to {@code false} after
79      * precedence predicates are evaluated.</li>
80      * <li>{@code this}: if the semantic context is not changed as a result of
81      * precedence predicate evaluation.</li>
82      * <li>A non-{@code null} {@link SemanticContext}: the new simplified
83      * semantic context after precedence predicates are evaluated.</li>
84      * </ul>
85      */
86     virtual Ref<SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack);
87
88     static Ref<SemanticContext> And(Ref<SemanticContext> const& a, Ref<SemanticContext> const& b);
89
90     /// See also: ParserATNSimulator::getPredsForAmbigAlts.
91     static Ref<SemanticContext> Or(Ref<SemanticContext> const& a, Ref<SemanticContext> const& b);
92
93     class Predicate;
94     class PrecedencePredicate;
95     class Operator;
96     class AND;
97     class OR;
98
99   private:
100     static std::vector<Ref<PrecedencePredicate>> filterPrecedencePredicates(const Set &collection);
101   };
102
103   class ANTLR4CPP_PUBLIC SemanticContext::Predicate : public SemanticContext {
104   public:
105     const size_t ruleIndex;
106     const size_t predIndex;
107     const bool isCtxDependent; // e.g., $i ref in pred
108
109   protected:
110     Predicate();
111
112   public:
113     Predicate(size_t ruleIndex, size_t predIndex, bool isCtxDependent);
114
115     virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) override;
116     virtual size_t hashCode() const override;
117     virtual bool operator == (const SemanticContext &other) const override;
118     virtual std::string toString() const override;
119   };
120
121   class ANTLR4CPP_PUBLIC SemanticContext::PrecedencePredicate : public SemanticContext {
122   public:
123     const int precedence;
124
125   protected:
126     PrecedencePredicate();
127
128   public:
129     PrecedencePredicate(int precedence);
130
131     virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) override;
132     virtual Ref<SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) override;
133     virtual int compareTo(PrecedencePredicate *o);
134     virtual size_t hashCode() const override;
135     virtual bool operator == (const SemanticContext &other) const override;
136     virtual std::string toString() const override;
137   };
138
139   /**
140    * This is the base class for semantic context "operators", which operate on
141    * a collection of semantic context "operands".
142    *
143    * @since 4.3
144    */
145   class ANTLR4CPP_PUBLIC SemanticContext::Operator : public SemanticContext {
146   public:
147     virtual ~Operator() override;
148
149     /**
150      * Gets the operands for the semantic context operator.
151      *
152      * @return a collection of {@link SemanticContext} operands for the
153      * operator.
154      *
155      * @since 4.3
156      */
157
158     virtual std::vector<Ref<SemanticContext>> getOperands() const = 0;
159   };
160
161   /**
162    * A semantic context which is true whenever none of the contained contexts
163    * is false.
164    */
165   class ANTLR4CPP_PUBLIC SemanticContext::AND : public SemanticContext::Operator {
166   public:
167     std::vector<Ref<SemanticContext>> opnds;
168
169     AND(Ref<SemanticContext> const& a, Ref<SemanticContext> const& b) ;
170
171     virtual std::vector<Ref<SemanticContext>> getOperands() const override;
172     virtual bool operator == (const SemanticContext &other) const override;
173     virtual size_t hashCode() const override;
174
175     /**
176      * The evaluation of predicates by this context is short-circuiting, but
177      * unordered.</p>
178      */
179     virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) override;
180     virtual Ref<SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) override;
181     virtual std::string toString() const override;
182   };
183
184   /**
185    * A semantic context which is true whenever at least one of the contained
186    * contexts is true.
187    */
188   class ANTLR4CPP_PUBLIC SemanticContext::OR : public SemanticContext::Operator {
189   public:
190     std::vector<Ref<SemanticContext>> opnds;
191
192     OR(Ref<SemanticContext> const& a, Ref<SemanticContext> const& b);
193
194     virtual std::vector<Ref<SemanticContext>> getOperands() const override;
195     virtual bool operator == (const SemanticContext &other) const override;
196     virtual size_t hashCode() const override;
197
198     /**
199      * The evaluation of predicates by this context is short-circuiting, but
200      * unordered.
201      */
202     virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) override;
203     virtual Ref<SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) override;
204     virtual std::string toString() const override;
205   };
206
207 } // namespace atn
208 } // namespace antlr4
209
210 // Hash function for SemanticContext, used in the MurmurHash::update function
211
212 namespace std {
213   using antlr4::atn::SemanticContext;
214
215   template <> struct hash<SemanticContext>
216   {
217     size_t operator () (SemanticContext &x) const
218     {
219       return x.hashCode();
220     }
221   };
222 }