antlr/runtime/CSharp/tests/issue-2693/Program.cs

91 lines
2.8 KiB
C#
Raw Normal View History

Restructure C# runtime and fix Issue #2693 (#3057) * Fixes for https://github.com/antlr/antlr4/issues/2693 * Adding script for comparing Java and C# profile output. * Update lexer grammar. Tightening up the grammar rules. Still Java9. * Tighten up grammar. * Adding in new base classes for C# runtime tests for profiling. * Complete test for Profile = true. Update ProfileDescriptor to now parse and output profile. The grammar is asm8080 from grammars-v4, tightened up. The input is the example provided there, truncated to included fewer lines as that causes a null-ptr crash with the older runtime. I verified by modifying the .csproj in /tmp. * Restructuring the Antlr C# runtime so that it is consistent with all other runtimes, a source directory (now antlr4/runtime/CSharp/src), and a test directory (antlr4/runtime/CSharp). In the test area, I added a test for profiling in issue-2593. This test requires the Antlr tool and Antlr C# tool to be build. The path is assumed in a relative path to the test, ../../../../tool/target/antlr4-*-SNAPSHOT-complete.jar, with globbing performed. The test simply checks the return result, output not important. There are no changes to the runtime C# source files other than placing them under src/. Several other build files were changed to reflect the new location of the Antlr C# runtime. I updated the instructions for users on how to build the runtime, including information on checking the environment--now explicitly specified here so people know what to install!
2021-02-02 09:51:41 +08:00
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 asm8080Lexer(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 asm8080Parser(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);
}
}