Merge pull request #2934 from Marti2203/master

Made C# Trace Listener usable for most cases
This commit is contained in:
Terence Parr 2020-10-08 08:57:09 -07:00 committed by GitHub
commit 79808cdcbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -258,3 +258,4 @@ YYYY/MM/DD, github id, Full name, email
2020/09/06, ArthurSonzogni, Sonzogni Arthur, arthursonzogni@gmail.com
2020/09/12, Clcanny, Charles Ruan, a837940593@gmail.com
2020/09/15, rmcgregor1990, Robert McGregor, rmcgregor1990@gmail.com
2020/10/08, Marti2203, Martin Mirchev, mirchevmartin2203@gmail.com

View File

@ -22,20 +22,20 @@ namespace Antlr4.Runtime
#if !PORTABLE
public class TraceListener : IParseTreeListener
{
private readonly TextWriter Output;
public TraceListener(TextWriter output) {
Output = output;
public TraceListener(TextWriter output,Parser enclosing) {
_output = output;
_enclosing = enclosing;
}
public virtual void EnterEveryRule(ParserRuleContext ctx)
{
Output.WriteLine("enter " + this._enclosing.RuleNames[ctx.RuleIndex] + ", LT(1)=" + this._enclosing._input.LT(1).Text);
_output.WriteLine("enter " + this._enclosing.RuleNames[ctx.RuleIndex] + ", LT(1)=" + this._enclosing._input.LT(1).Text);
}
public virtual void ExitEveryRule(ParserRuleContext ctx)
{
Output.WriteLine("exit " + this._enclosing.RuleNames[ctx.RuleIndex] + ", LT(1)=" + this._enclosing._input.LT(1).Text);
_output.WriteLine("exit " + this._enclosing.RuleNames[ctx.RuleIndex] + ", LT(1)=" + this._enclosing._input.LT(1).Text);
}
public virtual void VisitErrorNode(IErrorNode node)
@ -46,15 +46,17 @@ namespace Antlr4.Runtime
{
ParserRuleContext parent = (ParserRuleContext)((IRuleNode)node.Parent).RuleContext;
IToken token = node.Symbol;
Output.WriteLine("consume " + token + " rule " + this._enclosing.RuleNames[parent.RuleIndex]);
_output.WriteLine("consume " + token + " rule " + this._enclosing.RuleNames[parent.RuleIndex]);
}
internal TraceListener(Parser _enclosing)
{
this._enclosing = _enclosing;
_output = Console.Out;
}
private readonly Parser _enclosing;
private readonly TextWriter _output;
}
#endif