public void Run(Repl repl, string input) { var path = Environment.CurrentDirectory; path = path + Path.DirectorySeparatorChar + "Generated"; var grammars = _repl._workspace.AllDocuments().Where(d => d.FullPath.EndsWith(".g4")).ToList(); var old = Environment.CurrentDirectory; try { Environment.CurrentDirectory = path; Assembly asm = Assembly.LoadFile(path + "/bin/Debug/netcoreapp3.1/Test.dll"); Type[] types = asm.GetTypes(); Type type = asm.GetType("Easy.Program"); var methods = type.GetMethods(); MethodInfo methodInfo = type.GetMethod("Parse"); string txt = input; if (input == null) { txt = repl.input_output_stack.Pop(); } object[] parm = new object[] { txt }; DateTime before = DateTime.Now; var res = methodInfo.Invoke(null, parm); DateTime after = DateTime.Now; System.Console.Error.WriteLine("Time to parse: " + (after - before)); var tree = res as IParseTree; var t2 = tree as ParserRuleContext; var m2 = type.GetProperty("Parser"); object[] p2 = new object[0]; var r2 = m2.GetValue(null, p2); var m3 = type.GetProperty("Lexer"); object[] p3 = new object[0]; var r3 = m3.GetValue(null, p3); var m4 = type.GetProperty("TokenStream"); object[] p4 = new object[0]; var r4 = m4.GetValue(null, p4); System.Console.Error.WriteLine("# tokens per sec = " + (r4 as ITokenStream).Size / (after - before).TotalSeconds); Environment.CurrentDirectory = old; var serializeOptions = new JsonSerializerOptions(); serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter()); serializeOptions.WriteIndented = false; var tuple = new AntlrJson.ParsingResultSet() { Text = txt, FileName = "stdin", Stream = r4 as ITokenStream, Nodes = new IParseTree[] { t2 }, Parser = r2 as Parser, Lexer = r3 as Lexer }; string js1 = JsonSerializer.Serialize(tuple, serializeOptions); repl.input_output_stack.Push(js1); } finally { Environment.CurrentDirectory = old; } }
public void Execute(Repl repl, ReplParser.JsonContext tree, bool piped) { var lines = repl.input_output_stack.Pop(); var doc = repl.stack.Peek(); var pr = ParsingResultsFactory.Create(doc); var lexer = pr.Lexer; var parser = pr.Parser; var serializeOptions = new JsonSerializerOptions(); serializeOptions.Converters.Add(new AntlrJson.ParseTreeConverter()); serializeOptions.WriteIndented = false; var parse_info = JsonSerializer.Deserialize <AntlrJson.ParsingResultSet>(lines, serializeOptions); var nodes = parse_info.Nodes; foreach (var node in nodes) { ParseTreeWalker.Default.Walk(new JsonWalk(parser), node); } }
public void Run(Repl repl, string[] parameters) { // Create a directory containing a C# project with grammars. var path = Environment.CurrentDirectory; path = path + Path.DirectorySeparatorChar + "Generated"; var grammars = _repl._workspace.AllDocuments().Where(d => d.FullPath.EndsWith(".g4")).ToList(); var old = Environment.CurrentDirectory; try { Directory.CreateDirectory(path); Environment.CurrentDirectory = path; { StringBuilder sb = new StringBuilder(); sb.AppendLine(@" <Project Sdk=""Microsoft.NET.Sdk"" > <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup>" ); foreach (var grammar in grammars) { sb.AppendLine("<Antlr4 Include=\"" + Path.GetFileName(grammar.FullPath) + "\" />"); } sb.AppendLine(@"</ItemGroup> <ItemGroup> <PackageReference Include=""Antlr4.Runtime.Standard"" Version =""4.8.0"" /> <PackageReference Include=""Antlr4BuildTasks"" Version = ""8.7"" /> <PackageReference Include= ""AntlrTreeEditing"" Version = ""1.9"" /> </ItemGroup> <PropertyGroup> <RestoreProjectStyle>PackageReference</RestoreProjectStyle> </PropertyGroup> <PropertyGroup Condition=""'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"" > <NoWarn>1701;1702;3021</NoWarn> </PropertyGroup> </Project>"); var fn_csproj = path + Path.DirectorySeparatorChar + "Test.csproj"; System.IO.File.WriteAllText(fn_csproj, sb.ToString()); } // Copy all files in workspace to temporary directory. foreach (var doc in _repl._workspace.AllDocuments()) { var fn = Path.GetFileName(doc.FullPath); var code = doc.Code; System.IO.File.WriteAllText(fn, code); } // Add in Main.cs to get parse. { StringBuilder sb = new StringBuilder(); sb.Append(@" namespace Easy { using Antlr4.Runtime; using Antlr4.Runtime.Tree; using System.Text; using System.Runtime.CompilerServices; public class Program { public static Parser Parser { get; set; } public static Lexer Lexer { get; set; } public static ITokenStream TokenStream { get; set; } public static IParseTree Tree { get; set; } public static IParseTree Parse(string input) { var str = new AntlrInputStream(input); var lexer = new "); var lexer_name = ""; var parser_name = ""; var lexer = grammars.Where(d => d.FullPath.EndsWith("Lexer.g4")).ToList(); var parser = grammars.Where(d => d.FullPath.EndsWith("Parser.g4")).ToList(); if (lexer.Count == 1) { lexer_name = Path.GetFileName(lexer.First().FullPath.Replace(".g4", "")); parser_name = Path.GetFileName(parser.First().FullPath.Replace(".g4", "")); } else if (lexer.Count == 0) { // Combined. var combined = grammars.Where(d => d.FullPath.EndsWith(".g4")).ToList(); if (combined.Count == 1) { var combined_name = Path.GetFileName(combined.First().FullPath).Replace(".g4", ""); lexer_name = combined_name + "Lexer"; parser_name = combined_name + "Parser"; } } sb.Append(lexer_name); sb.Append(@"(str); Lexer = lexer; var tokens = new CommonTokenStream(lexer); TokenStream = tokens; var parser = new "); sb.Append(parser_name); sb.Append(@"(tokens); Parser = parser; var tree = parser."); sb.Append(parameters[0]); sb.AppendLine(@"(); Tree = tree; return tree; } } }"); System.IO.File.WriteAllText("Program.cs", sb.ToString()); } List <string> arguments = new List <string>(); arguments.Add("build"); ProcessStartInfo startInfo = new ProcessStartInfo("dotnet", JoinArguments(arguments)) { UseShellExecute = false, CreateNoWindow = true, //RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, }; Process process = new Process(); process.StartInfo = startInfo; process.ErrorDataReceived += HandleOutputDataReceived; process.OutputDataReceived += HandleOutputDataReceived; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); var success = process.ExitCode == 0; if (!success) { throw new Exception("Build failed."); } } finally { Environment.CurrentDirectory = old; } }
public Generate(Repl repl) { _repl = repl; }
public Grun(Repl repl) { _repl = repl; }
public Docs(Repl repl) { _repl = repl; }