diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs index f3ee43d6dd..f3b6e22919 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/CSharpCodeParser.cs @@ -893,7 +893,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } } - protected bool TryParseDirective(in SyntaxListBuilder builder, IEnumerable whitespace, CSharpTransitionSyntax transition, string directive) + protected bool TryParseDirective(in SyntaxListBuilder builder, IReadOnlyList whitespace, CSharpTransitionSyntax transition, string directive) { if (_directiveParserMap.TryGetValue(directive, out var handler)) { @@ -1679,7 +1679,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } } - private bool TryParseKeyword(in SyntaxListBuilder builder, IEnumerable whitespace, CSharpTransitionSyntax transition) + private bool TryParseKeyword(in SyntaxListBuilder builder, IReadOnlyList whitespace, CSharpTransitionSyntax transition) { var result = CSharpTokenizer.GetTokenKeyword(CurrentToken); Debug.Assert(CurrentToken.Kind == SyntaxKind.Keyword && result.HasValue); @@ -2387,7 +2387,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } } - private IEnumerable SkipToNextImportantToken(in SyntaxListBuilder builder) + private IReadOnlyList SkipToNextImportantToken(in SyntaxListBuilder builder) { while (!EndOfFile) { @@ -2406,7 +2406,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy return whitespace; } } - return Enumerable.Empty(); + return Array.Empty(); } private void DefaultSpanContextConfig(SpanContextBuilder spanContext) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs index c62be1c46e..8c416986ec 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.cs @@ -1085,9 +1085,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } } - private bool TryParseAttributeName(out IEnumerable nameTokens) + private bool TryParseAttributeName(out IReadOnlyList nameTokens) { - nameTokens = Enumerable.Empty(); + nameTokens = Array.Empty(); // // We are currently here // If we encounter a transition (@) here, it can be parsed as CSharp or Markup depending on the feature flag. diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TokenizerBackedParser.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TokenizerBackedParser.cs index e6f80228fb..2cf825d005 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TokenizerBackedParser.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/TokenizerBackedParser.cs @@ -266,14 +266,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy return true; } - protected internal IEnumerable ReadWhile(params SyntaxKind[] types) + protected internal IReadOnlyList ReadWhile(Func condition) { - return ReadWhile(token => types.Any(expected => expected == token.Kind)); - } + if (!EnsureCurrent() || !condition(CurrentToken)) + { + return Array.Empty(); + } + + var result = new List(); + do + { + result.Add(CurrentToken); + NextToken(); + } + while (EnsureCurrent() && condition(CurrentToken)); - protected internal IEnumerable ReadWhile(Func condition) - { - return ReadWhileLazy(condition).ToList(); + return result; } protected bool AtIdentifier(bool allowKeywords) @@ -283,17 +291,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy (allowKeywords && Language.IsKeyword(CurrentToken))); } - // Don't open this to sub classes because it's lazy but it looks eager. - // You have to advance the Enumerable to read the next characters. - internal IEnumerable ReadWhileLazy(Func condition) - { - while (EnsureCurrent() && condition(CurrentToken)) - { - yield return CurrentToken; - NextToken(); - } - } - protected RazorCommentBlockSyntax ParseRazorComment() { if (!Language.KnowsTokenType(KnownTokenType.CommentStart) || @@ -430,13 +427,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy protected internal void AcceptWhile(Func condition) { - Accept(ReadWhileLazy(condition)); + Accept(ReadWhile(condition)); } - protected internal void Accept(IEnumerable tokens) + protected internal void Accept(IReadOnlyList tokens) { - foreach (var token in tokens) + for(int i = 0; i < tokens.Count; i++) { + var token = tokens[i]; Accept(token); } }