-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[CSharp] Fix for #3510 -- accept grammars with either "->Channel(HIDDEN)" or "->channel(HIDDEN)" for CSharp #3547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Antlr4.Runtime; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks redundant (all files from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
| 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> |
| 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 |
| 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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 111 222 aaa 333 BBB 444 |
| 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 | ||
|
|
There was a problem hiding this comment.
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):
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 toruntime-testsuite.