-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.jl
1927 lines (1402 loc) · 60.7 KB
/
Events.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
module Events
# REFERENCES
#
# [1] Extensible Markup Language (XML) 1.0 (Fifth Edition),
# https://www.w3.org/TR/xml/
# ----------------------------------------
# EXPORTED INTERFACE
# ----------------------------------------
export events
# ----------------------------------------
# IMPORTED NAMESPACES
# ----------------------------------------
include("./Lexical.jl")
import .Lexical
# ----------------------------------------
# TYPE DECLARATIONS
# ----------------------------------------
# This is used for element declarations only, but has to be declared earlier because it appears in ElementDeclaration.
#
abstract type AbstractModel
end
struct ExternalIdentifier
public_identifier ::Union{Nothing, String}
system_identifier ::Union{Nothing, String}
location ::Lexical.Location
end
struct CDATAMarkedSectionStart
location ::Lexical.Location
end
struct CDATAMarkedSectionEnd
is_recovery ::Bool
location ::Lexical.Location
end
struct CharacterReference
value ::String # It's someone else's job to verify that the value is a legitimate character.
location ::Lexical.Location
end
struct CommentStart
location ::Lexical.Location
end
struct CommentEnd
is_recovery ::Bool
location ::Lexical.Location
end
struct DTDEnd
location ::Lexical.Location
end
struct DTDStart
root ::String
external_identifier ::Union{Nothing, ExternalIdentifier}
location ::Lexical.Location
end
struct DTDInternalStart
location ::Lexical.Location
end
struct DataContent
value ::String
is_ws ::Bool
location ::Lexical.Location
end
struct ElementDeclaration
is_recovery ::Bool
name ::String
content_model ::AbstractModel
location ::Lexical.Location
end
struct EntityDeclarationExternalGeneralData
name ::String
external_identifier ::Union{Nothing, ExternalIdentifier}
ndata_declaration ::Union{Nothing, String}
location ::Lexical.Location
end
struct EntityDeclarationExternalGeneralText
name ::String
external_identifier ::Union{Nothing, ExternalIdentifier}
location ::Lexical.Location
end
struct EntityDeclarationExternalParameter
name ::String
external_identifier ::Union{Nothing, ExternalIdentifier}
location ::Lexical.Location
end
struct EntityDeclarationInternalGeneral
name ::String
entity_value ::String
location ::Lexical.Location
end
struct EntityDeclarationInternalParameter
name ::String
entity_value ::String
location ::Lexical.Location
end
struct EntityReferenceGeneral
name ::String
location ::Lexical.Location
end
struct EntityReferenceParameter
name ::String
location ::Lexical.Location
end
struct NotationDeclaration
name ::String
external_identifier ::Union{Nothing, ExternalIdentifier}
location ::Lexical.Location
end
struct ProcessingInstruction
target ::String
value ::String
location ::Lexical.Location
end
struct MarkupError
message ::String # I'll eventually do something more sophisticated here.
location ::Lexical.Location
end
struct AttributeSpecification
name ::String
value ::Array{Union{DataContent, CharacterReference, EntityReferenceGeneral, EntityReferenceParameter}, 1}
location ::Lexical.Location
end
struct ElementEnd
is_recovery ::Bool
name ::String
location ::Lexical.Location
end
struct ElementStart
is_recovery ::Bool
is_empty ::Bool
name ::String
attributes ::Array{AttributeSpecification, 1}
location ::Lexical.Location
end
struct ConditionalSectionEnd
location ::Lexical.Location
end
struct ConditionalSectionStart
is_recovery ::Bool
conditional ::Union{String, EntityReferenceParameter}
location ::Lexical.Location
end
# These are for element declarations.
#
struct AnyModel <: AbstractModel
end
struct ElementModel <: AbstractModel
element_name ::String
end
struct EmptyModel <: AbstractModel
end
struct MixedModel <: AbstractModel
items ::Array{AbstractModel, 1}
end
struct ChoiceGroup <: AbstractModel
items ::Array{AbstractModel, 1}
end
struct SequenceGroup <: AbstractModel
items ::Array{AbstractModel, 1}
end
struct OneOrMore <: AbstractModel
item ::AbstractModel
end
struct Optional <: AbstractModel
item ::AbstractModel
end
struct ZeroOrMore <: AbstractModel
item ::AbstractModel
end
# These are for attribute declarations.
#
struct EntityType
end
struct EntitiesType
end
struct EnumeratedNotationType
names ::Array{String, 1}
end
struct EnumeratedType
nmtokens ::Array{String, 1}
end
struct IDRefType
end
struct IDRefsType
end
struct IDType
end
struct NameTokenType
end
struct NameTokensType
end
struct StringType
end
struct DefaultValue
is_fixed ::Bool
value ::Array{Union{DataContent, CharacterReference, EntityReferenceGeneral, EntityReferenceParameter}, 1}
end
struct Implied
end
struct Required
end
struct AttributeDeclaration
is_recovery ::Bool # This basically is a synthesized attribute.
name ::String
type ::Union{EntityType, EntitiesType, EnumeratedNotationType, EnumeratedType, IDRefType,
IDRefsType, IDType, NameTokenType, NameTokensType, StringType}
default ::Union{DefaultValue, Implied, Required}
end
struct AttributeDeclarations
is_recovery ::Bool # This is a synthesized attribute, synthesized from the contained AttributeDeclaration children.
name ::String
declarations ::Array{AttributeDeclaration, 1}
location ::Lexical.Location
end
# ----------------------------------------
# FUNCTIONS
# ----------------------------------------
CommentEnd(location) = CommentEnd(false, location)
CDATAMarkedSectionEnd(location) = CDATAMarkedSectionEnd(false, location)
ConditionalSectionStart(conditional, location) = ConditionalSectionStart(false, conditional, location)
ElementEnd(name, location) = ElementEnd(false, name, location)
ElementStart(name, attributes, location) = ElementStart(false, name, attributes, location)
ElementStart(is_empty, name, attributes, location) = ElementStart(false, is_empty, name, attributes, location)
DataContent(tokens::Array, location) = DataContent(stringify(tokens), location)
DataContent(value, location) = DataContent(value, false, location)
# These are needed for writing tests, because these types contain other structs inside an array.
#
function Base.:(==)(left::ElementStart, right::ElementStart)
return (left.is_recovery == right.is_recovery
&& left.name == right.name
&& left.attributes == right.attributes
&& left.location == right.location)
end
function Base.:(==)(left::AttributeSpecification, right::AttributeSpecification)
return (left.name == right.name
&& left.value == right.value
&& left.location == right.location)
end
function Base.:(==)(left::ElementDeclaration, right::ElementDeclaration)
return (left.is_recovery == right.is_recovery
&& left.name == right.name
&& left.content_model == right.content_model
&& left.location == right.location)
end
function Base.:(==)(left::MixedModel, right::MixedModel)
return left.items == right.items
end
function Base.:(==)(left::OneOrMore, right::OneOrMore)
return left.item == right.item
end
function Base.:(==)(left::Optional, right::Optional)
return left.item == right.item
end
function Base.:(==)(left::ZeroOrMore, right::ZeroOrMore)
return left.item == right.item
end
function Base.:(==)(left::ChoiceGroup, right::ChoiceGroup)
return left.items == right.items
end
function Base.:(==)(left::SequenceGroup, right::SequenceGroup)
return left.items == right.items
end
function Base.:(==)(left::AttributeDeclarations, right::AttributeDeclarations)
return (left.is_recovery == right.is_recovery
&& left.name == right.name
&& left.declarations == right.declarations
&& left.location == right.location)
end
function Base.:(==)(left::AttributeDeclaration, right::AttributeDeclaration)
return (left.is_recovery == right.is_recovery
&& left.name == right.name
&& left.type == right.type
&& left.default == right.default)
end
function Base.:(==)(left::EnumeratedType, right::EnumeratedType)
return left.nmtokens == right.nmtokens
end
function Base.:(==)(left::EnumeratedNotationType, right::EnumeratedNotationType)
return left.names == right.names
end
function Base.:(==)(left::DefaultValue, right::DefaultValue)
return left.value == right.value
end
function attribute_declarations(mdo, tokens, channel)
function attribute_declaration(element_name, tokens, channel)
attribute_name = take!(tokens) # Collect the attribute name token that got us here ...
consume_white_space!(tokens) # ... but discard any following white space.
( attribute_type_recovery, attribute_type ) = collect_attribute_type(element_name, attribute_name, tokens, channel)
consume_white_space!(tokens) # Discard any trailing white space.
( attribute_default_recovery, attribute_default ) = collect_attribute_default(element_name, attribute_name, tokens, channel)
return AttributeDeclaration(any([ attribute_type_recovery, attribute_default_recovery ]),
attribute_name.value, attribute_type, attribute_default)
end
function collect_attribute_default(element_name, attribute_name, tokens, channel)
attribute_default = Implied()
is_recovery = false
if is_reserved_name("#IMPLIED", tokens, channel)
take!(tokens)
elseif is_reserved_name("#REQUIRED", tokens, channel)
take!(tokens)
attribute_default = Required()
else
is_fixed = false
if is_reserved_name("#FIXED", tokens, channel)
is_fixed = true
take!(tokens)
consume_white_space!(tokens)
end
( value_recovery, value ) = collect_attribute_value(attribute_name, tokens, channel)
attribute_default = DefaultValue(is_fixed, value)
is_recovery = any([ is_recovery, value_recovery ])
end
return ( is_recovery, attribute_default )
end
function collect_attribute_type(element_name, attribute_name, tokens, channel)
attribute_type = StringType()
is_recovery = false
if is_keyword("CDATA", tokens, channel)
take!(tokens)
elseif is_keyword("ENTITY", tokens, channel)
take!(tokens)
attribute_type = EntityType()
elseif is_keyword("ENTITIES", tokens, channel)
take!(tokens)
attribute_type = EntitiesType()
elseif is_keyword("IDREF", tokens, channel)
take!(tokens)
attribute_type = IDRefType()
elseif is_keyword("IDREFS", tokens, channel)
take!(tokens)
attribute_type = IDRefsType()
elseif is_keyword("ID", tokens, channel)
take!(tokens)
attribute_type = IDType()
elseif is_keyword("NMTOKEN", tokens, channel)
take!(tokens)
attribute_type = NameTokenType()
elseif is_keyword("NMTOKENS", tokens, channel)
take!(tokens)
attribute_type = NameTokensType()
elseif is_keyword("NOTATION", tokens, channel)
notation = take!(tokens)
ws = consume_white_space!(tokens)
if isnothing(ws)
# See [1], § 3.3.1 ... the white space is required. This is really bogus, because the only thing that
# can follow NOTATION in this position is an open parenthesis, so there would be no ambiguity in making
# the white space optional
#
put!(channel, MarkupError("ERROR: White space is required following the 'NOTATION' keyword.",
Lexical.location_of(notation)))
is_recovery = true
end
if !is_token(Lexical.grpo, tokens)
put!(channel, MarkupError("ERROR: Expecting '(' to open an enumerated notation attribute value.",
Lexical.location_of(notation)))
is_recovery = true
end
# Regardless of whether or not we saw GRPO, try parsing the group as a recovery mechanism.
#
( names_recovery, names ) = collect_group(notation, is_name,
"ERROR: Expecting a name for an enumerated attribute value.",
tokens, channel)
is_recovery = any([ is_recovery, names_recovery ])
attribute_type = EnumeratedNotationType(names)
elseif is_token(Lexical.grpo, tokens)
( nmtokens_recovery, nmtokens ) = collect_group(attribute_name, is_nmtoken,
"ERROR: Expecting a NMTOKEN for an enumerated attribute value.",
tokens, channel)
is_recovery = any([ is_recovery, nmtokens_recovery ])
attribute_type = EnumeratedType(nmtokens)
else
put!(channel, MarkupError("ERROR: Expecting an attribute type.", Lexical.location_of(attribute_name)))
end
return ( is_recovery, attribute_type )
end
function collect_group(token, matcher, message, tokens, channel)
# Careful here ... we're called whether or not the GRPO was present. So only consume it if it's there.
#
if is_token(Lexical.grpo, tokens)
grpo = take!(tokens)
consume_white_space!(tokens)
end
items = Array{String, 1}()
is_recovery = false
while true
if matcher(tokens)
push!(items, take!(tokens).value)
consume_white_space!(tokens)
elseif is_token(Lexical.or, tokens) || is_token(Lexical.grpc, tokens)
# Leave the token there ... we'll consume it below.
#
put!(channel, MarkupError(message, Lexical.location_of(token)))
is_recovery = true
else
put!(channel, MarkupError(message, Lexical.location_of(token)))
is_recovery = true
take!(tokens) # Drop the token and try recovering.
consume_white_space!(tokens)
end
if is_token(Lexical.or, tokens)
take!(tokens)
consume_white_space!(tokens)
elseif is_token(Lexical.grpc, tokens)
take!(tokens)
consume_white_space!(tokens)
break
elseif matcher(tokens)
put!(channel, MarkupError("ERROR: Expecting '|' between items in an enumerated attribute value.",
Lexical.location_of(token)))
is_recovery = true
else
put!(channel, MarkupError("ERROR: Expecting '|' or ')'.", Lexical.location_of(token)))
is_recovery = true
break
end
end
return ( is_recovery, items )
end
attlist = take!(tokens) # Consume the ATTLIST keyword that got us here.
ws = consume_white_space!(tokens)
is_recovery = false
if isnothing(ws)
# See [1], § 3.3 ... the white space is required.
#
put!(channel, MarkupError("ERROR: White space is required following the 'ATTLIST' keyword.", Lexical.location_of(attlist)))
is_recovery = true
end
if is_name(tokens)
element_name = take!(tokens)
attribute_declarations = Array{AttributeDeclaration, 1}()
while true
if is_token(Lexical.ws, tokens)
consume_white_space!(tokens)
if is_name(tokens)
push!(attribute_declarations, attribute_declaration(element_name, tokens, channel))
else
break
end
else
break
end
end
if is_token(Lexical.tagc, tokens)
take!(tokens)
else
is_recovery = true
put!(channel, MarkupError("ERROR: Expecting '>' to end an attribute list declaration.",
Lexical.location_of(element_name)))
end
is_recovery = any(vcat(is_recovery, map(declaration -> declaration.is_recovery, attribute_declarations)))
put!(channel, AttributeDeclarations(is_recovery, element_name.value, attribute_declarations, Lexical.location_of(mdo)))
else
put!(channel, MarkupError("ERROR: Expecting an element name.", Lexical.location_of(attlist)))
end
end
function cdata_marked_section(mdo, dso, tokens, channel)
text = take!(tokens) # Consume the CDATA keyword that got us here.
ws = consume_white_space!(tokens)
if !isnothing(ws)
# Discard the white space ... there isn't much we can do with it.
#
put!(channel, MarkupError("ERROR: White space is not allowed after the 'CDATA' keyword.", Lexical.location_of(text)))
end
if is_token(Lexical.dso, tokens)
take!(tokens)
put!(channel, CDATAMarkedSectionStart(Lexical.location_of(mdo)))
consumed = Array{Lexical.Token, 1}()
while true
if is_token(Lexical.msc, tokens)
msc = take!(tokens)
if is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, DataContent(consumed, locations_of(mdo, consumed)[:head]))
put!(channel, CDATAMarkedSectionEnd(Lexical.location_of(msc)))
break
else
put!(channel, DataContent(consumed, locations_of(mdo, consumed)[:head]))
put!(channel, MarkupError("ERROR: Expecting '>' to end a CDATA marked section.", Lexical.location_of(msc)))
put!(channel, CDATAMarkedSectionEnd(true, Lexical.location_of(msc)))
break
end
elseif is_eoi(tokens)
put!(channel, DataContent(consumed, locations_of(mdo, consumed)[:head]))
put!(channel, MarkupError("ERROR: Expecting ']]>' to end a CDATA marked section.",
locations_of(mdo, consumed)[:tail]))
put!(channel, CDATAMarkedSectionEnd(true, locations_of(mdo, consumed)[:tail]))
break
else
push!(consumed, take!(tokens))
end
end
else
put!(channel, MarkupError("ERROR: Expecting '[' to open a CDATA marked section.", Lexical.location_of(text)))
end
end
function comment(mdo, tokens, channel)
com = take!(tokens) # Consume the COM token that got us here.
consumed = Array{Lexical.Token, 1}()
put!(channel, CommentStart(Lexical.location_of(mdo)))
while true
if is_token(Lexical.com, tokens)
tail = take!(tokens)
# We cannot encounter EOI here ... if we hit "--" at EOI, it gets classified as TEXT, not COM. See
# consume_until() in src/Lexical.jl
#
if is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, DataContent(consumed, locations_of(com, consumed)[:head]))
put!(channel, CommentEnd(Lexical.location_of(com)))
break
else
put!(channel, DataContent(consumed, locations_of(com, consumed)[:head]))
put!(channel, MarkupError("ERROR: '--' is not allowed inside a comment.", locations_of(com, consumed)[:tail]))
put!(channel, CommentEnd(true, locations_of(com, consumed)[:tail]))
break
end
elseif is_eoi(tokens)
put!(channel, DataContent(consumed, locations_of(com, consumed)[:head]))
put!(channel, MarkupError("ERROR: Expecting '-->' to end a comment.", locations_of(com, consumed)[:tail]))
put!(channel, CommentEnd(true, locations_of(com, consumed)[:tail]))
break
else
push!(consumed, take!(tokens))
end
end
end
function conditional_marked_section(mdo, dso, conditional, tokens, channel)
if is_token(Lexical.dso, tokens)
is_recovery(string::String) = !(all(map(isuppercase, collect(string))))
is_recovery(_) = false
take!(tokens) # Discard the DSO token that follows the conditional ... we don't need it.
put!(channel, ConditionalSectionStart(is_recovery(conditional), conditional, Lexical.location_of(mdo)))
else
# It would be more accurate to use the location of the conditional here, but it isn't necessarily a token ... it
# could be a properly-parsed parameter entity reference.
#
put!(channel, MarkupError("ERROR: Expecting '[' to open a conditional marked section.", Lexical.location_of(mdo)))
put!(channel, ConditionalSectionStart(true, conditional, Lexical.location_of(mdo)))
end
end
function document_type_declaration(mdo, tokens, channel)
doctype = take!(tokens) # Consume the DOCTYPE keyword that got us here ...
consume_white_space!(tokens) # ... but discard any following white space.
if is_name(tokens)
root = take!(tokens)
consume_white_space!(tokens)
external_identifier = collect_external_identifier(mdo, tokens, channel)
consume_white_space!(tokens)
if is_token(Lexical.tagc, tokens)
tagc = take!(tokens)
put!(channel, DTDStart(root.value, external_identifier, Lexical.location_of(doctype)))
put!(channel, DTDEnd(Lexical.location_of(doctype)))
elseif is_token(Lexical.dso, tokens)
dso = take!(tokens)
put!(channel, DTDStart(root.value, external_identifier, Lexical.location_of(doctype)))
put!(channel, DTDInternalStart(Lexical.location_of(doctype)))
else
put!(channel, MarkupError("ERROR: Expecting '>' to end a document type declaration.", Lexical.location_of(root)))
end
else
put!(channel, MarkupError("ERROR: Expecting a root element name.", Lexical.location_of(doctype)))
end
end
function element_declaration(mdo, tokens, channel)
element = take!(tokens) # Consume the ELEMENT keyword that got us here.
ws = consume_white_space!(tokens)
is_recovery = false
if isnothing(ws)
# See [1], § 3.2 ... the white space is required.
#
put!(channel, MarkupError("ERROR: White space is required following the 'ELEMENT' keyword.", Lexical.location_of(element)))
is_recovery = true
end
if is_name(tokens)
element_name = take!(tokens)
ws = consume_white_space!(tokens)
if isnothing(ws)
# See [1], § 3.2 ... the white space is required. This is a bit lame ... the white space wouldn't be needed
# to disambiguate in some cases (e.g., if the content model begins with a parenthesis), but ... well
# ... whatever.
#
put!(channel, MarkupError("ERROR: White space is required following an element name.",
Lexical.location_of(element_name)))
is_recovery = true
end
content_model = AnyModel()
if is_keyword("ANY", tokens, channel)
take!(tokens)
content_model = AnyModel()
elseif is_keyword("EMPTY", tokens, channel)
take!(tokens)
content_model = EmptyModel()
elseif is_token(Lexical.grpo, tokens)
grpo = take!(tokens)
consume_white_space!(tokens)
if is_reserved_name("#PCDATA", tokens, channel)
take!(tokens)
consume_white_space!(tokens)
( content_model_recovery, items ) = collect_mixed_content_model(tokens, channel)
is_recovery = is_recovery || content_model_recovery
if is_token(Lexical.grpc, tokens)
take!(tokens)
if is_token(Lexical.rep, tokens)
take!(tokens)
elseif length(items) > 0
# The trailing '*' is only required if element names were encountered while parsing the content
# model. Really. See § 3.2.2, the first branch of production [51].
#
put!(channel, MarkupError("ERROR: Expecting '*' to end a mixed content model.",
Lexical.location_of(element_name)))
is_recovery = true
end
else
put!(channel, MarkupError("ERROR: Expecting ')*' to end a mixed content model.",
Lexical.location_of(element_name)))
is_recovery = true
end
content_model = MixedModel(items)
else
( group_recovery, content_model ) = collect_content_model_group(grpo, tokens, channel)
is_recovery = is_recovery || group_recovery
if isnothing(content_model)
content_model = AnyModel()
else
if is_token(Lexical.grpc, tokens)
take!(tokens)
consume_white_space!(tokens)
else
consume_white_space!(tokens)
put!(channel, MarkupError("ERROR: Expecting ')' to end a content model group.", Lexical.location_of(grpo)))
is_recovery = true
end
content_model = collect_occurrence_indicator(content_model, tokens)
end
end
else
put!(channel, MarkupError("ERROR: Expecting 'ANY', 'EMPTY', or '(' to open a content model.",
Lexical.location_of(element)))
is_recovery = true
end
consume_white_space!(tokens)
if is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, ElementDeclaration(is_recovery, element_name.value, content_model, Lexical.location_of(mdo)))
else
take!(tokens)
put!(channel, MarkupError("ERROR: Expecting '>' to end an element declaration.", Lexical.location_of(element_name)))
put!(channel, ElementDeclaration(true, element_name.value, content_model, Lexical.location_of(element)))
end
else
put!(channel, MarkupError("ERROR: Expecting an element name.", Lexical.location_of(element)))
end
end
function element_end(tokens, channel)
etago = take!(tokens) # Consume the ETAGO that got us here.
if is_name(tokens)
name = take!(tokens)
consume_white_space!(tokens)
if is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, ElementEnd(name.value, Lexical.location_of(name)))
else
put!(channel, MarkupError("ERROR: Expecting '>' to end an element close tag.", Lexical.location_of(name)))
put!(channel, ElementEnd(true, name.value, Lexical.location_of(name)))
end
else
put!(channel, MarkupError("ERROR: Expecting an element name.", Lexical.location_of(etago)))
end
end
function element_start(tokens, channel)
stago = take!(tokens) # Consume the STAGO that got us here.
if is_name(tokens)
name = take!(tokens)
attributes = collect_attributes(tokens, channel) # We should really flip the is_recovery flag on the
# ElementStart if any of the attributes contain a MarkupError.
consume_white_space!(tokens)
if is_token(Lexical.net, tokens)
take!(tokens)
if is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, ElementStart(true, name.value, attributes, Lexical.location_of(name)))
put!(channel, ElementEnd(name.value, Lexical.location_of(name)))
else
put!(channel, ElementStart(true, true, name.value, attributes, Lexical.location_of(name)))
put!(channel, MarkupError("ERROR: Expecting '>' to end an element open tag.", Lexical.location_of(name)))
put!(channel, ElementEnd(true, name.value, Lexical.location_of(name)))
end
elseif is_token(Lexical.tagc, tokens)
take!(tokens)
put!(channel, ElementStart(name.value, attributes, Lexical.location_of(name)))
else
put!(channel, ElementStart(true, false, name.value, attributes, Lexical.location_of(name)))
put!(channel, MarkupError("ERROR: Expecting '>' to end an element open tag.", Lexical.location_of(name)))
end
else
put!(channel, MarkupError("ERROR: Expecting an element name.", Lexical.location_of(stago)))
end
end
function entity_declaration(mdo, tokens, channel)
function collect_entity_definition(entity_name, tokens, channel)
external_identifier = collect_external_identifier(entity_name, tokens, channel)
if isnothing(external_identifier)
entity_value = collect_string(Lexical.location_of(entity_name), tokens, channel)
if isnothing(entity_value)
# Something is messed up.
#
return ( external_identifier = nothing, entity_value = nothing, ndata_name = nothing )
else
return ( external_identifier = external_identifier, entity_value = stringify(entity_value), ndata_name = nothing )
end
else
consume_white_space!(tokens)
if is_keyword("NDATA", tokens, channel)
ndata = take!(tokens) # Consume the NDATA keyword ...
consume_white_space!(tokens) # ... but discard any following white space.
if is_name(tokens)
ndata_name = take!(tokens)
return ( external_identifier = external_identifier, entity_value = nothing, ndata_name = ndata_name )
else
put!(channel, MarkupError("ERROR: Expecting a notation name.", Lexical.location_of(entity_name)))
return ( external_identifier = external_identifier, entity_value = nothing, ndata_name = nothing )
end
else
return ( external_identifier = external_identifier, entity_value = nothing, ndata_name = nothing )
end
end
end
entity = take!(tokens) # Consume the ENTITY keyword that got us here.
ws = consume_white_space!(tokens)