2013-02-16 05:30:47 +08:00
|
|
|
/*
|
|
|
|
* [The "BSD license"]
|
|
|
|
* Copyright (c) 2013 Terence Parr
|
|
|
|
* Copyright (c) 2013 Sam Harwell
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using Antlr4.Runtime;
|
|
|
|
using Antlr4.Runtime.Misc;
|
|
|
|
using Antlr4.Runtime.Tree;
|
|
|
|
using Sharpen;
|
|
|
|
|
|
|
|
namespace Antlr4.Runtime
|
|
|
|
{
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>A rule context is a record of a single rule invocation.</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// 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 and tree parsers, we use the subclass
|
|
|
|
/// ParserRuleContext.
|
|
|
|
/// </remarks>
|
|
|
|
/// <seealso cref="ParserRuleContext">ParserRuleContext</seealso>
|
|
|
|
public class RuleContext : IRuleNode
|
|
|
|
{
|
|
|
|
/// <summary>What context invoked this rule?</summary>
|
|
|
|
public Antlr4.Runtime.RuleContext parent;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 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.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// 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.
|
|
|
|
/// </remarks>
|
|
|
|
public int invokingState = -1;
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public RuleContext()
|
|
|
|
{
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public RuleContext(Antlr4.Runtime.RuleContext parent, int invokingState)
|
|
|
|
{
|
|
|
|
this.parent = parent;
|
|
|
|
//if ( parent!=null ) System.out.println("invoke "+stateNumber+" from "+parent);
|
|
|
|
this.invokingState = invokingState;
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public static Antlr4.Runtime.RuleContext GetChildContext(Antlr4.Runtime.RuleContext
|
|
|
|
parent, int invokingState)
|
|
|
|
{
|
|
|
|
return new Antlr4.Runtime.RuleContext(parent, invokingState);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int Depth()
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
Antlr4.Runtime.RuleContext p = this;
|
|
|
|
while (p != null)
|
|
|
|
{
|
|
|
|
p = p.parent;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A context is empty if there is no invoking state; meaning nobody call
|
|
|
|
/// current context.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// A context is empty if there is no invoking state; meaning nobody call
|
|
|
|
/// current context.
|
|
|
|
/// </remarks>
|
|
|
|
public virtual bool IsEmpty()
|
|
|
|
{
|
|
|
|
return invokingState == -1;
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual Interval SourceInterval
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// satisfy the ParseTree / SyntaxTree interface
|
|
|
|
return Interval.Invalid;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-17 00:35:41 +08:00
|
|
|
RuleContext IRuleNode.RuleContext
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual Antlr4.Runtime.RuleContext Parent
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-19 05:31:24 +08:00
|
|
|
IRuleNode IRuleNode.Parent
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IParseTree IParseTree.Parent
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ITree ITree.Parent
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual Antlr4.Runtime.RuleContext Payload
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-19 05:31:24 +08:00
|
|
|
object ITree.Payload
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>Return the combined text of all child nodes.</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Return the combined text of all child nodes. This method only considers
|
|
|
|
/// 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.
|
|
|
|
/// </remarks>
|
|
|
|
public virtual string GetText()
|
|
|
|
{
|
|
|
|
if (ChildCount == 0)
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
for (int i = 0; i < ChildCount; i++)
|
|
|
|
{
|
|
|
|
builder.Append(GetChild(i).GetText());
|
|
|
|
}
|
|
|
|
return builder.ToString();
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int GetRuleIndex()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual IParseTree GetChild(int i)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-19 05:31:24 +08:00
|
|
|
ITree ITree.GetChild(int i)
|
|
|
|
{
|
|
|
|
return GetChild(i);
|
|
|
|
}
|
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual int ChildCount
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-19 07:02:08 +08:00
|
|
|
public virtual T Accept<T>(IParseTreeVisitor<T> visitor)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
return visitor.VisitChildren(this);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Print out a whole tree, not just a node, in LISP format
|
|
|
|
/// (root child1 ..
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// 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.
|
|
|
|
/// </remarks>
|
|
|
|
public virtual string ToStringTree(Parser recog)
|
|
|
|
{
|
|
|
|
return Trees.ToStringTree(this, recog);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Print out a whole tree, not just a node, in LISP format
|
|
|
|
/// (root child1 ..
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Print out a whole tree, not just a node, in LISP format
|
|
|
|
/// (root child1 .. childN). Print just a node if this is a leaf.
|
|
|
|
/// </remarks>
|
|
|
|
public virtual string ToStringTree(IList<string> ruleNames)
|
|
|
|
{
|
|
|
|
return Trees.ToStringTree(this, ruleNames);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual string ToStringTree()
|
|
|
|
{
|
|
|
|
return ToStringTree((IList<string>)null);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return ToString((IList<string>)null, (Antlr4.Runtime.RuleContext)null);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-18 11:22:16 +08:00
|
|
|
public string ToString(IRecognizer recog)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
2013-02-17 10:10:50 +08:00
|
|
|
return ToString(recog, ParserRuleContext.EmptyContext);
|
2013-02-16 22:14:20 +08:00
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public string ToString(IList<string> ruleNames)
|
|
|
|
{
|
|
|
|
return ToString(ruleNames, null);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
// recog null unless ParserRuleContext, in which case we use subclass toString(...)
|
2013-02-18 11:22:16 +08:00
|
|
|
public virtual string ToString(IRecognizer recog, Antlr4.Runtime.RuleContext
|
2013-02-16 22:14:20 +08:00
|
|
|
stop)
|
|
|
|
{
|
2013-02-17 05:23:03 +08:00
|
|
|
string[] ruleNames = recog != null ? recog.RuleNames : null;
|
2013-02-16 22:14:20 +08:00
|
|
|
IList<string> ruleNamesList = ruleNames != null ? Arrays.AsList(ruleNames) : null;
|
|
|
|
return ToString(ruleNamesList, stop);
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
|
2013-02-16 22:14:20 +08:00
|
|
|
public virtual string ToString(IList<string> ruleNames, Antlr4.Runtime.RuleContext
|
|
|
|
stop)
|
|
|
|
{
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
Antlr4.Runtime.RuleContext p = this;
|
|
|
|
buf.Append("[");
|
|
|
|
while (p != null && p != stop)
|
|
|
|
{
|
|
|
|
if (ruleNames == null)
|
|
|
|
{
|
|
|
|
if (!p.IsEmpty())
|
|
|
|
{
|
|
|
|
buf.Append(p.invokingState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ruleIndex = p.GetRuleIndex();
|
|
|
|
string ruleName = ruleIndex >= 0 && ruleIndex < ruleNames.Count ? ruleNames[ruleIndex
|
2013-02-18 05:09:30 +08:00
|
|
|
] : ruleIndex.ToString();
|
2013-02-16 22:14:20 +08:00
|
|
|
buf.Append(ruleName);
|
|
|
|
}
|
|
|
|
if (p.parent != null && (ruleNames != null || !p.parent.IsEmpty()))
|
|
|
|
{
|
|
|
|
buf.Append(" ");
|
|
|
|
}
|
|
|
|
p = p.parent;
|
|
|
|
}
|
|
|
|
buf.Append("]");
|
|
|
|
return buf.ToString();
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
}
|