From 9eb723dfb76799f8b9857a2eb04b0acba016afdb Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Sat, 24 Oct 2015 21:15:18 +0100 Subject: [PATCH 1/7] [Compiler VB Scanner] Peep functionality. This adds peep functionality into the scanner, which is combination of CanGet and Peek. It does not alter the existing content of the out parameter (ch) when CanGet is false. NOTE: This is different mechinism to TryParse methods, this is deliberate so that existing value can be used in subsequent conditions. --- .../Portable/Scanner/ScannerBuffer.vb | 30 +++++++++++++- .../Scanner/ScannerInterpolatedString.vb | 40 +++++++++---------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/ScannerBuffer.vb b/src/Compilers/VisualBasic/Portable/Scanner/ScannerBuffer.vb index 30e698a182c37..7847c892f8b92 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/ScannerBuffer.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/ScannerBuffer.vb @@ -95,6 +95,34 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return p End Function + ''' + ''' + ''' + ''' Skip Position + ''' The char at the skip position, does not alter current value on false. + ''' True - if it can get the skip position, otherwise False. + Private Function Peep(skip As Integer, ByRef ch As Char) As Boolean + If (_lineBufferOffset + skip < _bufferLen) Then + Debug.Assert(skip >= -MaxCharsLookBehind) + Dim position = _lineBufferOffset + Dim page = _curPage + position += skip + ch = page._arr(position And s_PAGE_MASK) + + Dim start = page._pageStart + Dim expectedStart = position And s_NOT_PAGE_MASK + + If start <> expectedStart Then + page = GetPage(position) + ch = page._arr(position And s_PAGE_MASK) + End If + Return True + Else + Return False + End If + + End Function + ' PERF CRITICAL Private Function Peek(skip As Integer) As Char Debug.Assert(CanGet(skip)) @@ -215,4 +243,4 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return result End Function End Class -End Namespace +End Namespace \ No newline at end of file diff --git a/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb b/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb index 40a522c7dbfdd..df7c361f161b7 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb @@ -19,13 +19,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Dim leadingTriviaLength = GetWhitespaceLength(0) Dim offset = leadingTriviaLength Dim length As Integer - - If Not CanGet(offset) Then + Dim c As Char + If Not Peep(offset, c) Then Return MakeEndOfInterpolatedStringToken() End If - Dim c = Peek(offset) - ' This should only ever happen for $" or } Debug.Assert(leadingTriviaLength = 0 OrElse c = "$"c OrElse c = FULLWIDTH_DOLLAR_SIGN OrElse IsRightCurlyBracket(c)) @@ -37,8 +35,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Select Case c Case "$"c, FULLWIDTH_DOLLAR_SIGN + Dim ch As Char - If CanGet(offset + 1) AndAlso IsDoubleQuote(Peek(offset + 1)) Then + If Peep(offset + 1, ch) AndAlso IsDoubleQuote(ch) Then kind = SyntaxKind.DollarSignDoubleQuoteToken length = 2 scanTrailingTrivia = False ' Trailing whitespace should be scanned as interpolated string text. @@ -74,7 +73,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Case Else If IsDoubleQuote(c) Then - Debug.Assert(Not CanGet(offset + 1) OrElse Not IsDoubleQuote(Peek(offset + 1))) + Dim ch As Char + Debug.Assert(Not Peep(offset + 1, ch) OrElse Not IsDoubleQuote(ch)) kind = SyntaxKind.DoubleQuoteToken length = 1 @@ -112,22 +112,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function IsInterpolatedStringPunctuation(Optional offset As Integer = 0) As Boolean - If Not CanGet(offset) Then Return False - - Dim c = Peek(offset) + Dim c, ch As Char + If Not Peep(offset, c) Then Return False If IsLeftCurlyBracket(c) Then - Return Not CanGet(offset + 1) OrElse Not IsLeftCurlyBracket(Peek(offset + 1)) + Return Not Peep(offset + 1, ch) OrElse Not IsLeftCurlyBracket(ch) ElseIf IsRightCurlyBracket(c) Then - Return Not CanGet(offset + 1) OrElse Not IsRightCurlyBracket(Peek(offset + 1)) + Return Not Peep(offset + 1, ch) OrElse Not IsRightCurlyBracket(ch) ElseIf IsDoubleQuote(c) 'A subtle difference between this case and the one above. ' In both interpolated and literal strings the two quote characters used in an escape sequence don't have to match. ' It's enough that the next character is *a* quote char. It doesn't have to be the same quote. ' If we want to preserve consistency the quotes need to be special cased. - Return Not CanGet(offset + 1) OrElse Not IsDoubleQuote(Peek(offset + 1)) + Return Not Peep(offset + 1, ch) OrElse Not IsDoubleQuote(ch) Else Return False @@ -135,22 +134,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function ScanInterpolatedStringText(scanTrailingWhitespaceAsTrivia As Boolean) As SyntaxToken - If Not CanGet() Then Return MakeEndOfInterpolatedStringToken() + Dim c As Char + If Not Peep(0, c) Then Return MakeEndOfInterpolatedStringToken() Dim offset = 0 Dim pendingWhitespace = 0 Dim valueBuilder = GetScratch() - - Do While CanGet(offset) - - Dim c = Peek(offset) + Dim ch As Char + Do While Peep(offset, c) ' Any combination of fullwidth and ASCII curly braces of the same direction is an escaping sequence for the corresponding ASCII curly brace. ' We insert that curly brace doubled and because this is the escaping sequence understood by String.Format, that will be replaced by a single brace. ' This is deliberate design and it aligns with existing rules for double quote escaping in strings. If IsLeftCurlyBracket(c) Then - If CanGet(offset + 1) AndAlso IsLeftCurlyBracket(Peek(offset + 1)) Then + If Peep(offset + 1, ch) AndAlso IsLeftCurlyBracket(ch) Then ' This is an escape sequence. valueBuilder.Append("{{") @@ -164,7 +162,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ElseIf IsRightCurlyBracket(c) Then - If CanGet(offset + 1) AndAlso IsRightCurlyBracket(Peek(offset + 1)) Then + If Peep(offset + 1, ch) AndAlso IsRightCurlyBracket(ch) Then ' This is an escape sequence. valueBuilder.Append("}}") @@ -178,7 +176,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ElseIf IsDoubleQuote(c) - If CanGet(offset + 1) AndAlso IsDoubleQuote(Peek(offset + 1)) Then + If Peep(offset + 1, ch) AndAlso IsDoubleQuote(ch) Then ' This is a VB double quote escape. Oddly enough this logic allows mixing and matching of ' smart and dumb double quotes in any order. Regardless we always emit as a standard double quote. ' This is consistent with their handling in string literals. @@ -232,4 +230,4 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function End Class -End Namespace +End Namespace \ No newline at end of file From b59e3a3280042a9eff2fbcb0de901094247b08fe Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Sat, 24 Oct 2015 23:58:21 +0100 Subject: [PATCH 2/7] [Compier VB Scanner] Usage of Peep in ScannerXML --- .../Portable/Scanner/ScannerXml.vb | 181 +++++++++--------- 1 file changed, 90 insertions(+), 91 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb b/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb index 4c743cb28de4e..b696c20ddeeac 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb @@ -34,10 +34,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Exit Do End If - If Not CanGet(len) Then + If Not Peep(len, c) Then Exit Do End If - c = Peek(len) Loop If len > 0 Then @@ -68,9 +67,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' // Whitespace Dim leadingTrivia As SyntaxList(Of VisualBasicSyntaxNode) = Nothing - - While CanGet() - Dim c As Char = Peek() + Dim c As Char + While Peep(0, c) Select Case (c) ' // Whitespace @@ -93,7 +91,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax leadingTrivia = ScanXmlTrivia(c) Case "/"c - If CanGet(1) AndAlso Peek(1) = ">" Then + Dim ch As Char + If Peep(1, ch) AndAlso ch = ">"c Then Return XmlMakeEndEmptyElementToken(leadingTrivia) End If Return XmlMakeDivToken(leadingTrivia) @@ -113,12 +112,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return XmlMakeDoubleQuoteToken(leadingTrivia, c, isOpening:=True) Case "<"c - If CanGet(1) Then - Dim ch As Char = Peek(1) + Dim ch As Char, cx As Char + If Peep(1, ch) Then Select Case ch Case "!"c - If CanGet(2) Then - Select Case (Peek(2)) + If Peep(2, cx) Then + Select Case cx Case "-"c If NextIs(3, "-"c) Then Return XmlMakeBeginCommentToken(leadingTrivia, s_scanNoTriviaFunc) @@ -207,14 +206,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax '// NL WS* ' '// Example ' This is a comment Private Function ScanXmlForPossibleStatement(state As ScannerState) As Boolean - If Not CanGet() Then + Dim c As Char + If Not Peep(0, c) Then Return False End If Dim token As SyntaxToken Dim possibleStatement As Boolean = False Dim offsets = CreateOffsetRestorePoint() - Dim c As Char = Peek() Select Case c Case "#"c, @@ -308,9 +307,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Dim scratch = GetScratch() - - While CanGet(Here) - Dim c As Char = Peek(Here) + Dim c As Char + While Peep(Here, c) Select Case (c) Case CARRIAGE_RETURN, LINE_FEED @@ -342,12 +340,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Debug.Assert(Here = 0) - If CanGet(1) Then - Dim ch As Char = Peek(1) + Dim ch As Char + If Peep(1, ch) Then Select Case ch Case "!"c - If CanGet(2) Then - Select Case (Peek(2)) + Dim cx As Char + If Peep(2, cx) Then + Select Case cx Case "-"c If NextIs(3, "-"c) Then Return XmlMakeBeginCommentToken(precedingTrivia, s_scanNoTriviaFunc) @@ -492,8 +491,8 @@ ScanChars: End If Dim Here = 0 - While CanGet(Here) - Dim c As Char = Peek(Here) + Dim c As Char + While Peep(Here, c) Select Case (c) Case CARRIAGE_RETURN, LINE_FEED @@ -511,9 +510,8 @@ ScanChars: Return XmlMakeCommentToken(precedingTrivia, Here) End If - If CanGet(Here + 2) Then + If Peep(Here + 2, c) Then - c = Peek(Here + 2) Here += 2 ' // if > is not found then this is an error. Return the -- string @@ -583,9 +581,9 @@ ScanChars: Dim scratch = GetScratch() Dim Here = 0 + Dim c As Char - While CanGet(Here) - Dim c As Char = Peek(Here) + While Peep(Here, c) Select Case (c) Case CARRIAGE_RETURN, LINE_FEED @@ -647,11 +645,11 @@ ScanChars: Dim precedingTrivia = _triviaListPool.Allocate(Of VisualBasicSyntaxNode)() Dim result As SyntaxToken + Dim c As Char - If state = ScannerState.StartProcessingInstruction AndAlso CanGet() Then + If state = ScannerState.StartProcessingInstruction AndAlso Peep(0, c) Then ' // Whitespace ' // S ::= (#x20 | #x9 | #xD | #xA)+ - Dim c = Peek() Select Case c Case CARRIAGE_RETURN, LINE_FEED, " "c, CHARACTER_TABULATION Dim wsTrivia = ScanXmlTrivia(c) @@ -660,8 +658,7 @@ ScanChars: End If Dim Here = 0 - While CanGet(Here) - Dim c As Char = Peek(Here) + While Peep(Here, c) Select Case (c) Case CARRIAGE_RETURN, LINE_FEED @@ -720,8 +717,8 @@ CleanUp: ' // Misc ::= Comment | PI | S Dim precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode) = Nothing - While CanGet() - Dim c As Char = Peek() + Dim c As Char + While Peep(0, c) Select Case (c) ' // Whitespace @@ -732,8 +729,8 @@ CleanUp: precedingTrivia = ScanXmlTrivia(c) Case "<"c - If CanGet(1) Then - Dim ch As Char = Peek(1) + Dim ch As Char + If Peep(1, ch) Then Select Case ch Case "!"c If NextAre(2, "--") Then @@ -752,31 +749,31 @@ CleanUp: Return XmlMakeLessToken(precedingTrivia) - ' TODO: review + ' TODO: review - ' If Not m_State.m_ScannedElement OrElse c = "?"c OrElse c = "!"c Then - ' ' // Remove tEOL from token ring if any exists + ' If Not m_State.m_ScannedElement OrElse c = "?"c OrElse c = "!"c Then + ' ' // Remove tEOL from token ring if any exists - ' If tEOL IsNot Nothing Then - ' m_FirstFreeToken = tEOL - ' End If + ' If tEOL IsNot Nothing Then + ' m_FirstFreeToken = tEOL + ' End If - ' m_State.m_LexicalState = LexicalState.XmlMarkup - ' MakeToken(tokens.tkLT, 1) - ' m_InputStreamPosition += 1 - ' Return - ' End If - 'End If + ' m_State.m_LexicalState = LexicalState.XmlMarkup + ' MakeToken(tokens.tkLT, 1) + ' m_InputStreamPosition += 1 + ' Return + ' End If + 'End If - 'm_State.EndXmlState() + 'm_State.EndXmlState() - 'If tEOL IsNot Nothing Then - ' tEOL.m_EOL.m_NextLineAlreadyScanned = True - 'Else - ' MakeToken(tokens.tkLT, 1) - ' m_InputStreamPosition += 1 - 'End If - 'Return + 'If tEOL IsNot Nothing Then + ' tEOL.m_EOL.m_NextLineAlreadyScanned = True + 'Else + ' MakeToken(tokens.tkLT, 1) + ' m_InputStreamPosition += 1 + 'End If + 'Return Case Else Return SyntaxFactory.Token(precedingTrivia.Node, SyntaxKind.EndOfXmlToken, Nothing, String.Empty) @@ -795,9 +792,9 @@ CleanUp: Dim Here = 0 Dim scratch = GetScratch() + Dim c As Char - While CanGet(Here) - Dim c As Char = Peek(Here) + While Peep(Here, c) Select Case (c) @@ -888,9 +885,8 @@ ScanChars: Dim Here = 0 Dim scratch = GetScratch() - - While CanGet(Here) - Dim c As Char = Peek(Here) + Dim c As Char + While Peep(Here, c) If c = terminatingChar Or c = altTerminatingChar Then If Here > 0 Then result = XmlMakeAttributeDataToken(precedingTrivia, Here, scratch) @@ -988,12 +984,15 @@ CleanUp: ''' 1 is an error ''' Private Function ScanSurrogatePair(c1 As Char, Here As Integer) As XmlCharResult + Dim c2 As Char +#If DEBUG Then Debug.Assert(Here >= 0) - Debug.Assert(CanGet(Here)) - Debug.Assert(Peek(Here) = c1) + dim ok = Peep(Here, c2) + Debug.Assert(ok) + Debug.Assert(c2 = c1) +#End If - If IsHighSurrogate(c1) AndAlso CanGet(Here + 1) Then - Dim c2 = Peek(Here + 1) + If IsHighSurrogate(c1) AndAlso Peep(Here + 1, c2) Then If IsLowSurrogate(c2) Then Return New XmlCharResult(c1, c2) @@ -1033,9 +1032,9 @@ CleanUp: Private Function ScanXmlChar(Here As Integer) As XmlCharResult Debug.Assert(Here >= 0) - Debug.Assert(CanGet(Here)) - - Dim c = Peek(Here) + Dim c As Char + Dim ok = Peep(Here, c) + Debug.Assert(ok) If Not isValidUtf16(c) Then Return Nothing @@ -1066,8 +1065,8 @@ CleanUp: 'TODO - Fix ScanXmlNCName to conform to XML spec instead of old loose scanning. - While CanGet(Here) - Dim c As Char = Peek(Here) + Dim c As Char + While Peep(Here, c) Select Case (c) @@ -1134,8 +1133,8 @@ CreateNCNameToken: Debug.Assert(Peek() = "&"c) ' skip 1 char for "&" - If CanGet(1) Then - Dim c As Char = Peek(1) + Dim c As Char + If Peep(1, c) Then Select Case (c) Case "#"c @@ -1150,7 +1149,8 @@ CreateNCNameToken: value = Intern({result.Char1, result.Char2}) End If - If CanGet(Here) AndAlso Peek(Here) = ";"c Then + Dim ch As Char + If Peep(Here, ch) AndAlso ch = ";"c Then Return XmlMakeEntityLiteralToken(precedingTrivia, Here + 1, value) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, Here, value) @@ -1162,9 +1162,9 @@ CreateNCNameToken: Case "a"c ' // & ' // ' - - If CanGet(4) AndAlso NextAre(2, "mp") Then - If Peek(4) = ";"c Then + Dim ch As Char + If Peep(4, ch) AndAlso NextAre(2, "mp") Then + If ch = ";"c Then Return XmlMakeAmpLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 4, "&") @@ -1172,9 +1172,9 @@ CreateNCNameToken: Return DirectCast(noSemicolon.SetDiagnostics({noSemicolonError}), XmlTextTokenSyntax) End If - ElseIf CanGet(5) AndAlso NextAre(2, "pos") Then + ElseIf Peep(5, ch) AndAlso NextAre(2, "pos") Then - If Peek(5) = ";"c Then + If ch = ";"c Then Return XmlMakeAposLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 5, "'") @@ -1185,10 +1185,10 @@ CreateNCNameToken: Case "l"c ' // < + Dim ch As Char + If Peep(3, ch) AndAlso NextIs(2, "t"c) Then - If CanGet(3) AndAlso NextIs(2, "t"c) Then - - If Peek(3) = ";"c Then + If ch = ";"c Then Return XmlMakeLtLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 3, "<") @@ -1199,10 +1199,10 @@ CreateNCNameToken: Case "g"c ' // > + Dim ch As Char + If Peep(3, ch) AndAlso NextIs(2, "t"c) Then - If CanGet(3) AndAlso NextIs(2, "t"c) Then - - If Peek(3) = ";"c Then + If ch = ";"c Then Return XmlMakeGtLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 3, ">") @@ -1213,10 +1213,10 @@ CreateNCNameToken: Case "q"c ' // " + Dim ch As Char + If Peep(5, ch) AndAlso NextAre(2, "uot") Then - If CanGet(5) AndAlso NextAre(2, "uot") Then - - If Peek(5) = ";"c Then + If ch = ";"c Then Return XmlMakeQuotLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 5, """") @@ -1236,8 +1236,8 @@ CreateNCNameToken: Private Function ScanXmlCharRef(ByRef index As Integer) As XmlCharResult Debug.Assert(index >= 0) - - If Not CanGet(index) Then + Dim ch As Char + If Not Peep(index, ch) Then Return Nothing End If @@ -1245,12 +1245,11 @@ CreateNCNameToken: Dim charRefSb As New StringBuilder Dim Here = index - Dim ch = Peek(Here) If ch = "x"c Then Here += 1 - While CanGet(Here) - ch = Peek(Here) + While Peep(Here, ch) + If XmlCharType.IsHexDigit(ch) Then charRefSb.Append(ch) Else @@ -1266,8 +1265,8 @@ CreateNCNameToken: Return result End If Else - While CanGet(Here) - ch = Peek(Here) + While Peep(Here, ch) + If XmlCharType.IsDigit(ch) Then charRefSb.Append(ch) Else From 1202e1dead95788a2c1fe9de04f9719935111550 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Sun, 25 Oct 2015 04:58:13 +0000 Subject: [PATCH 3/7] Extends usage of Peep in to VB scanner. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 339 ++++++++---------- 1 file changed, 142 insertions(+), 197 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index 70e34368ff738..cbec6fc3c1bad 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -225,16 +225,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function ScanNextCharAsToken(leadingTrivia As SyntaxList(Of VisualBasicSyntaxNode)) As SyntaxToken Dim token As SyntaxToken - - If Not CanGet() Then + Dim c As Char + If Not Peep(0, c) Then token = MakeEofToken(leadingTrivia) Else _badTokenCount += 1 If _badTokenCount < BadTokenCountLimit Then ' // Don't break up surrogate pairs - Dim c = Peek() - Dim length = If(IsHighSurrogate(c) AndAlso CanGet(1) AndAlso IsLowSurrogate(Peek(1)), 2, 1) + Dim length = If(IsHighSurrogate(c) AndAlso Peep(1, c) AndAlso IsLowSurrogate(c), 2, 1) token = MakeBadToken(leadingTrivia, length, ERRID.ERR_IllegalChar) Else ' If we get too many characters that we cannot make sense of, absorb the rest of the input. @@ -265,9 +264,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Dim condLineStart = _lineBufferOffset + Dim c As Char - While (CanGet()) - Dim c As Char = Peek() + While Peep(0, c) Select Case (c) @@ -322,8 +321,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Sub EatThroughLine() - While CanGet() - Dim c As Char = Peek() + Dim c As Char + While Peep(0, c) If IsNewLine(c) Then EatThroughLineBreak(c) @@ -415,7 +414,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function NextIs(offset As Integer, c As Char) As Boolean - Return CanGet(offset) AndAlso (Peek(offset) = c) + Dim ch As Char + Return Peep(offset, ch) AndAlso (ch = c) End Function Private Function CanGet() As Boolean @@ -533,27 +533,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function ScanLineContinuation(tList As SyntaxListBuilder) As Boolean - If Not CanGet() Then - Return False - End If - - If Not IsAfterWhitespace() Then - Return False - End If - - Dim ch As Char = Peek() - If Not IsUnderscore(ch) Then + Dim ch As Char + If Not Peep(0, ch) OrElse + Not IsAfterWhitespace() OrElse + Not IsUnderscore(ch) Then Return False End If Dim Here = 1 - While CanGet(Here) - ch = Peek(Here) - If IsWhitespace(ch) Then - Here += 1 - Else - Exit While - End If + While Peep(Here, ch) AndAlso IsWhitespace(ch) + Here += 1 End While ' Line continuation is valid at the end of the @@ -579,8 +568,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' implicit line continuations. (See Scanner::EatLineContinuation.) Otherwise, ' include the new line and any additional spaces as trivia. If startComment = 0 AndAlso - CanGet(Here) AndAlso - Not IsNewLine(Peek(Here)) Then + Peep(Here, ch) AndAlso + Not IsNewLine(ch) Then tList.Add(MakeEndOfLineTrivia(GetText(newLine))) If spaces > 0 Then @@ -601,12 +590,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ''' Consumes all trivia until a nontrivia char is found ''' Friend Function ScanMultilineTrivia() As SyntaxList(Of VisualBasicSyntaxNode) - If Not CanGet() Then + Dim ch As Char + If Not Peep(0, ch) Then Return Nothing End If - Dim ch = Peek() - ' optimization for a common case ' the ASCII range between ': and ~ , with exception of except "'", "_" and R cannot start trivia If ch > ":"c AndAlso ch <= "~"c AndAlso ch <> "'"c AndAlso ch <> "_"c AndAlso ch <> "R"c AndAlso ch <> "r"c Then @@ -626,7 +614,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ''' Scans a single piece of trivia ''' Private Function TryScanSinglePieceOfMultilineTrivia(tList As SyntaxListBuilder) As Boolean - If CanGet() Then + Dim ch As Char + If Peep(0, ch) Then Dim atNewLine = IsAtNewLine() @@ -641,7 +630,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If End If - Dim ch = Peek() If IsWhitespace(ch) Then ' eat until linebreak or non-whitespace Dim wslen = GetWhitespaceLength(1) @@ -686,11 +674,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' check for # Private Function StartsDirective(Here As Integer) As Boolean - If CanGet(Here) Then - Dim ch = Peek(Here) - Return IsHash(ch) - End If - Return False + Dim ch As Char + Return Peep(Here, ch) AndAlso IsHash(ch) End Function Private Function IsAtNewLine() As Boolean @@ -730,8 +715,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Sub Private Sub ScanSingleLineTriviaInXmlDoc(tList As SyntaxListBuilder) - If CanGet() Then - Dim c As Char = Peek() + Dim c As Char + If Peep(0, c) Then + Select Case (c) ' // Whitespace ' // S ::= (#x20 | #x9 | #xD | #xA)+ @@ -763,7 +749,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Sub ScanWhitespaceAndLineContinuations(tList As SyntaxListBuilder) - If CanGet() AndAlso IsWhitespace(Peek()) Then + Dim ch As Char + If Peep(0, ch) AndAlso IsWhitespace(ch) Then tList.Add(ScanWhitespace(1)) ' collect { lineCont, ws } While ScanLineContinuation(tList) @@ -833,9 +820,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' Case 3 is required to parse single line if's and numeric labels. ' Case 4 is required to limit explicit line continuations to single new line - If CanGet() Then + Dim ch As Char + If Peep(0, ch) Then - Dim ch As Char = Peek() Dim startOfTerminatorTrivia = _lineBufferOffset If IsNewLine(ch) Then @@ -847,11 +834,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' collect { ws, colon } Do Dim len = GetWhitespaceLength(0) - If Not CanGet(len) Then + If Not Peep(len, ch) Then Exit Do End If - ch = Peek(len) If Not IsColonAndNotColonEquals(ch, offset:=len) Then Exit Do End If @@ -887,7 +873,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function GetWhitespaceLength(len As Integer) As Integer ' eat until linebreak or non-whitespace - While CanGet(len) AndAlso IsWhitespace(Peek(len)) + Dim ch As Char + While Peep(len, ch) AndAlso IsWhitespace(ch) len += 1 End While Return len @@ -895,7 +882,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function GetXmlWhitespaceLength(len As Integer) As Integer ' eat until linebreak or non-whitespace - While CanGet(len) AndAlso IsXmlWhitespace(Peek(len)) + Dim ch As Char + While Peep(len, ch) AndAlso IsXmlWhitespace(ch) len += 1 End While Return len @@ -924,26 +912,26 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax AdvanceChar() ' eat until linebreak or non-whitespace - While CanGet() AndAlso IsWhitespace(Peek) + Dim ch As Char + While Peep(0, ch) AndAlso IsWhitespace(ch) AdvanceChar() End While End Sub Private Function PeekStartComment(i As Integer) As Integer - - If CanGet(i) Then - Dim ch = Peek(i) + Dim ch As Char, c2 As Char, c3 As Char + If Peep(i, ch) Then If IsSingleQuote(ch) Then Return 1 ElseIf MatchOneOrAnotherOrFullwidth(ch, "R"c, "r"c) AndAlso - CanGet(i + 2) AndAlso MatchOneOrAnotherOrFullwidth(Peek(i + 1), "E"c, "e"c) AndAlso - MatchOneOrAnotherOrFullwidth(Peek(i + 2), "M"c, "m"c) Then + Peep(i + 2, c2) AndAlso MatchOneOrAnotherOrFullwidth(Peek(i + 1), "E"c, "e"c) AndAlso + MatchOneOrAnotherOrFullwidth(c2, "M"c, "m"c) Then - If Not CanGet(i + 3) OrElse IsNewLine(Peek(i + 3)) Then + If Not Peep(i + 3, c3) OrElse IsNewLine(c3) Then ' have only 'REM' Return 3 - ElseIf Not IsIdentifierPartCharacter(Peek(i + 3)) Then + ElseIf Not IsIdentifierPartCharacter(c3) Then ' have 'REM ' Return 4 End If @@ -961,9 +949,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Dim looksLikeDocComment As Boolean = StartsXmlDoc(0) ' eat all chars until EoL - While CanGet(length) AndAlso - Not IsNewLine(Peek(length)) - + Dim ch As Char + While Peep(length, ch) AndAlso Not IsNewLine(ch) length += 1 End While @@ -997,6 +984,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function ScanTokenCommon(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode), ch As Char, fullWidth As Boolean) As SyntaxToken Dim lengthWithMaybeEquals As Integer = 1 + Dim cx As Char Select Case ch Case CARRIAGE_RETURN, LINE_FEED Return ScanNewlineAsStatementTerminator(ch, precedingTrivia) @@ -1033,7 +1021,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return If(dl, MakeHashToken(precedingTrivia, fullWidth)) Case "&"c - If CanGet(1) AndAlso BeginsBaseLiteral(Peek(1)) Then + If Peep(1, ch) AndAlso BeginsBaseLiteral(ch) Then Return ScanNumericLiteral(precedingTrivia) End If @@ -1105,7 +1093,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return MakeExclamationToken(precedingTrivia, fullWidth) Case "."c - If CanGet(1) AndAlso IsDecimalDigit(Peek(1)) Then + If Peep(1, cx) AndAlso IsDecimalDigit(cx) Then Return ScanNumericLiteral(precedingTrivia) Else Return MakeDotToken(precedingTrivia, fullWidth) @@ -1153,13 +1141,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return ScanIdentifierOrKeyword(precedingTrivia) Case "_"c - If CanGet(1) AndAlso IsIdentifierPartCharacter(Peek(1)) Then + If Peep(1, cx) AndAlso IsIdentifierPartCharacter(cx) Then Return ScanIdentifierOrKeyword(precedingTrivia) End If Dim err As ERRID = ERRID.ERR_ExpectedIdentifier Dim len = GetWhitespaceLength(1) - If Not CanGet(len) OrElse IsNewLine(Peek(len)) OrElse PeekStartComment(len) > 0 Then + If Not Peep(len, cx) OrElse IsNewLine(cx) OrElse PeekStartComment(len) > 0 Then err = ERRID.ERR_LineContWithCommentOrNoPrecSpace End If @@ -1178,7 +1166,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Case "$"c, FULLWIDTH_DOLLAR_SIGN - If Not fullWidth AndAlso CanGet(1) AndAlso IsDoubleQuote(Peek(1)) Then + If Not fullWidth AndAlso Peep(1, cx) AndAlso IsDoubleQuote(cx) Then Return MakePunctuationToken(precedingTrivia, 2, SyntaxKind.DollarSignDoubleQuoteToken) End If @@ -1203,8 +1191,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' at this point it is very likely that we are located at the beginning of a token Private Function TryScanToken(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode)) As SyntaxToken - If CanGet() Then - Return ScanTokenCommon(precedingTrivia, Peek(), False) + Dim ch As Char + If Peep(0, ch) Then + Return ScanTokenCommon(precedingTrivia, ch, False) End If Return MakeEofToken(precedingTrivia) End Function @@ -1221,8 +1210,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Dim Here = Index Dim eq As Char - While CanGet(Here) - eq = Peek(Here) + While Peep(Here, eq) Here += 1 If Not IsWhitespace(eq) Then If eq = "="c OrElse eq = FULLWIDTH_EQUALS_SIGN Then @@ -1237,16 +1225,18 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function ScanRightAngleBracket(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode), charIsFullWidth As Boolean) As SyntaxToken - Debug.Assert(CanGet) ' > - Debug.Assert(Peek() = ">"c OrElse Peek() = FULLWIDTH_GREATER_THAN_SIGN) - + Dim c As Char +#If DEBUG Then + Dim ok = Peep(0, c) + Debug.Assert(ok) ' > + Debug.Assert(c = ">"c OrElse c = FULLWIDTH_GREATER_THAN_SIGN) +#End If Dim length As Integer = 1 ' // Allow whitespace between the characters of a two-character token. length = GetWhitespaceLength(length) - If CanGet(length) Then - Dim c As Char = Peek(length) + If Peep(length, c) Then If c = "="c OrElse c = FULLWIDTH_EQUALS_SIGN Then length += 1 @@ -1264,20 +1254,23 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function ScanLeftAngleBracket(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode), charIsFullWidth As Boolean, scanTrailingTrivia As ScanTriviaFunc) As SyntaxToken - Debug.Assert(CanGet) ' < - Debug.Assert(Peek() = "<"c OrElse Peek() = FULLWIDTH_LESS_THAN_SIGN) - + Dim c As Char +#If DEBUG Then + Dim ok = Peep(0, c) + Debug.Assert(ok) ' < + Debug.Assert(c = "<"c OrElse c = FULLWIDTH_LESS_THAN_SIGN) +#End If Dim length As Integer = 1 ' Check for XML tokens - If Not charIsFullWidth AndAlso CanGet(length) Then - Dim c As Char = Peek(length) + If Not charIsFullWidth AndAlso Peep(length, c) Then + Dim c2 As Char Select Case c Case "!"c - If CanGet(length + 2) Then + If Peep(length + 2, c2) Then Select Case (Peek(length + 1)) Case "-"c - If CanGet(length + 3) AndAlso Peek(length + 2) = "-"c Then + If CanGet(length + 3) AndAlso c2 = "-"c Then Return XmlMakeBeginCommentToken(precedingTrivia, scanTrailingTrivia) End If Case "["c @@ -1299,8 +1292,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' // Allow whitespace between the characters of a two-character token. length = GetWhitespaceLength(length) - If CanGet(length) Then - Dim c As Char = Peek(length) + If Peep(length, c) Then If c = "="c OrElse c = FULLWIDTH_EQUALS_SIGN Then length += 1 @@ -1311,8 +1303,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ElseIf c = "<"c OrElse c = FULLWIDTH_LESS_THAN_SIGN Then length += 1 - If CanGet(length) Then - c = Peek(length) + If Peep(length, c) Then 'if the second "<" is a part of "<%" - like in "<<%" , we do not want to use it. If c <> "%"c AndAlso c <> FULLWIDTH_PERCENT_SIGN Then @@ -1363,9 +1354,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Debug.Assert(IsIdentifierStartCharacter(Peek)) Debug.Assert(PeekStartComment(0) = 0) ' comment should be handled by caller - Dim ch = Peek() - If CanGet(1) Then - Dim ch1 = Peek(1) + Dim ch = Peek() : Dim ch1 As Char + If Peep(1, ch1) Then If IsConnectorPunctuation(ch) AndAlso Not IsIdentifierPartCharacter(ch1) Then Return MakeBadToken(precedingTrivia, 1, ERRID.ERR_ExpectedIdentifier) End If @@ -1376,8 +1366,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' // The C++ compiler refuses to inline IsIdentifierCharacter, so the ' // < 128 test is inline here. (This loop gets a *lot* of traffic.) ' TODO: make sure we get good perf here - While CanGet(len) - ch = Peek(len) + While Peep(len, ch) Dim code = Convert.ToUInt16(ch) If code < 128 AndAlso IsNarrowIdentifierCharacter(code) OrElse @@ -1391,16 +1380,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax 'Check for a type character Dim TypeCharacter As TypeCharacter = TypeCharacter.None - If CanGet(len) Then - ch = Peek(len) + If Peep(len, ch) Then FullWidthRepeat: Select Case ch Case "!"c ' // If the ! is followed by an identifier it is a dictionary lookup operator, not a type character. - If CanGet(len + 1) Then - Dim NextChar As Char = Peek(len + 1) - + Dim NextChar As Char + If Peep(len + 1, NextChar) Then If IsIdentifierStartCharacter(NextChar) OrElse MatchOneOrAnotherOrFullwidth(NextChar, "["c, "]"c) Then Exit Select @@ -1477,32 +1464,32 @@ FullWidthRepeat: End Function Private Function ScanBracketedIdentifier(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode)) As SyntaxToken - Debug.Assert(CanGet) ' [ - Debug.Assert(Peek() = "["c OrElse Peek() = FULLWIDTH_LEFT_SQUARE_BRACKET) - + Dim ch As Char +#If DEBUG Then + Debug.Assert(Peep(0, ch)) ' [ + Debug.Assert(ch = "["c OrElse ch = FULLWIDTH_LEFT_SQUARE_BRACKET) +#End If Dim IdStart As Integer = 1 Dim Here As Integer = IdStart Dim InvalidIdentifier As Boolean = False - If Not CanGet(Here) Then + If Not Peep(Here, ch) Then Return MakeBadToken(precedingTrivia, Here, ERRID.ERR_MissingEndBrack) End If - Dim ch = Peek(Here) - ' check if we can start an ident. If Not IsIdentifierStartCharacter(ch) OrElse (IsConnectorPunctuation(ch) AndAlso - Not (CanGet(Here + 1) AndAlso - IsIdentifierPartCharacter(Peek(Here + 1)))) Then + Not (Peep(Here + 1, ch) AndAlso + IsIdentifierPartCharacter(ch))) Then InvalidIdentifier = True End If ' check ident until ] - While CanGet(Here) - Dim [Next] As Char = Peek(Here) + Dim [Next] As Char + While Peep(Here, [Next]) If [Next] = "]"c OrElse [Next] = FULLWIDTH_RIGHT_SQUARE_BRACKET Then Dim IdStringLength As Integer = Here - IdStart @@ -1567,7 +1554,7 @@ FullWidthRepeat: Dim ch = Peek() If ch = "&"c OrElse ch = FULLWIDTH_AMPERSAND Then Here += 1 - ch = If(CanGet(Here), Peek(Here), ChrW(0)) + If Not Peep(Here, ch) Then ch = ChrW(0) FullWidthRepeat: Select Case ch @@ -1576,11 +1563,7 @@ FullWidthRepeat: IntegerLiteralStart = Here Base = LiteralBase.Hexadecimal - While CanGet(Here) - ch = Peek(Here) - If Not IsHexDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsHexDigit(ch) Here += 1 End While @@ -1589,11 +1572,7 @@ FullWidthRepeat: IntegerLiteralStart = Here Base = LiteralBase.Octal - While CanGet(Here) - ch = Peek(Here) - If Not IsOctalDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsOctalDigit(ch) Here += 1 End While @@ -1608,11 +1587,7 @@ FullWidthRepeat: Else ' no base specifier - just go through decimal digits. IntegerLiteralStart = Here - While CanGet(Here) - ch = Peek(Here) - If Not IsDecimalDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsDecimalDigit(ch) Here += 1 End While End If @@ -1622,21 +1597,17 @@ FullWidthRepeat: ' // Unless there was an explicit base specifier (which indicates an integer literal), ' // read the rest of a float literal. - If Base = LiteralBase.Decimal AndAlso CanGet(Here) Then + If Base = LiteralBase.Decimal AndAlso Peep(Here, ch) Then ' // First read a '.' followed by a sequence of one or more digits. - ch = Peek(Here) + If (ch = "."c Or ch = FULLWIDTH_FULL_STOP) AndAlso - CanGet(Here + 1) AndAlso - IsDecimalDigit(Peek(Here + 1)) Then + Peep(Here + 1, ch) AndAlso + IsDecimalDigit(ch) Then Here += 2 ' skip dot and first digit ' all following decimal digits belong to the literal (fractional part) - While CanGet(Here) - ch = Peek(Here) - If Not IsDecimalDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsDecimalDigit(ch) Here += 1 End While literalKind = NumericLiteralKind.Float @@ -1644,24 +1615,19 @@ FullWidthRepeat: ' // Read an exponent symbol followed by an optional sign and a sequence of ' // one or more digits. - If CanGet(Here) AndAlso BeginsExponent(Peek(Here)) Then + Dim cx As Char + If Peep(Here, cx) AndAlso BeginsExponent(cx) Then Here += 1 - If CanGet(Here) Then - ch = Peek(Here) - + If Peep(Here, ch) Then If MatchOneOrAnotherOrFullwidth(ch, "+"c, "-"c) Then Here += 1 End If End If - If CanGet(Here) AndAlso IsDecimalDigit(Peek(Here)) Then + If Peep(Here, cx) AndAlso IsDecimalDigit(cx) Then Here += 1 - While CanGet(Here) - ch = Peek(Here) - If Not IsDecimalDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsDecimalDigit(ch) Here += 1 End While Else @@ -1680,8 +1646,7 @@ FullWidthRepeat: Dim TypeCharacter As TypeCharacter = TypeCharacter.None - If CanGet(Here) Then - ch = Peek(Here) + If Peep(Here, ch) Then FullWidthRepeat2: Select Case ch @@ -1757,8 +1722,7 @@ FullWidthRepeat2: literalKind = NumericLiteralKind.Decimal ' check if this was not attempt to use obsolete exponent - If CanGet(Here + 1) Then - ch = Peek(Here + 1) + If Peep(Here + 1, ch) Then If IsDecimalDigit(ch) OrElse MatchOneOrAnotherOrFullwidth(ch, "+"c, "-"c) Then Return MakeBadToken(precedingTrivia, Here, ERRID.ERR_ObsoleteExponent) @@ -1947,25 +1911,16 @@ FullWidthRepeat2: ByRef Here As Integer ) As Boolean Debug.Assert(Here >= 0) + Dim ch As Char - If Not CanGet(Here) Then - Return False - End If - - Dim ch = Peek(Here) - If Not IsDecimalDigit(ch) Then + If Not Peep(Here, ch) OrElse Not IsDecimalDigit(ch) Then Return False End If Dim IntegralValue As Integer = IntegralLiteralCharacterValue(ch) Here += 1 - While CanGet(Here) - ch = Peek(Here) - - If Not IsDecimalDigit(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso IsDecimalDigit(ch) Dim nextDigit = IntegralLiteralCharacterValue(ch) If IntegralValue < 214748364 OrElse @@ -2000,7 +1955,7 @@ FullWidthRepeat2: Dim YearIsTwoDigits As Boolean = False Dim DaysToMonth As Integer() = Nothing Dim yearIsFirst As Boolean = False - + Dim ch As Char, sep1 As Char ' // Unfortunately, we can't fall back on OLE Automation's date parsing because ' // they don't have the same range as the URT's DateTime class @@ -2017,8 +1972,9 @@ FullWidthRepeat2: ' // If we see a /, then it's a date - If CanGet(Here) AndAlso IsDateSeparatorCharacter(Peek(Here)) Then + If Peep(Here, ch) AndAlso IsDateSeparatorCharacter(ch) Then Dim FirstDateSeparator As Integer = Here + sep1 = ch ' // We've got a date HaveDateValue = True @@ -2037,11 +1993,12 @@ FullWidthRepeat2: GoTo baddate End If + Dim c1 As Char ' Do we have a day value? - If CanGet(Here) AndAlso IsDateSeparatorCharacter(Peek(Here)) Then + If Peep(Here, c1) AndAlso IsDateSeparatorCharacter(c1) Then ' // Check to see they used a consistent separator - If Peek(Here) <> Peek(FirstDateSeparator) Then + If c1 <> sep1 Then GoTo baddate End If @@ -2063,11 +2020,11 @@ FullWidthRepeat2: End If ' // Do we have a year value? - - If CanGet(Here) AndAlso IsDateSeparatorCharacter(Peek(Here)) Then + Dim c1 As Char + If Peep(Here, c1) AndAlso IsDateSeparatorCharacter(c1) Then ' // Check to see they used a consistent separator - If Peek(Here) <> Peek(FirstDateSeparator) Then + If c1 <> sep1 Then GoTo baddate End If @@ -2106,8 +2063,8 @@ FullWidthRepeat2: If HaveTimeValue Then ' // Do we see a :? - - If CanGet(Here) AndAlso IsColon(Peek(Here)) Then + Dim c1 As Char + If Peep(Here, c1) AndAlso IsColon(c1) Then Here += 1 ' // Now let's get the minute value @@ -2120,7 +2077,7 @@ FullWidthRepeat2: ' // Do we have a second value? - If CanGet(Here) AndAlso IsColon(Peek(Here)) Then + If Peep(Here, c1) AndAlso IsColon(c1) Then ' // Yes. HaveSecondValue = True Here += 1 @@ -2135,24 +2092,24 @@ FullWidthRepeat2: ' // Check AM/PM - If CanGet(Here) Then - If Peek(Here) = "A"c OrElse Peek(Here) = FULLWIDTH_LATIN_CAPITAL_LETTER_A OrElse - Peek(Here) = "a"c OrElse Peek(Here) = FULLWIDTH_LATIN_SMALL_LETTER_A Then + If Peep(Here, c1) Then + If c1 = "A"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_A OrElse + c1 = "a"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_A Then HaveAM = True Here += 1 - ElseIf Peek(Here) = "P"c OrElse Peek(Here) = FULLWIDTH_LATIN_CAPITAL_LETTER_P OrElse - Peek(Here) = "p"c OrElse Peek(Here) = FULLWIDTH_LATIN_SMALL_LETTER_P Then + ElseIf c1 = "P"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_P OrElse + c1 = "p"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_P Then HavePM = True Here += 1 End If - If CanGet(Here) AndAlso (HaveAM OrElse HavePM) Then - If Peek(Here) = "M"c OrElse Peek(Here) = FULLWIDTH_LATIN_CAPITAL_LETTER_M OrElse - Peek(Here) = "m"c OrElse Peek(Here) = FULLWIDTH_LATIN_SMALL_LETTER_M Then + If Peep(Here, c1) AndAlso (HaveAM OrElse HavePM) Then + If c1 = "M"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_M OrElse + c1 = "m"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_M Then Here = GetWhitespaceLength(Here + 1) @@ -2169,7 +2126,7 @@ FullWidthRepeat2: End If End If - If Not CanGet(Here) OrElse Not IsHash(Peek(Here)) Then + If Not Peep(Here, ch) OrElse Not IsHash(ch) Then GoTo baddate End If @@ -2281,15 +2238,11 @@ baddate: ' // If we can find a closing #, then assume it's a malformed date, ' // otherwise, it's not a date - While CanGet(Here) - Dim ch As Char = Peek(Here) - If IsHash(ch) OrElse IsNewLine(ch) Then - Exit While - End If + While Peep(Here, ch) AndAlso Not (IsHash(ch) OrElse IsNewLine(ch)) Here += 1 End While - If Not CanGet(Here) OrElse IsNewLine(Peek(Here)) Then + If Not Peep(Here, ch) OrElse IsNewLine(ch) Then ' // No closing # Return Nothing Else @@ -2309,26 +2262,22 @@ baddate: ' // Check for a Char literal, which can be of the form: ' // """"c or ""c - - If CanGet(3) AndAlso IsDoubleQuote(Peek(2)) Then - If IsDoubleQuote(Peek(1)) Then - If IsDoubleQuote(Peek(3)) AndAlso - CanGet(4) AndAlso - IsLetterC(Peek(4)) Then + Dim c1, c2, c3, c4 As Char + If Peep(3, c3) AndAlso IsDoubleQuote(Peek(2)) Then + c1 = Peek(1) + If IsDoubleQuote(c1) Then + If IsDoubleQuote(c3) AndAlso Peep(4, c4) AndAlso IsLetterC(c4) Then ' // Double-quote Char literal: """"c Return MakeCharacterLiteralToken(precedingTrivia, """"c, 5) End If - ElseIf IsLetterC(Peek(3)) Then + ElseIf IsLetterC(c3) Then ' // Char literal. "x"c - Return MakeCharacterLiteralToken(precedingTrivia, Peek(1), 4) + Return MakeCharacterLiteralToken(precedingTrivia, c1, 4) End If End If - - If CanGet(2) AndAlso - IsDoubleQuote(Peek(1)) AndAlso - IsLetterC(Peek(2)) Then + If Peep(2, c2) AndAlso IsDoubleQuote(Peek(1)) AndAlso IsLetterC(c2) Then ' // Error. ""c is not a legal char constant Return MakeBadToken(precedingTrivia, 3, ERRID.ERR_IllegalCharConstant) @@ -2337,13 +2286,9 @@ baddate: Dim haveNewLine As Boolean = False Dim scratch = GetScratch() - While CanGet(length) - ch = Peek(length) - + While Peep(length, ch) If IsDoubleQuote(ch) Then - If CanGet(length + 1) Then - ch = Peek(length + 1) - + If Peep(length + 1, ch) Then If IsDoubleQuote(ch) Then ' // An escaped double quote scratch.Append(""""c) From 434ddf8f319878067f96b8f4a734c32fd1ba02a7 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Mon, 26 Oct 2015 23:40:41 +0000 Subject: [PATCH 4/7] Reducation in the number of introduced temporary char variables. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 105 +++++++++--------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index cbec6fc3c1bad..30a589811dcb0 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -919,19 +919,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Sub Private Function PeekStartComment(i As Integer) As Integer - Dim ch As Char, c2 As Char, c3 As Char + Dim ch As Char If Peep(i, ch) Then If IsSingleQuote(ch) Then Return 1 ElseIf MatchOneOrAnotherOrFullwidth(ch, "R"c, "r"c) AndAlso - Peep(i + 2, c2) AndAlso MatchOneOrAnotherOrFullwidth(Peek(i + 1), "E"c, "e"c) AndAlso - MatchOneOrAnotherOrFullwidth(c2, "M"c, "m"c) Then + Peep(i + 2, ch) AndAlso MatchOneOrAnotherOrFullwidth(Peek(i + 1), "E"c, "e"c) AndAlso + MatchOneOrAnotherOrFullwidth(ch, "M"c, "m"c) Then - If Not Peep(i + 3, c3) OrElse IsNewLine(c3) Then + If Not Peep(i + 3, ch) OrElse IsNewLine(ch) Then ' have only 'REM' Return 3 - ElseIf Not IsIdentifierPartCharacter(c3) Then + ElseIf Not IsIdentifierPartCharacter(ch) Then ' have 'REM ' Return 4 End If @@ -1264,13 +1264,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' Check for XML tokens If Not charIsFullWidth AndAlso Peep(length, c) Then - Dim c2 As Char Select Case c Case "!"c - If Peep(length + 2, c2) Then + If Peep(length + 2, c) Then Select Case (Peek(length + 1)) Case "-"c - If CanGet(length + 3) AndAlso c2 = "-"c Then + If CanGet(length + 3) AndAlso c = "-"c Then Return XmlMakeBeginCommentToken(precedingTrivia, scanTrailingTrivia) End If Case "["c @@ -1354,9 +1353,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Debug.Assert(IsIdentifierStartCharacter(Peek)) Debug.Assert(PeekStartComment(0) = 0) ' comment should be handled by caller - Dim ch = Peek() : Dim ch1 As Char - If Peep(1, ch1) Then - If IsConnectorPunctuation(ch) AndAlso Not IsIdentifierPartCharacter(ch1) Then + Dim cx As Char, ch = Peek() + If Peep(1, cx) Then + If IsConnectorPunctuation(ch) AndAlso Not IsIdentifierPartCharacter(cx) Then Return MakeBadToken(precedingTrivia, 1, ERRID.ERR_ExpectedIdentifier) End If End If @@ -1366,6 +1365,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax ' // The C++ compiler refuses to inline IsIdentifierCharacter, so the ' // < 128 test is inline here. (This loop gets a *lot* of traffic.) ' TODO: make sure we get good perf here + While Peep(len, ch) Dim code = Convert.ToUInt16(ch) @@ -1386,10 +1386,10 @@ FullWidthRepeat: Select Case ch Case "!"c ' // If the ! is followed by an identifier it is a dictionary lookup operator, not a type character. - Dim NextChar As Char - If Peep(len + 1, NextChar) Then - If IsIdentifierStartCharacter(NextChar) OrElse - MatchOneOrAnotherOrFullwidth(NextChar, "["c, "]"c) Then + ' Dim ch As Char + If Peep(len + 1, ch) Then + If IsIdentifierStartCharacter(ch) OrElse + MatchOneOrAnotherOrFullwidth(ch, "["c, "]"c) Then Exit Select End If End If @@ -1488,10 +1488,9 @@ FullWidthRepeat: End If ' check ident until ] - Dim [Next] As Char - While Peep(Here, [Next]) + While Peep(Here, ch) - If [Next] = "]"c OrElse [Next] = FULLWIDTH_RIGHT_SQUARE_BRACKET Then + If ch = "]"c OrElse ch = FULLWIDTH_RIGHT_SQUARE_BRACKET Then Dim IdStringLength As Integer = Here - IdStart If IdStringLength > 0 AndAlso Not InvalidIdentifier Then @@ -1513,9 +1512,9 @@ FullWidthRepeat: ' // The sequence "[]" does not define a valid identifier. Return MakeBadToken(precedingTrivia, Here + 1, ERRID.ERR_ExpectedIdentifier) End If - ElseIf IsNewLine([Next]) Then + ElseIf IsNewLine(ch) Then Exit While - ElseIf Not IsIdentifierPartCharacter([Next]) Then + ElseIf Not IsIdentifierPartCharacter(ch) Then InvalidIdentifier = True Exit While End If @@ -1615,8 +1614,7 @@ FullWidthRepeat: ' // Read an exponent symbol followed by an optional sign and a sequence of ' // one or more digits. - Dim cx As Char - If Peep(Here, cx) AndAlso BeginsExponent(cx) Then + If Peep(Here, ch) AndAlso BeginsExponent(ch) Then Here += 1 If Peep(Here, ch) Then @@ -1625,7 +1623,7 @@ FullWidthRepeat: End If End If - If Peep(Here, cx) AndAlso IsDecimalDigit(cx) Then + If Peep(Here, ch) AndAlso IsDecimalDigit(ch) Then Here += 1 While Peep(Here, ch) AndAlso IsDecimalDigit(ch) Here += 1 @@ -1733,17 +1731,17 @@ FullWidthRepeat2: End If Case "U"c, "u"c - If literalKind <> NumericLiteralKind.Float AndAlso CanGet(Here + 1) Then - Dim NextChar As Char = Peek(Here + 1) + If literalKind <> NumericLiteralKind.Float AndAlso Peep(Here + 1, ch) Then + ' Dim ch As Char = Peek(Here + 1) 'unsigned suffixes - US, UL, UI - If MatchOneOrAnotherOrFullwidth(NextChar, "S"c, "s"c) Then + If MatchOneOrAnotherOrFullwidth(ch, "S"c, "s"c) Then TypeCharacter = TypeCharacter.UShortLiteral Here += 2 - ElseIf MatchOneOrAnotherOrFullwidth(NextChar, "I"c, "i"c) Then + ElseIf MatchOneOrAnotherOrFullwidth(ch, "I"c, "i"c) Then TypeCharacter = TypeCharacter.UIntegerLiteral Here += 2 - ElseIf MatchOneOrAnotherOrFullwidth(NextChar, "L"c, "l"c) Then + ElseIf MatchOneOrAnotherOrFullwidth(ch, "L"c, "l"c) Then TypeCharacter = TypeCharacter.ULongLiteral Here += 2 End If @@ -1955,7 +1953,7 @@ FullWidthRepeat2: Dim YearIsTwoDigits As Boolean = False Dim DaysToMonth As Integer() = Nothing Dim yearIsFirst As Boolean = False - Dim ch As Char, sep1 As Char + Dim ch, cx As Char, sep1 As Char ' // Unfortunately, we can't fall back on OLE Automation's date parsing because ' // they don't have the same range as the URT's DateTime class @@ -1993,12 +1991,11 @@ FullWidthRepeat2: GoTo baddate End If - Dim c1 As Char ' Do we have a day value? - If Peep(Here, c1) AndAlso IsDateSeparatorCharacter(c1) Then + If Peep(Here, cx) AndAlso IsDateSeparatorCharacter(cx) Then ' // Check to see they used a consistent separator - If c1 <> sep1 Then + If cx <> sep1 Then GoTo baddate End If @@ -2020,11 +2017,10 @@ FullWidthRepeat2: End If ' // Do we have a year value? - Dim c1 As Char - If Peep(Here, c1) AndAlso IsDateSeparatorCharacter(c1) Then + If Peep(Here, cx) AndAlso IsDateSeparatorCharacter(cx) Then ' // Check to see they used a consistent separator - If c1 <> sep1 Then + If cx <> sep1 Then GoTo baddate End If @@ -2063,8 +2059,7 @@ FullWidthRepeat2: If HaveTimeValue Then ' // Do we see a :? - Dim c1 As Char - If Peep(Here, c1) AndAlso IsColon(c1) Then + If Peep(Here, cx) AndAlso IsColon(cx) Then Here += 1 ' // Now let's get the minute value @@ -2077,7 +2072,7 @@ FullWidthRepeat2: ' // Do we have a second value? - If Peep(Here, c1) AndAlso IsColon(c1) Then + If Peep(Here, cx) AndAlso IsColon(cx) Then ' // Yes. HaveSecondValue = True Here += 1 @@ -2092,24 +2087,24 @@ FullWidthRepeat2: ' // Check AM/PM - If Peep(Here, c1) Then - If c1 = "A"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_A OrElse - c1 = "a"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_A Then + If Peep(Here, cx) Then + If cx = "A"c OrElse cx = FULLWIDTH_LATIN_CAPITAL_LETTER_A OrElse + cx = "a"c OrElse cx = FULLWIDTH_LATIN_SMALL_LETTER_A Then HaveAM = True Here += 1 - ElseIf c1 = "P"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_P OrElse - c1 = "p"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_P Then + ElseIf cx = "P"c OrElse cx = FULLWIDTH_LATIN_CAPITAL_LETTER_P OrElse + cx = "p"c OrElse cx = FULLWIDTH_LATIN_SMALL_LETTER_P Then HavePM = True Here += 1 End If - If Peep(Here, c1) AndAlso (HaveAM OrElse HavePM) Then - If c1 = "M"c OrElse c1 = FULLWIDTH_LATIN_CAPITAL_LETTER_M OrElse - c1 = "m"c OrElse c1 = FULLWIDTH_LATIN_SMALL_LETTER_M Then + If Peep(Here, cx) AndAlso (HaveAM OrElse HavePM) Then + If cx = "M"c OrElse cx = FULLWIDTH_LATIN_CAPITAL_LETTER_M OrElse + cx = "m"c OrElse cx = FULLWIDTH_LATIN_SMALL_LETTER_M Then Here = GetWhitespaceLength(Here + 1) @@ -2246,7 +2241,7 @@ baddate: ' // No closing # Return Nothing Else - Debug.Assert(IsHash(Peek(Here))) + Debug.Assert(IsHash(ch)) Here += 1 ' consume trailing # Return MakeBadToken(precedingTrivia, Here, ERRID.ERR_InvalidDate) End If @@ -2262,22 +2257,22 @@ baddate: ' // Check for a Char literal, which can be of the form: ' // """"c or ""c - Dim c1, c2, c3, c4 As Char - If Peep(3, c3) AndAlso IsDoubleQuote(Peek(2)) Then - c1 = Peek(1) - If IsDoubleQuote(c1) Then - If IsDoubleQuote(c3) AndAlso Peep(4, c4) AndAlso IsLetterC(c4) Then + Dim cx As Char + If Peep(3, cx) AndAlso IsDoubleQuote(Peek(2)) Then + ch = Peek(1) + If IsDoubleQuote(ch) Then + If IsDoubleQuote(cx) AndAlso Peep(4, ch) AndAlso IsLetterC(ch) Then ' // Double-quote Char literal: """"c Return MakeCharacterLiteralToken(precedingTrivia, """"c, 5) End If - ElseIf IsLetterC(c3) Then + ElseIf IsLetterC(cx) Then ' // Char literal. "x"c - Return MakeCharacterLiteralToken(precedingTrivia, c1, 4) + Return MakeCharacterLiteralToken(precedingTrivia, ch, 4) End If End If - If Peep(2, c2) AndAlso IsDoubleQuote(Peek(1)) AndAlso IsLetterC(c2) Then + If Peep(2, ch) AndAlso IsDoubleQuote(Peek(1)) AndAlso IsLetterC(ch) Then ' // Error. ""c is not a legal char constant Return MakeBadToken(precedingTrivia, 3, ERRID.ERR_IllegalCharConstant) From e6edb1cc33920f9b7b12fc987992dd48b6c6a7e7 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Wed, 28 Oct 2015 01:02:35 +0000 Subject: [PATCH 5/7] [Compiler VB Scanner] Tweaks to the temporaries use with `Peep`. Removed some commented out code. --- .../VisualBasic/Portable/Scanner/Scanner.vb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb index 30a589811dcb0..a97fc2c1de907 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/Scanner.vb @@ -984,7 +984,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Private Function ScanTokenCommon(precedingTrivia As SyntaxList(Of VisualBasicSyntaxNode), ch As Char, fullWidth As Boolean) As SyntaxToken Dim lengthWithMaybeEquals As Integer = 1 - Dim cx As Char Select Case ch Case CARRIAGE_RETURN, LINE_FEED Return ScanNewlineAsStatementTerminator(ch, precedingTrivia) @@ -1093,7 +1092,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return MakeExclamationToken(precedingTrivia, fullWidth) Case "."c - If Peep(1, cx) AndAlso IsDecimalDigit(cx) Then + If Peep(1, ch) AndAlso IsDecimalDigit(ch) Then Return ScanNumericLiteral(precedingTrivia) Else Return MakeDotToken(precedingTrivia, fullWidth) @@ -1141,13 +1140,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return ScanIdentifierOrKeyword(precedingTrivia) Case "_"c - If Peep(1, cx) AndAlso IsIdentifierPartCharacter(cx) Then + If Peep(1, ch) AndAlso IsIdentifierPartCharacter(ch) Then Return ScanIdentifierOrKeyword(precedingTrivia) End If Dim err As ERRID = ERRID.ERR_ExpectedIdentifier Dim len = GetWhitespaceLength(1) - If Not Peep(len, cx) OrElse IsNewLine(cx) OrElse PeekStartComment(len) > 0 Then + If Not Peep(len, ch) OrElse IsNewLine(ch) OrElse PeekStartComment(len) > 0 Then err = ERRID.ERR_LineContWithCommentOrNoPrecSpace End If @@ -1166,6 +1165,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Case "$"c, FULLWIDTH_DOLLAR_SIGN + Dim cx As Char If Not fullWidth AndAlso Peep(1, cx) AndAlso IsDoubleQuote(cx) Then Return MakePunctuationToken(precedingTrivia, 2, SyntaxKind.DollarSignDoubleQuoteToken) End If @@ -1732,8 +1732,6 @@ FullWidthRepeat2: Case "U"c, "u"c If literalKind <> NumericLiteralKind.Float AndAlso Peep(Here + 1, ch) Then - ' Dim ch As Char = Peek(Here + 1) - 'unsigned suffixes - US, UL, UI If MatchOneOrAnotherOrFullwidth(ch, "S"c, "s"c) Then TypeCharacter = TypeCharacter.UShortLiteral @@ -1953,7 +1951,7 @@ FullWidthRepeat2: Dim YearIsTwoDigits As Boolean = False Dim DaysToMonth As Integer() = Nothing Dim yearIsFirst As Boolean = False - Dim ch, cx As Char, sep1 As Char + Dim ch, cx, FirstDateSeparator As Char ' // Unfortunately, we can't fall back on OLE Automation's date parsing because ' // they don't have the same range as the URT's DateTime class @@ -1971,8 +1969,7 @@ FullWidthRepeat2: ' // If we see a /, then it's a date If Peep(Here, ch) AndAlso IsDateSeparatorCharacter(ch) Then - Dim FirstDateSeparator As Integer = Here - sep1 = ch + FirstDateSeparator = ch ' // We've got a date HaveDateValue = True @@ -1995,7 +1992,7 @@ FullWidthRepeat2: If Peep(Here, cx) AndAlso IsDateSeparatorCharacter(cx) Then ' // Check to see they used a consistent separator - If cx <> sep1 Then + If cx <> FirstDateSeparator Then GoTo baddate End If @@ -2020,7 +2017,7 @@ FullWidthRepeat2: If Peep(Here, cx) AndAlso IsDateSeparatorCharacter(cx) Then ' // Check to see they used a consistent separator - If cx <> sep1 Then + If cx <> FirstDateSeparator Then GoTo baddate End If From 65f9bfe8f1fb0b4bef433c6da618bcf8b6309411 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Thu, 29 Oct 2015 20:20:50 +0000 Subject: [PATCH 6/7] Reduce the amount of temporary char variables introduced by using `Peep` --- .../Portable/Scanner/ScannerXml.vb | 55 ++++++++----------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb b/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb index b696c20ddeeac..04c79d50b4b4c 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/ScannerXml.vb @@ -91,8 +91,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax leadingTrivia = ScanXmlTrivia(c) Case "/"c - Dim ch As Char - If Peep(1, ch) AndAlso ch = ">"c Then + If Peep(1, c) AndAlso c = ">"c Then Return XmlMakeEndEmptyElementToken(leadingTrivia) End If Return XmlMakeDivToken(leadingTrivia) @@ -112,12 +111,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return XmlMakeDoubleQuoteToken(leadingTrivia, c, isOpening:=True) Case "<"c - Dim ch As Char, cx As Char - If Peep(1, ch) Then - Select Case ch + If Peep(1, c) Then + Select Case c Case "!"c - If Peep(2, cx) Then - Select Case cx + If Peep(2, c) Then + Select Case c Case "-"c If NextIs(3, "-"c) Then Return XmlMakeBeginCommentToken(leadingTrivia, s_scanNoTriviaFunc) @@ -127,7 +125,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax Return XmlMakeBeginCDataToken(leadingTrivia, s_scanNoTriviaFunc) End If Case "D"c - If Nextare(3, "OCTYPE") Then + If NextAre(3, "OCTYPE") Then Return XmlMakeBeginDTDToken(leadingTrivia) End If End Select @@ -340,13 +338,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End If Debug.Assert(Here = 0) - Dim ch As Char - If Peep(1, ch) Then - Select Case ch + If Peep(1, c) Then + Select Case c Case "!"c - Dim cx As Char - If Peep(2, cx) Then - Select Case cx + If Peep(2, c) Then + Select Case c Case "-"c If NextIs(3, "-"c) Then Return XmlMakeBeginCommentToken(precedingTrivia, s_scanNoTriviaFunc) @@ -729,9 +725,8 @@ CleanUp: precedingTrivia = ScanXmlTrivia(c) Case "<"c - Dim ch As Char - If Peep(1, ch) Then - Select Case ch + If Peep(1, c) Then + Select Case c Case "!"c If NextAre(2, "--") Then Return XmlMakeBeginCommentToken(precedingTrivia, s_scanNoTriviaFunc) @@ -987,7 +982,7 @@ CleanUp: Dim c2 As Char #If DEBUG Then Debug.Assert(Here >= 0) - dim ok = Peep(Here, c2) + Dim ok = Peep(Here, c2) Debug.Assert(ok) Debug.Assert(c2 = c1) #End If @@ -1162,9 +1157,8 @@ CreateNCNameToken: Case "a"c ' // & ' // ' - Dim ch As Char - If Peep(4, ch) AndAlso NextAre(2, "mp") Then - If ch = ";"c Then + If Peep(4, c) AndAlso NextAre(2, "mp") Then + If c = ";"c Then Return XmlMakeAmpLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 4, "&") @@ -1172,9 +1166,9 @@ CreateNCNameToken: Return DirectCast(noSemicolon.SetDiagnostics({noSemicolonError}), XmlTextTokenSyntax) End If - ElseIf Peep(5, ch) AndAlso NextAre(2, "pos") Then + ElseIf Peep(5, c) AndAlso NextAre(2, "pos") Then - If ch = ";"c Then + If c = ";"c Then Return XmlMakeAposLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 5, "'") @@ -1185,10 +1179,9 @@ CreateNCNameToken: Case "l"c ' // < - Dim ch As Char - If Peep(3, ch) AndAlso NextIs(2, "t"c) Then + If Peep(3, c) AndAlso NextIs(2, "t"c) Then - If ch = ";"c Then + If c = ";"c Then Return XmlMakeLtLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 3, "<") @@ -1199,10 +1192,9 @@ CreateNCNameToken: Case "g"c ' // > - Dim ch As Char - If Peep(3, ch) AndAlso NextIs(2, "t"c) Then + If Peep(3, c) AndAlso NextIs(2, "t"c) Then - If ch = ";"c Then + If c = ";"c Then Return XmlMakeGtLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 3, ">") @@ -1213,10 +1205,9 @@ CreateNCNameToken: Case "q"c ' // " - Dim ch As Char - If Peep(5, ch) AndAlso NextAre(2, "uot") Then + If Peep(5, c) AndAlso NextAre(2, "uot") Then - If ch = ";"c Then + If c = ";"c Then Return XmlMakeQuotLiteralToken(precedingTrivia) Else Dim noSemicolon = XmlMakeEntityLiteralToken(precedingTrivia, 5, """") From 049aea832bd88bdd77c2834dbf08d3827071fd38 Mon Sep 17 00:00:00 2001 From: AdamSpeight2008 Date: Thu, 29 Oct 2015 22:19:53 +0000 Subject: [PATCH 7/7] Reduction in the number of temporary char variables introduces with the usage of `Peep` --- .../Portable/Scanner/ScannerInterpolatedString.vb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb b/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb index df7c361f161b7..db9467271e183 100644 --- a/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb +++ b/src/Compilers/VisualBasic/Portable/Scanner/ScannerInterpolatedString.vb @@ -112,21 +112,21 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax End Function Private Function IsInterpolatedStringPunctuation(Optional offset As Integer = 0) As Boolean - Dim c, ch As Char + Dim c As Char If Not Peep(offset, c) Then Return False If IsLeftCurlyBracket(c) Then - Return Not Peep(offset + 1, ch) OrElse Not IsLeftCurlyBracket(ch) + Return Not Peep(offset + 1, c) OrElse Not IsLeftCurlyBracket(c) ElseIf IsRightCurlyBracket(c) Then - Return Not Peep(offset + 1, ch) OrElse Not IsRightCurlyBracket(ch) + Return Not Peep(offset + 1, c) OrElse Not IsRightCurlyBracket(c) ElseIf IsDoubleQuote(c) 'A subtle difference between this case and the one above. ' In both interpolated and literal strings the two quote characters used in an escape sequence don't have to match. ' It's enough that the next character is *a* quote char. It doesn't have to be the same quote. ' If we want to preserve consistency the quotes need to be special cased. - Return Not Peep(offset + 1, ch) OrElse Not IsDoubleQuote(ch) + Return Not Peep(offset + 1, c) OrElse Not IsDoubleQuote(c) Else Return False