Coverage Report

Created: 2025-11-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/external/antlr4-cpp-runtime~/runtime/src/Recognizer.h
Line
Count
Source
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 "ProxyErrorListener.h"
9
#include "support/Casts.h"
10
#include "atn/SerializedATNView.h"
11
#include "internal/Synchronization.h"
12
13
namespace antlr4 {
14
15
  class ANTLR4CPP_PUBLIC Recognizer {
16
  public:
17
    static constexpr size_t EOF = std::numeric_limits<size_t>::max();
18
19
    Recognizer();
20
    Recognizer(Recognizer const&) = delete;
21
    virtual ~Recognizer();
22
23
    Recognizer& operator=(Recognizer const&) = delete;
24
25
    virtual std::vector<std::string> const& getRuleNames() const = 0;
26
27
    /**
28
     * Get the vocabulary used by the recognizer.
29
     *
30
     * @return A {@link Vocabulary} instance providing information about the
31
     * vocabulary used by the grammar.
32
     */
33
    virtual dfa::Vocabulary const& getVocabulary() const = 0;
34
35
    /// <summary>
36
    /// Get a map from token names to token types.
37
    /// <p/>
38
    /// Used for XPath and tree pattern compilation.
39
    /// </summary>
40
    virtual std::map<std::string_view, size_t> getTokenTypeMap();
41
42
    /// <summary>
43
    /// Get a map from rule names to rule indexes.
44
    /// <p/>
45
    /// Used for XPath and tree pattern compilation.
46
    /// </summary>
47
    virtual std::map<std::string, size_t> getRuleIndexMap();
48
49
    virtual size_t getTokenType(std::string_view tokenName);
50
51
    /// <summary>
52
    /// If this recognizer was generated, it will have a serialized ATN
53
    /// representation of the grammar.
54
    /// <p/>
55
    /// For interpreters, we don't know their serialized ATN despite having
56
    /// created the interpreter from it.
57
    /// </summary>
58
0
    virtual atn::SerializedATNView getSerializedATN() const {
59
0
      throw "there is no serialized ATN";
60
0
    }
61
62
    /// <summary>
63
    /// For debugging and other purposes, might want the grammar name.
64
    ///  Have ANTLR generate an implementation for this method.
65
    /// </summary>
66
    virtual std::string getGrammarFileName() const = 0;
67
68
    /// Get the ATN interpreter (in fact one of it's descendants) used by the recognizer for prediction.
69
    /// @returns The ATN interpreter used by the recognizer for prediction.
70
    template <class T>
71
    T* getInterpreter() const {
72
      return antlrcpp::downCast<T *>(_interpreter);
73
    }
74
75
    /**
76
     * Set the ATN interpreter used by the recognizer for prediction.
77
     *
78
     * @param interpreter The ATN interpreter used by the recognizer for
79
     * prediction.
80
     */
81
    void setInterpreter(atn::ATNSimulator *interpreter);
82
83
    /// What is the error header, normally line/character position information?
84
    virtual std::string getErrorHeader(RecognitionException *e);
85
86
    /** How should a token be displayed in an error message? The default
87
     *  is to display just the text, but during development you might
88
     *  want to have a lot of information spit out.  Override in that case
89
     *  to use t.toString() (which, for CommonToken, dumps everything about
90
     *  the token). This is better than forcing you to override a method in
91
     *  your token objects because you don't have to go modify your lexer
92
     *  so that it creates a new Java type.
93
     *
94
     * @deprecated This method is not called by the ANTLR 4 Runtime. Specific
95
     * implementations of {@link ANTLRErrorStrategy} may provide a similar
96
     * feature when necessary. For example, see
97
     * {@link DefaultErrorStrategy#getTokenErrorDisplay}.
98
     */
99
    virtual std::string getTokenErrorDisplay(Token *t);
100
101
    /// <exception cref="NullPointerException"> if {@code listener} is {@code null}. </exception>
102
    virtual void addErrorListener(ANTLRErrorListener *listener);
103
104
    virtual void removeErrorListener(ANTLRErrorListener *listener);
105
106
    virtual void removeErrorListeners();
107
108
    virtual ProxyErrorListener& getErrorListenerDispatch();
109
110
    // subclass needs to override these if there are sempreds or actions
111
    // that the ATN interp needs to execute
112
    virtual bool sempred(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);
113
114
    virtual bool precpred(RuleContext *localctx, int precedence);
115
116
    virtual void action(RuleContext *localctx, size_t ruleIndex, size_t actionIndex);
117
118
1.78M
    size_t getState() const { return _stateNumber; }
119
120
    // Get the ATN used by the recognizer for prediction.
121
    virtual const atn::ATN& getATN() const = 0;
122
123
    /// <summary>
124
    /// Indicate that the recognizer has changed internal state that is
125
    ///  consistent with the ATN state passed in.  This way we always know
126
    ///  where we are in the ATN as the parser goes along. The rule
127
    ///  context objects form a stack that lets us see the stack of
128
    ///  invoking rules. Combine this and we have complete ATN
129
    ///  configuration information.
130
    /// </summary>
131
0
    void setState(size_t atnState) { _stateNumber = atnState; }
132
133
    virtual IntStream* getInputStream() = 0;
134
135
    virtual void setInputStream(IntStream *input) = 0;
136
137
    virtual TokenFactory<CommonToken>* getTokenFactory() = 0;
138
139
    template<typename T1>
140
    void setTokenFactory(TokenFactory<T1> *input);
141
142
  protected:
143
    atn::ATNSimulator *_interpreter; // Set and deleted in descendants (or the profiler).
144
145
    // Mutex to manage synchronized access for multithreading.
146
    internal::Mutex _mutex;
147
148
  private:
149
    static std::map<const dfa::Vocabulary*, std::map<std::string_view, size_t>> _tokenTypeMapCache;
150
    static std::map<std::vector<std::string>, std::map<std::string, size_t>> _ruleIndexMapCache;
151
152
    ProxyErrorListener _proxListener; // Manages a collection of listeners.
153
154
    size_t _stateNumber;
155
156
    void InitializeInstanceFields();
157
158
  };
159
160
} // namespace antlr4