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;
|
2014-04-28 20:22:22 +08:00
|
|
|
using Antlr4.Runtime.Sharpen;
|
2013-02-16 05:30:47 +08:00
|
|
|
using Antlr4.Runtime.Tree;
|
|
|
|
|
|
|
|
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.
|
2014-02-17 04:51:37 +08:00
|
|
|
/// For the special case of parsers, we use the subclass
|
2013-02-16 22:14:20 +08:00
|
|
|
/// ParserRuleContext.
|
|
|
|
/// </remarks>
|
2014-07-05 05:23:31 +08:00
|
|
|
/// <seealso cref="ParserRuleContext"/>
|
2013-02-16 22:14:20 +08:00
|
|
|
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
|
|
|
|
2014-02-17 03:33:54 +08:00
|
|
|
public static Antlr4.Runtime.RuleContext GetChildContext(Antlr4.Runtime.RuleContext parent, int invokingState)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
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>
|
2014-07-02 10:31:37 +08:00
|
|
|
public virtual bool IsEmpty
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
2014-07-02 10:31:37 +08:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return invokingState == -1;
|
|
|
|
}
|
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 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.
|
2013-04-18 10:42:17 +08:00
|
|
|
/// <p/>
|
2013-02-16 22:14:20 +08:00
|
|
|
/// 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
|
|
|
|
2014-07-02 10:31:37 +08:00
|
|
|
public virtual int RuleIndex
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
2014-07-02 10:31:37 +08:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
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 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(...)
|
2014-02-17 05:25:16 +08:00
|
|
|
public virtual string ToString(IRecognizer recog, Antlr4.Runtime.RuleContext stop)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
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
|
|
|
|
2014-02-17 03:33:54 +08:00
|
|
|
public virtual string ToString(IList<string> ruleNames, Antlr4.Runtime.RuleContext stop)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
StringBuilder buf = new StringBuilder();
|
|
|
|
Antlr4.Runtime.RuleContext p = this;
|
|
|
|
buf.Append("[");
|
|
|
|
while (p != null && p != stop)
|
|
|
|
{
|
|
|
|
if (ruleNames == null)
|
|
|
|
{
|
2014-07-02 10:31:37 +08:00
|
|
|
if (!p.IsEmpty)
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
buf.Append(p.invokingState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-02 10:31:37 +08:00
|
|
|
int ruleIndex = p.RuleIndex;
|
2014-02-17 05:25:16 +08:00
|
|
|
string ruleName = ruleIndex >= 0 && ruleIndex < ruleNames.Count ? ruleNames[ruleIndex] : ruleIndex.ToString();
|
2013-02-16 22:14:20 +08:00
|
|
|
buf.Append(ruleName);
|
|
|
|
}
|
2014-07-02 10:31:37 +08:00
|
|
|
if (p.parent != null && (ruleNames != null || !p.parent.IsEmpty))
|
2013-02-16 22:14:20 +08:00
|
|
|
{
|
|
|
|
buf.Append(" ");
|
|
|
|
}
|
|
|
|
p = p.parent;
|
|
|
|
}
|
|
|
|
buf.Append("]");
|
|
|
|
return buf.ToString();
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 05:30:47 +08:00
|
|
|
}
|