C# runtime and test harness support for writing test output to file

This commit is contained in:
Ben Hamilton 2017-02-10 11:25:59 -08:00
parent 182f3c4647
commit 6611c1ae27
4 changed files with 81 additions and 26 deletions

View File

@ -1,6 +1,6 @@
writeln(s) ::= <<Console.WriteLine(<s>);>> writeln(s) ::= <<Output.WriteLine(<s>);>>
write(s) ::= <<Console.Write(<s>);>> write(s) ::= <<Output.Write(<s>);>>
writeList(s) ::= <<Console.WriteLine(<s; separator="+">);>> writeList(s) ::= <<Output.WriteLine(<s; separator="+">);>>
False() ::= "false" False() ::= "false"
@ -176,8 +176,14 @@ public class PositionAdjustingLexerATNSimulator : LexerATNSimulator {
BasicListener(X) ::= << BasicListener(X) ::= <<
@parser::members { @parser::members {
public class LeafListener : TBaseListener { public class LeafListener : TBaseListener {
private readonly TextWriter Output;
public LeafListener(TextWriter output) {
Output = output;
}
public override void VisitTerminal(ITerminalNode node) { public override void VisitTerminal(ITerminalNode node) {
Console.WriteLine(node.Symbol.Text); Output.WriteLine(node.Symbol.Text);
} }
} }
} }
@ -185,7 +191,7 @@ public class LeafListener : TBaseListener {
WalkListener(s) ::= << WalkListener(s) ::= <<
ParseTreeWalker walker = new ParseTreeWalker(); ParseTreeWalker walker = new ParseTreeWalker();
walker.Walk(new LeafListener(), <s>); walker.Walk(new LeafListener(Output), <s>);
>> >>
TreeNodeWithAltNumField(X) ::= << TreeNodeWithAltNumField(X) ::= <<
@ -204,6 +210,12 @@ public class MyRuleNode : ParserRuleContext {
TokenGetterListener(X) ::= << TokenGetterListener(X) ::= <<
@parser::members { @parser::members {
public class LeafListener : TBaseListener { public class LeafListener : TBaseListener {
private readonly TextWriter Output;
public LeafListener(TextWriter output) {
Output = output;
}
public override void ExitA(TParser.AContext ctx) { public override void ExitA(TParser.AContext ctx) {
if (ctx.ChildCount==2) if (ctx.ChildCount==2)
{ {
@ -214,11 +226,11 @@ public class LeafListener : TBaseListener {
} }
sb.Length = sb.Length - 2; sb.Length = sb.Length - 2;
sb.Append ("]"); sb.Append ("]");
Console.Write ("{0} {1} {2}", ctx.INT (0).Symbol.Text, Output.Write ("{0} {1} {2}", ctx.INT (0).Symbol.Text,
ctx.INT (1).Symbol.Text, sb.ToString()); ctx.INT (1).Symbol.Text, sb.ToString());
} }
else else
Console.WriteLine(ctx.ID().Symbol); Output.WriteLine(ctx.ID().Symbol);
} }
} }
} }
@ -227,12 +239,18 @@ public class LeafListener : TBaseListener {
RuleGetterListener(X) ::= << RuleGetterListener(X) ::= <<
@parser::members { @parser::members {
public class LeafListener : TBaseListener { public class LeafListener : TBaseListener {
private readonly TextWriter Output;
public LeafListener(TextWriter output) {
Output = output;
}
public override void ExitA(TParser.AContext ctx) { public override void ExitA(TParser.AContext ctx) {
if (ctx.ChildCount==2) { if (ctx.ChildCount==2) {
Console.Write("{0} {1} {2}",ctx.b(0).Start.Text, Output.Write("{0} {1} {2}",ctx.b(0).Start.Text,
ctx.b(1).Start.Text,ctx.b()[0].Start.Text); ctx.b(1).Start.Text,ctx.b()[0].Start.Text);
} else } else
Console.WriteLine(ctx.b(0).Start.Text); Output.WriteLine(ctx.b(0).Start.Text);
} }
} }
} }
@ -242,12 +260,18 @@ public class LeafListener : TBaseListener {
LRListener(X) ::= << LRListener(X) ::= <<
@parser::members { @parser::members {
public class LeafListener : TBaseListener { public class LeafListener : TBaseListener {
private readonly TextWriter Output;
public LeafListener(TextWriter output) {
Output = output;
}
public override void ExitE(TParser.EContext ctx) { public override void ExitE(TParser.EContext ctx) {
if (ctx.ChildCount==3) { if (ctx.ChildCount==3) {
Console.Write("{0} {1} {2}\n",ctx.e(0).Start.Text, Output.Write("{0} {1} {2}\n",ctx.e(0).Start.Text,
ctx.e(1).Start.Text, ctx.e()[0].Start.Text); ctx.e(1).Start.Text, ctx.e()[0].Start.Text);
} else } else
Console.WriteLine(ctx.INT().Symbol.Text); Output.WriteLine(ctx.INT().Symbol.Text);
} }
} }
} }
@ -256,11 +280,17 @@ public class LeafListener : TBaseListener {
LRWithLabelsListener(X) ::= << LRWithLabelsListener(X) ::= <<
@parser::members { @parser::members {
public class LeafListener : TBaseListener { public class LeafListener : TBaseListener {
private readonly TextWriter Output;
public LeafListener(TextWriter output) {
Output = output;
}
public override void ExitCall(TParser.CallContext ctx) { public override void ExitCall(TParser.CallContext ctx) {
Console.Write("{0} {1}",ctx.e().Start.Text,ctx.eList()); Output.Write("{0} {1}",ctx.e().Start.Text,ctx.eList());
} }
public override void ExitInt(TParser.IntContext ctx) { public override void ExitInt(TParser.IntContext ctx) {
Console.WriteLine(ctx.INT().Symbol.Text); Output.WriteLine(ctx.INT().Symbol.Text);
} }
} }
} }
@ -274,12 +304,12 @@ void foo() {
} }
>> >>
Declare_foo() ::= <<public void foo() {Console.WriteLine("foo");}>> Declare_foo() ::= <<public void foo() {Output.WriteLine("foo");}>>
Invoke_foo() ::= "this.foo();" Invoke_foo() ::= "this.foo();"
Declare_pred() ::= <<bool pred(bool v) { Declare_pred() ::= <<bool pred(bool v) {
Console.WriteLine("eval="+v.ToString().ToLower()); Output.WriteLine("eval="+v.ToString().ToLower());
return v; return v;
} }
>> >>

View File

@ -3,6 +3,7 @@
* can be found in the LICENSE.txt file in the project root. * can be found in the LICENSE.txt file in the project root.
*/ */
using System; using System;
using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using Antlr4.Runtime; using Antlr4.Runtime;
@ -33,6 +34,8 @@ namespace Antlr4.Runtime
private ICharStream _input; private ICharStream _input;
protected readonly TextWriter Output;
private Tuple<ITokenSource, ICharStream> _tokenFactorySourcePair; private Tuple<ITokenSource, ICharStream> _tokenFactorySourcePair;
/// <summary>How to create token objects</summary> /// <summary>How to create token objects</summary>
@ -94,9 +97,12 @@ namespace Antlr4.Runtime
/// </remarks> /// </remarks>
private string _text; private string _text;
public Lexer(ICharStream input) public Lexer(ICharStream input) : this(input, Console.Out) { }
public Lexer(ICharStream input, TextWriter output)
{ {
this._input = input; this._input = input;
this.Output = output;
this._tokenFactorySourcePair = Tuple.Create((ITokenSource)this, input); this._tokenFactorySourcePair = Tuple.Create((ITokenSource)this, input);
} }

View File

@ -3,6 +3,7 @@
* can be found in the LICENSE.txt file in the project root. * can be found in the LICENSE.txt file in the project root.
*/ */
using System; using System;
using System.IO;
using System.Text; using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using Antlr4.Runtime.Atn; using Antlr4.Runtime.Atn;
@ -21,14 +22,20 @@ namespace Antlr4.Runtime
#if !PORTABLE #if !PORTABLE
public class TraceListener : IParseTreeListener public class TraceListener : IParseTreeListener
{ {
private readonly TextWriter Output;
public TraceListener(TextWriter output) {
Output = output;
}
public virtual void EnterEveryRule(ParserRuleContext ctx) public virtual void EnterEveryRule(ParserRuleContext ctx)
{ {
System.Console.Out.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) public virtual void ExitEveryRule(ParserRuleContext ctx)
{ {
System.Console.Out.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) public virtual void VisitErrorNode(IErrorNode node)
@ -39,7 +46,7 @@ namespace Antlr4.Runtime
{ {
ParserRuleContext parent = (ParserRuleContext)((IRuleNode)node.Parent).RuleContext; ParserRuleContext parent = (ParserRuleContext)((IRuleNode)node.Parent).RuleContext;
IToken token = node.Symbol; IToken token = node.Symbol;
System.Console.Out.WriteLine("consume " + token + " rule " + this._enclosing.RuleNames[parent.RuleIndex]); Output.WriteLine("consume " + token + " rule " + this._enclosing.RuleNames[parent.RuleIndex]);
} }
internal TraceListener(Parser _enclosing) internal TraceListener(Parser _enclosing)
@ -161,9 +168,14 @@ namespace Antlr4.Runtime
/// </remarks> /// </remarks>
private int _syntaxErrors; private int _syntaxErrors;
public Parser(ITokenStream input) protected readonly TextWriter Output;
public Parser(ITokenStream input) : this(input, Console.Out) { }
public Parser(ITokenStream input, TextWriter output)
{ {
TokenStream = input; TokenStream = input;
Output = output;
} }
/// <summary>reset the parser's state</summary> /// <summary>reset the parser's state</summary>
@ -1143,10 +1155,10 @@ namespace Antlr4.Runtime
{ {
if (seenOne) if (seenOne)
{ {
System.Console.Out.WriteLine(); Output.WriteLine();
} }
System.Console.Out.WriteLine("Decision " + dfa.decision + ":"); Output.WriteLine("Decision " + dfa.decision + ":");
System.Console.Out.Write(dfa.ToString(Vocabulary)); Output.Write(dfa.ToString(Vocabulary));
seenOne = true; seenOne = true;
} }
} }

View File

@ -37,6 +37,7 @@ namespace <file.genPackage> {
<endif> <endif>
<namedActions.header> <namedActions.header>
using System; using System;
using System.IO;
using System.Text; using System.Text;
using System.Diagnostics; using System.Diagnostics;
using System.Collections.Generic; using System.Collections.Generic;
@ -357,8 +358,10 @@ case <f.ruleIndex> : return <f.name>_sempred(<if(!recog.modes)>(<f.ctxType>)<end
>> >>
parser_ctor(parser) ::= << parser_ctor(parser) ::= <<
public <csIdentifier.(parser.name)>(ITokenStream input) public <csIdentifier.(parser.name)>(ITokenStream input) : this(input, Console.Out) { }
: base(input)
public <csIdentifier.(parser.name)>(ITokenStream input, TextWriter output)
: base(input, output)
{ {
Interpreter = new ParserATNSimulator(this, _ATN, decisionToDFA, sharedContextCache); Interpreter = new ParserATNSimulator(this, _ATN, decisionToDFA, sharedContextCache);
} }
@ -958,6 +961,7 @@ namespace <file.genPackage> {
<endif> <endif>
<namedActions.header> <namedActions.header>
using System; using System;
using System.IO;
using System.Text; using System.Text;
using Antlr4.Runtime; using Antlr4.Runtime;
using Antlr4.Runtime.Atn; using Antlr4.Runtime.Atn;
@ -1001,7 +1005,10 @@ public partial class <csIdentifier.(lexer.name)> : <superClass; null="Lexer"> {
<namedActions.members> <namedActions.members>
public <csIdentifier.(lexer.name)>(ICharStream input) public <csIdentifier.(lexer.name)>(ICharStream input)
: base(input) : this(input, Console.Out) { }
public <csIdentifier.(lexer.name)>(ICharStream input, TextWriter output)
: base(input, output)
{ {
Interpreter = new LexerATNSimulator(this, _ATN, decisionToDFA, sharedContextCache); Interpreter = new LexerATNSimulator(this, _ATN, decisionToDFA, sharedContextCache);
} }