internal virtual RazorSyntaxTree ParseDocument(RazorLanguageVersion version, string document, IEnumerable <DirectiveDescriptor> directives, bool designTime = false, RazorParserFeatureFlags featureFlags = null, string fileKind = null) { directives ??= Array.Empty <DirectiveDescriptor>(); var source = TestRazorSourceDocument.Create(document, filePath: null, relativePath: null, normalizeNewLines: true); var options = CreateParserOptions(version, directives, designTime, featureFlags, fileKind); var context = new ParserContext(source, options); var codeParser = new CSharpCodeParser(directives, context); var markupParser = new HtmlMarkupParser(context); codeParser.HtmlParser = markupParser; markupParser.CodeParser = codeParser; var root = markupParser.ParseDocument().CreateRed(); var diagnostics = context.ErrorSink.Errors; var codeDocument = RazorCodeDocument.Create(source); var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options); codeDocument.SetSyntaxTree(syntaxTree); var defaultDirectivePass = new DefaultDirectiveSyntaxTreePass(); syntaxTree = defaultDirectivePass.Execute(codeDocument, syntaxTree); return(syntaxTree); }
public static RazorSyntaxTree Rewrite(RazorSyntaxTree syntaxTree, string tagHelperPrefix, IEnumerable <TagHelperDescriptor> descriptors) { var errorSink = new ErrorSink(); syntaxTree = MarkupElementRewriter.AddMarkupElements(syntaxTree); var rewriter = new Rewriter( syntaxTree.Source, tagHelperPrefix, descriptors, syntaxTree.Options.FeatureFlags, errorSink); var rewritten = rewriter.Visit(syntaxTree.Root); var errorList = new List <RazorDiagnostic>(); errorList.AddRange(errorSink.Errors); errorList.AddRange(descriptors.SelectMany(d => d.GetAllDiagnostics())); var diagnostics = CombineErrors(syntaxTree.Diagnostics, errorList).OrderBy(error => error.Span.AbsoluteIndex); var newSyntaxTree = RazorSyntaxTree.Create(rewritten, syntaxTree.Source, diagnostics, syntaxTree.Options); newSyntaxTree = MarkupElementRewriter.RemoveMarkupElements(newSyntaxTree); return(newSyntaxTree); }
public void Moves_Whitespace_Preceeding_ExpressionBlock_To_Parent_Block() { // Arrange var content = @" <div> @result </div> <div> @(result) </div>"; var parsed = ParseDocument( RazorLanguageVersion.Latest, content, Array.Empty <DirectiveDescriptor>()); var rewriter = new WhitespaceRewriter(); // Act var rewritten = rewriter.Visit(parsed.Root); // Assert var rewrittenTree = RazorSyntaxTree.Create(rewritten, parsed.Source, parsed.Diagnostics, parsed.Options); BaselineTest(rewrittenTree); }
internal virtual RazorSyntaxTree ParseDocument(string document, IEnumerable <DirectiveDescriptor> directives, bool designTime = false) { directives = directives ?? Array.Empty <DirectiveDescriptor>(); var source = TestRazorSourceDocument.Create(document, fileName: null); var options = CreateParserOptions(directives, designTime); var context = new ParserContext(source, options); var codeParser = new CSharpCodeParser(directives, context); var markupParser = new HtmlMarkupParser(context); codeParser.HtmlParser = markupParser; markupParser.CodeParser = codeParser; markupParser.ParseDocument(); var root = context.Builder.Build(); var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error)); var codeDocument = RazorCodeDocument.Create(source); var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options); codeDocument.SetSyntaxTree(syntaxTree); var defaultDirectivePass = new DefaultDirectiveSyntaxTreePass(); syntaxTree = defaultDirectivePass.Execute(codeDocument, syntaxTree); return(syntaxTree); }
internal virtual RazorSyntaxTree ParseCodeBlock( string document, IEnumerable <DirectiveDescriptor> directives, bool designTime) { directives = directives ?? Array.Empty <DirectiveDescriptor>(); var source = TestRazorSourceDocument.Create(document, fileName: null); var options = CreateParserOptions(directives, designTime); var context = new ParserContext(source, options); var parser = new CSharpCodeParser(directives, context); parser.HtmlParser = new HtmlMarkupParser(context) { CodeParser = parser, }; parser.ParseBlock(); var root = context.Builder.Build(); var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error)); return(RazorSyntaxTree.Create(root, source, diagnostics, options)); }
internal virtual RazorSyntaxTree ParseCodeBlock( RazorLanguageVersion version, string document, IEnumerable <DirectiveDescriptor> directives, bool designTime) { directives = directives ?? Array.Empty <DirectiveDescriptor>(); var source = TestRazorSourceDocument.Create(document, filePath: null, relativePath: null, normalizeNewLines: true); var options = CreateParserOptions(version, directives, designTime); var context = new ParserContext(source, options); var codeParser = new CSharpCodeParser(directives, context); var markupParser = new HtmlMarkupParser(context); codeParser.HtmlParser = markupParser; markupParser.CodeParser = codeParser; var root = codeParser.ParseBlock().CreateRed(); var diagnostics = context.ErrorSink.Errors; var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options); return(syntaxTree); }
public static RazorSyntaxTree RemoveMarkupElements(RazorSyntaxTree syntaxTree) { var rewriter = new RemoveMarkupElementRewriter(); var rewrittenRoot = rewriter.Visit(syntaxTree.Root); var newSyntaxTree = RazorSyntaxTree.Create(rewrittenRoot, syntaxTree.Source, syntaxTree.Diagnostics, syntaxTree.Options); return(newSyntaxTree); }
public (PartialParseResultInternal, RazorSyntaxTree) Parse(SourceChange change) { var result = GetPartialParseResult(change); // Remember if this was provisionally accepted for next partial parse. _lastResultProvisional = (result & PartialParseResultInternal.Provisional) == PartialParseResultInternal.Provisional; var newSyntaxTree = RazorSyntaxTree.Create(ModifiedSyntaxTreeRoot, OriginalSyntaxTree.Source, OriginalSyntaxTree.Diagnostics, OriginalSyntaxTree.Options); return(result, newSyntaxTree); }
internal static IReadOnlyList <ClassifiedSpanInternal> GetClassifiedSpans(Block root, string filePath) { // We don't care about the options and diagnostic here. var syntaxTree = RazorSyntaxTree.Create( root, TestRazorSourceDocument.Create(filePath: filePath), Array.Empty <RazorDiagnostic>(), RazorParserOptions.CreateDefault()); return(syntaxTree.GetClassifiedSpans()); }
private void VerifyPartialParseTree(TestParserManager manager, string content, string expectedCode = null) { if (expectedCode != null) { // Verify if the syntax tree represents the expected input. var syntaxTreeContent = manager.PartialParsingSyntaxTreeRoot.ToFullString(); Assert.Contains(expectedCode, syntaxTreeContent, StringComparison.Ordinal); } var sourceDocument = TestRazorSourceDocument.Create(content); var syntaxTree = RazorSyntaxTree.Create(manager.PartialParsingSyntaxTreeRoot, sourceDocument, manager.CurrentSyntaxTree.Diagnostics, manager.CurrentSyntaxTree.Options); BaselineTest(syntaxTree); }
private void RunPartialParseTest(TestEdit edit, PartialParseResultInternal additionalFlags = 0) { var templateEngine = CreateProjectEngine(); var document = TestRazorCodeDocument.Create(edit.OldSnapshot.GetText()); templateEngine.Engine.Process(document); var syntaxTree = document.GetSyntaxTree(); var parser = new RazorSyntaxTreePartialParser(syntaxTree); var(result, _) = parser.Parse(edit.Change); Assert.Equal(PartialParseResultInternal.Accepted | additionalFlags, result); var newSource = TestRazorSourceDocument.Create(edit.NewSnapshot.GetText()); var newSyntaxTree = RazorSyntaxTree.Create(parser.ModifiedSyntaxTreeRoot, newSource, parser.OriginalSyntaxTree.Diagnostics, parser.OriginalSyntaxTree.Options); BaselineTest(newSyntaxTree); }
public RazorSyntaxTree Execute(RazorCodeDocument codeDocument, RazorSyntaxTree syntaxTree) { if (codeDocument == null) { throw new ArgumentNullException(nameof(codeDocument)); } if (syntaxTree == null) { throw new ArgumentNullException(nameof(syntaxTree)); } var whitespaceRewriter = new WhitespaceRewriter(); var rewritten = whitespaceRewriter.Visit(syntaxTree.Root); var rewrittenSyntaxTree = RazorSyntaxTree.Create(rewritten, syntaxTree.Source, syntaxTree.Diagnostics, syntaxTree.Options); return(rewrittenSyntaxTree); }
public virtual RazorSyntaxTree Parse(RazorSourceDocument source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var context = new ParserContext(source, Options); var codeParser = new CSharpCodeParser(Options.Directives, context); var markupParser = new HtmlMarkupParser(context); codeParser.HtmlParser = markupParser; markupParser.CodeParser = codeParser; var diagnostics = context.ErrorSink.Errors; var root = markupParser.ParseDocument().CreateRed(); return(RazorSyntaxTree.Create(root, source, diagnostics, Options)); }
internal virtual RazorSyntaxTree ParseHtmlBlock(RazorLanguageVersion version, string document, IEnumerable <DirectiveDescriptor> directives, bool designTime = false) { directives = directives ?? Array.Empty <DirectiveDescriptor>(); var source = TestRazorSourceDocument.Create(document, filePath: null); var options = CreateParserOptions(version, directives, designTime); var context = new ParserContext(source, options); var parser = new HtmlMarkupParser(context); parser.CodeParser = new CSharpCodeParser(directives, context) { HtmlParser = parser, }; parser.ParseBlock(); var root = context.Builder.Build(); var diagnostics = context.ErrorSink.Errors; return(RazorSyntaxTree.Create(root, source, diagnostics, options)); }
public virtual RazorSyntaxTree Parse(RazorSourceDocument source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var context = new ParserContext(source, Options); var codeParser = new CSharpCodeParser(Options.Directives, context); var markupParser = new HtmlMarkupParser(context); codeParser.HtmlParser = markupParser; markupParser.CodeParser = codeParser; markupParser.ParseDocument(); var root = context.Builder.Build(); // Temporary code while we're still using legacy diagnostics in the SyntaxTree. var diagnostics = context.ErrorSink.Errors.Select(error => RazorDiagnostic.Create(error)); return(RazorSyntaxTree.Create(root, source, diagnostics, Options)); }