Skip to content
Merged
Changes from all commits
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
59 changes: 23 additions & 36 deletions src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
#End Region

#Region "Buffer helpers"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you measured performance effects of this change? Is this method being inlined by JIT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build I have is Debug so it isn't optimised. It the IL it is a method call.

    IL_00ce: nop
    IL_00cf: ldarg.0
    IL_00d0: ldloc.1
    IL_00d1: ldc.i4.2
    IL_00d2: add
    IL_00d3: ldstr "CDATA["
    IL_00d8: call instance bool Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax.Scanner::NextAre(int32, string)
    IL_00dd: ldc.i4.0
    IL_00de: ceq
    IL_00e0: stloc.2
    IL_00e1: ldloc.2
    IL_00e2: brtrue.s IL_00f2

Since its been extracted out we can "play" with different internal implementations.

Private Function NextAre(offset As Integer, chars As String) As Boolean
Debug.Assert(Not String.IsNullOrEmpty(chars))
Dim n = chars.Length
If Not CanGetCharAtOffset(offset + n) Then Return False
For i = 0 To n - 1
If chars(i) <> PeekAheadChar(offset + i) Then Return False
Next
Return True
End Function

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate method declarations with an empty line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So noted and added in 6396a55

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rewrite this method for clarity as suggested by @pharring In the issue thread?


Private Function CanGetChar() As Boolean
Return _lineBufferOffset < _bufferLen
End Function
Expand Down Expand Up @@ -1109,9 +1120,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Return ScanStringLiteral(precedingTrivia)

Case "A"c
If CanGetCharAtOffset(2) AndAlso
PeekAheadChar(1) = "s"c AndAlso
PeekAheadChar(2) = " "c Then
If NextAre(1, "s ") Then

' TODO: do we allow widechars in keywords?
Dim spelling = "As"
Expand All @@ -1122,10 +1131,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
End If

Case "E"c
If CanGetCharAtOffset(3) AndAlso
PeekAheadChar(1) = "n"c AndAlso
PeekAheadChar(2) = "d"c AndAlso
PeekAheadChar(3) = " "c Then
If NextAre(1, "nd ") Then

' TODO: do we allow widechars in keywords?
Dim spelling = "End"
Expand All @@ -1136,9 +1142,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
End If

Case "I"c
If CanGetCharAtOffset(2) AndAlso
PeekAheadChar(1) = "f"c AndAlso
PeekAheadChar(2) = " "c Then
If NextAre(1, "f ") Then

' TODO: do we allow widechars in keywords?
Dim spelling = "If"
Expand All @@ -1148,9 +1152,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Return ScanIdentifierOrKeyword(precedingTrivia)
End If

Case "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, "k"c, "l"c,
"m"c, "n"c, "o"c, "p"c, "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, "w"c, "x"c,
"y"c, "z"c
Case "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, "m"c,
"n"c, "o"c, "p"c, "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, "w"c, "x"c, "y"c, "z"c
Return ScanIdentifierOrKeyword(precedingTrivia)

Case "B"c, "C"c, "D"c, "F"c, "G"c, "H"c, "J"c, "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, "Q"c,
Expand Down Expand Up @@ -1347,43 +1350,32 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Return ScanStringLiteral(precedingTrivia)

Case "A"c
If CanGetCharAtOffset(2) AndAlso
PeekAheadChar(1) = "s"c AndAlso
PeekAheadChar(2) = " "c Then

If NextAre(1, "s ") Then
Dim spelling = GetText(2)
Return MakeKeyword(SyntaxKind.AsKeyword, spelling, precedingTrivia)
Else
Return ScanIdentifierOrKeyword(precedingTrivia)
End If

Case "E"c
If CanGetCharAtOffset(3) AndAlso
PeekAheadChar(1) = "n"c AndAlso
PeekAheadChar(2) = "d"c AndAlso
PeekAheadChar(3) = " "c Then

If NextAre(1, "nd ") Then
Dim spelling = GetText(3)
Return MakeKeyword(SyntaxKind.EndKeyword, spelling, precedingTrivia)
Else
Return ScanIdentifierOrKeyword(precedingTrivia)
End If

Case "I"c
If CanGetCharAtOffset(2) AndAlso
PeekAheadChar(1) = "f"c AndAlso
PeekAheadChar(2) = " "c Then

If NextAre(1, "f ") Then
' TODO: do we allow widechars in keywords?
Dim spelling = GetText(2)
Return MakeKeyword(SyntaxKind.IfKeyword, spelling, precedingTrivia)
Else
Return ScanIdentifierOrKeyword(precedingTrivia)
End If

Case "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, "k"c, "l"c,
"m"c, "n"c, "o"c, "p"c, "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, "w"c, "x"c,
"y"c, "z"c
Case "a"c, "b"c, "c"c, "d"c, "e"c, "f"c, "g"c, "h"c, "i"c, "j"c, "k"c, "l"c, "m"c,
"n"c, "o"c, "p"c, "q"c, "r"c, "s"c, "t"c, "u"c, "v"c, "w"c, "x"c, "y"c, "z"c
Return ScanIdentifierOrKeyword(precedingTrivia)

Case "B"c, "C"c, "D"c, "F"c, "G"c, "H"c, "J"c, "K"c, "L"c, "M"c, "N"c, "O"c, "P"c, "Q"c,
Expand Down Expand Up @@ -1496,13 +1488,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Return XmlMakeBeginCommentToken(precedingTrivia, scanTrailingTrivia)
End If
Case "["c
If CanGetCharAtOffset(length + 8) AndAlso
PeekAheadChar(length + 2) = "C"c AndAlso
PeekAheadChar(length + 3) = "D"c AndAlso
PeekAheadChar(length + 4) = "A"c AndAlso
PeekAheadChar(length + 5) = "T"c AndAlso
PeekAheadChar(length + 6) = "A"c AndAlso
PeekAheadChar(length + 7) = "["c Then

If NextAre(length + 2, "CDATA[") Then

Return XmlMakeBeginCDataToken(precedingTrivia, scanTrailingTrivia)
End If
Expand Down