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/RuleContext.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 "tree/ParseTree.h"
9
10
namespace antlr4 {
11
12
  /** A rule context is a record of a single rule invocation.
13
   *
14
   *  We form a stack of these context objects using the parent
15
   *  pointer. A parent pointer of null indicates that the current
16
   *  context is the bottom of the stack. The ParserRuleContext subclass
17
   *  as a children list so that we can turn this data structure into a
18
   *  tree.
19
   *
20
   *  The root node always has a null pointer and invokingState of -1.
21
   *
22
   *  Upon entry to parsing, the first invoked rule function creates a
23
   *  context object (asubclass specialized for that rule such as
24
   *  SContext) and makes it the root of a parse tree, recorded by field
25
   *  Parser._ctx.
26
   *
27
   *  public final SContext s() throws RecognitionException {
28
   *      SContext _localctx = new SContext(_ctx, getState()); <-- create new node
29
   *      enterRule(_localctx, 0, RULE_s);                     <-- push it
30
   *      ...
31
   *      exitRule();                                          <-- pop back to _localctx
32
   *      return _localctx;
33
   *  }
34
   *
35
   *  A subsequent rule invocation of r from the start rule s pushes a
36
   *  new context object for r whose parent points at s and use invoking
37
   *  state is the state with r emanating as edge label.
38
   *
39
   *  The invokingState fields from a context object to the root
40
   *  together form a stack of rule indication states where the root
41
   *  (bottom of the stack) has a -1 sentinel value. If we invoke start
42
   *  symbol s then call r1, which calls r2, the  would look like
43
   *  this:
44
   *
45
   *     SContext[-1]   <- root node (bottom of the stack)
46
   *     R1Context[p]   <- p in rule s called r1
47
   *     R2Context[q]   <- q in rule r1 called r2
48
   *
49
   *  So the top of the stack, _ctx, represents a call to the current
50
   *  rule and it holds the return address from another rule that invoke
51
   *  to this rule. To invoke a rule, we must always have a current context.
52
   *
53
   *  The parent contexts are useful for computing lookahead sets and
54
   *  getting error information.
55
   *
56
   *  These objects are used during parsing and prediction.
57
   *  For the special case of parsers, we use the subclass
58
   *  ParserRuleContext.
59
   *
60
   *  @see ParserRuleContext
61
   */
62
  class ANTLR4CPP_PUBLIC RuleContext : public tree::ParseTree {
63
  public:
64
0
    static bool is(const tree::ParseTree &parseTree) { return parseTree.getTreeType() == tree::ParseTreeType::RULE; }
65
66
0
    static bool is(const tree::ParseTree *parseTree) { return parseTree != nullptr && is(*parseTree); }
67
68
    /// What state invoked the rule associated with this context?
69
    /// The "return address" is the followState of invokingState
70
    /// If parent is null, this should be -1 and this context object represents the start rule.
71
    size_t invokingState;
72
73
    RuleContext();
74
    RuleContext(RuleContext *parent, size_t invokingState);
75
76
    virtual int depth();
77
78
    /// A context is empty if there is no invoking state; meaning nobody called current context.
79
    virtual bool isEmpty();
80
81
    // satisfy the ParseTree / SyntaxTree interface
82
83
    virtual misc::Interval getSourceInterval() override;
84
85
    virtual std::string getText() override;
86
87
    virtual size_t getRuleIndex() const;
88
89
    /** For rule associated with this parse tree internal node, return
90
     *  the outer alternative number used to match the input. Default
91
     *  implementation does not compute nor store this alt num. Create
92
     *  a subclass of ParserRuleContext with backing field and set
93
     *  option contextSuperClass.
94
     *  to set it.
95
     *
96
     *  @since 4.5.3
97
     */
98
    virtual size_t getAltNumber() const;
99
100
    /** Set the outer alternative number for this context node. Default
101
     *  implementation does nothing to avoid backing field overhead for
102
     *  trees that don't need it.  Create
103
     *  a subclass of ParserRuleContext with backing field and set
104
     *  option contextSuperClass.
105
     *
106
     *  @since 4.5.3
107
     */
108
    virtual void setAltNumber(size_t altNumber);
109
110
    virtual std::any accept(tree::ParseTreeVisitor *visitor) override;
111
112
    /// <summary>
113
    /// Print out a whole tree, not just a node, in LISP format
114
    ///  (root child1 .. childN). Print just a node if this is a leaf.
115
    ///  We have to know the recognizer so we can get rule names.
116
    /// </summary>
117
    virtual std::string toStringTree(Parser *recog, bool pretty = false) override;
118
119
    /// <summary>
120
    /// Print out a whole tree, not just a node, in LISP format
121
    ///  (root child1 .. childN). Print just a node if this is a leaf.
122
    /// </summary>
123
    virtual std::string toStringTree(std::vector<std::string> &ruleNames, bool pretty = false);
124
125
    virtual std::string toStringTree(bool pretty = false) override;
126
    virtual std::string toString() override;
127
    std::string toString(Recognizer *recog);
128
    std::string toString(const std::vector<std::string> &ruleNames);
129
130
    // recog null unless ParserRuleContext, in which case we use subclass toString(...)
131
    std::string toString(Recognizer *recog, RuleContext *stop);
132
133
    virtual std::string toString(const std::vector<std::string> &ruleNames, RuleContext *stop);
134
135
0
    bool operator == (const RuleContext &other) { return this == &other; } // Simple address comparison.
136
137
  private:
138
    void InitializeInstanceFields();
139
  };
140
141
} // namespace antlr4