public IEnumerable <string> Generate(string before, string after) { var result = new List <string>(); //parse code var beforeAst = Parse(before); var afterAast = Parse(after); //run tree edit distance var zss = new PythonZss(beforeAst, afterAast); var editDistance = zss.Compute(); //get primary edits var rootAndNonRootEdits = WitnessFunctions.SplitEditsByRootsAndNonRoots(editDistance); //replace insert and delete by update var unparser = new Unparser(); foreach (var edit in rootAndNonRootEdits.Item1) { if (edit is Update) { result.Add("Update " + unparser.Unparse(edit.TargetNode) + " to " + unparser.Unparse(edit.ModifiedNode)); } else if (edit is Insert) { result.Add("Insert " + unparser.Unparse(edit.ModifiedNode)); } } //for each edit, create a hint return(result); }
public IEnumerable <string> Apply(Transformation transformation, string program) { var unparser = new Unparser(); var result = transformation.GetSynthesizedProgram().Invoke(CreateInputState(program)) as IEnumerable <PythonNode>; return(result == null ? new List <string>() : result.Select(x => unparser.Unparse(x))); }
static void Main(string[] args) { var grammar = new UDLGrammar(); //Console.WriteLine(grammar.GetNonTerminalsAsText()); //Console.WriteLine(); //Console.WriteLine(grammar.GetNonTerminalsAsText(omitBoundMembers: true)); var parser = new Parser(grammar); ParseTree parseTree = parser.Parse(File.ReadAllText(path), path); Unparser unparser = new Unparser(grammar); Directory.CreateDirectory("unparse_logs"); var stream = File.Create(@"unparse_logs\unparsed_text"); unparser.Unparse(parseTree.Root.AstNode).WriteToStream(stream, unparser); //string str = unparser.Unparse(parseTree.Root.AstNode).AsString(unparser); //Console.WriteLine(str); }
// static string path2 = @"..\..\..\Sarcasm.UnitTest\Test files\Binary1.expr"; static void Main(string[] args) { var stopwatch = Stopwatch.StartNew(); stopwatch.Start(); var grammar = new MiniPLG.GrammarP(); ShowTimeAndRestart(stopwatch, "Creation of grammar"); var parser = MultiParser.Create(grammar); ShowTimeAndRestart(stopwatch, "Creation of parser"); var parseTree = parser.Parse(File.ReadAllText(path), path); ShowTimeAndRestart(stopwatch, "Parsing"); var astRootValue = parseTree.RootAstValue; // string generatedCode = new MiniPLC.CSharpGenerator().Generate(astRootValue); // string generatedCode = new MiniPLC.CppGenerator().Generate(astRootValue); #if false var jsonGrammar = new JsonGrammar(); Unparser universalUnparser = new Unparser(jsonGrammar); var universalParser = MultiParser.Create(jsonGrammar); string text = universalUnparser.Unparse(astRootValue).AsText(universalUnparser); var foo = universalUnparser.Unparse(astRootValue).Select(utoken => utoken.GetDecoration()).ToList(); var parseTree2 = universalParser.Parse(text); var astRootValue2 = parseTree2.RootAstValue; string text2 = universalUnparser.Unparse(astRootValue2).AsText(universalUnparser); bool eq = text == text2; #endif Unparser unparser = new Unparser(grammar); ShowTimeAndRestart(stopwatch, "Creation of unparser"); //var utokens = unparser.Unparse(astRootValue).ToList(); //ShowTimeAndRestart(stopwatch, "Unparsing to utokens"); //unparser.EnableParallelProcessing = false; //string unparsedText = unparser.Unparse(astRootValue).AsText(unparser); //var document = parseTree.GetDocument(); //string unparsedText = unparser.Unparse(document).AsText(unparser); //ShowTimeAndRestart(stopwatch, "Converting utokens to string"); unparser.EnableParallelProcessing = false; //foreach (Utoken utoken in unparser.Unparse(astRootValue)) // Console.WriteLine(utoken.ToString()); unparser.Unparse(astRootValue).ConsumeAll(); ShowTimeAndRestart(stopwatch, "Sequential unparsing to string"); unparser.EnableParallelProcessing = true; //foreach (Utoken utoken in unparser.Unparse(astRootValue)) // Console.WriteLine(utoken.ToString()); unparser.Unparse(astRootValue).ConsumeAll(); ShowTimeAndRestart(stopwatch, "Parallel unparsing to string"); //var utokensReverse = unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ToList(); //ShowTimeAndRestart(stopwatch, "Reverse unparsing to utokens"); unparser.EnableParallelProcessing = false; unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll(); ShowTimeAndRestart(stopwatch, "Reverse sequential unparsing to string"); unparser.EnableParallelProcessing = true; unparser.Unparse(astRootValue, Unparser.Direction.RightToLeft).ConsumeAll(); ShowTimeAndRestart(stopwatch, "Reverse parallel unparsing to string"); //stopwatch.Stop(); //Console.WriteLine(stopwatch.ElapsedMilliseconds); //stopwatch.Start(); //var parser2 = MultiParser.Create(grammar); //stopwatch.Stop(); //Console.WriteLine(stopwatch.ElapsedMilliseconds); //var parseTree2 = parser.Parse(File.ReadAllText(path2), path2, grammar.B.Expression); //var parseTree3 = parser.Parse(File.ReadAllText(path2), path2, (NonTerminal)grammar.B.Expression); //MiniPL.DomainDefinitions.Expression astValue2 = parseTree2.RootAstValue; //Unparser unparser = new Unparser(grammar); //Directory.CreateDirectory("unparse_logs"); //var stream = File.Create(@"unparse_logs\unparsed_text"); //unparser.Unparse(parseTree.Root.AstNode).WriteToStream(stream, unparser); ////string str = unparser.Unparse(parseTree.Root.AstNode).AsString(unparser); ////Console.WriteLine(str); }