-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.go
More file actions
1504 lines (1419 loc) · 43 KB
/
Copy pathxml.go
File metadata and controls
1504 lines (1419 loc) · 43 KB
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
// Copyright (c) 2021-2025 Richard Rodger, MIT License
// Package xml is a Jsonic plugin that parses XML into a tree of
// elements. The parser supports: elements with open/close and
// self-closing tags, attributes (single and double quoted with entity
// decoding), mixed element/text content, predefined and numeric
// character entity references, namespace resolution from xmlns/xmlns:*
// declarations, comments, CDATA sections, processing instructions and
// DOCTYPE declarations.
//
// The returned tree uses `map[string]any` nodes with keys `name`,
// `localName`, optional `prefix`, optional `namespace`, `attributes`
// (map of string -> string) and `children` (array of nested elements
// or text strings).
package xml
import (
"encoding/binary"
"fmt"
"regexp"
"strconv"
"strings"
"unicode/utf16"
"unicode/utf8"
jsonic "github.com/jsonicjs/jsonic/go"
)
const Version = "0.1.1"
// Defaults are merged with caller-supplied options when the plugin is
// registered via jsonic.UseDefaults.
//
// Option keys:
//
// namespaces bool resolve xmlns / xmlns:* into prefix /
// localName / namespace fields on every
// element. Default: true.
// entities bool decode the five predefined entities and
// numeric character references in text and
// attribute values. Default: true.
// customEntities map[string]string extra named entities to recognise.
// strictEntities bool enforce XML 1.0 §4.1: every named entity
// reference must resolve to a declared
// entity. Default: true. When false,
// references to unknown names are left
// as-is in the output.
// embed bool when true, keep Jsonic's JSON/JSONIC
// grammar in place and splice an XML
// literal alternate into the `val` rule
// so `<tag>…</tag>` can appear wherever
// Jsonic expects a value. When false
// (default) the parser is reconfigured
// as a pure-XML parser.
var Defaults = map[string]any{
"namespaces": true,
"entities": true,
"customEntities": map[string]string{},
"strictEntities": true,
"embed": false,
}
// Xml is the Jsonic plugin entry point. Register via:
//
// j := jsonic.Make()
// j.UseDefaults(xml.Xml, xml.Defaults)
// result, err := j.Parse(src)
func Xml(j *jsonic.Jsonic, options map[string]any) error {
// Guard against re-invocation: Use() re-runs plugins on SetOptions calls.
if j.Decoration("xml-init") != nil {
return nil
}
j.Decorate("xml-init", true)
namespacesOn := toBool(options["namespaces"], true)
entitiesOn := toBool(options["entities"], true)
customEntities := toStringMap(options["customEntities"])
strictEntities := toBool(options["strictEntities"], true)
embed := toBool(options["embed"], false)
decode, declared := buildEntityDecoder(entitiesOn, customEntities)
// Reserve #XIG (ignored) and #XOP/#XCL/#XSC (tag tokens) so they have
// stable tins before the grammar references them. The tins are then
// passed to the tag matcher by closure.
xigTin := j.Token("#XIG", "")
xopTin := j.Token("#XOP", "")
xclTin := j.Token("#XCL", "")
xscTin := j.Token("#XSC", "")
if !embed {
// Register a dummy fixed token bound to a character that cannot
// legally appear in XML source (ASCII SOH). This keeps the
// lexer's internal `FixedSorted` list non-empty, which in turn
// disables an otherwise-hardcoded fallback that still ends text
// tokens on `{ } [ ] : ,` even when those symbols have been
// removed from the fixed token map. Without this, XML text
// content containing a comma would be truncated at the comma.
// In embed mode the JSON structural tokens remain in place, so
// the dummy is not needed.
soh := "\x01"
_ = j.Token("#XDUM", soh)
}
// Shared options installed in both modes: the custom matcher, the
// text-end character `<`, and the XML-specific error templates.
j.SetOptions(jsonic.Options{
Lex: &jsonic.LexOptions{
Match: map[string]*jsonic.MatchSpec{
"xmltag": {Order: 100_000, Make: buildXmlTagMatcher(decode, declared, entitiesOn, strictEntities, embed, xigTin, xopTin, xclTin, xscTin)},
},
},
Ender: []string{"<"},
Error: map[string]string{
"xml_mismatched_tag": "closing tag </$fsrc> does not match opening tag <$openname>",
"xml_invalid_tag": "invalid tag: $fsrc",
"xml_unterminated": "unterminated $kind",
"comment_double_dash": "comment body cannot contain \"--\"",
"cdata_terminator_in_text": "character data cannot contain \"]]>\"",
"pi_target_invalid": "processing instruction target is missing or invalid",
"lt_in_attr_value": "\"<\" is not allowed in an attribute value",
"bad_entity_ref": "malformed entity reference (need &name; or &#NNN; or &#xHHH;)",
"duplicate_attribute": "duplicate attribute name in tag",
"invalid_xml_char": "illegal control character in XML data",
"reserved_namespace": "invalid use of a reserved namespace prefix or URI",
"unbound_prefix": "element or attribute uses an undeclared namespace prefix",
"undeclared_entity": "reference to undeclared entity",
},
Hint: map[string]string{
"xml_mismatched_tag": "Each opening tag must be paired with a matching closing tag.\nExpected </$openname> but found </$fsrc>.",
"xml_invalid_tag": "The tag syntax is not valid XML.",
"xml_unterminated": "The $kind starting at this position is not terminated.",
"comment_double_dash": "XML 1.0 disallows \"--\" inside a comment body.",
"cdata_terminator_in_text": "The literal \"]]>\" must only appear as the end of a CDATA section.",
"pi_target_invalid": "A processing instruction must start with a Name; the XML declaration <?xml...?> is the special case.",
"lt_in_attr_value": "Use the entity reference < to include \"<\" in an attribute value.",
"bad_entity_ref": "Replace literal \"&\" with &, or terminate the entity reference with \";\".",
"duplicate_attribute": "Each attribute name in an open tag must be unique.",
"invalid_xml_char": "Only #x9, #xA, #xD and code points >= #x20 are legal XML characters.",
"reserved_namespace": "The \"xml\" prefix is fixed to " + xmlNSURI + "; the \"xmlns\" prefix cannot be redeclared, and neither URI may be bound to any other prefix or as the default namespace.",
"unbound_prefix": "Declare the prefix with xmlns:prefix=\"...\" on this element or one of its ancestors.",
"undeclared_entity": "Declare the entity in the DOCTYPE internal subset, add it to the customEntities option, or set strictEntities: false to allow unresolved references through.",
},
})
if !embed {
// Pure XML mode: reconfigure the parser so Jsonic's own value
// grammar is unreachable and all lexers other than our tag
// matcher are quiescent.
//
// Note: we deliberately do NOT install a Text.Modify hook
// here. While the root element is open the custom matcher
// itself emits the text tokens (with entity decoding and
// well-formedness checks); Jsonic's text matcher only sees
// whitespace before and after the root element where no
// decoding is needed.
j.SetOptions(jsonic.Options{
Rule: &jsonic.RuleOptions{
Start: "xml",
Exclude: "jsonic,imp",
},
Fixed: &jsonic.FixedOptions{Token: map[string]*string{
"#OB": nil, "#CB": nil, "#OS": nil, "#CS": nil,
"#CL": nil, "#CA": nil,
}},
Number: &jsonic.NumberOptions{Lex: boolPtr(false)},
Value: &jsonic.ValueOptions{Lex: boolPtr(false)},
String: &jsonic.StringOptions{Lex: boolPtr(false)},
Comment: &jsonic.CommentOptions{Lex: boolPtr(false)},
Space: &jsonic.SpaceOptions{Lex: boolPtr(false)},
Line: &jsonic.LineOptions{Lex: boolPtr(false)},
})
}
// IGNORE set: drop #XIG (comments, PIs, DOCTYPE) along with the
// default members so any of them is skipped by the parser. In
// embed mode this preserves all default ignored tokens; in pure
// mode the SP/LN/CM tokens are never produced (we disabled their
// lexers), but keeping them here is harmless.
j.SetTokenSet("IGNORE", []jsonic.Tin{
j.Token("#SP", ""), j.Token("#LN", ""), j.Token("#CM", ""), xigTin,
})
// Grammar declarations. Mirror the TypeScript grammar exactly.
refs := map[jsonic.FuncRef]any{
"@xml-bc": jsonic.StateAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
if r.Child == nil || r.Child == jsonic.NoRule || r.Child.Node == nil {
return
}
// The Go parser follows the Next chain forward from the root
// rule to find the final result holder, so the current rule's
// node is what the caller will see. Set it (and the original
// root's node via the Prev chain as well for safety).
r.Node = r.Child.Node
root := firstRule(r)
root.Node = r.Child.Node
// Mark the document as having seen its root so the
// @no-root-yet condition rejects any subsequent attempt
// to push a second root element (XML 1.0 §2.1).
ctx.U["rootSeen"] = true
if namespacesOn {
if el, ok := r.Node.(map[string]any); ok {
if code := resolveNamespaces(el, nil); code != "" {
ctx.ParseErr = &jsonic.Token{
Name: "#BD", Tin: jsonic.TinBD,
Err: code, Why: code, Src: code,
}
}
}
}
}),
"@no-root-yet": jsonic.AltCond(func(_ *jsonic.Rule, ctx *jsonic.Context) bool {
seen, _ := ctx.U["rootSeen"].(bool)
return !seen
}),
"@element-open": jsonic.AltAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
v := r.O0.Val.(map[string]any)
name := v["name"].(string)
attrs := v["attributes"].(map[string]any)
r.Node = map[string]any{
"name": name,
"localName": name,
"attributes": applyAttrDefaults(attrs, name, ctx),
"children": []any{},
}
}),
"@element-selfclose": jsonic.AltAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
v := r.O0.Val.(map[string]any)
name := v["name"].(string)
attrs := v["attributes"].(map[string]any)
r.Node = map[string]any{
"name": name,
"localName": name,
"attributes": applyAttrDefaults(attrs, name, ctx),
"children": []any{},
}
}),
"@element-close": jsonic.AltAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
el, _ := r.Node.(map[string]any)
openName, _ := el["name"].(string)
closeName, _ := r.C0.Val.(string)
if openName != closeName {
// The Go parser's top-level error handling reports parse
// errors under a single "unexpected" code, so encode our
// specific error code into the token's `Src`: that string
// is substituted into the error detail via $fsrc and will
// appear in err.Error() for consumers (and tests) that
// want to key on the specific cause.
r.C0.Src = "xml_mismatched_tag: </" + closeName + "> does not match <" + openName + ">"
if r.C0.Use == nil {
r.C0.Use = map[string]any{}
}
r.C0.Use["openname"] = openName
r.C0.Err = "xml_mismatched_tag"
ctx.ParseErr = r.C0
}
}),
"@child-text": jsonic.AltAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
el, _ := r.Node.(map[string]any)
children, _ := el["children"].([]any)
el["children"] = append(children, r.O0.Val)
r.U["done"] = true
}),
"@child-bc": jsonic.StateAction(func(r *jsonic.Rule, ctx *jsonic.Context) {
if done, _ := r.U["done"].(bool); done {
return
}
if r.Child == nil || r.Child == jsonic.NoRule || r.Child.Node == nil {
return
}
el, ok := r.Node.(map[string]any)
if !ok {
return
}
children, _ := el["children"].([]any)
el["children"] = append(children, r.Child.Node)
}),
"@element-is-selfclosed": jsonic.AltCond(func(r *jsonic.Rule, ctx *jsonic.Context) bool {
v, _ := r.U["selfclose"].(int)
return v == 1
}),
}
gs := &jsonic.GrammarSpec{
Ref: refs,
Rule: map[string]*jsonic.GrammarRuleSpec{
"xml": {
Open: []*jsonic.GrammarAltSpec{
{S: "#ZZ"},
{S: "#TX", R: "xml"},
{P: "element", C: "@no-root-yet"},
},
Close: []*jsonic.GrammarAltSpec{
{S: "#ZZ"},
{S: "#TX", R: "xml"},
},
},
"element": {
Open: []*jsonic.GrammarAltSpec{
{S: "#XSC", A: "@element-selfclose", U: map[string]any{"selfclose": 1}},
{S: "#XOP", P: "content", A: "@element-open"},
},
Close: []*jsonic.GrammarAltSpec{
{C: "@element-is-selfclosed"},
{S: "#XCL", A: "@element-close"},
},
},
"content": {
Open: []*jsonic.GrammarAltSpec{
{S: "#XCL", B: 1},
{P: "child"},
},
Close: []*jsonic.GrammarAltSpec{
{S: "#XCL", B: 1},
{R: "content"},
},
},
"child": {
Open: []*jsonic.GrammarAltSpec{
{S: "#TX", A: "@child-text"},
{S: "#XOP", B: 1, P: "element"},
{S: "#XSC", B: 1, P: "element"},
},
},
},
}
if err := j.Grammar(gs); err != nil {
return fmt.Errorf("xml: apply grammar: %w", err)
}
if embed {
// Splice XML literals into the Jsonic `val` rule. When the
// parser is looking for a value and sees `#XOP` or `#XSC`,
// push the `element` rule (backtracking by 1 so element.open
// can read the same token and dispatch).
j.Rule("val", func(rs *jsonic.RuleSpec, _ *jsonic.Parser) {
rs.Open = append(rs.Open,
&jsonic.AltSpec{
S: [][]jsonic.Tin{{xopTin}},
B: 1, P: "element", G: "xml",
},
&jsonic.AltSpec{
S: [][]jsonic.Tin{{xscTin}},
B: 1, P: "element", G: "xml",
},
)
})
// In embed mode the top-level wrapper is Jsonic's `val` rule,
// so the @xml-bc hook that copies the root element to
// ctx.root().node is not invoked. Resolve namespaces instead
// when the element rule closes directly under a val rule.
if namespacesOn {
j.Rule("element", func(rs *jsonic.RuleSpec, _ *jsonic.Parser) {
rs.AddBC(func(r *jsonic.Rule, ctx *jsonic.Context) {
if r.Parent != nil && r.Parent != jsonic.NoRule &&
r.Parent.Name == "val" {
if el, ok := r.Node.(map[string]any); ok {
resolveNamespaces(el, nil)
}
}
})
})
}
}
return nil
}
// dtdEntities reads the per-parse DOCTYPE-declared entity map (set
// by the DOCTYPE matcher path). Returns nil if none have been
// registered yet.
func dtdEntities(lex *jsonic.Lex) map[string]string {
if lex == nil || lex.Ctx == nil || lex.Ctx.U == nil {
return nil
}
m, _ := lex.Ctx.U["dtdEntities"].(map[string]string)
return m
}
// dtdAttrDefaults reads the per-parse DOCTYPE-supplied attribute
// default map keyed by element name (set by the DOCTYPE matcher
// path). Returns nil if none have been registered yet.
func dtdAttrDefaults(ctx *jsonic.Context) map[string]map[string]string {
if ctx == nil || ctx.U == nil {
return nil
}
m, _ := ctx.U["dtdAttrDefaults"].(map[string]map[string]string)
return m
}
// applyAttrDefaults merges in DOCTYPE-supplied default attribute
// values for any attribute missing from the parsed element instance.
// Returns the original map if no defaults apply.
func applyAttrDefaults(
attrs map[string]any, elemName string, ctx *jsonic.Context,
) map[string]any {
all := dtdAttrDefaults(ctx)
if all == nil {
return attrs
}
defaults, ok := all[elemName]
if !ok {
return attrs
}
for k, v := range defaults {
if _, present := attrs[k]; !present {
attrs[k] = v
}
}
return attrs
}
// parseDoctypeAttlists scans a DOCTYPE internal-subset body and
// extracts every `<!ATTLIST element attr type defaultDecl>` default
// attribute value, keyed by element name and attribute name. Both
// literal defaults and `#FIXED "value"` defaults are returned;
// `#REQUIRED` and `#IMPLIED` declarations contribute nothing because
// they have no default value.
func parseDoctypeAttlists(body string) map[string]map[string]string {
skipSpace := func(s int) int {
for s < len(body) && isSpace(body[s]) {
s++
}
return s
}
out := map[string]map[string]string{}
i := 0
for i < len(body) {
idx := strings.Index(body[i:], "<!ATTLIST")
if idx < 0 {
break
}
j := i + idx + len("<!ATTLIST")
j = skipSpace(j)
elemName, after, ok := readName(body, j)
if !ok {
i = j + 1
continue
}
j = after
for j < len(body) {
j = skipSpace(j)
if j >= len(body) {
break
}
if body[j] == '>' {
j++
break
}
attrName, attrEnd, ok := readName(body, j)
if !ok {
j++
continue
}
j = attrEnd
j = skipSpace(j)
// Skip AttType.
if j < len(body) && body[j] == '(' {
close := strings.Index(body[j:], ")")
if close < 0 {
j = len(body)
break
}
j = j + close + 1
} else if strings.HasPrefix(body[j:], "NOTATION") {
j += len("NOTATION")
j = skipSpace(j)
if j < len(body) && body[j] == '(' {
close := strings.Index(body[j:], ")")
if close < 0 {
j = len(body)
break
}
j = j + close + 1
}
} else {
for j < len(body) && body[j] >= 'A' && body[j] <= 'Z' {
j++
}
}
j = skipSpace(j)
// DefaultDecl.
if strings.HasPrefix(body[j:], "#REQUIRED") {
j += len("#REQUIRED")
continue
}
if strings.HasPrefix(body[j:], "#IMPLIED") {
j += len("#IMPLIED")
continue
}
if strings.HasPrefix(body[j:], "#FIXED") {
j += len("#FIXED")
j = skipSpace(j)
}
if j < len(body) && (body[j] == '"' || body[j] == '\'') {
quote := body[j]
j++
valStart := j
for j < len(body) && body[j] != quote {
j++
}
if j >= len(body) {
break
}
value := body[valStart:j]
if out[elemName] == nil {
out[elemName] = map[string]string{}
}
out[elemName][attrName] = value
j++
}
}
i = j
}
return out
}
// parseDoctypeEntities scans a DOCTYPE internal-subset body and
// extracts every internal general entity declaration of the form
// `<!ENTITY name "value">` (or single-quoted). Parameter entity
// declarations (`<!ENTITY % name ...>`) and external entity
// declarations (`<!ENTITY name SYSTEM "...">` etc.) are skipped, as
// are `<!ELEMENT`, `<!ATTLIST`, and `<!NOTATION` declarations.
//
// Returned values are stored verbatim — character and entity
// references inside an entity value are expanded only when the
// outer entity is referenced.
func parseDoctypeEntities(body string) map[string]string {
out := map[string]string{}
i := 0
for i < len(body) {
idx := strings.Index(body[i:], "<!ENTITY")
if idx < 0 {
break
}
j := i + idx + len("<!ENTITY")
for j < len(body) && isSpace(body[j]) {
j++
}
// Parameter entity: skip.
if j < len(body) && body[j] == '%' {
end := strings.Index(body[j:], ">")
if end < 0 {
break
}
i = j + end + 1
continue
}
// Read the entity name.
name, after, ok := readName(body, j)
if !ok {
i = j + 1
continue
}
j = after
for j < len(body) && isSpace(body[j]) {
j++
}
// Quoted value -> internal entity. SYSTEM/PUBLIC -> skip.
if j < len(body) && (body[j] == '"' || body[j] == '\'') {
quote := body[j]
j++
valStart := j
for j < len(body) && body[j] != quote {
j++
}
if j >= len(body) {
break
}
out[name] = body[valStart:j]
j++
}
end := strings.Index(body[j:], ">")
if end < 0 {
break
}
i = j + end + 1
}
return out
}
// xmlDepth reads the per-parse XML nesting counter from the lex context.
// Returns 0 if not set.
func xmlDepth(lex *jsonic.Lex) int {
if lex == nil || lex.Ctx == nil {
return 0
}
if lex.Ctx.U == nil {
lex.Ctx.U = map[string]any{}
return 0
}
v, _ := lex.Ctx.U["xmlDepth"].(int)
return v
}
// setXmlDepth writes the XML nesting counter, clamping at zero.
func setXmlDepth(lex *jsonic.Lex, d int) {
if lex == nil || lex.Ctx == nil {
return
}
if lex.Ctx.U == nil {
lex.Ctx.U = map[string]any{}
}
if d < 0 {
d = 0
}
lex.Ctx.U["xmlDepth"] = d
}
// DecodeBOM detects a byte-order mark at the start of `src` and, when
// the input is encoded as UTF-16 LE/BE or UTF-32 LE/BE, returns a
// transcoded UTF-8 string. UTF-8 BOMs are returned with the BOM bytes
// stripped. For input without a recognised BOM, the original string
// is returned unchanged.
//
// Use this when feeding XML files of unknown encoding into the
// parser:
//
// body, _ := os.ReadFile(path)
// doc, err := j.Parse(xml.DecodeBOM(string(body)))
func DecodeBOM(src string) string {
b := []byte(src)
n := len(b)
switch {
case n >= 4 && b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xfe && b[3] == 0xff:
return decodeUTF32(b[4:], binary.BigEndian)
case n >= 4 && b[0] == 0xff && b[1] == 0xfe && b[2] == 0x00 && b[3] == 0x00:
return decodeUTF32(b[4:], binary.LittleEndian)
case n >= 2 && b[0] == 0xfe && b[1] == 0xff:
return decodeUTF16(b[2:], binary.BigEndian)
case n >= 2 && b[0] == 0xff && b[1] == 0xfe:
return decodeUTF16(b[2:], binary.LittleEndian)
case n >= 3 && b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf:
return string(b[3:])
}
return src
}
func decodeUTF16(b []byte, order binary.ByteOrder) string {
if len(b)%2 != 0 {
b = b[:len(b)-1]
}
units := make([]uint16, len(b)/2)
for i := range units {
units[i] = order.Uint16(b[i*2:])
}
return string(utf16.Decode(units))
}
func decodeUTF32(b []byte, order binary.ByteOrder) string {
if len(b)%4 != 0 {
b = b[:len(b)-(len(b)%4)]
}
out := make([]rune, len(b)/4)
for i := range out {
out[i] = rune(order.Uint32(b[i*4:]))
}
return string(out)
}
// firstRule walks back through Prev links to find the originating rule
// instance (matches the root rule used by the parser as the result
// holder).
func firstRule(r *jsonic.Rule) *jsonic.Rule {
cur := r
for cur.Prev != nil && cur.Prev != jsonic.NoRule {
cur = cur.Prev
}
return cur
}
// predefinedEntities is the five XML-predefined entities.
var predefinedEntities = map[string]string{
"amp": "&",
"lt": "<",
"gt": ">",
"quot": "\"",
"apos": "'",
}
// entityRE matches a single entity reference: named, decimal numeric, or
// hexadecimal numeric. (?:...) would be ideal but the Go stdlib regexp
// supports named groups; this uses plain groups for portability.
var entityRE = regexp.MustCompile(`&(#x[0-9a-fA-F]+|#[0-9]+|[A-Za-z_][A-Za-z0-9_]*);`)
// EntityDecoder decodes XML entity references in `s`. The optional
// `dtd` map supplies general entity declarations parsed from the
// DOCTYPE internal subset; values are recursively expanded with
// cycle detection.
type EntityDecoder func(s string, dtd map[string]string) string
// buildEntityDecoder returns a function that decodes the five
// predefined entities, numeric character references, any
// caller-supplied custom entities, and per-parse DTD entities.
// When `enabled` is false the function is an identity. The second
// return value is the merged set of always-declared names used for
// strict-entity validation in the matcher.
func buildEntityDecoder(
enabled bool, custom map[string]string,
) (EntityDecoder, map[string]string) {
base := make(map[string]string, len(predefinedEntities)+len(custom))
for k, v := range predefinedEntities {
base[k] = v
}
for k, v := range custom {
base[k] = v
}
if !enabled {
return func(s string, _ map[string]string) string { return s }, base
}
var expand func(s string, dtd map[string]string, seen map[string]bool) string
expand = func(s string, dtd map[string]string, seen map[string]bool) string {
if !strings.Contains(s, "&") {
return s
}
return entityRE.ReplaceAllStringFunc(s, func(match string) string {
ref := match[1 : len(match)-1]
if ref[0] == '#' {
var code int64
var err error
if len(ref) > 1 && (ref[1] == 'x' || ref[1] == 'X') {
code, err = strconv.ParseInt(ref[2:], 16, 32)
} else {
code, err = strconv.ParseInt(ref[1:], 10, 32)
}
if err != nil {
return match
}
return string(rune(code))
}
if v, ok := base[ref]; ok {
return v
}
if dtd != nil {
if v, ok := dtd[ref]; ok {
if seen[ref] {
// Recursive reference; break the cycle.
return match
}
seen[ref] = true
out := expand(v, dtd, seen)
delete(seen, ref)
return out
}
}
return match
})
}
return func(s string, dtd map[string]string) string {
return expand(s, dtd, map[string]bool{})
}, base
}
// buildXmlTagMatcher returns a MakeLexMatcher that recognises every
// top-level XML `<...>` construct at the current lex position. On a
// successful match it consumes the full construct and emits exactly
// one of:
//
// #XOP <name attr="v" ...> val = {"name":..., "attributes":...}
// #XSC <name attr="v" ... /> val = {"name":..., "attributes":...}
// #XCL </name> val = name (string)
// #XIG <!-- ... --> | <?...?> | <!DOCTYPE ...> (ignored)
// #TX <![CDATA[ ... ]]> val = cdata body (verbatim, no entity decoding)
func buildXmlTagMatcher(
decode EntityDecoder,
declared map[string]string,
entitiesOn bool,
strict bool,
embed bool,
xigTin, xopTin, xclTin, xscTin jsonic.Tin,
) jsonic.MakeLexMatcher {
_ = embed // embed flag is no longer needed for text-handling
return func(_ *jsonic.LexConfig, _ *jsonic.Options) jsonic.LexMatcher {
return func(lex *jsonic.Lex, _ *jsonic.Rule) *jsonic.Token {
pnt := lex.Cursor()
src := lex.Src
srclen := len(src)
sI := pnt.SI
// Strip a UTF-8 byte-order mark at the very start of input.
if sI == 0 && srclen >= 3 &&
src[0] == 0xef && src[1] == 0xbb && src[2] == 0xbf {
pnt.SI = 3
return nil
}
// Inside an open XML element (depth > 0), consume
// characters up to the next `<` as a single #TX text
// token. Validates well-formedness of character data:
// rejects "]]>" and bare/malformed entity references.
if sI < srclen && src[sI] != '<' {
if depth := xmlDepth(lex); depth > 0 {
i := sI
for i < srclen && src[i] != '<' {
i++
}
if i == sI {
return nil
}
raw := src[sI:i]
if code := checkChars(raw); code != "" {
return lex.Bad(code)
}
if strings.Contains(raw, "]]>") {
return lex.Bad("cdata_terminator_in_text")
}
if code := checkEntityRefs(raw, dtdEntities(lex), declared, strict); code != "" {
return lex.Bad(code)
}
// §2.11 end-of-line normalisation.
normalised := normaliseLineEndings(raw)
var val any = normalised
if entitiesOn {
val = decode(normalised, dtdEntities(lex))
}
tkn := lex.Token("#TX", jsonic.TinTX, val, raw)
advance(pnt, sI, i)
return tkn
}
}
if sI >= srclen || src[sI] != '<' {
return nil
}
// Comment: <!-- ... -->
if strings.HasPrefix(src[sI:], "<!--") {
end := strings.Index(src[sI+4:], "-->")
if end < 0 {
return lex.Bad("unterminated_comment")
}
bodyStart := sI + 4
bodyEnd := bodyStart + end
body := src[bodyStart:bodyEnd]
// WF: "--" must not occur in a comment body.
if strings.Contains(body, "--") {
return lex.Bad("comment_double_dash")
}
if code := checkChars(body); code != "" {
return lex.Bad(code)
}
finish := bodyEnd + 3
tsrc := src[sI:finish]
tkn := lex.Token("#XIG", xigTin, tsrc, tsrc)
advance(pnt, sI, finish)
return tkn
}
// CDATA: <![CDATA[ ... ]]>
if strings.HasPrefix(src[sI:], "<![CDATA[") {
body := sI + 9
end := strings.Index(src[body:], "]]>")
if end < 0 {
return lex.Bad("unterminated_cdata")
}
finish := body + end + 3
text := src[body : body+end]
if code := checkChars(text); code != "" {
return lex.Bad(code)
}
tsrc := src[sI:finish]
// §2.11 line-end normalisation applies to CDATA too.
tkn := lex.Token("#TX", jsonic.TinTX, normaliseLineEndings(text), tsrc)
advance(pnt, sI, finish)
return tkn
}
// DOCTYPE: <!DOCTYPE ... [...] > (allows a single level of [] subset)
if strings.HasPrefix(src[sI:], "<!DOCTYPE") {
i := sI + 9
depth := 0
subsetStart, subsetEnd := -1, -1
for i < srclen {
ch := src[i]
// Skip over quoted strings so `]` and `>` inside an
// entity value or attribute default cannot terminate
// the subset prematurely.
if ch == '"' || ch == '\'' {
i++
for i < srclen && src[i] != ch {
i++
}
if i < srclen {
i++
}
continue
}
if ch == '[' {
if depth == 0 {
subsetStart = i + 1
}
depth++
} else if ch == ']' {
depth--
if depth == 0 {
subsetEnd = i
}
} else if ch == '>' && depth <= 0 {
break
}
i++
}
if i >= srclen {
return lex.Bad("unterminated_doctype")
}
finish := i + 1
// Extract internal-subset declarations and stash them
// on the per-parse context. The matcher's text /
// attribute paths and the element actions read these
// back via lex.Ctx.U.
if subsetStart >= 0 && subsetEnd > subsetStart && lex.Ctx != nil {
subset := src[subsetStart:subsetEnd]
if lex.Ctx.U == nil {
lex.Ctx.U = map[string]any{}
}
if found := parseDoctypeEntities(subset); len(found) > 0 {
existing, _ := lex.Ctx.U["dtdEntities"].(map[string]string)
if existing == nil {
existing = map[string]string{}
}
for k, v := range found {
existing[k] = v
}
lex.Ctx.U["dtdEntities"] = existing
}
if found := parseDoctypeAttlists(subset); len(found) > 0 {
existing, _ := lex.Ctx.U["dtdAttrDefaults"].(map[string]map[string]string)
if existing == nil {
existing = map[string]map[string]string{}
}
for elem, defs := range found {
if existing[elem] == nil {
existing[elem] = map[string]string{}
}
for k, v := range defs {
existing[elem][k] = v
}
}
lex.Ctx.U["dtdAttrDefaults"] = existing
}
}
tsrc := src[sI:finish]
tkn := lex.Token("#XIG", xigTin, tsrc, tsrc)
advance(pnt, sI, finish)
return tkn
}
// Processing instruction: <? ... ?>
if sI+1 < srclen && src[sI+1] == '?' {
end := strings.Index(src[sI+2:], "?>")
if end < 0 {
return lex.Bad("unterminated_pi")
}
bodyEnd := sI + 2 + end
// WF: PI target must be a Name.
_, after, ok := readName(src, sI+2)
if !ok || after > bodyEnd {
return lex.Bad("pi_target_invalid")
}
if after < bodyEnd && !isSpace(src[after]) {
return lex.Bad("pi_target_invalid")
}
if code := checkChars(src[sI+2 : bodyEnd]); code != "" {
return lex.Bad(code)
}
finish := bodyEnd + 2
tsrc := src[sI:finish]
tkn := lex.Token("#XIG", xigTin, tsrc, tsrc)
advance(pnt, sI, finish)
return tkn
}
// Closing tag: </name>
if sI+1 < srclen && src[sI+1] == '/' {
name, after, ok := readName(src, sI+2)
// WF: empty close tag `</>` is invalid.
if !ok {
return lex.Bad("xml_invalid_tag")
}
i := after
for i < srclen && isSpace(src[i]) {
i++
}
if i >= srclen || src[i] != '>' {
return lex.Bad("xml_invalid_tag")
}
finish := i + 1
tsrc := src[sI:finish]