RuleContext.h 5.4 KB

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