-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
917 lines (779 loc) · 21.3 KB
/
parse.go
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
package schemax
import (
"sort"
"github.com/JesseCoretta/go-antlr4512"
)
var (
new4512Schema func() antlr4512.Schema = antlr4512.NewSchema
parseLS func(string) (antlr4512.LDAPSyntax, error) = antlr4512.ParseLDAPSyntax
parseMR func(string) (antlr4512.MatchingRule, error) = antlr4512.ParseMatchingRule
parseAT func(string) (antlr4512.AttributeType, error) = antlr4512.ParseAttributeType
parseMU func(string) (antlr4512.MatchingRuleUse, error) = antlr4512.ParseMatchingRuleUse
parseOC func(string) (antlr4512.ObjectClass, error) = antlr4512.ParseObjectClass
parseDC func(string) (antlr4512.DITContentRule, error) = antlr4512.ParseDITContentRule
parseNF func(string) (antlr4512.NameForm, error) = antlr4512.ParseNameForm
parseDS func(string) (antlr4512.DITStructureRule, error) = antlr4512.ParseDITStructureRule
)
/*
ParseLDAPSyntax returns an error following an attempt to parse raw into an
instance of [LDAPSyntax] and append it to the [Schema.LDAPSyntaxes] stack.
*/
func (r Schema) ParseLDAPSyntax(raw string) error {
def, err := parseLS(raw)
if err == nil {
var _def LDAPSyntax
if _def, err = r.marshalLS(def); err == nil {
r.LDAPSyntaxes().push(_def)
}
}
return err
}
/*
ParseMatchingRule returns an error following an attempt to parse raw into an
instance of [MatchingRule] and append it to the [Schema.MatchingRules] stack.
*/
func (r Schema) ParseMatchingRule(raw string) error {
def, err := parseMR(raw)
if err == nil {
var _def MatchingRule
if _def, err = r.marshalMR(def); err == nil {
r.MatchingRules().push(_def)
}
}
return err
}
/*
ParseMatchingRuleUse returns an error following an attempt to parse raw into an
instance of [MatchingRuleUse] and append it to the [Schema.MatchingRuleUses] stack.
*/
func (r Schema) ParseMatchingRuleUse(raw string) error {
def, err := parseMU(raw)
if err == nil {
var _def MatchingRuleUse
if _def, err = r.marshalMU(def); err == nil {
r.MatchingRuleUses().push(_def)
}
}
return err
}
/*
ParseAttributeType returns an error following an attempt to parse raw into an
instance of [AttributeType] and append it to the [Schema.AttributeTypes] stack.
*/
func (r Schema) ParseAttributeType(raw string) error {
def, err := parseAT(raw)
if err == nil {
var _def AttributeType
if _def, err = r.marshalAT(def); err == nil {
r.AttributeTypes().push(_def)
}
}
return err
}
/*
ParseObjectClass returns an error following an attempt to parse raw into an
instance of [ObjectClass] and append it to the [Schema.ObjectClasses] stack.
*/
func (r Schema) ParseObjectClass(raw string) error {
def, err := parseOC(raw)
if err == nil {
var _def ObjectClass
if _def, err = r.marshalOC(def); err == nil {
r.ObjectClasses().push(_def)
}
}
return err
}
/*
ParseDITContentRule returns an error following an attempt to parse raw into an
instance of [DITContentRule] and append it to the [Schema.DITContentRules] stack.
*/
func (r Schema) ParseDITContentRule(raw string) error {
def, err := parseDC(raw)
if err == nil {
var _def DITContentRule
if _def, err = r.marshalDC(def); err == nil {
r.DITContentRules().push(_def)
}
}
return err
}
/*
ParseNameForm returns an error following an attempt to parse raw into an
instance of [NameForm] and append it to the [Schema.NameForms] stack.
*/
func (r Schema) ParseNameForm(raw string) error {
def, err := parseNF(raw)
if err == nil {
var _def NameForm
if _def, err = r.marshalNF(def); err == nil {
r.NameForms().push(_def)
}
}
return err
}
/*
ParseDITStructureRule returns an error following an attempt to parse raw into an
instance of [DITStructureRule] and append it to the [Schema.DITStructureRules] stack.
*/
func (r Schema) ParseDITStructureRule(raw string) error {
def, err := parseDS(raw)
if err == nil {
var _def DITStructureRule
if _def, err = r.marshalDS(def); err == nil {
r.DITStructureRules().push(_def)
}
}
return err
}
/*
incorporate returns an error following an attempt to marshal the contents
of s into r. The concept of "incorporation" is another term for post-parsing
"hand-off" from antlr4512 to schemax, and is the last of two (2) phases
involving the antlr4512 package.
Empty slice types within s shall not result in an error.
*/
func (r Schema) incorporate(s antlr4512.Schema) (err error) {
for _, err := range []error{
r.incorporateLS(s.LS),
r.incorporateMR(s.MR),
r.incorporateAT(s.AT),
r.incorporateMU(s.MU),
r.incorporateOC(s.OC),
r.incorporateDC(s.DC),
r.incorporateNF(s.NF),
r.incorporateDS(s.DS),
} {
if err != nil {
break
}
}
return
}
func (r Schema) incorporateLS(s antlr4512.LDAPSyntaxes) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def LDAPSyntax
if def, err = r.marshalLS(s[i]); err == nil {
r.LDAPSyntaxes().push(def)
}
}
return
}
func (r Schema) marshalLS(s antlr4512.LDAPSyntax) (def LDAPSyntax, err error) {
// try to resolve a macro only if no
// numeric OID is present.
if len(s.Macro) == 2 && len(s.OID) == 0 {
mc, found := r.Macros().Resolve(s.Macro[0])
if found {
s.OID = mc + `.` + s.Macro[1]
}
}
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
}
// fail attempts to marshal a duplicate definition.
if lup := r.LDAPSyntaxes().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}
_def := newLDAPSyntax()
_def.OID = s.OID
_def.Desc = s.Desc
_def.schema = r
_def.Extensions.setDefinition(LDAPSyntax{_def})
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = LDAPSyntax{_def}
}
return
}
func (r Schema) incorporateMR(s antlr4512.MatchingRules) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def MatchingRule
if def, err = r.marshalMR(s[i]); err == nil {
r.MatchingRules().push(def)
}
}
return
}
func (r Schema) marshalMR(s antlr4512.MatchingRule) (def MatchingRule, err error) {
// try to resolve a macro only if no
// numeric OID is present.
if len(s.Macro) == 2 && len(s.OID) == 0 {
mc, found := r.Macros().Resolve(s.Macro[0])
if found {
s.OID = mc + `.` + s.Macro[1]
}
}
// fail attempts to marshal a duplicate definition.
if lup := r.MatchingRules().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
} else if !isNumericOID(s.Syntax) {
// try to resolve a quotedDescriptor to an OID
// by scanning the DESC values of each syntax
// within the receiver. Use of Whitespace and
// case folding are not significant in the
// matching process.
if syn := r.LDAPSyntaxes().get(s.Syntax); !syn.IsZero() {
// overwrite the DESC reference of syntax
// with the intended syntax's numeric OID.
s.Syntax = syn.NumericOID()
}
}
syn := r.LDAPSyntaxes().get(s.Syntax)
if syn.IsZero() {
// throw an error due to bad syntax ref
err = mkerr(ErrLDAPSyntaxNotFound.Error() + `(` + s.Syntax + `)`)
return
}
_def := newMatchingRule()
_def.OID = s.OID
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Syntax = syn
_def.Extensions.setDefinition(MatchingRule{_def})
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = MatchingRule{_def}
}
return
}
func (r Schema) incorporateMU(s antlr4512.MatchingRuleUses) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def MatchingRuleUse
if def, err = r.marshalMU(s[i]); err == nil {
r.MatchingRuleUses().push(def)
}
}
return
}
func (r Schema) marshalMU(s antlr4512.MatchingRuleUse) (def MatchingRuleUse, err error) {
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
}
lup := r.MatchingRules().get(s.OID)
if lup.IsZero() {
// silently ignore attempts to reference a bogus matchingRule OID
return
}
// fail attempts to marshal a duplicate definition.
if lup := r.MatchingRuleUses().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}
_def := newMatchingRuleUse()
_def.OID = lup
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Extensions.setDefinition(MatchingRuleUse{_def})
sortL := r.Options().Positive(SortLists)
for i := 0; i < len(sortList(s.Applies, sortL)); i++ {
_at := s.Applies[i]
at := r.AttributeTypes().get(_at)
if at.IsZero() {
err = ErrAttributeTypeNotFound
return
}
_def.Applies.push(at)
}
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = MatchingRuleUse{_def}
}
return
}
func (r Schema) incorporateAT(s antlr4512.AttributeTypes) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def AttributeType
if def, err = r.marshalAT(s[i]); err == nil {
r.AttributeTypes().push(def)
}
}
return
}
func (r Schema) marshalAT(s antlr4512.AttributeType) (def AttributeType, err error) {
// try to resolve a macro only if no numeric OID is present.
if s.OID = handleMacro(r, s.Macro, s.OID); !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
} else if lup := r.AttributeTypes().get(s.OID); !lup.IsZero() {
// fail on attempts to marshal a duplicate definition.
err = ErrDuplicateDef
return
}
// alloc
_def := newAttributeType()
// Take any primitive values we can
_def.OID = s.OID
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.NoUserMod = s.Immutable
_def.Collective = s.Collective
_def.Single = s.Single
_def.MUB = s.MUB
_def.schema = r
_def.Extensions.setDefinition(AttributeType{_def})
// avoid bogus Boolean states
if _def.Single && _def.Collective {
err = mkerr("AttributeType is both single-valued and collective; aborting (" + s.OID + `)`)
return
}
// verify and set an LDAPSyntax if specified
if syn := s.Syntax; len(syn) > 0 {
llup := r.LDAPSyntaxes().get(syn)
if llup.IsZero() {
// throw an error due to bad syntax ref
err = mkerr(ErrLDAPSyntaxNotFound.Error() + `(` + syn + `)`)
return
}
_def.Syntax = llup
}
// Attempt to marshal any (EQ/ORD/SS) matching rules
// from the antlr instance into the destination instance
if err = _def.marshalMRs(s); err != nil {
return
}
// verify and set supertype, if specified.
if _sup := s.SuperType; len(_sup) > 0 {
sup := r.AttributeTypes().get(_sup)
if sup.IsZero() {
err = mkerr(ErrAttributeTypeNotFound.Error() + `( supertype: ` + _sup + `)`)
return
}
_def.SuperType = sup
}
// marshal our intended attribute usage
_def.marshalUsage(s)
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
// Process and set any names
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = AttributeType{_def}
}
return
}
func (r *attributeType) marshalMRs(s antlr4512.AttributeType) (err error) {
// verify any non-zero matchingrule references
for idx, mrl := range []string{
s.Equality, // EQUALITY
s.Substring, // SUBSTR
s.Ordering, // ORDERING
} {
if len(mrl) > 0 {
// If mrl was non-zero, lookup using its
// text value (name or OID). Fail if not
// found, assign to appropriate field
// otherwise.
llup := r.schema.MatchingRules().get(mrl)
if llup.IsZero() {
err = mkerr(ErrMatchingRuleNotFound.Error() + `(` + mrl + `)`)
break
}
switch idx {
case 1:
r.Substring = llup
case 2:
r.Ordering = llup
default:
r.Equality = llup
}
}
}
return
}
func (r *attributeType) marshalUsage(s antlr4512.AttributeType) {
// verify and set usage IF NOT default ("userApplications")
if len(s.Usage) > 0 {
switch lc(s.Usage) {
case `directoryoperation`:
r.Usage = DirectoryOperationUsage
case `distributedoperation`:
r.Usage = DistributedOperationUsage
case `dsaoperation`:
r.Usage = DSAOperationUsage
}
}
}
func (r Schema) incorporateOC(s antlr4512.ObjectClasses) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def ObjectClass
if def, err = r.marshalOC(s[i]); err == nil {
r.ObjectClasses().push(def)
}
}
return
}
func (r Schema) marshalOC(s antlr4512.ObjectClass) (def ObjectClass, err error) {
// try to resolve a macro only if no numeric OID is present.
if s.OID = handleMacro(r, s.Macro, s.OID); !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
}
// fail attempts to marshal a duplicate definition.
if lup := r.ObjectClasses().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}
_def := newObjectClass()
_def.OID = s.OID
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Extensions.setDefinition(ObjectClass{_def})
sortL := r.Options().Positive(SortLists)
// verify and set kind if not default of STRUCTURAL
switch k := lc(s.Kind); k {
case `structural`, ``:
_def.Kind = StructuralKind
case `auxiliary`:
_def.Kind = AuxiliaryKind
case `abstract`:
_def.Kind = AbstractKind
default:
err = mkerr("Invalid kind for objectClass: " + k)
return
}
for _, must := range sortList(s.Must, sortL) {
m := r.AttributeTypes().get(must)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MUST clause: " + must)
return
}
_def.Must.push(m)
}
for _, may := range sortList(s.May, sortL) {
m := r.AttributeTypes().get(may)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MAY clause: " + may)
return
}
_def.May.push(m)
}
for _, sup := range sortList(s.SuperClasses, sortL) {
m := r.ObjectClasses().get(sup)
if m.IsZero() {
err = mkerr("Unknown SuperClass: " + sup)
return
}
_def.SuperClasses.push(m)
}
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
// Process and set any names
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = ObjectClass{_def}
}
return
}
func (r Schema) incorporateDC(s antlr4512.DITContentRules) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def DITContentRule
if def, err = r.marshalDC(s[i]); err == nil {
r.DITContentRules().push(def)
}
}
return
}
func (r Schema) checkDCOID(s antlr4512.DITContentRule) (err error) {
if soc := r.ObjectClasses().Get(s.OID); soc.IsZero() {
err = mkerr(ErrObjectClassNotFound.Error() + `( superclass: ` + s.OID + `)`)
} else if lup := r.DITContentRules().get(s.OID); !lup.IsZero() {
// fail attempts to marshal a duplicate definition.
err = ErrDuplicateDef
}
return
}
func (r Schema) marshalDC(s antlr4512.DITContentRule) (def DITContentRule, err error) {
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
}
_def := newDITContentRule()
if err = r.checkDCOID(s); err != nil {
return
}
soc := r.ObjectClasses().Get(s.OID)
_def.OID = soc
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Extensions.setDefinition(DITContentRule{_def})
sortL := r.Options().Positive(SortLists)
for _, must := range sortList(s.Must, sortL) {
m := r.AttributeTypes().get(must)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MUST clause: " + must)
return
}
_def.Must.push(m)
}
for _, may := range sortList(s.May, sortL) {
m := r.AttributeTypes().get(may)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MAY clause: " + may)
return
}
_def.May.push(m)
}
for _, not := range sortList(s.Not, sortL) {
m := r.AttributeTypes().get(not)
if m.IsZero() {
err = mkerr("Unknown AttributeType for NOT clause: " + not)
return
}
_def.Not.push(m)
}
for _, aux := range sortList(s.Aux, sortL) {
m := r.ObjectClasses().get(aux)
if m.IsZero() {
err = mkerr("Unknown ObjectClass for AUX clause: " + aux)
return
}
_def.Aux.push(m)
}
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = DITContentRule{_def}
if !def.Compliant() {
err = ErrDefNonCompliant
}
}
return
}
func (r Schema) incorporateNF(s antlr4512.NameForms) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def NameForm
if def, err = r.marshalNF(s[i]); err == nil {
r.NameForms().push(def)
}
}
return
}
func (r Schema) marshalNF(s antlr4512.NameForm) (def NameForm, err error) {
if !isNumericOID(s.OID) {
err = ErrMissingNumericOID
return
}
// fail attempts to marshal a duplicate definition.
if lup := r.NameForms().get(s.OID); !lup.IsZero() {
err = ErrDuplicateDef
return
}
_def := newNameForm()
_def.OID = s.OID
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Extensions.setDefinition(NameForm{_def})
sortL := r.Options().Positive(SortLists)
oc := r.ObjectClasses().get(s.OC)
if oc.IsZero() {
err = mkerr(ErrObjectClassNotFound.Error() + `( structural: ` + s.OC + `)`)
return
}
_def.Structural = oc
for _, must := range sortList(s.Must, sortL) {
m := r.AttributeTypes().get(must)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MUST clause: " + must)
return
}
_def.Must.push(m)
}
for _, may := range sortList(s.May, sortL) {
m := r.AttributeTypes().get(may)
if m.IsZero() {
err = mkerr("Unknown AttributeType for MAY clause: " + may)
return
}
_def.May.push(m)
}
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = NameForm{_def}
}
return
}
func (r Schema) incorporateDS(s antlr4512.DITStructureRules) (err error) {
for i := 0; i < len(s) && err == nil; i++ {
var def DITStructureRule
if def, err = r.marshalDS(s[i]); err == nil {
r.DITStructureRules().push(def)
}
}
return
}
func (r Schema) marshalDS(s antlr4512.DITStructureRule) (def DITStructureRule, err error) {
var ruleid uint
var ok bool
if ruleid, ok = atoui(s.ID); !ok {
err = mkerr("Invalid structure rule ID " + s.ID)
return
}
// fail attempts to marshal a duplicate definition.
if lup := r.DITStructureRules().get(ruleid); !lup.IsZero() {
err = ErrDuplicateDef
return
}
_def := newDITStructureRule()
_def.ID = ruleid
_def.Desc = s.Desc
_def.Obsolete = s.Obsolete
_def.schema = r
_def.Extensions.setDefinition(DITStructureRule{_def})
sortL := r.Options().Positive(SortLists)
nf := r.NameForms().get(s.Form)
if nf.IsZero() {
err = mkerr(ErrNameFormNotFound.Error() + `(` + s.Form + `)`)
return
}
_def.Form = nf
for _, sup := range sortList(s.SuperRules, sortL) {
m := r.DITStructureRules().get(sup)
if m.IsZero() {
err = mkerr("Unknown rule for SUP clause: " + sup)
return
}
_def.SuperRules.push(m)
}
// Marshal our extensions, taking our sorting preference into account
marshalExt(s.Extensions, _def.Extensions,
r.Options().Positive(SortExtensions))
for _, name := range s.Name {
_def.Name.push(name)
}
str, err := _def.prepareString() // perform one-time text/template op
if err == nil {
// Save the stringer
_def.stringer = func() string {
// Return a preserved value.
return str
}
def = DITStructureRule{_def}
}
return
}
func sortList(list []string, sortBySlice bool) []string {
if !sortBySlice {
return list
}
sort.Strings(list)
return list
}
/*
marshalExt funnels mext into ext with or without sorting.
*/
func marshalExt(mext map[string][]string, ext Extensions, sortByXStr bool) {
if !sortByXStr {
for k, v := range mext {
ext.Set(k, v...)
}
return
}
// Get a list of keys from mext
keys := make([]string, 0, len(mext))
for k := range mext {
keys = append(keys, k)
}
// Sort keys alphabetically
sort.Strings(keys)
// Use new sorting scheme to influence
// Extension initialization within ext.
for _, k := range keys {
ext.Set(k, mext[k]...)
}
}
func handleMacro(r Schema, m []string, o string) (resv string) {
if len(o) > 0 {
resv = o
return
}
if len(m) == 2 && len(o) == 0 {
mc, found := r.Macros().Resolve(m[0])
if found {
resv = mc + `.` + m[1]
}
}
return
}