-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Lexer_StringLiteral.cs
708 lines (660 loc) · 29.8 KB
/
Lexer_StringLiteral.cs
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.PooledObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax
{
internal partial class Lexer
{
private void ScanStringLiteral(ref TokenInfo info, bool allowEscapes = true)
{
var quoteCharacter = TextWindow.PeekChar();
if (quoteCharacter == '\'' || quoteCharacter == '"')
{
TextWindow.AdvanceChar();
_builder.Length = 0;
while (true)
{
char ch = TextWindow.PeekChar();
if (ch == '\\' && allowEscapes)
{
// normal string & char constants can have escapes
char c2;
ch = this.ScanEscapeSequence(out c2);
_builder.Append(ch);
if (c2 != SlidingTextWindow.InvalidCharacter)
{
_builder.Append(c2);
}
}
else if (ch == quoteCharacter)
{
TextWindow.AdvanceChar();
break;
}
else if (SyntaxFacts.IsNewLine(ch) ||
(ch == SlidingTextWindow.InvalidCharacter && TextWindow.IsReallyAtEnd()))
{
//String and character literals can contain any Unicode character. They are not limited
//to valid UTF-16 characters. So if we get the SlidingTextWindow's sentinel value,
//double check that it was not real user-code contents. This will be rare.
Debug.Assert(TextWindow.Width > 0);
this.AddError(ErrorCode.ERR_NewlineInConst);
break;
}
else
{
TextWindow.AdvanceChar();
_builder.Append(ch);
}
}
info.Text = TextWindow.GetText(true);
if (quoteCharacter == '\'')
{
info.Kind = SyntaxKind.CharacterLiteralToken;
if (_builder.Length != 1)
{
this.AddError((_builder.Length != 0) ? ErrorCode.ERR_TooManyCharsInConst : ErrorCode.ERR_EmptyCharConst);
}
if (_builder.Length > 0)
{
info.StringValue = TextWindow.Intern(_builder);
info.CharValue = info.StringValue[0];
}
else
{
info.StringValue = string.Empty;
info.CharValue = SlidingTextWindow.InvalidCharacter;
}
}
else
{
info.Kind = SyntaxKind.StringLiteralToken;
if (_builder.Length > 0)
{
info.StringValue = TextWindow.Intern(_builder);
}
else
{
info.StringValue = string.Empty;
}
}
}
else
{
info.Kind = SyntaxKind.None;
info.Text = null;
}
}
private char ScanEscapeSequence(out char surrogateCharacter)
{
var start = TextWindow.Position;
surrogateCharacter = SlidingTextWindow.InvalidCharacter;
char ch = TextWindow.NextChar();
Debug.Assert(ch == '\\');
ch = TextWindow.NextChar();
switch (ch)
{
// escaped characters that translate to themselves
case '\'':
case '"':
case '\\':
break;
// translate escapes as per C# spec 2.4.4.4
case '0':
ch = '\u0000';
break;
case 'a':
ch = '\u0007';
break;
case 'b':
ch = '\u0008';
break;
case 'f':
ch = '\u000c';
break;
case 'n':
ch = '\u000a';
break;
case 'r':
ch = '\u000d';
break;
case 't':
ch = '\u0009';
break;
case 'v':
ch = '\u000b';
break;
case 'x':
case 'u':
case 'U':
TextWindow.Reset(start);
SyntaxDiagnosticInfo error;
ch = TextWindow.NextUnicodeEscape(surrogateCharacter: out surrogateCharacter, info: out error);
AddError(error);
break;
default:
this.AddError(start, TextWindow.Position - start, ErrorCode.ERR_IllegalEscape);
break;
}
return ch;
}
private void ScanVerbatimStringLiteral(ref TokenInfo info, bool allowNewlines = true)
{
_builder.Length = 0;
if (TextWindow.PeekChar() == '@' && TextWindow.PeekChar(1) == '"')
{
TextWindow.AdvanceChar(2);
bool done = false;
char ch;
_builder.Length = 0;
while (!done)
{
switch (ch = TextWindow.PeekChar())
{
case '"':
TextWindow.AdvanceChar();
if (TextWindow.PeekChar() == '"')
{
// Doubled quote -- skip & put the single quote in the string
TextWindow.AdvanceChar();
_builder.Append(ch);
}
else
{
done = true;
}
break;
case SlidingTextWindow.InvalidCharacter:
if (!TextWindow.IsReallyAtEnd())
{
goto default;
}
// Reached the end of the source without finding the end-quote. Give
// an error back at the starting point.
this.AddError(ErrorCode.ERR_UnterminatedStringLit);
done = true;
break;
default:
if (!allowNewlines && SyntaxFacts.IsNewLine(ch))
{
this.AddError(ErrorCode.ERR_UnterminatedStringLit);
done = true;
break;
}
TextWindow.AdvanceChar();
_builder.Append(ch);
break;
}
}
info.Kind = SyntaxKind.StringLiteralToken;
info.Text = TextWindow.GetText(false);
info.StringValue = _builder.ToString();
}
else
{
info.Kind = SyntaxKind.None;
info.Text = null;
info.StringValue = null;
}
}
private void ScanInterpolatedStringLiteral(bool isVerbatim, ref TokenInfo info)
{
// We have a string of the form
// $" ... "
// or, if isVerbatim is true, of possible forms
// $@" ... "
// @$" ... "
// Where the contents contains zero or more sequences
// { STUFF }
// where these curly braces delimit STUFF in expression "holes".
// In order to properly find the closing quote of the whole string,
// we need to locate the closing brace of each hole, as strings
// may appear in expressions in the holes. So we
// need to match up any braces that appear between them.
// But in order to do that, we also need to match up any
// /**/ comments, ' characters quotes, () parens
// [] brackets, and "" strings, including interpolated holes in the latter.
SyntaxDiagnosticInfo error = null;
bool closeQuoteMissing;
ScanInterpolatedStringLiteralTop(null, isVerbatim, ref info, ref error, out closeQuoteMissing);
this.AddError(error);
}
internal void ScanInterpolatedStringLiteralTop(ArrayBuilder<Interpolation> interpolations, bool isVerbatim, ref TokenInfo info, ref SyntaxDiagnosticInfo error, out bool closeQuoteMissing)
{
var subScanner = new InterpolatedStringScanner(this, isVerbatim);
subScanner.ScanInterpolatedStringLiteralTop(interpolations, ref info, out closeQuoteMissing);
error = subScanner.error;
info.Text = TextWindow.GetText(false);
}
internal struct Interpolation
{
public readonly int OpenBracePosition;
public readonly int ColonPosition;
public readonly int CloseBracePosition;
public readonly bool CloseBraceMissing;
public bool ColonMissing => ColonPosition <= 0;
public bool HasColon => ColonPosition > 0;
public int LastPosition => CloseBraceMissing ? CloseBracePosition - 1 : CloseBracePosition;
public int FormatEndPosition => CloseBracePosition - 1;
public Interpolation(int openBracePosition, int colonPosition, int closeBracePosition, bool closeBraceMissing)
{
this.OpenBracePosition = openBracePosition;
this.ColonPosition = colonPosition;
this.CloseBracePosition = closeBracePosition;
this.CloseBraceMissing = closeBraceMissing;
}
}
/// <summary>
/// Turn a (parsed) interpolated string nonterminal into an interpolated string token.
/// </summary>
/// <param name="interpolatedString"></param>
static internal SyntaxToken RescanInterpolatedString(InterpolatedStringExpressionSyntax interpolatedString)
{
var text = interpolatedString.ToString();
var kind = SyntaxKind.InterpolatedStringToken;
// TODO: scan the contents (perhaps using ScanInterpolatedStringLiteralContents) to reconstruct any lexical
// errors such as // inside an expression hole
return SyntaxFactory.Literal(
interpolatedString.GetFirstToken().GetLeadingTrivia(),
text,
kind,
text,
interpolatedString.GetLastToken().GetTrailingTrivia());
}
private class InterpolatedStringScanner
{
public readonly Lexer lexer;
public bool isVerbatim;
public bool allowNewlines;
public SyntaxDiagnosticInfo error;
public InterpolatedStringScanner(
Lexer lexer,
bool isVerbatim)
{
this.lexer = lexer;
this.isVerbatim = isVerbatim;
this.allowNewlines = isVerbatim;
}
private bool IsAtEnd()
{
return IsAtEnd(isVerbatim && allowNewlines);
}
private bool IsAtEnd(bool allowNewline)
{
char ch = lexer.TextWindow.PeekChar();
return
!allowNewline && SyntaxFacts.IsNewLine(ch) ||
(ch == SlidingTextWindow.InvalidCharacter && lexer.TextWindow.IsReallyAtEnd());
}
internal void ScanInterpolatedStringLiteralTop(ArrayBuilder<Interpolation> interpolations, ref TokenInfo info, out bool closeQuoteMissing)
{
if (isVerbatim)
{
Debug.Assert(
(lexer.TextWindow.PeekChar() == '@' && lexer.TextWindow.PeekChar(1) == '$') ||
(lexer.TextWindow.PeekChar() == '$' && lexer.TextWindow.PeekChar(1) == '@'));
// @$ or $@
lexer.TextWindow.AdvanceChar();
lexer.TextWindow.AdvanceChar();
}
else
{
Debug.Assert(lexer.TextWindow.PeekChar() == '$');
lexer.TextWindow.AdvanceChar(); // $
}
Debug.Assert(lexer.TextWindow.PeekChar() == '"');
lexer.TextWindow.AdvanceChar(); // "
ScanInterpolatedStringLiteralContents(interpolations);
if (lexer.TextWindow.PeekChar() != '"')
{
Debug.Assert(IsAtEnd());
if (error == null)
{
int position = IsAtEnd(true) ? lexer.TextWindow.Position - 1 : lexer.TextWindow.Position;
error = lexer.MakeError(position, 1, isVerbatim ? ErrorCode.ERR_UnterminatedStringLit : ErrorCode.ERR_NewlineInConst);
}
closeQuoteMissing = true;
}
else
{
// found the closing quote
lexer.TextWindow.AdvanceChar(); // "
closeQuoteMissing = false;
}
info.Kind = SyntaxKind.InterpolatedStringToken;
}
private void ScanInterpolatedStringLiteralContents(ArrayBuilder<Interpolation> interpolations)
{
while (true)
{
if (IsAtEnd())
{
// error: end of line before end of string
return;
}
switch (lexer.TextWindow.PeekChar())
{
case '"':
if (isVerbatim && lexer.TextWindow.PeekChar(1) == '"')
{
lexer.TextWindow.AdvanceChar(); // "
lexer.TextWindow.AdvanceChar(); // "
continue;
}
// found the end of the string
return;
case '}':
var pos = lexer.TextWindow.Position;
lexer.TextWindow.AdvanceChar(); // }
// ensure any } characters are doubled up
if (lexer.TextWindow.PeekChar() == '}')
{
lexer.TextWindow.AdvanceChar(); // }
}
else if (error == null)
{
error = lexer.MakeError(pos, 1, ErrorCode.ERR_UnescapedCurly, "}");
}
continue;
case '{':
if (lexer.TextWindow.PeekChar(1) == '{')
{
lexer.TextWindow.AdvanceChar();
lexer.TextWindow.AdvanceChar();
}
else
{
int openBracePosition = lexer.TextWindow.Position;
lexer.TextWindow.AdvanceChar();
int colonPosition = 0;
ScanInterpolatedStringLiteralHoleBalancedText('}', true, ref colonPosition);
int closeBracePosition = lexer.TextWindow.Position;
bool closeBraceMissing = false;
if (lexer.TextWindow.PeekChar() == '}')
{
lexer.TextWindow.AdvanceChar();
}
else
{
closeBraceMissing = true;
if (error == null)
{
error = lexer.MakeError(openBracePosition - 1, 2, ErrorCode.ERR_UnclosedExpressionHole);
}
}
interpolations?.Add(new Interpolation(openBracePosition, colonPosition, closeBracePosition, closeBraceMissing));
}
continue;
case '\\':
if (isVerbatim)
{
goto default;
}
var escapeStart = lexer.TextWindow.Position;
char c2;
char ch = lexer.ScanEscapeSequence(out c2);
if ((ch == '{' || ch == '}') && error == null)
{
error = lexer.MakeError(escapeStart, lexer.TextWindow.Position - escapeStart, ErrorCode.ERR_EscapedCurly, ch);
}
continue;
default:
// found some other character in the string portion
lexer.TextWindow.AdvanceChar();
continue;
}
}
}
private void ScanFormatSpecifier()
{
Debug.Assert(lexer.TextWindow.PeekChar() == ':');
lexer.TextWindow.AdvanceChar();
while (true)
{
char ch = lexer.TextWindow.PeekChar();
if (ch == '\\' && !isVerbatim)
{
// normal string & char constants can have escapes
var pos = lexer.TextWindow.Position;
char c2;
ch = lexer.ScanEscapeSequence(out c2);
if ((ch == '{' || ch == '}') && error == null)
{
error = lexer.MakeError(pos, 1, ErrorCode.ERR_EscapedCurly, ch);
}
}
else if (ch == '"')
{
if (isVerbatim && lexer.TextWindow.PeekChar(1) == '"')
{
lexer.TextWindow.AdvanceChar();
lexer.TextWindow.AdvanceChar();
}
else
{
return; // premature end of string! let caller complain about unclosed interpolation
}
}
else if (ch == '{')
{
var pos = lexer.TextWindow.Position;
lexer.TextWindow.AdvanceChar();
// ensure any { characters are doubled up
if (lexer.TextWindow.PeekChar() == '{')
{
lexer.TextWindow.AdvanceChar(); // {
}
else if (error == null)
{
error = lexer.MakeError(pos, 1, ErrorCode.ERR_UnescapedCurly, "{");
}
}
else if (ch == '}')
{
if (lexer.TextWindow.PeekChar(1) == '}')
{
lexer.TextWindow.AdvanceChar();
lexer.TextWindow.AdvanceChar();
}
else
{
return; // end of interpolation
}
}
else if (IsAtEnd())
{
return; // premature end; let caller complain
}
else
{
lexer.TextWindow.AdvanceChar();
}
}
}
/// <summary>
/// Scan past the hole inside an interpolated string literal, leaving the current character on the '}' (if any)
/// </summary>
private void ScanInterpolatedStringLiteralHoleBalancedText(char endingChar, bool isHole, ref int colonPosition)
{
while (true)
{
if (IsAtEnd())
{
// the caller will complain
return;
}
char ch = lexer.TextWindow.PeekChar();
switch (ch)
{
case '#':
// preprocessor directives not allowed.
if (error == null)
{
error = lexer.MakeError(lexer.TextWindow.Position, 1, ErrorCode.ERR_SyntaxError, endingChar.ToString());
}
lexer.TextWindow.AdvanceChar();
continue;
case '$':
if (lexer.TextWindow.PeekChar(1) == '"' || lexer.TextWindow.PeekChar(1) == '@' && lexer.TextWindow.PeekChar(2) == '"')
{
bool isVerbatimSubstring = lexer.TextWindow.PeekChar(1) == '@';
var interpolations = default(ArrayBuilder<Interpolation>);
var info = default(TokenInfo);
bool wasVerbatim = this.isVerbatim;
bool wasAllowNewlines = this.allowNewlines;
try
{
this.isVerbatim = isVerbatimSubstring;
this.allowNewlines &= isVerbatim;
bool closeQuoteMissing;
ScanInterpolatedStringLiteralTop(interpolations, ref info, out closeQuoteMissing);
}
finally
{
this.isVerbatim = wasVerbatim;
this.allowNewlines = wasAllowNewlines;
}
continue;
}
goto default;
case ':':
// the first colon not nested within matching delimiters is the start of the format string
if (isHole)
{
Debug.Assert(colonPosition == 0);
colonPosition = lexer.TextWindow.Position;
ScanFormatSpecifier();
return;
}
goto default;
case '}':
case ')':
case ']':
if (ch == endingChar)
{
return;
}
if (error == null)
{
error = lexer.MakeError(lexer.TextWindow.Position, 1, ErrorCode.ERR_SyntaxError, endingChar.ToString());
}
goto default;
case '"':
case '\'':
// handle string or character literal inside an expression hole.
ScanInterpolatedStringLiteralNestedString();
continue;
case '@':
if (lexer.TextWindow.PeekChar(1) == '"')
{
// check for verbatim string inside an expression hole.
ScanInterpolatedStringLiteralNestedVerbatimString();
continue;
}
goto default;
case '/':
switch (lexer.TextWindow.PeekChar(1))
{
case '/':
if (isVerbatim && allowNewlines)
{
lexer.TextWindow.AdvanceChar(); // skip /
lexer.TextWindow.AdvanceChar(); // skip /
while (!IsAtEnd(false))
{
lexer.TextWindow.AdvanceChar(); // skip // comment character
}
}
else
{
// error: single-line comment not allowed in an interpolated string
if (error == null)
{
error = lexer.MakeError(lexer.TextWindow.Position, 2, ErrorCode.ERR_SingleLineCommentInExpressionHole);
}
lexer.TextWindow.AdvanceChar();
lexer.TextWindow.AdvanceChar();
}
continue;
case '*':
// check for and scan /* comment */
ScanInterpolatedStringLiteralNestedComment();
continue;
default:
lexer.TextWindow.AdvanceChar();
continue;
}
case '{':
// TODO: after the colon this has no special meaning.
ScanInterpolatedStringLiteralHoleBracketed('{', '}');
continue;
case '(':
// TODO: after the colon this has no special meaning.
ScanInterpolatedStringLiteralHoleBracketed('(', ')');
continue;
case '[':
// TODO: after the colon this has no special meaning.
ScanInterpolatedStringLiteralHoleBracketed('[', ']');
continue;
default:
// part of code in the expression hole
lexer.TextWindow.AdvanceChar();
continue;
}
}
}
private void ScanInterpolatedStringLiteralNestedComment()
{
Debug.Assert(lexer.TextWindow.PeekChar() == '/');
lexer.TextWindow.AdvanceChar();
Debug.Assert(lexer.TextWindow.PeekChar() == '*');
lexer.TextWindow.AdvanceChar();
while (true)
{
if (IsAtEnd())
{
return; // let the caller complain about the unterminated quote
}
var ch = lexer.TextWindow.PeekChar();
lexer.TextWindow.AdvanceChar();
if (ch == '*' && lexer.TextWindow.PeekChar() == '/')
{
lexer.TextWindow.AdvanceChar(); // skip */
return;
}
}
}
private void ScanInterpolatedStringLiteralNestedString()
{
var discarded = default(TokenInfo);
lexer.ScanStringLiteral(ref discarded, true);
}
private void ScanInterpolatedStringLiteralNestedVerbatimString()
{
var discarded = default(TokenInfo);
lexer.ScanVerbatimStringLiteral(ref discarded, allowNewlines: allowNewlines);
}
private void ScanInterpolatedStringLiteralHoleBracketed(char start, char end)
{
Debug.Assert(start == lexer.TextWindow.PeekChar());
lexer.TextWindow.AdvanceChar();
int colon = 0;
ScanInterpolatedStringLiteralHoleBalancedText(end, false, ref colon);
if (lexer.TextWindow.PeekChar() == end)
{
lexer.TextWindow.AdvanceChar();
}
else
{
// an error was given by the caller
}
}
}
}
}