/
Scanner.cs
2709 lines (2049 loc) · 81.4 KB
/
Scanner.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using YamlDotNet.Core.Tokens;
using YamlDotNet.Helpers;
namespace YamlDotNet.Core
{
/// <summary>
/// Converts a sequence of characters into a sequence of YAML tokens.
/// </summary>
public class Scanner : IScanner
{
private const int MaxVersionNumberLength = 9;
private static readonly SortedDictionary<char, char> SimpleEscapeCodes = new SortedDictionary<char, char>
{
{ '0', '\0' },
{ 'a', '\x07' },
{ 'b', '\x08' },
{ 't', '\x09' },
{ '\t', '\x09' },
{ 'n', '\x0A' },
{ 'v', '\x0B' },
{ 'f', '\x0C' },
{ 'r', '\x0D' },
{ 'e', '\x1B' },
{ ' ', '\x20' },
{ '"', '"' },
{ '\\', '\\' },
{ '/', '/' },
{ 'N', '\x85' },
{ '_', '\xA0' },
{ 'L', '\x2028' },
{ 'P', '\x2029' }
};
private readonly Stack<int> indents = new Stack<int>();
private readonly InsertionQueue<Token> tokens = new InsertionQueue<Token>();
private readonly Stack<SimpleKey> simpleKeys = new Stack<SimpleKey>();
private readonly CharacterAnalyzer<LookAheadBuffer> analyzer;
private readonly Cursor cursor;
private bool streamStartProduced;
private bool streamEndProduced;
private bool plainScalarFollowedByComment;
private int flowSequenceStartLine;
private bool flowCollectionFetched = false;
private bool startFlowCollectionFetched = false;
private int indent = -1;
private bool flowScalarFetched;
private bool simpleKeyAllowed;
private int flowLevel;
private int tokensParsed;
private bool tokenAvailable;
private Token? previous;
private Anchor? previousAnchor;
private Scalar? lastScalar = null;
private bool IsDocumentStart() =>
!analyzer.EndOfInput &&
cursor.LineOffset == 0 &&
analyzer.Check('-', 0) &&
analyzer.Check('-', 1) &&
analyzer.Check('-', 2) &&
analyzer.IsWhiteBreakOrZero(3);
private bool IsDocumentEnd() =>
!analyzer.EndOfInput &&
cursor.LineOffset == 0 &&
analyzer.Check('.', 0) &&
analyzer.Check('.', 1) &&
analyzer.Check('.', 2) &&
analyzer.IsWhiteBreakOrZero(3);
private bool IsDocumentIndicator() => IsDocumentStart() || IsDocumentEnd();
public bool SkipComments
{
get; private set;
}
/// <summary>
/// Gets the current token.
/// </summary>
public Token? Current
{
get; private set;
}
/// <summary>
/// Initializes a new instance of the <see cref="Scanner"/> class.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="skipComments">Indicates whether comments should be ignored</param>
public Scanner(TextReader input, bool skipComments = true)
{
analyzer = new CharacterAnalyzer<LookAheadBuffer>(new LookAheadBuffer(input, 1024));
cursor = new Cursor();
SkipComments = skipComments;
}
/// <summary>
/// Gets the current position inside the input stream.
/// </summary>
/// <value>The current position.</value>
public Mark CurrentPosition
{
get
{
return cursor.Mark();
}
}
/// <summary>
/// Moves to the next token.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (Current != null)
{
ConsumeCurrent();
}
return MoveNextWithoutConsuming();
}
public bool MoveNextWithoutConsuming()
{
if (!tokenAvailable && !streamEndProduced)
{
FetchMoreTokens();
}
if (tokens.Count > 0)
{
Current = tokens.Dequeue();
tokenAvailable = false;
return true;
}
else
{
Current = null;
return false;
}
}
/// <summary>
/// Consumes the current token and increments the parsed token count
/// </summary>
public void ConsumeCurrent()
{
++tokensParsed;
tokenAvailable = false;
previous = Current;
Current = null;
}
private char ReadCurrentCharacter()
{
var currentCharacter = analyzer.Peek(0);
Skip();
return currentCharacter;
}
private char ReadLine()
{
if (analyzer.Check("\r\n\x85")) // CR LF -> LF --- CR|LF|NEL -> LF
{
SkipLine();
return '\n';
}
var nextChar = analyzer.Peek(0); // LS|PS -> LS|PS
SkipLine();
return nextChar;
}
private void FetchMoreTokens()
{
// While we need more tokens to fetch, do it.
while (true)
{
// Check if we really need to fetch more tokens.
var needsMoreTokens = false;
if (tokens.Count == 0)
{
// Queue is empty.
needsMoreTokens = true;
}
else
{
// Check if any potential simple key may occupy the head position.
foreach (var simpleKey in simpleKeys)
{
if (simpleKey.IsPossible && simpleKey.TokenNumber == tokensParsed)
{
needsMoreTokens = true;
break;
}
}
}
// We are finished.
if (!needsMoreTokens)
{
break;
}
// Fetch the next token.
FetchNextToken();
}
tokenAvailable = true;
}
private static bool StartsWith(StringBuilder what, char start)
{
return what.Length > 0 && what[0] == start;
}
/// <summary>
/// Check the list of potential simple keys and remove the positions that
/// cannot contain simple keys anymore.
/// </summary>
private void StaleSimpleKeys()
{
// Check for a potential simple key for each flow level.
foreach (var key in simpleKeys)
{
// The specification requires that a simple key
// - is limited to a single line,
// - is shorter than 1024 characters.
if (key.IsPossible && (key.Line < cursor.Line || key.Index + 1024 < cursor.Index))
{
// Check if the potential simple key to be removed is required.
if (key.IsRequired)
{
var mark = cursor.Mark();
tokens.Enqueue(new Error("While scanning a simple key, could not find expected ':'.", mark, mark));
}
key.MarkAsImpossible();
}
}
}
private void FetchNextToken()
{
// Check if we just started scanning. Fetch STREAM-START then.
if (!streamStartProduced)
{
FetchStreamStart();
return;
}
// Eat whitespaces and comments until we reach the next token.
ScanToNextToken();
// Remove obsolete potential simple keys.
StaleSimpleKeys();
// Check the indentation level against the current column.
UnrollIndent(cursor.LineOffset);
// Ensure that the buffer contains at least 4 characters. 4 is the length
// of the longest indicators ('--- ' and '... ').
analyzer.Buffer.Cache(4);
// Is it the end of the stream?
if (analyzer.Buffer.EndOfInput)
{
lastScalar = null;
FetchStreamEnd();
}
// Is it a directive?
if (cursor.LineOffset == 0 && analyzer.Check('%'))
{
lastScalar = null;
FetchDirective();
return;
}
// Is it the document start indicator?
if (IsDocumentStart())
{
lastScalar = null;
FetchDocumentIndicator(true);
return;
}
// Is it the document end indicator?
if (IsDocumentEnd())
{
lastScalar = null;
FetchDocumentIndicator(false);
return;
}
// Is it the flow sequence start indicator?
if (analyzer.Check('['))
{
lastScalar = null;
FetchFlowCollectionStart(true);
return;
}
// Is it the flow mapping start indicator?
if (analyzer.Check('{'))
{
lastScalar = null;
FetchFlowCollectionStart(false);
return;
}
// Is it the flow sequence end indicator?
if (analyzer.Check(']'))
{
lastScalar = null;
FetchFlowCollectionEnd(true);
return;
}
// Is it the flow mapping end indicator?
if (analyzer.Check('}'))
{
lastScalar = null;
FetchFlowCollectionEnd(false);
return;
}
// Is it the flow entry indicator?
if (analyzer.Check(','))
{
lastScalar = null;
FetchFlowEntry();
return;
}
// Is it the block entry indicator?
if (analyzer.Check('-'))
{
if (analyzer.IsWhiteBreakOrZero(1))
{
FetchBlockEntry();
return;
}
else if (flowLevel > 0 && analyzer.Check(",[]{}", 1))
{
tokens.Enqueue(new Error("Invalid key indicator format.", cursor.Mark(), cursor.Mark()));
}
}
// Is it the key indicator?
if (analyzer.Check('?') &&
(flowLevel > 0 || analyzer.IsWhiteBreakOrZero(1)))
{
if (analyzer.IsWhiteBreakOrZero(1))
{
FetchKey();
return;
}
}
// Is it the value indicator?
if (analyzer.Check(':') &&
(flowLevel > 0 || analyzer.IsWhiteBreakOrZero(1)) &&
!(simpleKeyAllowed && flowLevel > 0) &&
!(flowScalarFetched && analyzer.Check(':', 1)))
{
if (analyzer.IsWhiteBreakOrZero(1) || analyzer.Check(',', 1) || flowScalarFetched || flowCollectionFetched || startFlowCollectionFetched)
{
if (lastScalar != null)
{
lastScalar.IsKey = true;
lastScalar = null;
}
FetchValue();
return;
}
}
// Is it an alias?
if (analyzer.Check('*'))
{
FetchAnchor(true);
return;
}
// Is it an anchor?
if (analyzer.Check('&'))
{
FetchAnchor(false);
return;
}
// Is it a tag?
if (analyzer.Check('!'))
{
FetchTag();
return;
}
// Is it a literal scalar?
if (analyzer.Check('|') && flowLevel == 0)
{
FetchBlockScalar(true);
return;
}
// Is it a folded scalar?
if (analyzer.Check('>') && flowLevel == 0)
{
FetchBlockScalar(false);
return;
}
// Is it a single-quoted scalar?
if (analyzer.Check('\''))
{
FetchQuotedScalar(true);
return;
}
// Is it a double-quoted scalar?
if (analyzer.Check('"'))
{
FetchQuotedScalar(false);
return;
}
// Is it a plain scalar?
// A plain scalar may start with any non-blank characters except
// '-', '?', ':', ',', '[', ']', '{', '}',
// '#', '&', '*', '!', '|', '>', '\'', '\"',
// '%', '@', '`'.
// In the block context (and, for the '-' indicator, in the flow context
// too), it may also start with the characters
// '-', '?', ':'
// if it is followed by a non-space character.
// The last rule is more restrictive than the specification requires.
var isInvalidPlainScalarCharacter = analyzer.IsWhiteBreakOrZero() || analyzer.Check("-?:,[]{}#&*!|>'\"%@`");
var isPlainScalar =
!isInvalidPlainScalarCharacter ||
(analyzer.Check('-') && !analyzer.IsWhite(1)) ||
(analyzer.Check("?:") && !analyzer.IsWhiteBreakOrZero(1)) ||
(simpleKeyAllowed && flowLevel > 0);
if (isPlainScalar)
{
if (plainScalarFollowedByComment)
{
var startMark = cursor.Mark();
tokens.Enqueue(new Error("While scanning plain scalar, found a comment between adjacent scalars.", startMark, startMark));
}
if (flowScalarFetched || flowCollectionFetched && !startFlowCollectionFetched)
{
if (analyzer.Check(':'))
{
Skip();
}
}
flowScalarFetched = false;
flowCollectionFetched = false;
startFlowCollectionFetched = false;
plainScalarFollowedByComment = false;
FetchPlainScalar();
return;
}
if (simpleKeyAllowed && indent >= cursor.LineOffset && analyzer.IsTab())
{
throw new SyntaxErrorException("While scanning a mapping, found invalid tab as indentation.");
}
if (analyzer.IsWhiteBreakOrZero())
{
Skip();
return;
}
// If we don't determine the token type so far, it is an error.
var start = cursor.Mark();
Skip();
var end = cursor.Mark();
throw new SyntaxErrorException(start, end, "While scanning for the next token, found character that cannot start any token.");
}
private bool CheckWhiteSpace()
{
return analyzer.Check(' ') || ((flowLevel > 0 || !simpleKeyAllowed) && analyzer.Check('\t'));
}
private void Skip()
{
cursor.Skip();
analyzer.Buffer.Skip(1);
}
private void SkipLine()
{
if (analyzer.IsCrLf())
{
cursor.SkipLineByOffset(2);
analyzer.Buffer.Skip(2);
}
else if (analyzer.IsBreak())
{
cursor.SkipLineByOffset(1);
analyzer.Buffer.Skip(1);
}
else if (!analyzer.IsZero())
{
throw new InvalidOperationException("Not at a break.");
}
}
private void ScanToNextToken()
{
// Until the next token is not find.
while (true)
{
// Eat whitespaces.
// Tabs are allowed:
// - in the flow context;
// - in the block context, but not at the beginning of the line or
// after '-', '?', or ':' (complex value).
while (CheckWhiteSpace())
{
Skip();
}
ProcessComment();
// If it is a line break, eat it.
if (analyzer.IsBreak())
{
SkipLine();
// In the block context, a new line may start a simple key.
if (flowLevel == 0)
{
simpleKeyAllowed = true;
}
}
else
{
// We have find a token.
break;
}
}
}
private void ProcessComment()
{
if (analyzer.Check('#'))
{
var start = cursor.Mark();
// Eat '#'
Skip();
// Eat leading whitespace
while (analyzer.IsSpace())
{
Skip();
}
using var textBuilder = StringBuilderPool.Rent();
var text = textBuilder.Builder;
while (!analyzer.IsBreakOrZero())
{
text.Append(ReadCurrentCharacter());
}
if (!SkipComments)
{
var isInline = previous != null
&& previous.End.Line == start.Line
&& previous.End.Column != 1
&& !(previous is StreamStart);
tokens.Enqueue(new Comment(text.ToString(), isInline, start, cursor.Mark()));
}
}
}
private void FetchStreamStart()
{
// Initialize the simple key stack.
simpleKeys.Push(new SimpleKey());
// A simple key is allowed at the beginning of the stream.
simpleKeyAllowed = true;
// We have started.
streamStartProduced = true;
// Create the STREAM-START token and append it to the queue.
var mark = cursor.Mark();
tokens.Enqueue(new StreamStart(mark, mark));
}
/// <summary>
/// Pop indentation levels from the indents stack until the current level
/// becomes less or equal to the column. For each indentation level, append
/// the BLOCK-END token.
/// </summary>
private void UnrollIndent(int column)
{
// In the flow context, do nothing.
if (flowLevel != 0)
{
return;
}
// Loop through the indentation levels in the stack.
while (indent > column)
{
// Create a token and append it to the queue.
var mark = cursor.Mark();
tokens.Enqueue(new BlockEnd(mark, mark));
// Pop the indentation level.
indent = indents.Pop();
}
}
/// <summary>
/// Produce the STREAM-END token and shut down the scanner.
/// </summary>
private void FetchStreamEnd()
{
cursor.ForceSkipLineAfterNonBreak();
// Reset the indentation level.
UnrollIndent(-1);
// Reset simple keys.
RemoveSimpleKey();
simpleKeyAllowed = false;
// Create the STREAM-END token and append it to the queue.
streamEndProduced = true;
var mark = cursor.Mark();
tokens.Enqueue(new StreamEnd(mark, mark));
}
private void FetchDirective()
{
// Reset the indentation level.
UnrollIndent(-1);
// Reset simple keys.
RemoveSimpleKey();
simpleKeyAllowed = false;
// Create the YAML-DIRECTIVE or TAG-DIRECTIVE token.
var token = ScanDirective();
// Append the token to the queue.
if (token != null)
{
tokens.Enqueue(token);
}
}
/// <summary>
/// Scan a YAML-DIRECTIVE or TAG-DIRECTIVE token.
///
/// Scope:
/// %YAML 1.1 # a comment \n
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// %TAG !yaml! tag:yaml.org,2002: \n
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// </summary>
private Token? ScanDirective()
{
// Eat '%'.
var start = cursor.Mark();
Skip();
// Scan the directive name.
var name = ScanDirectiveName(start);
// Is it a YAML directive?
Token directive;
switch (name)
{
case "YAML":
if (previous is DocumentStart || previous is StreamStart || previous is DocumentEnd)
{
directive = ScanVersionDirectiveValue(start);
}
else
{
throw new SemanticErrorException(start, cursor.Mark(), "While scanning a version directive, did not find preceding <document end>.");
}
break;
case "TAG":
directive = ScanTagDirectiveValue(start);
break;
default:
// warning: skipping reserved directive line
while (!analyzer.EndOfInput && !analyzer.Check('#') && !analyzer.IsBreak())
{
Skip();
}
return null;
}
// Eat the rest of the line including any comments.
while (analyzer.IsWhite())
{
Skip();
}
ProcessComment();
// Check if we are at the end of the line.
if (!analyzer.IsBreakOrZero())
{
throw new SyntaxErrorException(start, cursor.Mark(), "While scanning a directive, did not find expected comment or line break.");
}
// Eat a line break.
if (analyzer.IsBreak())
{
SkipLine();
}
return directive;
}
/// <summary>
/// Produce the DOCUMENT-START or DOCUMENT-END token.
/// </summary>
private void FetchDocumentIndicator(bool isStartToken)
{
// Reset the indentation level.
UnrollIndent(-1);
// Reset simple keys.
RemoveSimpleKey();
simpleKeyAllowed = false;
// Consume the token.
var start = cursor.Mark();
Skip();
Skip();
Skip();
if (isStartToken)
{
tokens.Enqueue(new DocumentStart(start, cursor.Mark()));
}
else
{
Token? errorToken = null;
while (!analyzer.EndOfInput && !analyzer.IsBreak() && !analyzer.Check('#'))
{
if (!analyzer.IsWhite())
{
errorToken = new Error("While scanning a document end, found invalid content after '...' marker.", start, cursor.Mark());
break;
}
Skip();
}
tokens.Enqueue(new DocumentEnd(start, start));
if (errorToken != null)
{
tokens.Enqueue(errorToken);
}
}
}
/// <summary>
/// Produce the FLOW-SEQUENCE-START or FLOW-MAPPING-START token.
/// </summary>
private void FetchFlowCollectionStart(bool isSequenceToken)
{
// The indicators '[' and '{' may start a simple key.
SaveSimpleKey();
// Increase the flow level.
IncreaseFlowLevel();
// A simple key may follow the indicators '[' and '{'.
simpleKeyAllowed = true;
// Consume the token.
var start = cursor.Mark();
Skip();
// Create the FLOW-SEQUENCE-START of FLOW-MAPPING-START token.
Token token;
if (isSequenceToken)
{
token = new FlowSequenceStart(start, start);
flowSequenceStartLine = token.Start.Line;
}
else
{
token = new FlowMappingStart(start, start);
}
tokens.Enqueue(token);
startFlowCollectionFetched = true;
}
/// <summary>
/// Increase the flow level and resize the simple key list if needed.
/// </summary>
private void IncreaseFlowLevel()
{
// Reset the simple key on the next level.
simpleKeys.Push(new SimpleKey());
// Increase the flow level.
++flowLevel;
}
/// <summary>
/// Produce the FLOW-SEQUENCE-END or FLOW-MAPPING-END token.
/// </summary>
private void FetchFlowCollectionEnd(bool isSequenceToken)
{
// Reset any potential simple key on the current flow level.
RemoveSimpleKey();
// Decrease the flow level.
DecreaseFlowLevel();
// No simple keys after the indicators ']' and '}'.
simpleKeyAllowed = false;
// Consume the token.
var start = cursor.Mark();
Skip();
Token? token, errorToken = null;
if (isSequenceToken)
{
if (analyzer.Check('#'))
{
errorToken = new Error("While scanning a flow sequence end, found invalid comment after ']'.", start, start);
}
token = new FlowSequenceEnd(start, start);
}
else
{
token = new FlowMappingEnd(start, start);
}
tokens.Enqueue(token);
if (errorToken != null)
{
tokens.Enqueue(errorToken);
}
flowCollectionFetched = true;
}
/// <summary>
/// Decrease the flow level.
/// </summary>
private void DecreaseFlowLevel()