public MainForm() { InitializeComponent(); IsDirty = false; compiler = null; GrammarFile = null; this.Disposed += new EventHandler(MainForm_Disposed); }
public bool BuildCode(Grammar grammar, TinyPG.Compiler.Compiler compiler) { this.output.AppendLine("Building code..."); compiler.Compile(grammar); if (!compiler.IsCompiled) { foreach (string err in compiler.Errors) { this.output.AppendLine(err); } this.output.AppendLine("Compilation contains errors, could not compile."); } new GeneratedFilesWriter(grammar).Generate(false); return(compiler.IsCompiled); }
private void CompileGrammar() { if (string.IsNullOrEmpty(GrammarFile)) { SaveGrammarAs(); } if (string.IsNullOrEmpty(GrammarFile)) { return; } compiler = new TinyPG.Compiler.Compiler(); StringBuilder output = new StringBuilder(); // clear tree tvParsetree.Nodes.Clear(); Program prog = new Program(ManageParseError, output); DateTime starttimer = DateTime.Now; grammar = prog.ParseGrammar(textEditor.Text, GrammarFile); if (grammar != null) { grammar.SourceFilename = GrammarFile; SetHighlighterLanguage(grammar.Directives["TinyPG"]["Language"]); if (prog.BuildCode(grammar, compiler)) { TimeSpan span = DateTime.Now.Subtract(starttimer); output.AppendLine("Compilation successful in " + span.TotalMilliseconds + "ms."); } } textOutput.Text = output.ToString(); textOutput.Select(textOutput.Text.Length, 0); textOutput.ScrollToCaret(); }
private void CompileGrammar() { DateTime starttimer = DateTime.Now; if (string.IsNullOrEmpty(GrammarFile)) SaveGrammarAs(); if (string.IsNullOrEmpty(GrammarFile)) return; compiler = new TinyPG.Compiler.Compiler(); StringBuilder output = new StringBuilder(); // clear tree tvParsetree.Nodes.Clear(); string input = textEditor.Text; Scanner scanner = new Scanner(); Parser parser = new Parser(scanner); ParseTree tree = parser.Parse(input, GrammarFile, new GrammarTree()); if (tree.Errors.Count > 0) { foreach (ParseError error in tree.Errors) output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message)); output.AppendLine("Syntax errors in grammar found."); if (tree.Errors.Count > 0) textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1); } else { grammar = (Grammar)tree.Eval(); grammar.Preprocess(); if (tree.Errors.Count == 0) { output.AppendLine(grammar.PrintGrammar()); output.AppendLine(grammar.PrintFirsts()); output.AppendLine("Parse successful!\r\n"); } } if (grammar != null) { SetHighlighterLanguage(grammar.Directives["TinyPG"]["Language"]); if (tree.Errors.Count > 0) { foreach (ParseError error in tree.Errors) output.AppendLine(string.Format("({0},{1}): {2}", error.Line, error.Column, error.Message)); output.AppendLine("Semantic errors in grammar found."); if (tree.Errors.Count > 0) textEditor.Select(tree.Errors[0].Position, tree.Errors[0].Length > 0 ? tree.Errors[0].Length : 1); } else { output.AppendLine("Building code..."); compiler.Compile(grammar); if (!compiler.IsCompiled) { foreach (string err in compiler.Errors) output.AppendLine(err); output.AppendLine("Compilation contains errors, could not compile."); } else { TimeSpan span = DateTime.Now.Subtract(starttimer); output.AppendLine("Compilation successfull in " + span.TotalMilliseconds + "ms."); } } } textOutput.Text = output.ToString(); textOutput.Select(textOutput.Text.Length, 0); textOutput.ScrollToCaret(); if (grammar != null && tree.Errors.Count == 0) { ICodeGenerator generator; string language = grammar.Directives["TinyPG"]["Language"]; foreach (Directive d in grammar.Directives) { generator = CodeGeneratorFactory.CreateGenerator(d.Name, language); if (generator != null && d.ContainsKey("FileName")) generator.FileName = d["FileName"]; if (generator != null && d["Generate"].ToLower() == "true") File.WriteAllText(grammar.GetOutputPath() + generator.FileName, generator.Generate(grammar, false)); } } }
private void CompileGrammar() { if (string.IsNullOrEmpty(GrammarFile)) SaveGrammarAs(); if (string.IsNullOrEmpty(GrammarFile)) return; compiler = new TinyPG.Compiler.Compiler(); StringBuilder output = new StringBuilder(); // clear tree tvParsetree.Nodes.Clear(); Program prog = new Program(ManageParseError, output); DateTime starttimer = DateTime.Now; grammar = prog.ParseGrammar(textEditor.Text, GrammarFile); if (grammar != null) { SetHighlighterLanguage(grammar.Directives["TinyPG"]["Language"]); if (prog.BuildCode(grammar, compiler)) { TimeSpan span = DateTime.Now.Subtract(starttimer); output.AppendLine("Compilation successful in " + span.TotalMilliseconds + "ms."); } } textOutput.Text = output.ToString(); textOutput.Select(textOutput.Text.Length, 0); textOutput.ScrollToCaret(); }