Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the roslyn tokenizer #10702

Merged
merged 17 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public HtmlMarkupParser HtmlParser
return null;
}

StartingBlock();

using (var pooledResult = Pool.Allocate<RazorSyntaxNode>())
using (PushSpanContextConfig(DefaultSpanContextConfig))
{
Expand All @@ -246,10 +248,10 @@ public HtmlMarkupParser HtmlParser
CurrentToken.Content[0] == SyntaxConstants.TransitionCharacter)
{
var split = Language.SplitToken(CurrentToken, 1, SyntaxKind.Transition);
transitionToken = split.Item1;
transitionToken = split.left;

// Back up to the end of the transition
Context.Source.Position -= split.Item2.Content.Length;
_tokenizer.Reset(Context.Source.Position - split.right.Content.Length);
NextToken();
}
else if (At(SyntaxKind.Transition))
Expand Down Expand Up @@ -1035,7 +1037,7 @@ not SyntaxKind.LeftBracket and
}
else
{
Context.Source.Position = bookmark;
_tokenizer.Reset(bookmark);
NextToken();
AcceptUntil(SyntaxKind.LessThan, SyntaxKind.LeftBrace, SyntaxKind.RightBrace);
return;
Expand Down Expand Up @@ -1764,8 +1766,10 @@ private void ParseExtensibleDirective(in SyntaxListBuilder<RazorSyntaxNode> buil

using (PushSpanContextConfig())
{
EndingBlock();
var razorBlock = HtmlParser.ParseRazorBlock(Tuple.Create("{", "}"), caseSensitive: true);
directiveBuilder.Add(razorBlock);
StartingBlock();
}

InitializeContext();
Expand Down Expand Up @@ -2377,7 +2381,7 @@ private void ParseUsingKeyword(SyntaxListBuilder<RazorSyntaxNode> builder, CShar
ReadWhile(IsSpacingTokenIncludingComments, ref whitespaceOrComments.AsRef());
var atLeftParen = At(SyntaxKind.LeftParenthesis);
var atIdentifier = At(SyntaxKind.Identifier);
var atStatic = At(CSharpSyntaxKind.StaticKeyword);
var atStaticOrGlobal = At(CSharpSyntaxKind.StaticKeyword, CSharpSyntaxKind.GlobalKeyword);

// Put the read tokens back and let them be handled later.
PutCurrentBack();
Expand All @@ -2390,7 +2394,7 @@ private void ParseUsingKeyword(SyntaxListBuilder<RazorSyntaxNode> builder, CShar
// using ( ==> Using Statement
ParseUsingStatement(builder, transition, block);
}
else if (atIdentifier || atStatic)
else if (atIdentifier || atStaticOrGlobal)
{
// using Identifier ==> Using Declaration
if (!topLevel)
Expand Down Expand Up @@ -2472,7 +2476,7 @@ private void ParseUsingDeclaration(in SyntaxListBuilder<RazorSyntaxNode> builder
var nonNamespaceTokenCount = TokenBuilder.Count;
AcceptWhile(IsSpacingTokenIncludingComments);
var start = CurrentStart;
if (At(SyntaxKind.Identifier))
if (At(SyntaxKind.Identifier) || At(CSharpSyntaxKind.GlobalKeyword))
jjonescz marked this conversation as resolved.
Show resolved Hide resolved
{
// non-static using
nonNamespaceTokenCount = TokenBuilder.Count;
Expand Down Expand Up @@ -2783,6 +2787,8 @@ private void OtherParserBlock(in SyntaxListBuilder<RazorSyntaxNode> builder)
var wasNested = IsNested;
IsNested = false;

EndingBlock();

RazorSyntaxNode? htmlBlock = null;
using (PushSpanContextConfig())
{
Expand All @@ -2792,6 +2798,8 @@ private void OtherParserBlock(in SyntaxListBuilder<RazorSyntaxNode> builder)
builder.Add(htmlBlock);
InitializeContext();

StartingBlock();

IsNested = wasNested;
NextToken();
}
Expand Down Expand Up @@ -2863,7 +2871,7 @@ private bool Balance(SyntaxListBuilder<RazorSyntaxNode> builder, BalancingModes
}
if ((mode & BalancingModes.BacktrackOnFailure) == BalancingModes.BacktrackOnFailure)
{
Context.Source.Position = startPosition;
_tokenizer.Reset(startPosition);
NextToken();
}
else
Expand Down Expand Up @@ -2923,12 +2931,23 @@ internal void Assert(CSharpSyntaxKind expectedKeyword)
result.Value == expectedKeyword);
}

protected internal bool At(CSharpSyntaxKind keyword)
protected internal bool At(params ReadOnlySpan<CSharpSyntaxKind> keywords)
{
var result = _tokenizer.Tokenizer.GetTokenKeyword(CurrentToken);
return At(SyntaxKind.Keyword) &&
result.HasValue &&
result.Value == keyword;
if (!At(SyntaxKind.Keyword) || result is not { } keywordKind)
{
return false;
}

foreach (var search in keywords)
{
if (keywordKind == search)
{
return true;
}
}

return false;
}

private string GetBlockName(SyntaxToken token)
Expand Down
Loading