aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr4_cpp_runtime/src/atn/SemanticContext.h
blob: 8116fc0b5623053227c12186183b4db605472423 (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* 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 "Recognizer.h"
#include "support/CPPUtils.h"
#include "atn/SemanticContextType.h"

namespace antlr4 {
namespace atn {

  /// A tree structure used to record the semantic context in which
  ///  an ATN configuration is valid.  It's either a single predicate,
  ///  a conjunction "p1 && p2", or a sum of products "p1||p2".
  ///
  ///  I have scoped the AND, OR, and Predicate subclasses of
  ///  SemanticContext within the scope of this outer class.
  class ANTLR4CPP_PUBLIC SemanticContext : public std::enable_shared_from_this<SemanticContext> {
  public:
    virtual ~SemanticContext() = default;

    SemanticContextType getContextType() const { return _contextType; }

    /// <summary>
    /// For context independent predicates, we evaluate them without a local
    /// context (i.e., null context). That way, we can evaluate them without
    /// having to create proper rule-specific context during prediction (as
    /// opposed to the parser, which creates them naturally). In a practical
    /// sense, this avoids a cast exception from RuleContext to myruleContext.
    /// <p/>
    /// For context dependent predicates, we must pass in a local context so that
    /// references such as $arg evaluate properly as _localctx.arg. We only
    /// capture context dependent predicates in the context in which we begin
    /// prediction, so we passed in the outer context here in case of context
    /// dependent predicate evaluation.
    /// </summary>
    virtual bool eval(Recognizer *parser, RuleContext *parserCallStack) const = 0;

    /**
     * Evaluate the precedence predicates for the context and reduce the result.
     *
     * @param parser The parser instance.
     * @param parserCallStack
     * @return The simplified semantic context after precedence predicates are
     * evaluated, which will be one of the following values.
     * <ul>
     * <li>{@link #NONE}: if the predicate simplifies to {@code true} after
     * precedence predicates are evaluated.</li>
     * <li>{@code null}: if the predicate simplifies to {@code false} after
     * precedence predicates are evaluated.</li>
     * <li>{@code this}: if the semantic context is not changed as a result of
     * precedence predicate evaluation.</li>
     * <li>A non-{@code null} {@link SemanticContext}: the new simplified
     * semantic context after precedence predicates are evaluated.</li>
     * </ul>
     */
    virtual Ref<const SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) const;

    virtual size_t hashCode() const = 0;

    virtual bool equals(const SemanticContext &other) const = 0;

    virtual std::string toString() const = 0;

    static Ref<const SemanticContext> And(Ref<const SemanticContext> a, Ref<const SemanticContext> b);

    /// See also: ParserATNSimulator::getPredsForAmbigAlts.
    static Ref<const SemanticContext> Or(Ref<const SemanticContext> a, Ref<const SemanticContext> b);

    class Empty;
    class Predicate;
    class PrecedencePredicate;
    class Operator;
    class AND;
    class OR;

  protected:
    explicit SemanticContext(SemanticContextType contextType) : _contextType(contextType) {}

  private:
    const SemanticContextType _contextType;
  };

  inline bool operator==(const SemanticContext &lhs, const SemanticContext &rhs) {
    return lhs.equals(rhs);
  }

  inline bool operator!=(const SemanticContext &lhs, const SemanticContext &rhs) {
    return !operator==(lhs, rhs);
  }

  class ANTLR4CPP_PUBLIC SemanticContext::Empty : public SemanticContext{
  public:
    /**
     * The default {@link SemanticContext}, which is semantically equivalent to
     * a predicate of the form {@code {true}?}.
     */
    static const Ref<const SemanticContext> Instance;
  };

  class ANTLR4CPP_PUBLIC SemanticContext::Predicate final : public SemanticContext {
  public:
    static bool is(const SemanticContext &semanticContext) { return semanticContext.getContextType() == SemanticContextType::PREDICATE; }

    static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }

    const size_t ruleIndex;
    const size_t predIndex;
    const bool isCtxDependent; // e.g., $i ref in pred

    Predicate(size_t ruleIndex, size_t predIndex, bool isCtxDependent);

    bool eval(Recognizer *parser, RuleContext *parserCallStack) const override;
    size_t hashCode() const override;
    bool equals(const SemanticContext &other) const override;
    std::string toString() const override;
  };

  class ANTLR4CPP_PUBLIC SemanticContext::PrecedencePredicate final : public SemanticContext {
  public:
    static bool is(const SemanticContext &semanticContext) { return semanticContext.getContextType() == SemanticContextType::PRECEDENCE; }

    static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }

    const int precedence;

    explicit PrecedencePredicate(int precedence);

    bool eval(Recognizer *parser, RuleContext *parserCallStack) const override;
    Ref<const SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) const override;
    size_t hashCode() const override;
    bool equals(const SemanticContext &other) const override;
    std::string toString() const override;
  };

  /**
   * This is the base class for semantic context "operators", which operate on
   * a collection of semantic context "operands".
   *
   * @since 4.3
   */
  class ANTLR4CPP_PUBLIC SemanticContext::Operator : public SemanticContext {
  public:
    static bool is(const SemanticContext &semanticContext) {
      const auto contextType = semanticContext.getContextType();
      return contextType == SemanticContextType::AND || contextType == SemanticContextType::OR;
    }

    static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }

    /**
     * Gets the operands for the semantic context operator.
     *
     * @return a collection of {@link SemanticContext} operands for the
     * operator.
     *
     * @since 4.3
     */

    virtual const std::vector<Ref<const SemanticContext>>& getOperands() const = 0;

  protected:
    using SemanticContext::SemanticContext;
  };

  /**
   * A semantic context which is true whenever none of the contained contexts
   * is false.
   */
  class ANTLR4CPP_PUBLIC SemanticContext::AND final : public SemanticContext::Operator {
  public:
    static bool is(const SemanticContext &semanticContext) { return semanticContext.getContextType() == SemanticContextType::AND; }

    static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }

    AND(Ref<const SemanticContext> a, Ref<const SemanticContext> b) ;

    const std::vector<Ref<const SemanticContext>>& getOperands() const override;

    /**
     * The evaluation of predicates by this context is short-circuiting, but
     * unordered.</p>
     */
    bool eval(Recognizer *parser, RuleContext *parserCallStack) const override;
    Ref<const SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) const override;
    size_t hashCode() const override;
    bool equals(const SemanticContext &other) const override;
    std::string toString() const override;

  private:
    std::vector<Ref<const SemanticContext>> _opnds;
  };

  /**
   * A semantic context which is true whenever at least one of the contained
   * contexts is true.
   */
  class ANTLR4CPP_PUBLIC SemanticContext::OR final : public SemanticContext::Operator {
  public:
    static bool is(const SemanticContext &semanticContext) { return semanticContext.getContextType() == SemanticContextType::OR; }

    static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }

    OR(Ref<const SemanticContext> a, Ref<const SemanticContext> b);

    const std::vector<Ref<const SemanticContext>>& getOperands() const override;

    /**
     * The evaluation of predicates by this context is short-circuiting, but
     * unordered.
     */
    bool eval(Recognizer *parser, RuleContext *parserCallStack) const override;
    Ref<const SemanticContext> evalPrecedence(Recognizer *parser, RuleContext *parserCallStack) const override;
    size_t hashCode() const override;
    bool equals(const SemanticContext &other) const override;
    std::string toString() const override;

  private:
    std::vector<Ref<const SemanticContext>> _opnds;
  };

}  // namespace atn
}  // namespace antlr4

namespace std {

  template <>
  struct hash<::antlr4::atn::SemanticContext> {
    size_t operator()(const ::antlr4::atn::SemanticContext &semanticContext) const {
      return semanticContext.hashCode();
    }
  };

}  // namespace std