diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs index 65e578b5ce..8455c9e197 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/DefaultDirectiveSyntaxTreePass.cs @@ -2,9 +2,8 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; +using System.Collections.Generic; using Microsoft.AspNetCore.Razor.Language.Extensions; -using Microsoft.AspNetCore.Razor.Language.Legacy; using Microsoft.AspNetCore.Razor.Language.Syntax; namespace Microsoft.AspNetCore.Razor.Language @@ -33,19 +32,37 @@ namespace Microsoft.AspNetCore.Razor.Language { private int _nestedLevel; private RazorSyntaxTree _syntaxTree; + private List _diagnostics; public NestedSectionVerifier(RazorSyntaxTree syntaxTree) { _syntaxTree = syntaxTree; + _diagnostics = new List(syntaxTree.Diagnostics); } public RazorSyntaxTree Verify() { var root = Visit(_syntaxTree.Root); - var rewrittenTree = new DefaultRazorSyntaxTree(root, _syntaxTree.Source, _syntaxTree.Diagnostics, _syntaxTree.Options); + var rewrittenTree = new DefaultRazorSyntaxTree(root, _syntaxTree.Source, _diagnostics, _syntaxTree.Options); return rewrittenTree; } + public override SyntaxNode Visit(SyntaxNode node) + { + try + { + return base.Visit(node); + } + catch (InsufficientExecutionStackException) + { + // We're very close to reaching the stack limit. Let's not go any deeper. + // It's okay to not show nested section errors in deeply nested cases instead of crashing. + _diagnostics.Add(RazorDiagnosticFactory.CreateRewriter_InsufficientStack(SourceSpan.Undefined)); + + return node; + } + } + public override SyntaxNode VisitRazorDirective(RazorDirectiveSyntax node) { if (_nestedLevel > 0) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.Legacy.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.Legacy.cs deleted file mode 100644 index e858000fac..0000000000 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Legacy/HtmlMarkupParser.Legacy.cs +++ /dev/null @@ -1,1366 +0,0 @@ -// 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. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using Microsoft.AspNetCore.Razor.Language.Syntax.InternalSyntax; - -namespace Microsoft.AspNetCore.Razor.Language.Legacy -{ - internal partial class HtmlMarkupParser : TokenizerBackedParser - { - private SourceLocation _lastTagStart = SourceLocation.Zero; - private SyntaxToken _bufferedOpenAngle; - - private bool CaseSensitive { get; set; } - - private StringComparison Comparison - { - get { return CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; } - } - - // Special tags include " As we will be treating this as a comment ending, there is no need to handle this case at all. - // 2.2.3 "--!>" - // 2.3 nor end with the string "" - - if (CurrentToken.Kind != SyntaxKind.DoubleHyphen) - { - return false; - } - - // Check condition 2.1 - if (NextIs(SyntaxKind.CloseAngle) || NextIs(next => IsHyphen(next) && NextIs(SyntaxKind.CloseAngle))) - { - return false; - } - - // Check condition 2.2 - var isValidComment = false; - LookaheadUntil((token, prevTokens) => - { - if (token.Kind == SyntaxKind.DoubleHyphen) - { - if (NextIs(SyntaxKind.CloseAngle)) - { - // Check condition 2.3: We're at the end of a comment. Check to make sure the text ending is allowed. - isValidComment = !IsCommentContentEndingInvalid(prevTokens); - return true; - } - else if (NextIs(ns => IsHyphen(ns) && NextIs(SyntaxKind.CloseAngle))) - { - // Check condition 2.3: we're at the end of a comment, which has an extra dash. - // Need to treat the dash as part of the content and check the ending. - // However, that case would have already been checked as part of check from 2.2.1 which - // would already fail this iteration and we wouldn't get here - isValidComment = true; - return true; - } - else if (NextIs(ns => ns.Kind == SyntaxKind.Bang && NextIs(SyntaxKind.CloseAngle))) - { - // This is condition 2.2.3 - isValidComment = false; - return true; - } - } - else if (token.Kind == SyntaxKind.OpenAngle) - { - // Checking condition 2.2.1 - if (NextIs(ns => ns.Kind == SyntaxKind.Bang && NextIs(SyntaxKind.DoubleHyphen))) - { - isValidComment = false; - return true; - } - } - - return false; - }); - - return isValidComment; - } - - private bool TryParseCData(in SyntaxListBuilder builder) - { - if (CurrentToken.Kind == SyntaxKind.Text && string.Equals(CurrentToken.Content, "cdata", StringComparison.OrdinalIgnoreCase)) - { - if (AcceptAndMoveNext()) - { - if (CurrentToken.Kind == SyntaxKind.LeftBracket) - { - return LegacyAcceptTokenUntilAll(builder, SyntaxKind.RightBracket, SyntaxKind.RightBracket, SyntaxKind.CloseAngle); - } - } - } - - return false; - } - - private bool TryParseXmlPI(in SyntaxListBuilder builder) - { - // Accept "?" - Assert(SyntaxKind.QuestionMark); - AcceptAndMoveNext(); - return LegacyAcceptTokenUntilAll(builder, SyntaxKind.QuestionMark, SyntaxKind.CloseAngle); - } - - private void SkipToEndScriptAndParseCode(in SyntaxListBuilder builder, AcceptedCharactersInternal endTagAcceptedCharacters = AcceptedCharactersInternal.Any) - { - // Special case for