public Boolean RunTest() { try { GlobalMemory.Clear(); var sStream = new ANTLRStringStream(input); var lexer = new SGLLexer(sStream); var tStream = new CommonTokenStream(lexer); // Parsing var parser = new SGLParser(tStream); var t = (CommonTree)parser.main().Tree; // Printing tree Console.WriteLine("; " + t.ToStringTree()); // TreeWalking var treeStream = new CommonTreeNodeStream(t); var tw = new SGLTreeWalker(treeStream, true); AbstractNode returned = tw.main(); returned.Evaluate(); if (debug) { realOutput = GlobalMemory.Instance.DebugString; } else { realOutput = GlobalMemory.Instance.StoryboardCode.ToString(); } // comparison realOutput = realOutput.Trim(); output.Trim(); if (output.Equals(realOutput)) { return(true); } else { return(false); } } catch (CompilerException ce) { Console.WriteLine(ce.GetExceptionAsString()); return(false); } catch (Exception ex) { Console.WriteLine("Es ist ein Fehler aufgetreten."); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return(false); } }
private CommonTreeNodeStream GenerateTree(CommonTokenStream input) { Stopwatch timeNeeded = timeRecording ? new Stopwatch() : null; if (timeRecording) { timeNeeded.Start(); } // Treewalker: Compiling the abstract syntax tree to storyboard code var parser = new SGLParser(input); var ast = (CommonTree)parser.main().Tree; treeString = ast.ToStringTree(); var astStream = new CommonTreeNodeStream(ast); // Saving the methods for the treewalker /*this.methods = parser.methods; * this.objectMethods = parser.objectMethods;*/ // Print tree Console.WriteLine("Tree:" + ast.ToStringTree()); if (timeRecording) { timeNeeded.Stop(); Console.WriteLine("Time needed for converting the tokens into a tree: " + timeNeeded.ElapsedMilliseconds + " ms (" + timeNeeded.Elapsed + ")"); } return(astStream); }
public Boolean RunTest() { try { ANTLRStringStream sStream = new ANTLRStringStream(input); SGLLexer lexer = new SGLLexer(sStream); CommonTokenStream tStream = new CommonTokenStream(lexer); // Parsing SGLParser parser = new SGLParser(tStream); CommonTree t = (CommonTree)parser.compilationUnit().Tree; // Printing tree Console.WriteLine("; " + t.ToStringTree()); // TreeWalking CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t); SGLTreeWalker tw = new SGLTreeWalker(treeStream); SGLNode returned = tw.compilationUnit(); returned.Evaluate(); realOutput = tw.GetStoryboardCode().ToString(); // comparison realOutput = realOutput.Trim(); output.Trim(); if (output.Equals(realOutput)) { return true; } else { return false; } } catch (SGLCompilerException ce) { if (ce.ErrorType.Equals("Antlr.Parser")) { Console.WriteLine("Error (wrong syntax) on " + ce.Message); } else { Console.WriteLine("Error (" + ce.ErrorType + ") on line " + ce.Line + ": " + ce.Message); } return false; } catch (Exception ex) { Console.WriteLine("Es ist ein Fehler aufgetreten."); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); return false; } }
public Boolean Run() { // run lexer on input var lexer = new SGLLexer(new ANTLRStringStream(Input)); var tokenStream = new CommonTokenStream(lexer); // run parser on tokens var parser = new SGLParser(tokenStream); var ast = (CommonTree)parser.main().Tree; // maybe test the tree if (testTree) { if (!ast.ToStringTree().Trim().Equals(ExpectedTree.Trim())) { result += "Test " + name + "failed! Tree comparison failed!"; result += "Expected tree: " + ExpectedTree.Trim(); result += "Given tree: " + ast.ToStringTree().Trim(); return(false); } } var astStream = new CommonTreeNodeStream(ast); // run walker on AST GlobalMemory.Clear(); var treewalker = new SGLTreeWalker(astStream, true); treewalker.main().Evaluate(); //String output = treewalker.GetStoryboardCode().ToString(); if (testOutput) { String output = GlobalMemory.Instance.StoryboardCode.ToString(); if (!output.Trim().Equals(ExpectedOutput.Trim())) { result += "Test " + name + "failed! Output comparison failed!"; result += "Expected output: \r\n" + ExpectedOutput.Trim(); result += "Given output: \r\n" + output.Trim(); return(false); } } if (testDebug) { String debug = GlobalMemory.Instance.StoryboardCode.ToString(); if (!debug.Trim().Equals(ExpectedDebug.Trim())) { result += "Test " + name + "failed! Debug comparison failed!"; result += "Expected debug: \r\n" + ExpectedDebug.Trim(); result += "Given debug: \r\n" + debug.Trim(); return(false); } } return(true); }
public static void Main(string[] args) { ANTLRStringStream Input = new ANTLRStringStream("int x = 3"); SGLLexer Lexer = new SGLLexer(Input); CommonTokenStream Tokens = new CommonTokenStream(Lexer); SGLParser parser = new SGLParser(Tokens); CommonTree t = (CommonTree)parser.compilationUnit().Tree; CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t); SGLTreeWalker tw = new SGLTreeWalker(treeStream); tw.compilationUnit(); }
private CommonTreeNodeStream GenerateTree(CommonTokenStream input) { Stopwatch timeNeeded = timeRecording ? new Stopwatch() : null; if (timeRecording) timeNeeded.Start(); // Treewalker: Compiling the abstract syntax tree to storyboard code var parser = new SGLParser(input); var ast = (CommonTree) parser.main().Tree; treeString = ast.ToStringTree(); var astStream = new CommonTreeNodeStream(ast); // Saving the methods for the treewalker /*this.methods = parser.methods; this.objectMethods = parser.objectMethods;*/ // Print tree Console.WriteLine("Tree:" + ast.ToStringTree()); if (timeRecording) { timeNeeded.Stop(); Console.WriteLine("Time needed for converting the tokens into a tree: " + timeNeeded.ElapsedMilliseconds + " ms (" + timeNeeded.Elapsed + ")"); } return astStream; }