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)) if (EndOfFile && ((mode & BalancingModes.NoErrorOnFailure) != BalancingModes.NoErrorOnFailure))
{ {
Context.ErrorSink.OnError( Context.ErrorSink.OnError(
start, RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF( new SourceSpan(start, contentLength: 1 /* { OR } */),
Language.GetSample(left), Language.GetSample(left),
Language.GetSample(right)), Language.GetSample(right)));
length: 1 /* { OR } */);
} }
return Balance(mode, left, right, start); return Balance(mode, left, right, start);
@ -218,11 +217,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if ((mode & BalancingModes.NoErrorOnFailure) != BalancingModes.NoErrorOnFailure) if ((mode & BalancingModes.NoErrorOnFailure) != BalancingModes.NoErrorOnFailure)
{ {
Context.ErrorSink.OnError( Context.ErrorSink.OnError(
start, RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF( new SourceSpan(start, contentLength: 1 /* { OR } */),
Language.GetSample(left), Language.GetSample(left),
Language.GetSample(right)), Language.GetSample(right)));
length: 1 /* { OR } */);
} }
if ((mode & BalancingModes.BacktrackOnFailure) == BalancingModes.BacktrackOnFailure) if ((mode & BalancingModes.BacktrackOnFailure) == BalancingModes.BacktrackOnFailure)
{ {
@ -435,44 +433,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
return false; 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() protected bool EnsureCurrent()
{ {
if (CurrentSymbol == null) if (CurrentSymbol == null)
@ -634,9 +594,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{ {
errorReported = true; errorReported = true;
Context.ErrorSink.OnError( Context.ErrorSink.OnError(
start, RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
LegacyResources.ParseError_RazorComment_Not_Terminated, new SourceSpan(start, contentLength: 2 /* @* */)));
length: 2 /* @* */);
} }
else else
{ {
@ -649,9 +608,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{ {
errorReported = true; errorReported = true;
Context.ErrorSink.OnError( Context.ErrorSink.OnError(
start, RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
LegacyResources.ParseError_RazorComment_Not_Terminated, new SourceSpan(start, contentLength: 2 /* @* */)));
length: 2 /* @* */);
} }
} }
else else

View File

@ -310,6 +310,26 @@ namespace Microsoft.AspNetCore.Razor.Language
return RazorDiagnostic.Create(Parsing_UnexpectedEndTag, location, tagName); 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 #endregion
#region Semantic Errors #region Semantic Errors

View File

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

View File

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

View File

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

View File

@ -25,10 +25,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
string.Empty, string.Empty,
HtmlSymbolType.Unknown)) HtmlSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any))), .Accepts(AcceptedCharactersInternal.Any))),
RazorDiagnostic.Create(new RazorError( RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
LegacyResources.ParseError_RazorComment_Not_Terminated, new SourceSpan(SourceLocation.Zero, contentLength: 2)));
SourceLocation.Zero,
length: 2)));
} }
[Fact] [Fact]
@ -79,10 +77,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.Accepts(AcceptedCharactersInternal.None)), .Accepts(AcceptedCharactersInternal.None)),
Factory.Code(Environment.NewLine) Factory.Code(Environment.NewLine)
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords))), .AsImplicitExpression(CSharpCodeParser.DefaultKeywords))),
RazorDiagnostic.Create(new RazorError( RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"), new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
new SourceLocation(4, 0, 4),
length: 1)));
} }
[Fact] [Fact]
@ -104,14 +100,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
string.Empty, string.Empty,
CSharpSymbolType.Unknown)) CSharpSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any)))), .Accepts(AcceptedCharactersInternal.Any)))),
RazorDiagnostic.Create(new RazorError( RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
LegacyResources.ParseError_RazorComment_Not_Terminated, new SourceSpan(new SourceLocation(5, 0, 5), contentLength: 2)),
new SourceLocation(5, 0, 5), RazorDiagnosticFactory.CreateParsing_ExpectedCloseBracketBeforeEOF(
length: 2)), new SourceSpan(new SourceLocation(4, 0, 4), contentLength: 1), "(", ")"));
RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
new SourceLocation(4, 0, 4),
length: 1)));
} }
[Fact] [Fact]
@ -176,10 +168,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.Accepts(AcceptedCharactersInternal.None), .Accepts(AcceptedCharactersInternal.None),
Factory.Span(SpanKindInternal.Comment, new CSharpSymbol(string.Empty, CSharpSymbolType.Unknown)) Factory.Span(SpanKindInternal.Comment, new CSharpSymbol(string.Empty, CSharpSymbolType.Unknown))
.Accepts(AcceptedCharactersInternal.Any)))), .Accepts(AcceptedCharactersInternal.Any)))),
RazorDiagnostic.Create(new RazorError( RazorDiagnosticFactory.CreateParsing_RazorCommentNotTerminated(
LegacyResources.ParseError_RazorComment_Not_Terminated, new SourceSpan(new SourceLocation(2, 0, 2), contentLength: 2)),
new SourceLocation(2, 0, 2),
length: 2)),
RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF( RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF(
new SourceSpan(new SourceLocation(1, 0, 1), contentLength: 1), LegacyResources.BlockName_Code, "}", "{")); 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 get
{ {
var factory = new SpanFactory(); var factory = new SpanFactory();
var unbalancedParenErrorString = "An opening \"(\" is missing the corresponding closing \")\".";
// document, expectedStatement, expectedErrors // document, expectedStatement, expectedErrors
return new TheoryData<string, StatementBlock, RazorDiagnostic[]> return new TheoryData<string, StatementBlock, RazorDiagnostic[]>
@ -260,7 +259,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
factory factory
.Code("try { someMethod(); } catch(Exception) when (") .Code("try { someMethod(); } catch(Exception) when (")
.AsStatement()), .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(", "@try { someMethod(); } catch(Exception) when (someMethod(",
@ -269,7 +272,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
factory factory
.Code("try { someMethod(); } catch(Exception) when (someMethod(") .Code("try { someMethod(); } catch(Exception) when (someMethod(")
.AsStatement()), .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) {", "@try { someMethod(); } catch(Exception) when (true) {",
@ -281,7 +288,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new[] new[]
{ {
RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF( RazorDiagnosticFactory.CreateParsing_ExpectedEndOfBlockBeforeEOF(
new SourceSpan(new SourceLocation(23, 0, 23), contentLength: 1), "catch", "}", "{") new SourceSpan(new SourceLocation(23, 0, 23), contentLength: 1), "catch", "}", "{"),
} }
}, },
}; };