Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed May 20, 2019
1 parent 17becd2 commit 3094cd5
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal enum BalancingModes
BacktrackOnFailure = 1,
NoErrorOnFailure = 2,
AllowCommentsAndTemplates = 4,
AllowEmbeddedTransitions = 8
AllowEmbeddedTransitions = 8,
StopAtEndOfLine = 16,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1356,10 +1356,9 @@ private void ParseExtensibleDirective(in SyntaxListBuilder<RazorSyntaxNode> buil
case DirectiveTokenKind.Attribute:
if (At(SyntaxKind.LeftBracket))
{
AcceptUntil(SyntaxKind.RightBracket, SyntaxKind.NewLine);
if (At(SyntaxKind.RightBracket))
if (Balance(directiveBuilder, BalancingModes.NoErrorOnFailure | BalancingModes.StopAtEndOfLine))
{
AcceptAndMoveNext();
TryAccept(SyntaxKind.RightBracket);
}
}
else
Expand Down Expand Up @@ -2446,7 +2445,9 @@ private bool Balance(SyntaxListBuilder<RazorSyntaxNode> builder, BalancingModes
{
var startPosition = CurrentStart.AbsoluteIndex;
var nesting = 1;
if (!EndOfFile)
var stopAtEndOfLine = (mode & BalancingModes.StopAtEndOfLine) == BalancingModes.StopAtEndOfLine;
if (!EndOfFile &&
!(stopAtEndOfLine && At(SyntaxKind.NewLine)))
{
var tokens = new List<SyntaxToken>();
do
Expand Down Expand Up @@ -2475,7 +2476,7 @@ private bool Balance(SyntaxListBuilder<RazorSyntaxNode> builder, BalancingModes
tokens.Add(CurrentToken);
}
}
while (nesting > 0 && NextToken());
while (nesting > 0 && NextToken() && !(stopAtEndOfLine && At(SyntaxKind.NewLine)));

if (nesting > 0)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Razor/test/RazorLanguage.Test/Legacy/RazorDirectivesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,22 @@ @custom [DllImport(""user32.dll"", SetLastError=false, ExactSpelling=false)]
new[] { descriptor });
}

[Fact]
public void DirectiveDescriptor_AttributeToken_BalancesBrackets()
{
// Arrange
var descriptor = DirectiveDescriptor.CreateDirective(
"custom",
DirectiveKind.SingleLine,
b => b.AddAttributeToken());

// Act & Assert
ParseDocumentTest(@"
@custom [SomeCustom(new int[] { 1, 2, 3 }
",
new[] { descriptor });
}

[Fact]
public void DirectiveDescriptor_AttributeToken_ErrorsIfDoesNotStartWithOpenBracket()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Markup span at (0:0,0 [2] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [45] )
Transition span at (2:1,0 [1] ) (Accepts:None) - Parent: Directive block at (2:1,0 [43] )
MetaCode span at (3:1,1 [6] ) (Accepts:None) - Parent: Directive block at (2:1,0 [43] )
Code span at (9:1,7 [1] ) (Accepts:Whitespace) - Parent: Directive block at (2:1,0 [43] )
Code span at (10:1,8 [33] ) (Accepts:NonWhitespace) - Parent: Directive block at (2:1,0 [43] )
Markup span at (43:1,41 [2] ) (Accepts:Whitespace) - Parent: Directive block at (2:1,0 [43] )
Markup span at (45:2,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [45] )
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
RazorDocument - [0..45)::45 - [LF@custom [SomeCustom(new int[] { 1, 2, 3 }LF]
MarkupBlock - [0..45)::45
MarkupTextLiteral - [0..2)::2 - [LF] - Gen<Markup> - SpanEditHandler;Accepts:Any
NewLine;[LF];
CSharpCodeBlock - [2..45)::43
RazorDirective - [2..45)::43 - Directive:{custom;SingleLine;Unrestricted}
CSharpTransition - [2..3)::1 - Gen<None> - SpanEditHandler;Accepts:None
Transition;[@];
RazorDirectiveBody - [3..45)::42
RazorMetaCode - [3..9)::6 - Gen<None> - SpanEditHandler;Accepts:None
Identifier;[custom];
CSharpCodeBlock - [9..45)::36
CSharpStatementLiteral - [9..10)::1 - [ ] - Gen<None> - SpanEditHandler;Accepts:Whitespace
Whitespace;[ ];
CSharpStatementLiteral - [10..43)::33 - [[SomeCustom(new int[] { 1, 2, 3 }] - Gen<DirectiveToken {;Attribute;Opt:False}> - DirectiveTokenEditHandler;Accepts:NonWhitespace
LeftBracket;[[];
Identifier;[SomeCustom];
LeftParenthesis;[(];
Keyword;[new];
Whitespace;[ ];
Keyword;[int];
LeftBracket;[[];
RightBracket;[]];
Whitespace;[ ];
LeftBrace;[{];
Whitespace;[ ];
IntegerLiteral;[1];
Comma;[,];
Whitespace;[ ];
IntegerLiteral;[2];
Comma;[,];
Whitespace;[ ];
IntegerLiteral;[3];
Whitespace;[ ];
RightBrace;[}];
MarkupEphemeralTextLiteral - [43..45)::2 - [LF] - Gen<None> - SpanEditHandler;Accepts:Whitespace
NewLine;[LF];
MarkupTextLiteral - [45..45)::0 - [] - Gen<Markup> - SpanEditHandler;Accepts:Any
Marker;[];

0 comments on commit 3094cd5

Please sign in to comment.