private void GenerateTree() { // TEMP - Create a test tree VisualisedTree = new GrammarTree("sampleGrammar.txt", new Vector2(100, 100), new Vector2(150, 100)); // Get all elements of the tree var elements = new List <UIElement>(); var lineConnections = new List <UIElement>(); foreach (var node in VisualisedTree.AllNodes) { elements.AddRange(TreeNodeDrawingFactory.GenerateNodeDrawing(node, null)); var connection = TreeNodeDrawingFactory.GenerateNodeConnection(node); if (connection != null) { lineConnections.Add(connection); } } // Actually draw the tree to the canvas AddToCanvas(lineConnections); AddToCanvas(elements); // Show stats about the tree on the canvas treeIndexText.Text = "Tree 1 of 1"; nonTerminalsText.Text = $"Non-Terminals: {VisualisedTree.AllNodes.Count(n => n.Data.NodeType == GrammarNodeType.NONTERMINAL)}"; terminalsText.Text = $"Terminals: {VisualisedTree.AllNodes.Count(n => n.Data.NodeType == GrammarNodeType.TERMINAL)}"; errorsText.Text = $"Errors: {VisualisedTree.AllNodes.Count(n => n.Data.NodeType == GrammarNodeType.ERROR)}"; }
public void SimpleExpression1_Test() { GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1.tpg"); Grammar G = (Grammar)GT.Eval(); G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH; G.Directives["TinyPG"]["OutputPath"] = OUTPUTPATH; // basic checks string temp = G.PrintFirsts(); Assert.IsTrue(!String.IsNullOrEmpty(temp)); temp = G.GetOutputPath(); Assert.IsTrue(!String.IsNullOrEmpty(temp)); temp = G.PrintGrammar(); Assert.IsTrue(!String.IsNullOrEmpty(temp)); Compiler.Compiler compiler = new Compiler.Compiler(); compiler.Compile(G); Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors"); CompilerResult result = compiler.Run("5+7/3*2+(4*2)"); Assert.IsTrue(result.Output.StartsWith("Parse was successful.")); }
// // You can use the following additional attributes as you write your tests: // // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } // #endregion private GrammarTree LoadGrammar(string filename) { string grammarfile = System.IO.File.ReadAllText(filename); Scanner scanner = new Scanner(); Parser parser = new Parser(scanner); GrammarTree tree = (GrammarTree)parser.Parse(grammarfile, filename, new GrammarTree()); return(tree); }
public void SimpleExpression1_VB_Test() { GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1_vb.tpg"); Grammar G = (Grammar)GT.Eval(); G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB; Compiler.Compiler compiler = new Compiler.Compiler(); compiler.Compile(G); Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors"); CompilerResult result = compiler.Run("5+7/3*2+(4*2)"); Assert.IsTrue(result.Output.StartsWith("Parse was successful.")); }
public void SimpleExpression4_VB_Test() { GrammarTree GT = LoadGrammar(TESTFILESPATH + @"GrammarHighlighter_vb.tpg"); Grammar G = (Grammar)GT.Eval(); G.Directives.Add(new Directive("TinyPG")); G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB; Compiler.Compiler compiler = new Compiler.Compiler(); compiler.Compile(G); Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors"); CompilerResult result = compiler.Run("using System.IO;\r\n"); Assert.IsTrue(result.Output.StartsWith("Parse was successful.")); }
public void SimpleExpression2_VB_Test() { GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression2_vb.tpg"); Grammar G = (Grammar)GT.Eval(); G.Directives.Add(new Directive("TinyPG")); G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB; Compiler.Compiler compiler = new Compiler.Compiler(); compiler.Compile(G); Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors"); CompilerResult result = compiler.Run("5+8/4*2+(4*2)"); Assert.IsTrue(Convert.ToInt32(result.Value) == 17); }
//Source textbox events private void tbParse_Source_TextChanged(object sender, EventArgs e) { if (tbSource_Editing) { return; } timApplyParseSyntaxColoring.Enabled = false; ssMain_Status.Text = "Parsing..."; int duration; //Parse duration = Environment.TickCount; _ide.SourceText = tbParse_Source.Text; _parser.Parse(); _gt = _ide.GrammarTree; duration = Environment.TickCount - duration; ssMain_Status.Text = "Parse (" + duration + ")"; //Compile duration = Environment.TickCount; tbCompiled.BeginUpdate(); _compiler.Compile(); tbCompiled.EndUpdate(); duration = Environment.TickCount - duration; ssMain_Status.Text += ", compile (" + duration + ")"; //Populate functions var compile_success = (_gt.ParseErrors.Count == 0) && (_gt.Functions.Count > 0); cbRun_Function.Items.Clear(); tsDebug_Functions.Items.Clear(); if (compile_success) { var et = _ide.ExecutionTree; foreach (var fun in et.Functions) { if (fun.Value.ArgumentsCount == 0) { cbRun_Function.Items.Add(fun.Key); tsDebug_Functions.Items.Add(fun.Key); } } if (cbRun_Function.Items.Count > 0) { cbRun_Function.SelectedIndex = 0; } if (tsDebug_Functions.Items.Count > 0) { tsDebug_Functions.SelectedIndex = 0; } } //Enable/disable buttons tsDebug_Run.Enabled = compile_success; tsDebug_StepInto.Enabled = compile_success; tsDebug_StepOver.Enabled = compile_success; butRun_Run.Enabled = compile_success; //Populate errors lbErrors.Items.Clear(); foreach (ParseError err in _gt.ParseErrors) { lbErrors.Items.Add(err); } if (_gt.ParseErrors.Count > 0) { lbErrors.Items.Add("Recoverable parsing errors occurred"); } else { lbErrors.Items.Add("Great parsing success"); } foreach (ParseError err in _ide.ExecutionTree.ParseErrors) { lbErrors.Items.Add(err); } if (_gt.ParseErrors.Count > 0) { lbErrors.Items.Add("Recoverable compiling errors occurred"); } else { lbErrors.Items.Add("Great compiling success"); } //Enable/disable buttons butAssembly.Enabled = (_gt.Functions.Count > 0); timApplyParseSyntaxColoring.Enabled = true; if (!_parse_source_undoing) { //Start timers so that we don't create an undo action every time a button is pushed timParseUndoIdleWait.Enabled = false; timParseUndoIdleWait.Enabled = true; timParseUndoMaxWait.Enabled = true; } }
//Source textbox events private void tbParse_Source_TextChanged(object sender, EventArgs e) { if (tbSource_Editing) return; timApplyParseSyntaxColoring.Enabled = false; ssMain_Status.Text = "Parsing..."; int duration; //Parse duration = Environment.TickCount; _ide.SourceText = tbParse_Source.Text; _parser.Parse(); _gt = _ide.GrammarTree; duration = Environment.TickCount - duration; ssMain_Status.Text = "Parse (" + duration + ")"; //Compile duration = Environment.TickCount; tbCompiled.BeginUpdate(); _compiler.Compile(); tbCompiled.EndUpdate(); duration = Environment.TickCount - duration; ssMain_Status.Text += ", compile (" + duration + ")"; //Populate functions var compile_success = (_gt.ParseErrors.Count == 0) && (_gt.Functions.Count > 0); cbRun_Function.Items.Clear(); tsDebug_Functions.Items.Clear(); if (compile_success) { var et = _ide.ExecutionTree; foreach (var fun in et.Functions) if (fun.Value.ArgumentsCount == 0) { cbRun_Function.Items.Add(fun.Key); tsDebug_Functions.Items.Add(fun.Key); } if (cbRun_Function.Items.Count > 0) cbRun_Function.SelectedIndex = 0; if (tsDebug_Functions.Items.Count > 0) tsDebug_Functions.SelectedIndex = 0; } //Enable/disable buttons tsDebug_Run.Enabled = compile_success; tsDebug_StepInto.Enabled = compile_success; tsDebug_StepOver.Enabled = compile_success; butRun_Run.Enabled = compile_success; //Populate errors lbErrors.Items.Clear(); foreach (ParseError err in _gt.ParseErrors) lbErrors.Items.Add(err); if (_gt.ParseErrors.Count > 0) lbErrors.Items.Add("Recoverable parsing errors occurred"); else lbErrors.Items.Add("Great parsing success"); foreach (ParseError err in _ide.ExecutionTree.ParseErrors) lbErrors.Items.Add(err); if (_gt.ParseErrors.Count > 0) lbErrors.Items.Add("Recoverable compiling errors occurred"); else lbErrors.Items.Add("Great compiling success"); //Enable/disable buttons butAssembly.Enabled = (_gt.Functions.Count > 0); timApplyParseSyntaxColoring.Enabled = true; if (!_parse_source_undoing) { //Start timers so that we don't create an undo action every time a button is pushed timParseUndoIdleWait.Enabled = false; timParseUndoIdleWait.Enabled = true; timParseUndoMaxWait.Enabled = true; } }