forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseInterpolatedString.vb
335 lines (241 loc) · 15.5 KB
/
ParseInterpolatedString.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Runtime.InteropServices
Imports Microsoft.CodeAnalysis.Syntax.InternalSyntax
Imports InternalSyntaxFactory = Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax.SyntaxFactory
'
'============ Methods for parsing portions of executable statements ==
'
Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Partial Friend Class Parser
Private Function ParseInterpolatedStringExpression() As InterpolatedStringExpressionSyntax
Debug.Assert(CurrentToken.Kind = SyntaxKind.DollarSignDoubleQuoteToken, $"{NameOf(ParseInterpolatedStringExpression)} called on the wrong token.")
ResetCurrentToken(ScannerState.InterpolatedStringPunctuation)
Debug.Assert(CurrentToken.Kind = SyntaxKind.DollarSignDoubleQuoteToken, "Rescanning $"" failed.")
Dim dollarSignDoubleQuoteToken = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken(ScannerState.InterpolatedStringContent)
Dim contentBuilder = _pool.Allocate(Of InterpolatedStringContentSyntax)
Dim doubleQuoteToken As PunctuationSyntax
Dim skipped As SyntaxListBuilder(Of SyntaxToken) = Nothing
Do
Dim content As InterpolatedStringContentSyntax
Select Case CurrentToken.Kind
Case SyntaxKind.InterpolatedStringTextToken
Dim textToken = DirectCast(CurrentToken, InterpolatedStringTextTokenSyntax)
' At this point we're either right before an {, an } (error), or a "".
GetNextToken(ScannerState.InterpolatedStringPunctuation)
Debug.Assert(CurrentToken.Kind <> SyntaxKind.InterpolatedStringTextToken,
"Two interpolated string text literal tokens encountered back-to-back. " &
"Scanner should have scanned these as a single token.")
content = SyntaxFactory.InterpolatedStringText(textToken)
Case SyntaxKind.OpenBraceToken
content = ParseInterpolatedStringInterpolation()
Case SyntaxKind.CloseBraceToken
If skipped.IsNull Then
skipped = _pool.Allocate(Of SyntaxToken)
End If
skipped.Add(CurrentToken)
GetNextToken(ScannerState.InterpolatedStringContent)
Continue Do
Case SyntaxKind.DoubleQuoteToken
doubleQuoteToken = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken()
Exit Do
Case SyntaxKind.EndOfInterpolatedStringToken
doubleQuoteToken = InternalSyntaxFactory.MissingPunctuation(SyntaxKind.DoubleQuoteToken)
GetNextToken(ScannerState.VB)
Exit Do
Case Else
doubleQuoteToken = InternalSyntaxFactory.MissingPunctuation(SyntaxKind.DoubleQuoteToken)
Exit Do
End Select
If Not skipped.IsNull Then
content = AddLeadingSyntax(content, _pool.ToListAndFree(skipped), ERRID.ERR_Syntax)
skipped = Nothing
End If
contentBuilder.Add(content)
Loop
If Not skipped.IsNull Then
doubleQuoteToken = AddLeadingSyntax(doubleQuoteToken, _pool.ToListAndFree(skipped), ERRID.ERR_Syntax)
skipped = Nothing
End If
Dim node = SyntaxFactory.InterpolatedStringExpression(dollarSignDoubleQuoteToken,
_pool.ToListAndFree(contentBuilder),
doubleQuoteToken)
Return CheckFeatureAvailability(Feature.InterpolatedStrings, node)
End Function
Private Function ParseInterpolatedStringInterpolation() As InterpolationSyntax
Debug.Assert(CurrentToken.Kind = SyntaxKind.OpenBraceToken, $"{NameOf(ParseInterpolatedStringInterpolation)} called on the wrong token.")
Dim colonToken As PunctuationSyntax = Nothing
Dim excessText As String = Nothing
Dim openBraceToken = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken(ScannerState.VB)
Dim expression As ExpressionSyntax
If CurrentToken.Kind = SyntaxKind.ColonToken Then
openBraceToken = DirectCast(RemoveTrailingColonTriviaAndConvertToColonToken(openBraceToken, colonToken, excessText), PunctuationSyntax)
expression = ReportSyntaxError(InternalSyntaxFactory.MissingExpression(), ERRID.ERR_ExpectedExpression)
Else
expression = ParseExpressionCore()
' Scanned this as a terminator. Fix it.
If CurrentToken.Kind = SyntaxKind.ColonToken Then
expression = DirectCast(RemoveTrailingColonTriviaAndConvertToColonToken(expression, colonToken, excessText), ExpressionSyntax)
End If
End If
Dim alignmentClauseOpt As InterpolationAlignmentClauseSyntax
If CurrentToken.Kind = SyntaxKind.CommaToken Then
Debug.Assert(colonToken Is Nothing)
Dim commaToken = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken(ScannerState.VB)
If CurrentToken.Kind = SyntaxKind.ColonToken Then
commaToken = DirectCast(RemoveTrailingColonTriviaAndConvertToColonToken(commaToken, colonToken, excessText), PunctuationSyntax)
End If
Dim signTokenOpt As PunctuationSyntax
If CurrentToken.Kind = SyntaxKind.MinusToken OrElse
CurrentToken.Kind = SyntaxKind.PlusToken Then
signTokenOpt = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken(ScannerState.VB)
If CurrentToken.Kind = SyntaxKind.ColonToken Then
signTokenOpt = DirectCast(RemoveTrailingColonTriviaAndConvertToColonToken(signTokenOpt, colonToken, excessText), PunctuationSyntax)
End If
Else
signTokenOpt = Nothing
End If
Dim widthToken As IntegerLiteralTokenSyntax
If CurrentToken.Kind = SyntaxKind.IntegerLiteralToken Then
widthToken = DirectCast(CurrentToken, IntegerLiteralTokenSyntax)
GetNextToken(ScannerState.VB)
If CurrentToken.Kind = SyntaxKind.ColonToken Then
widthToken = DirectCast(RemoveTrailingColonTriviaAndConvertToColonToken(widthToken, colonToken, excessText), IntegerLiteralTokenSyntax)
End If
Else
widthToken = ReportSyntaxError(InternalSyntaxFactory.MissingIntegerLiteralToken(), ERRID.ERR_ExpectedIntLiteral)
End If
Dim valueExpression As ExpressionSyntax = SyntaxFactory.NumericLiteralExpression(widthToken)
If signTokenOpt IsNot Nothing Then
valueExpression = SyntaxFactory.UnaryExpression(
If(signTokenOpt.Kind = SyntaxKind.PlusToken, SyntaxKind.UnaryPlusExpression, SyntaxKind.UnaryMinusExpression),
signTokenOpt,
valueExpression)
End If
alignmentClauseOpt = SyntaxFactory.InterpolationAlignmentClause(commaToken, valueExpression)
Else
alignmentClauseOpt = Nothing
End If
Dim formatStringClauseOpt As InterpolationFormatClauseSyntax
If CurrentToken.Kind = SyntaxKind.ColonToken AndAlso colonToken IsNot Nothing Then
' If colonToken IsNot Nothing we were able to recovery.
GetNextToken(ScannerState.InterpolatedStringFormatString)
Dim formatStringToken As InterpolatedStringTextTokenSyntax
If CurrentToken.Kind = SyntaxKind.InterpolatedStringTextToken Then
formatStringToken = DirectCast(CurrentToken, InterpolatedStringTextTokenSyntax)
GetNextToken(ScannerState.InterpolatedStringPunctuation)
If excessText IsNot Nothing Then
formatStringToken = InternalSyntaxFactory.InterpolatedStringTextToken(excessText & formatStringToken.Text,
excessText & formatStringToken.Value,
formatStringToken.GetLeadingTrivia(),
formatStringToken.GetTrailingTrivia())
End If
Else
If excessText IsNot Nothing Then
formatStringToken = InternalSyntaxFactory.InterpolatedStringTextToken(excessText,
excessText,
Nothing,
Nothing)
Else
formatStringToken = Nothing
End If
End If
If formatStringToken Is Nothing Then
formatStringToken = DirectCast(InternalSyntaxFactory.MissingToken(SyntaxKind.InterpolatedStringTextToken), InterpolatedStringTextTokenSyntax)
formatStringToken = ReportSyntaxError(formatStringToken, ERRID.ERR_Syntax)
ElseIf formatStringToken.GetTrailingTrivia() IsNot Nothing Then
formatStringToken = ReportSyntaxError(formatStringToken, ERRID.ERR_InterpolationFormatWhitespace)
End If
formatStringClauseOpt = SyntaxFactory.InterpolationFormatClause(colonToken, formatStringToken)
Else
formatStringClauseOpt = Nothing
If CurrentToken.Kind = SyntaxKind.ColonToken Then
' But if colonToken is null we weren't able to gracefully recover.
GetNextToken(ScannerState.InterpolatedStringFormatString)
End If
End If
Dim closeBraceToken As PunctuationSyntax
If CurrentToken.Kind = SyntaxKind.CloseBraceToken Then
' Must rescan this with interpolated string rules for trailing trivia.
' Specifically, any trailing trivia attached to the closing brace is actually interpolated string content.
ResetCurrentToken(ScannerState.InterpolatedStringPunctuation)
closeBraceToken = DirectCast(CurrentToken, PunctuationSyntax)
GetNextToken(ScannerState.InterpolatedStringContent)
ElseIf CurrentToken.Kind = SyntaxKind.EndOfInterpolatedStringToken Then
GetNextToken(ScannerState.VB)
closeBraceToken = DirectCast(HandleUnexpectedToken(SyntaxKind.CloseBraceToken), PunctuationSyntax)
Else
' Content rules will either resync at a } or at the closing ".
If Not IsValidStatementTerminator(CurrentToken) Then
ResetCurrentToken(ScannerState.InterpolatedStringFormatString)
End If
Debug.Assert(CurrentToken.Kind <> SyntaxKind.CloseBraceToken)
closeBraceToken = DirectCast(HandleUnexpectedToken(SyntaxKind.CloseBraceToken), PunctuationSyntax)
If CurrentToken.Kind = SyntaxKind.InterpolatedStringTextToken Then
ResetCurrentToken(ScannerState.InterpolatedStringContent)
GetNextToken(ScannerState.InterpolatedStringContent)
End If
End If
Return SyntaxFactory.Interpolation(openBraceToken, expression, alignmentClauseOpt, formatStringClauseOpt, closeBraceToken)
End Function
Private Shared Function RemoveTrailingColonTriviaAndConvertToColonToken(
token As SyntaxToken,
<Out> ByRef colonToken As PunctuationSyntax,
<Out> ByRef excessText As String
) As SyntaxToken
If Not token.HasTrailingTrivia Then
colonToken = Nothing
excessText = Nothing
Return token
End If
Dim triviaList As New CodeAnalysis.Syntax.InternalSyntax.SyntaxList(Of VisualBasicSyntaxNode)(token.GetTrailingTrivia())
Dim indexOfFirstColon As Integer = -1
Dim newTrailingTrivia As GreenNode
If triviaList.Count = 1 Then
indexOfFirstColon = 0
newTrailingTrivia = Nothing
excessText = Nothing
ElseIf triviaList(0).Kind = SyntaxKind.ColonTrivia Then
indexOfFirstColon = 0
newTrailingTrivia = Nothing
excessText = triviaList.GetEndOfTrivia(1).Node.ToFullString()
Else
For i = 0 To triviaList.Count - 1
If triviaList(i).Kind = SyntaxKind.ColonTrivia Then
indexOfFirstColon = i
Exit For
End If
Next
newTrailingTrivia = triviaList.GetStartOfTrivia(indexOfFirstColon).Node
If indexOfFirstColon = triviaList.Count - 1 Then
excessText = Nothing
Else
excessText = triviaList.GetEndOfTrivia(indexOfFirstColon + 1).Node.ToFullString()
Debug.Assert(triviaList.GetEndOfTrivia(indexOfFirstColon + 1).AnyAndOnly(SyntaxKind.ColonTrivia, SyntaxKind.WhitespaceTrivia))
End If
End If
Dim firstColonTrivia = DirectCast(triviaList(indexOfFirstColon), SyntaxTrivia)
colonToken = New PunctuationSyntax(SyntaxKind.ColonToken, firstColonTrivia.Text, Nothing, Nothing)
Return DirectCast(token.WithTrailingTrivia(newTrailingTrivia), SyntaxToken)
End Function
Private Function RemoveTrailingColonTriviaAndConvertToColonToken(
node As VisualBasicSyntaxNode,
<Out> ByRef colonToken As PunctuationSyntax,
<Out> ByRef excessText As String
) As VisualBasicSyntaxNode
Dim lastNonMissing = DirectCast(node.GetLastToken(), SyntaxToken)
Dim newLastNonMissing = RemoveTrailingColonTriviaAndConvertToColonToken(lastNonMissing, colonToken, excessText)
Dim newNode = LastTokenReplacer.Replace(node, Function(t) If(t Is lastNonMissing, newLastNonMissing, t))
' If no token was replaced we have failed to recover; let high contexts deal with it.
If newNode Is node Then
colonToken = Nothing
excessText = Nothing
End If
Return newNode
End Function
End Class
End Namespace