Moved TokenizerBackerParser out of LegacyRazorDiagnostic

This commit is contained in:
Ajay Bhargav Baaskaran 2017-12-20 11:14:04 -08:00
parent 7d4fb5dcab
commit b3be0ebb91
7 changed files with 79 additions and 140 deletions

View File

@ -168,11 +168,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if (EndOfFile && ((mode & BalancingModes.NoErrorOnFailure) != BalancingModes.NoErrorOnFailure))
{
Context.ErrorSink.OnError(
start,
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF(
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(start, contentLength: 1 /* { OR } */),
Language.GetSample(left),
Language.GetSample(right)),
length: 1 /* { OR } */);
Language.GetSample(right)));
}
return Balance(mode, left, right, start);
@ -218,11 +217,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if ((mode & BalancingModes.NoErrorOnFailure) != BalancingModes.NoErrorOnFailure)
{
Context.ErrorSink.OnError(
start,
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF(
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(start, contentLength: 1 /* { OR } */),
Language.GetSample(left),
Language.GetSample(right)),
length: 1 /* { OR } */);
Language.GetSample(right)));
}
if ((mode & BalancingModes.BacktrackOnFailure) == BalancingModes.BacktrackOnFailure)
{
@ -435,44 +433,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
return false;
}
protected internal bool Required(TSymbolType expected, bool errorIfNotFound, Func<string, string> errorBase)
{
var found = At(expected);
if (!found && errorIfNotFound)
{
string error;
if (Language.IsNewLine(CurrentSymbol))
{
error = LegacyResources.ErrorComponent_Newline;
}
else if (Language.IsWhiteSpace(CurrentSymbol))
{
error = LegacyResources.ErrorComponent_Whitespace;
}
else if (EndOfFile)
{
error = LegacyResources.ErrorComponent_EndOfFile;
}
else
{
error = LegacyResources.FormatErrorComponent_Character(CurrentSymbol.Content);
}
int errorLength;
if (CurrentSymbol == null || CurrentSymbol.Content == null)
{
errorLength = 1;
}
else
{
errorLength = Math.Max(CurrentSymbol.Content.Length, 1);
}
Context.ErrorSink.OnError(CurrentStart, errorBase(error), errorLength);
}
return found;
}
protected bool EnsureCurrent()
{
if (CurrentSymbol == null)
@ -634,9 +594,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{
errorReported = true;
Context.ErrorSink.OnError(
start,
LegacyResources.ParseError_RazorComment_Not_Terminated,
length: 2 /* @* */);
RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
new SourceSpan(start, contentLength: 2 /* @* */)));
}
else
{
@ -649,9 +608,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{
errorReported = true;
Context.ErrorSink.OnError(
start,
LegacyResources.ParseError_RazorComment_Not_Terminated,
length: 2 /* @* */);
RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
new SourceSpan(start, contentLength: 2 /* @* */)));
}
}
else

View File

@ -310,6 +310,26 @@ namespace Microsoft.AspNetCore.Razor.Language
return RazorDiagnostic.Create(Parsing_UnexpectedEndTag, location, tagName);
}
internal static readonly RazorDiagnosticDescriptor Parsing_ExpectedCloseBracketBeforeEOF =
new RazorDiagnosticDescriptor(
$"{DiagnosticPrefix}1027",
() => LegacyResources.ParseError_Expected_CloseBracket_Before_EOF,
RazorDiagnosticSeverity.Error);
public static RazorDiagnostic CreateParsing_ExpectedCloseBracketBeforeEOF(SourceSpan location, string openBrace, string closeBrace)
{
return RazorDiagnostic.Create(Parsing_ExpectedCloseBracketBeforeEOF, location, openBrace, closeBrace);
}
internal static readonly RazorDiagnosticDescriptor Parsing_RazorCommentNotTerminated =
new RazorDiagnosticDescriptor(
$"{DiagnosticPrefix}1028",
() => LegacyResources.ParseError_RazorComment_Not_Terminated,
RazorDiagnosticSeverity.Error);
public static RazorDiagnostic CreateParsing_RazorCommentNotTerminated(SourceSpan location)
{
return RazorDiagnostic.Create(Parsing_RazorCommentNotTerminated, location);
}
#endregion
#region Semantic Errors

View File

@ -148,10 +148,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
ImplicitExpressionTest(
"Html.En(code()", "Html.En(code()",
AcceptedCharactersInternal.Any,
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(8, 0, 8),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(8, 0, 8), contentLength: 1), "(", ")"));
}
[Fact]

View File

@ -207,10 +207,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code("Href(" + Environment.NewLine)
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(4, 0, 4),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
}
[Fact]
@ -224,10 +222,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code($"Foo(Bar(Baz){Environment.NewLine}Biz{Environment.NewLine}Boz")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(3, 0, 3),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(3, 0, 3), contentLength: 1), "(", ")"));
}
[Fact]
@ -243,10 +239,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code($"Foo(Bar(Baz){Environment.NewLine}Biz{Environment.NewLine}")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(3, 0, 3),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(3, 0, 3), contentLength: 1), "(", ")"));
}
[Fact]
@ -260,10 +254,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code($"Foo[Bar[Baz]{Environment.NewLine}Biz{Environment.NewLine}Boz")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("[", "]"),
new SourceLocation(3, 0, 3),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(3, 0, 3), contentLength: 1), "[", "]"));
}
[Fact]
@ -279,10 +271,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code($"Foo[Bar[Baz]{Environment.NewLine}Biz{Environment.NewLine}")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("[", "]"),
new SourceLocation(3, 0, 3),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(3, 0, 3), contentLength: 1), "[", "]"));
}
// Simple EOF handling errors:
@ -498,10 +488,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new StatementBlock(
Factory.Code("if(foo bar" + Environment.NewLine).AsStatement()
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(2, 0, 2),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 1), "(", ")"));
}
[Fact]
@ -512,10 +500,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new StatementBlock(
Factory.Code("foreach(foo bar" + Environment.NewLine).AsStatement()
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(7, 0, 7),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(7, 0, 7), contentLength: 1), "(", ")"));
}
[Fact]
@ -526,10 +512,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new StatementBlock(
Factory.Code("do { } while(foo bar" + Environment.NewLine).AsStatement()
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(12, 0, 12),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(12, 0, 12), contentLength: 1), "(", ")"));
}
[Fact]
@ -540,10 +524,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new StatementBlock(
Factory.Code("using(foo bar" + Environment.NewLine).AsStatement()
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(5, 0, 5),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(5, 0, 5), contentLength: 1), "(", ")"));
}
[Fact]
@ -561,10 +543,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Markup(" ").Accepts(AcceptedCharactersInternal.None)),
Factory.Code("}").AsStatement().Accepts(AcceptedCharactersInternal.None)
),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(2, 0, 2),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 1), "(", ")"));
}
[Fact]
@ -656,10 +636,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)),
expectedErrors: new[]
{
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(14, 0, 14),
length: 1)),
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(14, 0, 14), contentLength: 1), "(", ")"),
});
}

View File

@ -15,21 +15,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
get
{
var noErrors = new RazorDiagnostic[0];
Func<int, RazorDiagnostic[]> missingEndParenError = (index) =>
new RazorDiagnostic[1]
{
RazorDiagnostic.Create(new RazorError(
"An opening \"(\" is missing the corresponding closing \")\".",
new SourceLocation(index, 0, index),
length: 1))
};
Func<int, RazorDiagnostic[]> missingEndBracketError = (index) =>
new RazorDiagnostic[1]
{
RazorDiagnostic.Create(new RazorError(
"An opening \"[\" is missing the corresponding closing \"]\".",
new SourceLocation(index, 0, index),
length: 1))
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(index, 0, index), contentLength: 1), "[", "]"),
};
// implicitExpression, expectedImplicitExpression, acceptedCharacters, expectedErrors
@ -250,10 +240,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
ImplicitExpressionTest(
"foo(()", "foo(()",
acceptedCharacters: AcceptedCharactersInternal.Any,
errors: RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(4, 0, 4),
length: 1)));
errors: RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
}
[Fact]

View File

@ -25,10 +25,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
string.Empty,
HtmlSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any))),
RazorDiagnostic.Create(new RazorError(
LegacyResources.ParseError_RazorComment_Not_Terminated,
SourceLocation.Zero,
length: 2)));
RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
new SourceSpan(SourceLocation.Zero, contentLength: 2)));
}
[Fact]
@ -79,10 +77,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.Accepts(AcceptedCharactersInternal.None)),
Factory.Code(Environment.NewLine)
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords))),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(4, 0, 4),
length: 1)));
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
}
[Fact]
@ -104,14 +100,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
string.Empty,
CSharpSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any)))),
RazorDiagnostic.Create(new RazorError(
LegacyResources.ParseError_RazorComment_Not_Terminated,
new SourceLocation(5, 0, 5),
length: 2)),
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(4, 0, 4),
length: 1)));
RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
new SourceSpan(new SourceLocation(5, 0, 5), contentLength: 2)),
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
}
[Fact]
@ -176,10 +168,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.Accepts(AcceptedCharactersInternal.None),
Factory.Span(SpanKindInternal.Comment, new CSharpSymbol(string.Empty, CSharpSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any)))),
RazorDiagnostic.Create(new RazorError(
LegacyResources.ParseError_RazorComment_Not_Terminated,
new SourceLocation(2, 0, 2),
length: 2)),
RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 2)),
RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF(
new SourceSpan(new SourceLocation(1, 0, 1), contentLength: 1), LegacyResources.BlockName_Code, "}", "{"));
}

View File

@ -248,7 +248,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
get
{
var factory = new SpanFactory();
var unbalancedParenErrorString = "An opening \"(\" is missing the corresponding closing \")\".";
// document, expectedStatement, expectedErrors
return new TheoryData<string, StatementBlock, RazorDiagnostic[]>
@ -260,7 +259,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
factory
.Code("try { someMethod(); } catch(Exception) when (")
.AsStatement()),
new[] { RazorDiagnostic.Create(new RazorError(unbalancedParenErrorString, 45, 0, 45, 1)) }
new[]
{
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(45, 0, 45), contentLength: 1), "(", ")"),
}
},
{
"@try { someMethod(); } catch(Exception) when (someMethod(",
@ -269,7 +272,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
factory
.Code("try { someMethod(); } catch(Exception) when (someMethod(")
.AsStatement()),
new[] { RazorDiagnostic.Create(new RazorError(unbalancedParenErrorString, 45, 0, 45, 1)) }
new[]
{
RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
new SourceSpan(new SourceLocation(45, 0, 45), contentLength: 1), "(", ")"),
}
},
{
"@try { someMethod(); } catch(Exception) when (true) {",
@ -281,7 +288,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new[]
{
RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF(
new SourceSpan(new SourceLocation(23, 0, 23), contentLength: 1), "catch", "}", "{")
new SourceSpan(new SourceLocation(23, 0, 23), contentLength: 1), "catch", "}", "{"),
}
},
};