diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index 79a36641d5cdd..f49b04eec794a 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -389,6 +389,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax #End Region #Region "Buffer helpers" + + 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 + Private Function CanGetChar() As Boolean Return _lineBufferOffset < _bufferLen End Function @@ -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" @@ -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" @@ -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" @@ -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, @@ -1347,10 +1350,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 Dim spelling = GetText(2) Return MakeKeyword(SyntaxKind.AsKeyword, spelling, precedingTrivia) Else @@ -1358,11 +1358,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 Dim spelling = GetText(3) Return MakeKeyword(SyntaxKind.EndKeyword, spelling, precedingTrivia) Else @@ -1370,10 +1366,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 = GetText(2) Return MakeKeyword(SyntaxKind.IfKeyword, spelling, precedingTrivia) @@ -1381,9 +1374,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, @@ -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