Change ParserContext to take a RazorSourceDocument.
- This moves ParserContext closer to operating on a RazorSourceDocument and exposes it at the parsing layer. - Was not able to replace the `ITextDocument` property on `ParserContext` due to its current wiring. Our tokenizers rely on a single reader that iterates over the document and take turns tokenizing characters from that reader. The reader that the tokenizers pull from is also highly coupled with the parsers implementations; they end up moving the readers pointer frequently.
This commit is contained in:
parent
4056e86382
commit
3b53f04518
|
|
@ -9,14 +9,18 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
{
|
{
|
||||||
internal partial class ParserContext
|
internal partial class ParserContext
|
||||||
{
|
{
|
||||||
public ParserContext(ITextDocument source, RazorParserOptions options)
|
public ParserContext(RazorSourceDocument source, RazorParserOptions options)
|
||||||
{
|
{
|
||||||
if (source == null)
|
if (source == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(source));
|
throw new ArgumentNullException(nameof(source));
|
||||||
}
|
}
|
||||||
|
|
||||||
Source = source;
|
SourceDocument = source;
|
||||||
|
var chars = new char[source.Length];
|
||||||
|
source.CopyTo(0, chars, 0, source.Length);
|
||||||
|
|
||||||
|
Source = new SeekableTextReader(chars, source.FileName);
|
||||||
DesignTimeMode = options.DesignTime;
|
DesignTimeMode = options.DesignTime;
|
||||||
ParseOnlyLeadingDirectives = options.ParseOnlyLeadingDirectives;
|
ParseOnlyLeadingDirectives = options.ParseOnlyLeadingDirectives;
|
||||||
Builder = new SyntaxTreeBuilder();
|
Builder = new SyntaxTreeBuilder();
|
||||||
|
|
@ -29,6 +33,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
|
|
||||||
public ITextDocument Source { get; }
|
public ITextDocument Source { get; }
|
||||||
|
|
||||||
|
public RazorSourceDocument SourceDocument { get; }
|
||||||
|
|
||||||
public bool DesignTimeMode { get; }
|
public bool DesignTimeMode { get; }
|
||||||
|
|
||||||
public bool ParseOnlyLeadingDirectives { get; }
|
public bool ParseOnlyLeadingDirectives { get; }
|
||||||
|
|
|
||||||
|
|
@ -32,13 +32,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
throw new ArgumentNullException(nameof(source));
|
throw new ArgumentNullException(nameof(source));
|
||||||
}
|
}
|
||||||
|
|
||||||
var chars = new char[source.Length];
|
var context = new ParserContext(source, Options);
|
||||||
source.CopyTo(0, chars, 0, source.Length);
|
|
||||||
|
|
||||||
var reader = new SeekableTextReader(chars, source.FileName);
|
|
||||||
|
|
||||||
var context = new ParserContext(reader, Options);
|
|
||||||
|
|
||||||
var codeParser = new CSharpCodeParser(Options.Directives, context);
|
var codeParser = new CSharpCodeParser(Options.Directives, context);
|
||||||
var markupParser = new HtmlMarkupParser(context);
|
var markupParser = new HtmlMarkupParser(context);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Razor.Language.Legacy;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language
|
namespace Microsoft.AspNetCore.Razor.Language
|
||||||
{
|
{
|
||||||
public abstract class RazorSourceLineCollection
|
public abstract class RazorSourceLineCollection
|
||||||
|
|
|
||||||
|
|
@ -34,11 +34,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
|
|
||||||
internal virtual RazorSyntaxTree ParseDocument(string document, bool designTime = false)
|
internal virtual RazorSyntaxTree ParseDocument(string document, bool designTime = false)
|
||||||
{
|
{
|
||||||
var source = TestRazorSourceDocument.Create(document);
|
var source = TestRazorSourceDocument.Create(document, fileName: null);
|
||||||
var reader = new SeekableTextReader(document, filePath: null);
|
|
||||||
|
|
||||||
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
|
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
|
||||||
var context = new ParserContext(reader, options);
|
var context = new ParserContext(source, options);
|
||||||
|
|
||||||
var codeParser = new CSharpCodeParser(context);
|
var codeParser = new CSharpCodeParser(context);
|
||||||
var markupParser = new HtmlMarkupParser(context);
|
var markupParser = new HtmlMarkupParser(context);
|
||||||
|
|
@ -64,26 +62,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
|
|
||||||
internal virtual RazorSyntaxTree ParseHtmlBlock(string document, bool designTime = false)
|
internal virtual RazorSyntaxTree ParseHtmlBlock(string document, bool designTime = false)
|
||||||
{
|
{
|
||||||
var source = TestRazorSourceDocument.Create(document);
|
var source = TestRazorSourceDocument.Create(document, fileName: null);
|
||||||
|
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
|
||||||
|
var context = new ParserContext(source, options);
|
||||||
|
|
||||||
using (var reader = new SeekableTextReader(document, filePath: null))
|
var parser = new HtmlMarkupParser(context);
|
||||||
|
parser.CodeParser = new CSharpCodeParser(context)
|
||||||
{
|
{
|
||||||
var options = RazorParserOptions.Create(Array.Empty<DirectiveDescriptor>(), designTime);
|
HtmlParser = parser,
|
||||||
var context = new ParserContext(reader, options);
|
};
|
||||||
|
|
||||||
var parser = new HtmlMarkupParser(context);
|
parser.ParseBlock();
|
||||||
parser.CodeParser = new CSharpCodeParser(context)
|
|
||||||
{
|
|
||||||
HtmlParser = parser,
|
|
||||||
};
|
|
||||||
|
|
||||||
parser.ParseBlock();
|
var root = context.Builder.Build();
|
||||||
|
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
|
||||||
|
|
||||||
var root = context.Builder.Build();
|
return RazorSyntaxTree.Create(root, source, diagnostics, options);
|
||||||
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
|
|
||||||
|
|
||||||
return RazorSyntaxTree.Create(root, source, diagnostics, options);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal virtual RazorSyntaxTree ParseCodeBlock(string document, bool designTime = false)
|
internal virtual RazorSyntaxTree ParseCodeBlock(string document, bool designTime = false)
|
||||||
|
|
@ -96,26 +90,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
IEnumerable<DirectiveDescriptor> descriptors,
|
IEnumerable<DirectiveDescriptor> descriptors,
|
||||||
bool designTime)
|
bool designTime)
|
||||||
{
|
{
|
||||||
var source = TestRazorSourceDocument.Create(document);
|
var source = TestRazorSourceDocument.Create(document, fileName: null);
|
||||||
|
var options = RazorParserOptions.Create(descriptors, designTime);
|
||||||
|
var context = new ParserContext(source, options);
|
||||||
|
|
||||||
using (var reader = new SeekableTextReader(document, filePath: null))
|
var parser = new CSharpCodeParser(descriptors, context);
|
||||||
|
parser.HtmlParser = new HtmlMarkupParser(context)
|
||||||
{
|
{
|
||||||
var options = RazorParserOptions.Create(descriptors, designTime);
|
CodeParser = parser,
|
||||||
var context = new ParserContext(reader, options);
|
};
|
||||||
|
|
||||||
var parser = new CSharpCodeParser(descriptors, context);
|
parser.ParseBlock();
|
||||||
parser.HtmlParser = new HtmlMarkupParser(context)
|
|
||||||
{
|
|
||||||
CodeParser = parser,
|
|
||||||
};
|
|
||||||
|
|
||||||
parser.ParseBlock();
|
var root = context.Builder.Build();
|
||||||
|
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
|
||||||
|
|
||||||
var root = context.Builder.Build();
|
return RazorSyntaxTree.Create(root, source, diagnostics, options);
|
||||||
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
|
|
||||||
|
|
||||||
return RazorSyntaxTree.Create(root, source, diagnostics, options);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal SpanFactory CreateSpanFactory()
|
internal SpanFactory CreateSpanFactory()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue