Class: RuleContext
- Defined in:
- lib/antlr4/RuleContext.rb
Overview
A rule context is a record of a single rule invocation. It knows
which context invoked it, if any. If there is no parent context, then
naturally the invoking state is not valid. The parent link
provides a chain upwards from the current rule invocation to the root
of the invocation tree, forming a stack. We actually carry no
information about the rule associated with this context (except
when parsing). We keep only the state number of the invoking state from
the ATN submachine that invoked this. Contrast this with the s
pointer inside ParserRuleContext that tracks the current state
being "executed" for the current rule.
The parent contexts are useful for computing lookahead sets and
getting error information.
These objects are used during parsing and prediction.
For the special case of parsers, we use the subclass
ParserRuleContext.
@see ParserRuleContext
Direct Known Subclasses
Constant Summary collapse
- @@EMPTY =
nil
Instance Attribute Summary collapse
-
#invokingState ⇒ Object
Returns the value of attribute invokingState.
-
#parentCtx ⇒ Object
Returns the value of attribute parentCtx.
Class Method Summary collapse
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #depth ⇒ Object
- #getChild(i) ⇒ Object
- #getChildCount ⇒ Object
- #getChildren ⇒ Object
- #getPayload ⇒ Object
- #getRuleContext ⇒ Object
- #getRuleIndex ⇒ Object
-
#getSourceInterval ⇒ Object
satisfy the ParseTree / SyntaxTree interface.
-
#getText ⇒ Object
tokens which have been added to the parse tree.
-
#initialize(parent = nil, invoking_state = -1)) ⇒ RuleContext
constructor
A new instance of RuleContext.
-
#isEmpty ⇒ Object
A context is empty if there is no invoking state; meaning nobody call current context.
-
#to_s ⇒ Object
}.
-
#toString(ruleNames, stop) ⇒ Object
@Override public String toString() { return toString((List<String>)null, (RuleContext)null); }.
-
#toStringTree(ruleNames = nil, recog = nil) ⇒ Object
# Call this method to view a parse tree in a dialog box visually.#/ public Future<JDialog> inspect(@Nullable Parser parser) { List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; return inspect(ruleNames); }.
Constructor Details
#initialize(parent = nil, invoking_state = -1)) ⇒ RuleContext
Returns a new instance of RuleContext.
32 33 34 35 36 37 38 39 40 |
# File 'lib/antlr4/RuleContext.rb', line 32 def initialize(parent=nil, invoking_state=-1) super() # What context invoked this rule? @parentCtx = parent # What state invoked the rule associated with this context? # The "return address" is the followState of invokingState # If parent is null, this should be -1. @invokingState = invoking_state end |
Instance Attribute Details
#invokingState ⇒ Object
Returns the value of attribute invokingState.
31 32 33 |
# File 'lib/antlr4/RuleContext.rb', line 31 def invokingState @invokingState end |
#parentCtx ⇒ Object
Returns the value of attribute parentCtx.
31 32 33 |
# File 'lib/antlr4/RuleContext.rb', line 31 def parentCtx @parentCtx end |
Class Method Details
.EMPTY ⇒ Object
24 25 26 27 28 29 |
# File 'lib/antlr4/RuleContext.rb', line 24 def self.EMPTY if @@EMPTY.nil? then @@EMPTY = ParserRuleContext.new() end @@EMPTY end |
Instance Method Details
#accept(visitor) ⇒ Object
104 105 106 |
# File 'lib/antlr4/RuleContext.rb', line 104 def accept(visitor) return visitor.visitChildren(self) end |
#depth ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/antlr4/RuleContext.rb', line 42 def depth n = 0 p = self while not p.nil? do p = p.parentCtx n = n + 1 end return n end |
#getChild(i) ⇒ Object
93 94 95 |
# File 'lib/antlr4/RuleContext.rb', line 93 def getChild(i) return nil end |
#getChildCount ⇒ Object
97 98 99 |
# File 'lib/antlr4/RuleContext.rb', line 97 def getChildCount return 0 end |
#getChildren ⇒ Object
101 102 103 |
# File 'lib/antlr4/RuleContext.rb', line 101 def getChildren Array.new # [].map {|c| c } end |
#getPayload ⇒ Object
68 69 70 |
# File 'lib/antlr4/RuleContext.rb', line 68 def getPayload return self end |
#getRuleContext ⇒ Object
64 65 66 |
# File 'lib/antlr4/RuleContext.rb', line 64 def getRuleContext return self end |
#getRuleIndex ⇒ Object
89 90 91 |
# File 'lib/antlr4/RuleContext.rb', line 89 def getRuleIndex return -1 end |
#getSourceInterval ⇒ Object
satisfy the ParseTree / SyntaxTree interface
60 61 62 |
# File 'lib/antlr4/RuleContext.rb', line 60 def getSourceInterval return Antlr4::INVALID_INTERVAL end |
#getText ⇒ Object
tokens which have been added to the parse tree.
<p>
Since tokens on hidden channels (e.g. whitespace or comments) are not
added to the parse trees, they will not appear in the output of this
method.
/
79 80 81 82 83 84 85 86 87 |
# File 'lib/antlr4/RuleContext.rb', line 79 def getText if self.getChildCount() == 0 return "" end StringIO.open do |builder| self.getChildren().each {|child| builder.write(child.getText()) } return builder.string() end end |
#isEmpty ⇒ Object
A context is empty if there is no invoking state; meaning nobody call
current context.
54 55 56 |
# File 'lib/antlr4/RuleContext.rb', line 54 def isEmpty return self.invokingState == -1 end |
#to_s ⇒ Object
}
@Override
public String toStringTree() {
return toStringTree((List<String>)null);
}
173 174 175 |
# File 'lib/antlr4/RuleContext.rb', line 173 def to_s return self.toString(nil, nil) end |
#toString(ruleNames, stop) ⇒ Object
@Override
public String toString() {
return toString((List<String>)null, (RuleContext)null);
}
public final String toString(@Nullable Recognizer<?,?> recog) {
return toString(recog, ParserRuleContext.EMPTY);
}
public final String toString(@Nullable List<String> ruleNames) {
return toString(ruleNames, null);
}
// recog null unless ParserRuleContext, in which case we use subclass toString(...)
public String toString(@Nullable Recognizer<?,?> recog, @Nullable RuleContext stop) {
String[] ruleNames = recog != null ? recog.getRuleNames() : null;
List<String> ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null;
return toString(ruleNamesList, stop);
}
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 |
# File 'lib/antlr4/RuleContext.rb', line 197 def toString(ruleNames, stop) #->str#ruleNames:list, stop:RuleContext)->str: StringIO.open do |buf| p = self buf.write("[") while (not p.nil?) and p != stop do if ruleNames.nil? then if not p.isEmpty() buf.write(p.invokingState.to_s) end else ri = p.getRuleIndex() if ri >= 0 and ri < ruleNames.length ruleName = ruleNames[ri] else ruleName = ri.to_s end # ruleName = ruleNames[ri] if ri >= 0 and ri < len(ruleNames) else str(ri) buf.write(ruleName) end if p.parentCtx and (ruleNames or not p.parentCtx.isEmpty()) then buf.write(" ") end p = p.parentCtx end buf.write("]") return buf.string() end end |
#toStringTree(ruleNames = nil, recog = nil) ⇒ Object
# Call this method to view a parse tree in a dialog box visually.#/
public Future<JDialog> inspect(@Nullable Parser parser) {
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
return inspect(ruleNames);
}
public Future<JDialog> inspect(@Nullable List<String> ruleNames) {
TreeViewer viewer = new TreeViewer(ruleNames, this);
return viewer.open();
}
# Save this tree in a postscript file#/
public void save(@Nullable Parser parser, String fileName)
throws IOException, PrintException
{
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
save(ruleNames, fileName);
}
# Save this tree in a postscript file using a particular font name and size#/
public void save(@Nullable Parser parser, String fileName,
String fontName, int fontSize)
throws IOException
{
List<String> ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null;
save(ruleNames, fileName, fontName, fontSize);
}
# Save this tree in a postscript file#/
public void save(@Nullable List<String> ruleNames, String fileName)
throws IOException, PrintException
{
Trees.writePS(this, ruleNames, fileName);
}
# Save this tree in a postscript file using a particular font name and size#/
public void save(@Nullable List<String> ruleNames, String fileName,
String fontName, int fontSize)
throws IOException
{
Trees.writePS(this, ruleNames, fileName, fontName, fontSize);
}
# Print out a whole tree, not just a node, in LISP format
# (root child1 .. childN). Print just a node if this is a leaf.
# We have to know the recognizer so we can get rule names.
#/
@Override
public String toStringTree(@Nullable Parser recog) {
return Trees.toStringTree(this, recog);
}
Print out a whole tree, not just a node, in LISP format
(root child1 .. childN). Print just a node if this is a leaf.
163 164 165 |
# File 'lib/antlr4/RuleContext.rb', line 163 def toStringTree(ruleNames=nil ,recog=nil) return Trees.toStringTree(self, ruleNames, recog) end |