public void CanParseShader(string testFile, string knownGoodFile) { // Preprocess test code using CppNet. var sourceCode = Preprocess(File.ReadAllText(testFile), testFile); // Parse test code. var sourceSnapshot = new SourceSnapshot(sourceCode); var parserHost = new ParserHost(); var compilationUnit = HlslGrammar.CompilationUnit(sourceSnapshot, parserHost); // Check for parse errors. if (!compilationUnit.IsSuccess) { throw new Exception(string.Join(Environment.NewLine, compilationUnit.GetErrors().Select(x => string.Format("Line {0}, Col {1}: {2}{3}{4}", x.Location.StartLineColumn.Line, x.Location.StartLineColumn.Column, x.Message, Environment.NewLine, x.Location.Source.GetSourceLine(x.Location.StartPos).GetText())))); } Assert.That(compilationUnit.IsSuccess, Is.True); // Get pretty-printed version of parse tree. var parseTree = compilationUnit.CreateParseTree(); var parsedCode = parseTree.ToString(); // Compare pretty-printed parse tree with known good version // (if known good version exists). if (File.Exists(knownGoodFile)) { var knownGoodCode = File.ReadAllText(knownGoodFile).Replace("\r\n", "\n"); Assert.That(parsedCode.Replace("\r\n", "\n"), Is.EqualTo(knownGoodCode)); } }
public ParseResult Run([NotNull] string code, [CanBeNull] string gold) { if (_parserHost == null) { _parserHost = new ParserHost(); _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules); } var source = new SourceSnapshot(code); if (StartRule == null) { return(null); } var timer = System.Diagnostics.Stopwatch.StartNew(); try { var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule); this.Exception = null; this.TestTime = timer.Elapsed; return(res); } catch (Exception ex) { this.Exception = ex; this.TestTime = timer.Elapsed; return(null); } }
public ParseResult Run([NotNull] string code, [CanBeNull] string gold, RecoveryStrategy recoveryStrategy) { if (_parserHost == null) { _parserHost = new ParserHost(); _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules); } var source = new SourceSnapshot(code); if (StartRule == null) { return(null); } try { var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, recoveryStrategy); this.Exception = null; return(res); } catch (Exception ex) { this.Exception = ex; return(null); } }
static void Test(string text) { var source = new SourceSnapshot(text); var parserHost = new ParserHost(); var parseResult = JsonParser.Start(source, parserHost); var ast = JsonParserAstWalkers.Start(parseResult); Console.WriteLine("Pretty print: " + ast.ToString(PrettyPrintOptions.DebugIndent | PrettyPrintOptions.MissingNodes)); }
static void Test(string text) { var source = new SourceSnapshot(text); var parserHost = new ParserHost(); var parseResult = JsonParser.Start(source, parserHost); if (parseResult.IsSuccess) { var ast = JsonParserAst.Start.Create(parseResult); Console.WriteLine("Pretty print: " + ast); } else { foreach (var error in parseResult.GetErrors()) { var pos = error.Location.StartLineColumn; Console.WriteLine("{0}:{1}: {2}", pos.Line, pos.Column, error.Message); } } }
public void CanParse(string testFile, string formatedFile) { var sourceCode = File.ReadAllText(testFile); // Parse test code. var sourceSnapshot = new SourceSnapshot(sourceCode); var parserHost = new ParserHost(); var compilationUnit = RustGrammar.CompilationUnit(sourceSnapshot, parserHost); // Check for parse errors. if (!compilationUnit.IsSuccess) { var message = string.Join(Environment.NewLine, compilationUnit.GetErrors().Select(x => { return(string.Format("Line {0}, Col {1}: {2}{3}{4}{3}{5}", x.Location.StartLineColumn.Line, x.Location.StartLineColumn.Column, x.Message, Environment.NewLine, x.Location.Source.GetSourceLine(x.Location.StartPos).GetText().Split(new[] { Environment.NewLine }, StringSplitOptions.None).First(), "^".PadLeft(x.Location.StartLineColumn.Column))); })); Debug.WriteLine(message); } Assert.That(compilationUnit.IsSuccess, Is.True); // Get pretty-printed version of parse tree. var parseTree = compilationUnit.CreateParseTree(); var parsedCode = parseTree.ToString(); // Compare pretty-printed parse tree with known good version // (if known good version exists). if (File.Exists(formatedFile)) { var formatedCode = File.ReadAllText(formatedFile); Assert.That(parsedCode, Is.EqualTo(formatedCode)); } }
public ParseResult Run([NotNull] string code, [CanBeNull] string gold, RecoveryStrategy recoveryStrategy) { if (_parserHost == null) { _parserHost = new ParserHost(); _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules); } var source = new SourceSnapshot(code); if (StartRule == null) return null; try { var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, recoveryStrategy); this.Exception = null; return res; } catch (Exception ex) { this.Exception = ex; return null; } }
public ParseResult Run([NotNull] string code, [CanBeNull] string gold) { if (_parserHost == null) { _parserHost = new ParserHost(); _compositeGrammar = _parserHost.MakeCompositeGrammar(SynatxModules); } var source = new SourceSnapshot(code); if (StartRule == null) return null; var timer = System.Diagnostics.Stopwatch.StartNew(); try { var res = _parserHost.DoParsing(source, _compositeGrammar, StartRule, parseToEndOfString: true); this.Exception = null; this.TestTime = timer.Elapsed; return res; } catch (Exception ex) { this.Exception = ex; this.TestTime = timer.Elapsed; return null; } }