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 parseResult = JsonParser.Start(source); var ast = JsonParserAstWalkers.Start(parseResult); Console.WriteLine("Pretty print: " + ast.ToString(PrettyPrintOptions.DebugIndent | PrettyPrintOptions.MissingNodes)); }
public ValueModel(SourceSnapshot source, ParsedValue <string> value) { var location = new Location(source, value.Span); Value = value.ValueOrDefault; Type = TypesOfValue.Path; // FIXME: Дичь какая-то. Зачем это? Comments = new CommentBlockModel(location); }
static void Test(string text) { var session = new ParseSession(JsonParser.Start, compilerMessages: new ConsoleCompilerMessages()); var source = new SourceSnapshot(text); var parseResult = session.Parse(source); var parseTree = parseResult.CreateParseTree(); Console.WriteLine("Pretty print: " + parseTree); }
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); } } }
static void Main(string[] args) { var fileName = args.Length > 0 ? args[0] : @"../../Program.cs"; var preParser = new PreParser(); // preprocessor parser SourceSnapshot src; // parsed source using (var reader = new StreamReader(fileName)) { src = new SourceSnapshot(reader.ReadToEnd()); } var preAst = preParser.Parse(src); var preprocessed = Preprocessor.Run(preAst.Value, new string[0]); // transform AST with preprocessor var parser = new Parser(); // C# parser var parsingResult = parser.Parse(preprocessed.Source); var csAST = parsingResult.Value; Console.WriteLine("Usings: "); foreach (var usingDirective in csAST.UsingDirectives) { var ns = usingDirective as UsingDirective.Namespace; if (ns != null) { Console.WriteLine(ns.name); } var alias = usingDirective as UsingDirective.Alias; if (alias != null) { Console.WriteLine("{0} = {1}", alias.name, alias.alias); } } Console.WriteLine("\nClasses: "); ShowMembers(csAST.Members); }
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 IParseResult Run([NotNull] string code, [CanBeNull] string gold = null, int completionStartPos = -1, string completionPrefix = null, RecoveryAlgorithm recoveryAlgorithm = RecoveryAlgorithm.Smart) { var source = new SourceSnapshot(code); if (Language.StartRule == null) { return(null); } try { var parseSession = new ParseSession(Language.StartRule, compositeGrammar: Language.CompositeGrammar, completionPrefix: completionPrefix, completionStartPos: completionStartPos, parseToEndOfString: true, dynamicExtensions: DynamicExtensions, statistics: Statistics); switch (recoveryAlgorithm) { case RecoveryAlgorithm.Smart: parseSession.OnRecovery = ParseSession.SmartRecovery; break; case RecoveryAlgorithm.Panic: parseSession.OnRecovery = ParseSession.PanicRecovery; break; case RecoveryAlgorithm.FirstError: parseSession.OnRecovery = ParseSession.FirsrErrorRecovery; break; } var parseResult = parseSession.Parse(source); this.Exception = null; return(parseResult); } catch (Exception ex) { this.Exception = ex; 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; } }
private HashSet <Completion> CompleteWord(int pos, IAst astRoot, IParseResult parseResult, SourceSnapshot source, ITextSnapshot compiledSnapshot) { var completionList = new HashSet <Completion>(); var nspan = new NSpan(pos, pos); if (IsInsideComment(parseResult, nspan)) { return(completionList); } var visitor = new FindNodeAstVisitor(nspan); visitor.Visit(astRoot); var stack = visitor.Stack .Where(ast => !(ast is IEnumerable)) // Skip IAstList .ToArray(); if (ShouldComplete(stack, pos, compiledSnapshot)) { GetCompletions(completionList, stack); AddKeywordCompletions(stack, completionList); } return(completionList); }
public IParseResult Run([NotNull] string code, [CanBeNull] string gold = null, int completionStartPos = -1, string completionPrefix = null, RecoveryAlgorithm recoveryAlgorithm = RecoveryAlgorithm.Smart) { var source = new SourceSnapshot(code); if (Language.StartRule == null) return null; try { var parseSession = new ParseSession(Language.StartRule, compositeGrammar: Language.CompositeGrammar, completionPrefix: completionPrefix, completionStartPos: completionStartPos, parseToEndOfString: true, dynamicExtensions: DynamicExtensions, statistics: Statistics); switch (recoveryAlgorithm) { case RecoveryAlgorithm.Smart: parseSession.OnRecovery = ParseSession.SmartRecovery; break; case RecoveryAlgorithm.Panic: parseSession.OnRecovery = ParseSession.PanicRecovery; break; case RecoveryAlgorithm.FirstError: parseSession.OnRecovery = ParseSession.FirsrErrorRecovery; break; } var parseResult = parseSession.Parse(source); this.Exception = null; return parseResult; } 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; } }