From 78e3009571ebeb93fbcf19f5dc4793480eda07cf Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Wed, 4 Mar 2015 01:58:03 +0000 Subject: [PATCH 1/4] Implemented NextAre This function encapsulates a common coding pattern in the scanner source. That of a `CanGetCharAtOffset( )` followed by multiple multiple `PeekAheadChar( )` and comparison checks. As a result the scanner source is a bit better to work with. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 48 ++++++++----------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index 79a36641d5cdd..2e7483b072a56 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -389,6 +389,18 @@ 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 + Dim i = -1 + If CanGetCharAtOffset(offset + n) Then + Do + i += 1 + Loop While i < n AndAlso chars(i) = PeekAheadChar(offset + i) + End If + Return i = n + End Function Private Function CanGetChar() As Boolean Return _lineBufferOffset < _bufferLen End Function @@ -1109,9 +1121,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 +1132,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 +1143,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" @@ -1151,6 +1156,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax 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,9 +1353,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) @@ -1358,10 +1362,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) @@ -1370,9 +1371,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) @@ -1496,13 +1495,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 From 6396a558fec11b08b2f9a752a9cd92744811d3b6 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Wed, 4 Mar 2015 02:55:09 +0000 Subject: [PATCH 2/4] Inserted an empty line after NextAre method. --- src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index 2e7483b072a56..ee0c722208a05 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -401,6 +401,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Return i = n End Function + Private Function CanGetChar() As Boolean Return _lineBufferOffset < _bufferLen End Function From ae2f2834547ee932c03cccc62291545c570f259e Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Thu, 5 Mar 2015 02:00:19 +0000 Subject: [PATCH 3/4] NextAre implementation change. Using a different implementation, for clarity. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index ee0c722208a05..dfde758acd774 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -393,13 +393,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function NextAre(offset As Integer, chars As String) As Boolean Debug.Assert(Not String.IsNullOrEmpty(chars)) Dim n = chars.Length - Dim i = -1 - If CanGetCharAtOffset(offset + n) Then - Do - i += 1 - Loop While i < n AndAlso chars(i) = PeekAheadChar(offset + i) - End If - Return i = n + 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 From 6326f2d32b5715e586d8176b4315ce62541a3b78 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Thu, 5 Mar 2015 23:19:46 +0000 Subject: [PATCH 4/4] Clearing Up additional blank line. Also tidy up character sequences. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index dfde758acd774..f49b04eec794a 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -1152,10 +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, @@ -1353,7 +1351,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Case "A"c If NextAre(1, "s ") Then - Dim spelling = GetText(2) Return MakeKeyword(SyntaxKind.AsKeyword, spelling, precedingTrivia) Else @@ -1362,7 +1359,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Case "E"c If NextAre(1, "nd ") Then - Dim spelling = GetText(3) Return MakeKeyword(SyntaxKind.EndKeyword, spelling, precedingTrivia) Else @@ -1371,7 +1367,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Case "I"c If NextAre(1, "f ") Then - ' TODO: do we allow widechars in keywords? Dim spelling = GetText(2) Return MakeKeyword(SyntaxKind.IfKeyword, spelling, precedingTrivia) @@ -1379,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,