static json_value parse_json_file(string txt) { lexer.AllocateTarget(txt); Action <string, string, int, int> insert = (string x, string y, int a, int b) => { parser.Insert(x, y); if (parser.Error()) { throw new Exception($"[COMPILER] Parser error! L:{a}, C:{b}"); } while (parser.Reduce()) { var l = parser.LatestReduce(); l.Action(l); #if false Console.Write(l.Production.PadLeft(8) + " => "); Console.WriteLine(string.Join(" ", l.Childs.Select(z => z.Production))); Console.Write(l.Production.PadLeft(8) + " => "); Console.WriteLine(string.Join(" ", l.Childs.Select(z => z.Contents))); #endif parser.Insert(x, y); if (parser.Error()) { throw new Exception($"[COMPILER] Parser error! L:{a}, C:{b}"); } } }; while (lexer.Valid()) { var tk = lexer.Next(); insert(tk.Item1, tk.Item2, tk.Item3, tk.Item4); } if (parser.Error()) { throw new Exception(); } insert("$", "$", -1, -1); var tree = parser.Tree; #if false PrintTree(tree.root, "", true); #endif var root = tree.root.UserContents as json_value; var build = new StringBuilder(); root.print(build, true); #if false File.WriteAllText("export.json", build.ToString()); #endif return(root); }
private void bPGT_Click(object sender, EventArgs e) { if (scanner == null) { rtbPGS.AppendText("Create scanner instance before testing!\r\n"); return; } if (srparser == null) { rtbPGS.AppendText("Create parser instance before testing!\r\n"); return; } foreach (var line in rtbPGTEST.Lines) { rtbPGS.AppendText(" ------ TEST: " + line + "\r\n"); srparser.Clear(); Action <string, string> insert = (string x, string y) => { srparser.Insert(x, y); if (srparser.Error()) { rtbPGS.AppendText("PARSING ERROR" + "\r\n"); } while (srparser.Reduce()) { rtbPGS.AppendText(srparser.Stack() + "\r\n"); var l = srparser.LatestReduce(); rtbPGS.AppendText(l.Produnction.PadLeft(8) + " => "); rtbPGS.AppendText(string.Join(" ", l.Childs.Select(z => z.Produnction)) + "\r\n"); rtbPGS.AppendText(l.Produnction.PadLeft(8) + " => "); rtbPGS.AppendText(string.Join(" ", l.Childs.Select(z => z.Contents)) + "\r\n"); srparser.Insert(x, y); if (srparser.Error()) { rtbPGS.AppendText("PARSING ERROR" + "\r\n"); } } rtbPGS.AppendText(srparser.Stack() + "\r\n"); }; scanner.AllocateTarget(line); try { while (scanner.Valid()) { var ss = scanner.Next(); insert(ss.Item1, ss.Item2); } insert("$", "$"); var x = srparser.Tree; } catch (Exception ex) { rtbPGS.AppendText("Error!\r\nCheck test case!\r\n"); } rtbPGS.AppendText(" ------ END TEST ------\r\n"); } }