Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions runtime/CSharp/tests/issue-3510/ChannelTest.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
grammar ChannelTest;

prog : INT+ EOF;
INT : [0-9]+;
AAA : [a-z]+ -> channel(HIDDEN); // Note--lowercase "channel"
BBB : [A-Z]+ -> Channel(HIDDEN); // Note--uppercase "Channel"
WS : [ \t\r\n] -> skip;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be written using a descriptor (https://github.com/antlr/antlr4/tree/dev/runtime-testsuite/resources/org/antlr/v4/test/runtime/descriptors/LexerExec) to cover all runtimes but not C# only.

Also, please add tests on other commands if you can (see lexer-commands description):

  • skip
  • more
  • popMode
  • pushMode
  • type
  • channel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, was wondering what those were for.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, we have similar tests but in tool-testsuite: https://github.com/antlr/antlr4/blob/dev/tool-testsuite/test/org/antlr/v4/test/tool/TestLexerActions.java But actually they should be moved to runtime-testsuite.

18 changes: 18 additions & 0 deletions runtime/CSharp/tests/issue-3510/ErrorListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Antlr4.Runtime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks redundant (all files from runtime/CSharp/tests/issue-3510) since everything can be checked using a descriptor-based test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Will move this over to a descriptor.

using Antlr4.Runtime.Misc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class ErrorListener<S> : ConsoleErrorListener<S>
{
public bool had_error;

public override void SyntaxError(TextWriter output, IRecognizer recognizer, S offendingSymbol, int line,
int col, string msg, RecognitionException e)
{
had_error = true;
base.SyntaxError(output, recognizer, offendingSymbol, line, col, msg, e);
}
}
90 changes: 90 additions & 0 deletions runtime/CSharp/tests/issue-3510/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Antlr4.Runtime;
using System;
using System.Linq;
using System.Text;

public class Program
{
static void Main(string[] args)
{
bool show_tree = false;
bool show_tokens = false;
string file_name = null;
string input = null;
for (int i = 0; i < args.Length; ++i)
{
if (args[i].Equals("-tokens"))
{
show_tokens = true;
continue;
}
else if (args[i].Equals("-tree"))
{
show_tree = true;
continue;
}
else if (args[i].Equals("-input"))
input = args[i];
else if (args[i].Equals("-file"))
file_name = args[++i];
}
ICharStream str = null;
if (input == null && file_name == null)
{
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = System.Console.Read()) != -1)
{
sb.Append((char)ch);
}
input = sb.ToString();
str = CharStreams.fromString(input);
}
else if (input != null)
{
str = CharStreams.fromString(input);
}
else if (file_name != null)
{
str = CharStreams.fromPath(file_name);
}
var lexer = new ChannelTestLexer(str);
if (show_tokens)
{
StringBuilder new_s = new StringBuilder();
for (int i = 0; ; ++i)
{
var ro_token = lexer.NextToken();
var token = (CommonToken)ro_token;
token.TokenIndex = i;
new_s.AppendLine(token.ToString());
if (token.Type == Antlr4.Runtime.TokenConstants.EOF)
break;
}
System.Console.Error.WriteLine(new_s.ToString());
}
lexer.Reset();
var tokens = new CommonTokenStream(lexer);
var parser = new ChannelTestParser(tokens);
var listener_lexer = new ErrorListener<int>();
var listener_parser = new ErrorListener<IToken>();
lexer.AddErrorListener(listener_lexer);
parser.AddErrorListener(listener_parser);
parser.Profile = true;
var tree = parser.prog();
if (listener_lexer.had_error || listener_parser.had_error)
{
System.Console.Error.WriteLine("parse failed.");
}
else
{
System.Console.Error.WriteLine("parse succeeded.");
}
if (show_tree)
{
System.Console.Error.WriteLine(tree.ToStringTree());
}
System.Console.Out.WriteLine(String.Join(", ", parser.ParseInfo.getDecisionInfo().Select(d => d.ToString())));
System.Environment.Exit(listener_lexer.had_error || listener_parser.had_error ? 1 : 0);
}
}
26 changes: 26 additions & 0 deletions runtime/CSharp/tests/issue-3510/Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk" >
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<Antlr4 Include="ChannelTest.g4" />
</ItemGroup>

<PropertyGroup>
<AntlrToolPath>../../../../tool/target/antlr4-*-SNAPSHOT-complete.jar</AntlrToolPath>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Antlr4.csproj" />
<PackageReference Include="Antlr4BuildTasks" Version = "8.17" PrivateAssets="all"/>
</ItemGroup>

<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'" >
<NoWarn>1701;1702;3021</NoWarn>
</PropertyGroup>
</Project>
31 changes: 31 additions & 0 deletions runtime/CSharp/tests/issue-3510/Test.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31019.35
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "Test.csproj", "{FD11E8CC-1631-4FF3-9B44-F10084562311}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Antlr4", "..\..\src\Antlr4.csproj", "{A60B5000-4473-4D00-85C4-C3A4B469F608}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FD11E8CC-1631-4FF3-9B44-F10084562311}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD11E8CC-1631-4FF3-9B44-F10084562311}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD11E8CC-1631-4FF3-9B44-F10084562311}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD11E8CC-1631-4FF3-9B44-F10084562311}.Release|Any CPU.Build.0 = Release|Any CPU
{A60B5000-4473-4D00-85C4-C3A4B469F608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A60B5000-4473-4D00-85C4-C3A4B469F608}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A60B5000-4473-4D00-85C4-C3A4B469F608}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A60B5000-4473-4D00-85C4-C3A4B469F608}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2FFB66F7-2552-4F2B-B97E-77B5F8743ED4}
EndGlobalSection
EndGlobal
105 changes: 105 additions & 0 deletions runtime/CSharp/tests/issue-3510/TreeOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class TreeOutput
{
private static int changed = 0;
private static bool first_time = true;

public static StringBuilder OutputTree(IParseTree tree, Lexer lexer, Parser parser, CommonTokenStream stream)
{
changed = 0;
first_time = true;
var sb = new StringBuilder();
ParenthesizedAST(tree, sb, lexer, parser, stream);
return sb;
}

private static void ParenthesizedAST(IParseTree tree, StringBuilder sb, Lexer lexer, Parser parser, CommonTokenStream stream, int level = 0)
{
if (tree as TerminalNodeImpl != null)
{
TerminalNodeImpl tok = tree as TerminalNodeImpl;
Interval interval = tok.SourceInterval;
IList<IToken> inter = null;
if (tok.Symbol.TokenIndex >= 0)
inter = stream?.GetHiddenTokensToLeft(tok.Symbol.TokenIndex);
if (inter != null)
foreach (var t in inter)
{
var ty = tok.Symbol.Type;
var name = lexer.Vocabulary.GetSymbolicName(ty);
StartLine(sb, level);
sb.AppendLine("(" + name + " text = " + PerformEscapes(t.Text) + " " + lexer.ChannelNames[t.Channel]);
}
{
var ty = tok.Symbol.Type;
var name = lexer.Vocabulary.GetSymbolicName(ty);
StartLine(sb, level);
sb.AppendLine("( " + name + " i =" + tree.SourceInterval.a
+ " txt =" + PerformEscapes(tree.GetText())
+ " tt =" + tok.Symbol.Type
+ " " + lexer.ChannelNames[tok.Symbol.Channel]);
}
}
else
{
var x = tree as RuleContext;
var ri = x.RuleIndex;
var name = parser.RuleNames[ri];
StartLine(sb, level);
sb.Append("( " + name);
sb.AppendLine();
}
for (int i = 0; i<tree.ChildCount; ++i)
{
var c = tree.GetChild(i);
ParenthesizedAST(c, sb, lexer, parser, stream, level + 1);
}
if (level == 0)
{
for (int k = 0; k < 1 + changed - level; ++k) sb.Append(") ");
sb.AppendLine();
changed = 0;
}
}

private static void StartLine(StringBuilder sb, int level = 0)
{
if (changed - level >= 0)
{
if (!first_time)
{
for (int j = 0; j < level; ++j) sb.Append(" ");
for (int k = 0; k < 1 + changed - level; ++k) sb.Append(") ");
sb.AppendLine();
}
changed = 0;
first_time = false;
}
changed = level;
for (int j = 0; j < level; ++j) sb.Append(" ");
}

private static string ToLiteral(string input)
{
using (var writer = new StringWriter())
{
var literal = input;
literal = literal.Replace("\\", "\\\\");
return literal;
}
}

public static string PerformEscapes(string s)
{
StringBuilder new_s = new StringBuilder();
new_s.Append(ToLiteral(s));
return new_s.ToString();
}
}
1 change: 1 addition & 0 deletions runtime/CSharp/tests/issue-3510/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
111 222 aaa 333 BBB 444
11 changes: 11 additions & 0 deletions runtime/CSharp/tests/issue-3510/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/bash

dotnet restore
dotnet build
dotnet run -file input.txt
if [[ "$?" != "0" ]]
then
echo "Issue 3510 test failed."
exit 1
fi

Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,9 @@ LexerSkipCommand() ::= "Skip();"
LexerMoreCommand() ::= "More();"
LexerPopModeCommand() ::= "PopMode();"

LexerTypeCommand(arg, grammar) ::= "_type = <tokenType.(arg)>;"
LexerChannelCommand(arg, grammar) ::= "_channel = <channelName.(arg)>;"
LexerModeCommand(arg, grammar) ::= "_mode = <modeName.(arg)>;"
LexerTypeCommand(arg, grammar) ::= "Type = <tokenType.(arg)>;"
LexerChannelCommand(arg, grammar) ::= "Channel = <channelName.(arg)>;"
LexerModeCommand(arg, grammar) ::= "CurrentMode = <modeName.(arg)>;"
LexerPushModeCommand(arg, grammar) ::= "PushMode(<modeName.(arg)>);"

ActionText(t) ::= "<t.text>"
Expand Down