-
Notifications
You must be signed in to change notification settings - Fork 45
/
scanner.jl
1521 lines (1246 loc) · 43.7 KB
/
scanner.jl
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
include("queue.jl")
include("buffered_input.jl")
# Position within the document being parsed
immutable Mark
index::UInt64
line::UInt64
column::UInt64
end
function show(io::IO, mark::Mark)
@printf(io, "line %d, column %d", mark.line, mark.column)
end
# Where in the stream a particular token lies.
immutable Span
start_mark::Mark
end_mark::Mark
end
immutable SimpleKey
token_number::UInt64
required::Bool
mark::Mark
end
# Errors thrown by the scanner.
immutable ScannerError <: Exception
context::Union{AbstractString, Void}
context_mark::Union{Mark, Void}
problem::AbstractString
problem_mark::Mark
end
function show(io::IO, error::ScannerError)
if error.context !== nothing
print(io, error.context, " at ", error.context_mark, ": ")
end
print(io, error.problem, " at ", error.problem_mark)
end
include("tokens.jl")
# A stream type for the scanner, which is just a IO stream with scanner state.
type TokenStream
input::BufferedInput
# All tokens read.
done::Bool
# Tokens queued to be read.
token_queue::Queue{Token}
# Index of the start of the head of the stream. (0-based)
index::UInt64
# Index of the current column. (0-based)
column::UInt64
# Current line numebr. (0-based)
line::UInt64
# Number of tokens read, not including those still in token_queue.
tokens_taken::UInt64
# The number of unclosed '{' and '['. `flow_level == 0` means block
# context.
flow_level::UInt64
# Current indentation level.
indent::Int
# Past indentation levels.
indents::Vector{Int}
# Can a simple key start at the current position? A simple key may
# start:
# - at the beginning of the line, not counting indentation spaces
# (in block context),
# - after '{', '[', ',' (in the flow context),
# - after '?', ':', '-' (in the block context).
# In the block context, this flag also signifies if a block collection
# may start at the current position.
allow_simple_key::Bool
# Keep track of possible simple keys. This is a dictionary. The key
# is `flow_level`; there can be no more that one possible simple key
# for each level. The value is a SimpleKey record:
# (token_number, required, index, line, column, mark)
# A simple key may start with ALIAS, ANCHOR, TAG, SCALAR(flow),
# '[', or '{' tokens.
possible_simple_keys::Dict
function TokenStream(stream::IO)
tokstream = new(BufferedInput(stream), false, Queue{Token}(),
1, 0, 1, 0, 0, -1,
Vector{Int}(0), true, Dict())
fetch_stream_start(tokstream)
tokstream
end
end
function reset!(stream::TokenStream)
stream.done = false
fetch_stream_start(stream)
end
function get_mark(stream::TokenStream)
Mark(stream.index, stream.line, stream.column)
end
# Advance the stream by k characters.
function forwardchars!(stream::TokenStream, k::Integer)
for _ in 1:k
c = peek(stream.input)
forward!(stream.input)
stream.index += 1
if in(c, "\n\u0085\u2028\u2029") ||
(c == '\r' && peek(stream.input) == '\n')
stream.column = 0
stream.line += 1
else
stream.column += 1
end
end
stream.index += k
end
forwardchars!(stream::TokenStream) = forwardchars!(stream, 1)
function need_more_tokens(stream::TokenStream)
if stream.done
return false
elseif isempty(stream.token_queue)
return true
end
stale_possible_simple_keys(stream)
next_possible_simple_key(stream) == stream.tokens_taken
end
function peek(stream::TokenStream)
while need_more_tokens(stream)
fetch_more_tokens(stream)
end
if !isempty(stream.token_queue)
peek(stream.token_queue)
else
nothing
end
end
function forward!(stream::TokenStream)
while need_more_tokens(stream)
fetch_more_tokens(stream)
end
if !isempty(stream.token_queue)
stream.tokens_taken += 1
dequeue!(stream.token_queue)
else
nothing
end
end
# Read one or more tokens from the input stream.
function fetch_more_tokens(stream::TokenStream)
# Eat whitespace.
scan_to_next_token(stream::TokenStream)
# Remove obsolete possible simple keys.
stale_possible_simple_keys(stream)
# Compare the current indentation and column. It may add some tokens
# and decrease the current indentation level.
unwind_indent(stream, stream.column)
c = peek(stream.input)
if c == '\0' || c === nothing
fetch_stream_end(stream)
elseif c == '%' && check_directive(stream)
fetch_directive(stream)
elseif c == '-' && check_document_start(stream)
fetch_document_start(stream)
elseif c == '.' && check_document_end(stream)
fetch_document_end(stream)
stream.done = true
elseif c == '['
fetch_flow_sequence_start(stream)
elseif c == '{'
fetch_flow_mapping_start(stream)
elseif c == ']'
fetch_flow_sequence_end(stream)
elseif c == '}'
fetch_flow_mapping_end(stream)
elseif c == ','
fetch_flow_entry(stream)
elseif c == '-' && check_block_entry(stream)
fetch_block_entry(stream)
elseif c == '?' && check_key(stream)
fetch_key(stream)
elseif c == ':' && check_value(stream)
fetch_value(stream)
elseif c == '*'
fetch_alias(stream)
elseif c == '&'
fetch_anchor(stream)
elseif c == '!'
fetch_tag(stream)
elseif c == '|' && stream.flow_level == 0
fetch_literal(stream)
elseif c == '>' && stream.flow_level == 0
fetch_folded(stream)
elseif c == '\''
fetch_single(stream)
elseif c == '\"'
fetch_double(stream)
elseif check_plain(stream)
fetch_plain(stream)
else
# TODO: Throw a meaningful exception.
throw(c)
end
end
# Simple keys
# -----------
# Return the number of the nearest possible simple key.
function next_possible_simple_key(stream::TokenStream)
min_token_number = nothing
for (level, key) in stream.possible_simple_keys
key = stream.possible_simple_keys[level]
if min_token_number === nothing || key.token_number < min_token_number
min_token_number = key.token_number
end
end
min_token_number
end
# Remove entries that are no longer possible simple keys. According to
# the YAML specification, simple keys
# - should be limited to a single line,
# - should be no longer than 1024 characters.
# Disabling this procedure will allow simple keys of any length and
# height (may cause problems if indentation is broken though).
function stale_possible_simple_keys(stream::TokenStream)
for (level, key) in stream.possible_simple_keys
if key.mark.line != stream.line || stream.index - key.mark.index > 1024
if key.required
throw(ScannerError("while scanning a simple key", key.mark,
"could not find expected ':'", get_mark(stream)))
end
delete!(stream.possible_simple_keys, level)
end
end
end
function save_possible_simple_key(stream::TokenStream)
# Simple key required at the current position.
required = stream.flow_level == 0 && stream.indent == stream.column
@assert stream.allow_simple_key || !required
if stream.allow_simple_key
remove_possible_simple_key(stream)
token_number = stream.tokens_taken + length(stream.token_queue)
key = SimpleKey(token_number, required, get_mark(stream))
stream.possible_simple_keys[stream.flow_level] = key
end
end
function remove_possible_simple_key(stream::TokenStream)
# Remove the saved possible key position at the current flow level.
if haskey(stream.possible_simple_keys, stream.flow_level)
key = stream.possible_simple_keys[stream.flow_level]
if key.required
throw(ScannerError("while scanning a simple key", key.mark,
"could not find expected ':'", get_mark(stream)))
end
delete!(stream.possible_simple_keys, stream.flow_level)
end
end
function unwind_indent(stream::TokenStream, column)
# In the flow context, indentation is ignored. We make the scanner less
# restrictive than specification requires.
if stream.flow_level != 0
return
end
# In block context, we may need to issue the BLOCK-END tokens.
while stream.indent > column
mark = get_mark(stream)
stream.indent = pop!(stream.indents)
enqueue!(stream.token_queue, BlockEndToken(Span(mark, mark)))
end
end
function add_indent(stream::TokenStream, column)
if stream.indent < column
push!(stream.indents, stream.indent)
stream.indent = column
true
else
false
end
end
# Checkers
# --------
const whitespace = "\0 \t\r\n\u0085\u2028\u2029"
function check_directive(stream::TokenStream)
stream.column == 0
end
function check_document_start(stream::TokenStream)
stream.column == 0 &&
prefix(stream.input, 3) == "---" &&
in(peek(stream.input, 3), whitespace)
end
function check_document_end(stream::TokenStream)
stream.column == 0 &&
prefix(stream.input, 3) == "..." &&
(in(peek(stream.input, 3), whitespace) || peek(stream.input, 3) === nothing)
end
function check_block_entry(stream::TokenStream)
in(peek(stream.input, 1), whitespace)
end
function check_key(stream::TokenStream)
stream.flow_level > 0 || in(peek(stream.input, 1), whitespace)
end
function check_value(stream::TokenStream)
cnext = peek(stream.input, 1)
stream.flow_level > 0 || in(cnext, whitespace) || cnext === nothing
end
function check_plain(stream::TokenStream)
!in(peek(stream.input), "\0 \t\r\n\u0085\u2028\u2029-?:,[]{}#&*!|>\'\"%@`") ||
(!in(peek(stream.input, 1), whitespace) &&
(peek(stream.input) == '-' || (stream.flow_level == 0 &&
in(peek(stream.input), "?:"))))
end
# Fetchers
# --------
function fetch_stream_start(stream::TokenStream)
mark = get_mark(stream)
# TODO: support other other encodings.
enqueue!(stream.token_queue,
StreamStartToken(Span(mark, mark), "utf-8"))
end
function fetch_stream_end(stream::TokenStream)
# Set the current intendation to -1.
unwind_indent(stream, -1)
# Reset simple keys.
remove_possible_simple_key(stream)
stream.allow_simple_key = false
empty!(stream.possible_simple_keys)
mark = get_mark(stream)
enqueue!(stream.token_queue, StreamEndToken(Span(mark, mark)))
stream.done = true
end
function fetch_directive(stream::TokenStream)
# Set the current intendation to -1.
unwind_indent(stream, -1)
# Reset simple keys.
remove_possible_simple_key(stream)
stream.allow_simple_key = false
enqueue!(stream.token_queue, scan_directive(stream))
end
function fetch_document_start(stream::TokenStream)
fetch_document_indicator(stream, DocumentStartToken)
end
function fetch_document_end(stream::TokenStream)
fetch_document_indicator(stream, DocumentEndToken)
end
function fetch_document_indicator(stream::TokenStream, tokentype)
# Set the current intendation to -1.
unwind_indent(stream, -1)
# Reset simple keys. Note that there could not be a block collection
# after '---'.
remove_possible_simple_key(stream)
stream.allow_simple_key = false
# Add DOCUMENT-START or DOCUMENT-END.
start_mark = get_mark(stream)
forwardchars!(stream, 3)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, tokentype(Span(start_mark, end_mark)))
end
function fetch_flow_sequence_start(stream::TokenStream)
fetch_flow_collection_start(stream, FlowSequenceStartToken)
end
function fetch_flow_mapping_start(stream::TokenStream)
fetch_flow_collection_start(stream, FlowMappingStartToken)
end
function fetch_flow_collection_start(stream::TokenStream, tokentype)
# '[' and '{' may start a simple key.
save_possible_simple_key(stream)
# Increase the flow level.
stream.flow_level += 1
# Simple keys are allowed after '[' and '{'.
stream.allow_simple_key = true
# Add FLOW-SEQUENCE-START or FLOW-MAPPING-START.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, tokentype(Span(start_mark, end_mark)))
end
function fetch_flow_sequence_end(stream::TokenStream)
fetch_flow_collection_end(stream, FlowSequenceEndToken)
end
function fetch_flow_mapping_end(stream::TokenStream)
fetch_flow_collection_end(stream, FlowMappingEndToken)
end
function fetch_flow_collection_end(stream::TokenStream, tokentype)
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
# Decrease the flow level.
stream.flow_level -= 1
# No simple keys after ']' or '}'.
stream.allow_simple_key = false
# Add FLOW-SEQUENCE-END or FLOW-MAPPING-END.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, tokentype(Span(start_mark, end_mark)))
end
function fetch_flow_entry(stream::TokenStream)
# Simple keys are allowed after ','.
stream.allow_simple_key = true
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
# Add FLOW-ENTRY.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, FlowEntryToken(Span(start_mark, end_mark)))
end
function fetch_block_entry(stream::TokenStream)
# Block context needs additional checks.
if stream.flow_level == 0
# Are we allowed to start a new entry?
if !stream.allow_simple_key
throw(ScannerError(nothing, nothing,
"sequence entries not allowed here",
get_mark(stream)))
end
if add_indent(stream, stream.column)
mark = get_mark(stream)
enqueue!(stream.token_queue,
BlockSequenceStartToken(Span(mark, mark)))
end
# It's an error for the block entry to occur in the flow context,
# but we let the parser detect this.
else
return
end
# Simple keys are allowed after '-'.
stream.allow_simple_key = true
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
# Add BLOCK-ENTRY.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue,
BlockEntryToken(Span(start_mark, end_mark)))
end
function fetch_key(stream::TokenStream)
if stream.flow_level == 0
# Are we allowed to start a key (not nessesary a simple)?
if !stream.allow_simple_key
throw(ScannerError(nothing, nothing,
"mapping keys are not allowed here",
get_mark(stream)))
end
# We may need to add BLOCK-MAPPING-START.
if add_indent(stream, stream.column)
mark = get_mark(stream)
enqueue!(stream.token_queue,
BlockMappingStartToken(Span(mark, mark)))
end
end
# Simple keys are allowed after '?' in the block context.
stream.allow_simple_key = stream.flow_level == 0
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
# Add KEY.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, KeyToken(Span(start_mark, end_mark)))
end
function fetch_value(stream::TokenStream)
# Simple key
if haskey(stream.possible_simple_keys, stream.flow_level)
# Add KEY.
key = stream.possible_simple_keys[stream.flow_level]
delete!(stream.possible_simple_keys, stream.flow_level)
enqueue!(stream.token_queue, KeyToken(Span(key.mark, key.mark)),
key.token_number - stream.tokens_taken)
# If this key starts a new block mapping, we need to add
# BLOCK-MAPPING-START.
if stream.flow_level == 0 && add_indent(stream, key.mark.column)
enqueue!(stream.token_queue,
BlockMappingStartToken(Span(key.mark, key.mark)),
key.token_number - stream.tokens_taken)
end
stream.allow_simple_key = false
# Complex key
else
# Block context needs additional checks.
# (Do we really need them? They will be caught by the parser
# anyway.)
if stream.flow_level == 0
# We are allowed to start a complex value if and only if
# we can start a simple key.
if !stream.allow_simple_key
throw(ScannerError(nothing, nothing,
"mapping values are not allowed here",
get_mark(stream)))
end
end
# If this value starts a new block mapping, we need to add
# BLOCK-MAPPING-START. It will be detected as an error later by
# the parser.
if stream.flow_level == 0 && add_indent(stream, stream.column)
mark = get_mark(stream)
enqueue!(stream.token_queue,
BlockMappingStartToken(Span(mark, mark)))
end
# Simple keys are allowed after ':' in the block context.
stream.allow_simple_key = stream.flow_level == 0
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
end
# Add VALUE.
start_mark = get_mark(stream)
forwardchars!(stream)
end_mark = get_mark(stream)
enqueue!(stream.token_queue, ValueToken(Span(start_mark, end_mark)))
end
function fetch_alias(stream::TokenStream)
# ALIAS could be a simple key.
save_possible_simple_key(stream)
# No simple keys after ALIAS.
stream.allow_simple_key = false
# Scan and add ALIAS.
enqueue!(stream.token_queue, scan_anchor(stream, AliasToken))
end
function fetch_anchor(stream::TokenStream)
# ANCHOR could start a simple key.
save_possible_simple_key(stream)
# No simple keys after ANCHOR.
stream.allow_simple_key = false
# Scan and add ANCHOR.
enqueue!(stream.token_queue, scan_anchor(stream, AnchorToken))
end
function fetch_tag(stream::TokenStream)
# TAG could start a simple key.
save_possible_simple_key(stream)
# No simple keys after TAG.
stream.allow_simple_key = false
# Scan and add TAG.
enqueue!(stream.token_queue, scan_tag(stream))
end
function fetch_literal(stream::TokenStream)
fetch_block_scalar(stream, '|')
end
function fetch_folded(stream::TokenStream)
fetch_block_scalar(stream, '>')
end
function fetch_block_scalar(stream::TokenStream, style::Char)
# A simple key may follow a block scalar.
stream.allow_simple_key = true
# Reset possible simple key on the current level.
remove_possible_simple_key(stream)
# Scan and add SCALAR.
enqueue!(stream.token_queue, scan_block_scalar(stream, style))
end
function fetch_single(stream::TokenStream)
fetch_flow_scalar(stream, '\'')
end
function fetch_double(stream::TokenStream)
fetch_flow_scalar(stream, '"')
end
function fetch_flow_scalar(stream::TokenStream, style::Char)
# A flow scalar could be a simple key.
save_possible_simple_key(stream)
# No simple keys after flow scalars.
stream.allow_simple_key = false
# Scan and add SCALAR.
enqueue!(stream.token_queue, scan_flow_scalar(stream, style))
end
function fetch_plain(stream::TokenStream)
save_possible_simple_key(stream)
stream.allow_simple_key = false
enqueue!(stream.token_queue, scan_plain(stream))
end
# Scanners
# --------
# If the stream is at a line break, advance past it.
#
# Returns:
# '\r\n' : '\n'
# '\r' : '\n'
# '\n' : '\n'
# '\x85' : '\n'
# '\u2028' : '\u2028'
# '\u2029 : '\u2029'
# default : ''
#
function scan_line_break(stream::TokenStream)
if in(peek(stream.input), "\r\n\u0085")
if prefix(stream.input, 2) == "\r\n"
forwardchars!(stream, 2)
else
forwardchars!(stream)
end
return "\n"
elseif in(peek(stream.input), "\u2028\u2029")
ch = peek(stream.input)
forwardchars!(stream)
return ch
end
return ""
end
# Scan past whitespace to the next token.
function scan_to_next_token(stream::TokenStream)
if stream.index == 0 && peek(stream.input) == '\uFEFF'
forwardchars!(stream)
end
found = false
while !found
while peek(stream.input) == ' '
forwardchars!(stream)
end
if peek(stream.input) == '#'
forwardchars!(stream)
while !in(peek(stream.input), "\0\r\n\u0085\u2028\u2029")
forwardchars!(stream)
end
end
if scan_line_break(stream) != ""
if stream.flow_level == 0
stream.allow_simple_key = true
end
else
found = true
end
end
end
function scan_directive(stream::TokenStream)
start_mark = get_mark(stream)
forwardchars!(stream)
name = scan_directive_name(stream, start_mark)
value = nothing
if name == "YAML"
value = scan_yaml_directive_value(stream, start_mark)
end_mark = get_mark(stream)
elseif name == "TAG"
tag_handle = scan_tag_directive_handle(stream, start_mark)
tag_prefix = scan_tag_directive_prefix(stream, start_mark)
value = (tag_handle, tag_prefix)
end_mark = get_mark(stream)
else
end_mark = get_mark(stream)
while !in(peek(stream.input), "\0\r\n\u0085\u2028\u2029")
forwardchars!(stream)
end
end
scan_directive_ignored_line(stream, start_mark)
DirectiveToken(Span(start_mark, end_mark), name, value)
end
function scan_directive_name(stream::TokenStream, start_mark::Mark)
length = 0
c = peek(stream.input)
while isalnum(c) || c == '-' || c == '_'
length += 1
c = peek(stream.input, length)
end
if length == 0
throw(ScannerError("while scanning a directive", start_mark,
"expected alphanumeric character, but found '$(c)'",
get_mark(stream)))
end
value = prefix(stream.input, length)
forwardchars!(stream, length)
c = peek(stream.input)
if !in(c, "\0 \r\n\u0085\u2028\u2029")
throw(ScannerError("while scanning a directive", start_mark,
"expected alphanumeric character, but found '$(c)'",
get_mark(stream)))
end
value
end
function scan_yaml_directive_value(stream::TokenStream, start_mark::Mark)
while peek(stream.input) == ' '
forwardchars!(stream)
end
major = scan_yaml_directive_number(stream, start_mark)
if peek(stream.input) != '.'
throw(ScannerError("while scanning a directive", start_mark,
"expected '.' but found '$(peek(stream.input))'",
get_mark(stream)))
end
forwardchars!(stream)
minor = scan_yaml_directive_number(stream, start_mark)
if !in(peek(stream.input), "\0 \r\n\u0085\u2028\u2029")
throw(ScannerError("while scanning a directive", start_mark,
"expected ' ' or a line break, but found '$(peek(stream.input))'",
get_mark(stream)))
end
return (major, minor)
end
function scan_yaml_directive_number(stream::TokenStream, start_mark::Mark)
if !isdigit(peek(stream.input))
throw(ScannerError("while scanning a directive", start_mark,
"expected a digit, but found '$(peek(stream.input))'",
get_mark(stream)))
end
length = 0
while isdigit(peek(stream.input, length))
length += 1
end
value = parse(Int, prefix(stream.input, length))
forwardchars!(stream, length)
value
end
function scan_tag_directive_handle(stream::TokenStream, start_mark::Mark)
while peek(stream.input) == ' '
forwardchars!(stream)
end
value = scan_tag_handle(stream, "directive", start_mark)
if peek(stream.input) != ' '
throw(ScannerError("while scanning a directive", start_mark,
"expected ' ', but found '$(peek(stream.input))'",
get_mark(stream)))
end
value
end
function scan_tag_directive_prefix(stream::TokenStream, start_mark::Mark)
while peek(stream.input) == ' '
forwardchars!(stream)
end
value = scan_tag_uri(stream, "directive", start_mark)
if !in(peek(stream.input), "\0 \r\n\u0085\u2028\u2029")
throw(ScannerError("while scanning a directive", start_mark,
"expected ' ', but found $(peek(stream.input))",
get_mark(stream)))
end
value
end
function scan_directive_ignored_line(stream::TokenStream, start_mark::Mark)
while peek(stream.input) == ' '
forwardchars!(stream)
end
if peek(stream.input) == '#'
forwardchars!(stream)
while !in(peek(stream.input), "\0\r\n\u0085\u2028\u2029")
forwardchars!(stream)
end
end
if !in(peek(stream.input), "\0\r\n\u0085\u2028\u2029")
throw(ScannerError("while scanning a directive", start_mark,
"expected a comment or a line break, but found '$(peek(stream.input))'",
get_mark(stream)))
end
scan_line_break(stream)
end
function scan_anchor(stream::TokenStream, tokentype)
start_mark = get_mark(stream)
indicator = peek(stream.input)
if indicator == '*'
name = "alias"
else
name = "anchor"
end
forwardchars!(stream)
length = 0
c = peek(stream.input)
while isalnum(c) || c == '-' || c == '_'
length += 1
c = peek(stream.input, length)
end
if length == 0
throw(ScannerError("while scanning an $(name)", start_mark,
"expected an alphanumeric character, but found '$(peek(stream.input))'",
get_mark(stream)))
end
value = prefix(stream.input, length)
forwardchars!(stream, length)
if !in(peek(stream.input), "\0 \t\r\n\u0085\u2028\u2029?:,]}%@`")
throw(ScannerError("while scanning an $(name)", start_mark,
"expected an alphanumeric character, but found '$(peek(stream.input))'",
get_mark(stream)))
end
end_mark = get_mark(stream)
tokentype(Span(start_mark, end_mark), value)
end
function scan_tag(stream::TokenStream)
start_mark = get_mark(stream)
c = peek(stream.input, 1)
if c == '<'
handle = nothing
forwardchars!(stream, 2)
suffix = scan_tag_uri(stream, "tag", start_mark)
if peek(stream.input) != '>'
throw(ScannerError("while parsing a tag", start_mark,
"expected '>', but found '$(peek(stream.input))'",
get_mark(stream)))
end
forwardchars!(stream)
elseif in(c, "\0 \t\r\n\u0085\u2028\u2029")
handle = nothing
suffix = '!'
forwardchars!(stream)
else
length = 1
use_handle = false
while !in(c, "\0 \r\n\u0085\u2028\u2029")
if c == '!'
use_handle = true
break
end
length += 1
c = peek(stream.input, length)
end
if use_handle
handle = scan_tag_handle(stream, "tag", start_mark)
else
handle = "!"
forwardchars!(stream)
end
suffix = scan_tag_uri(stream, "tag", start_mark)
end
c = peek(stream.input)
if !in(c, "\0 \r\n\u0085\u2028\u2029")
throw(ScannerError("while scanning a tag", start_mark,
"expected ' ' or a line break, but found '$(c)'",