/
doc.go
1182 lines (1047 loc) · 24.5 KB
/
doc.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
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
package turtle
import (
"fmt"
nt "github.com/0x51-dev/rdf/ntriples"
"github.com/0x51-dev/rdf/turtle/grammar"
"github.com/0x51-dev/upeg/parser"
"github.com/0x51-dev/upeg/parser/op"
"sort"
"strings"
)
func EvaluateDocument(doc Document, cwd string) (nt.Document, error) {
return NewContext().evaluateDocument(doc, cwd)
}
func ValidateDocument(doc Document) bool {
return NewContext().validateDocument(doc)
}
type A struct{}
func (a A) Equal(v any) bool {
if _, ok := v.(A); ok {
return true
}
if v, ok := v.(*A); ok && v != nil {
return true
}
return false
}
func (a A) String() string {
return "a"
}
func (a A) verb() {}
type Base string
func ParseBase(n *parser.Node) (*Base, error) {
if n.Name != "Base" {
return nil, fmt.Errorf("Base: unknown %s", n.Name)
}
base := Base(n.Children()[0].Value())
return &base, nil
}
func (b Base) Equal(v any) bool {
if v, ok := v.(Base); ok {
return b == v
}
if v, ok := v.(*Base); ok && v != nil {
return b == *v
}
return false
}
func (b Base) String() string {
return fmt.Sprintf("@base <%s> .", string(b))
}
func (b Base) directive() {}
func (b Base) statement() {}
type BlankNode string
func ParseBlankNode(n *parser.Node) (*BlankNode, error) {
if n.Name != "BlankNode" {
return nil, fmt.Errorf("blank node: unknown %s", n.Name)
}
switch n = n.Children()[0]; n.Name {
case "BlankNodeLabel":
bn := BlankNode(n.Value())
return &bn, nil
case "Anon":
bn := BlankNode("[]")
return &bn, nil
default:
return nil, fmt.Errorf("blank node: unknown: %s", n.Name)
}
}
func (b BlankNode) Equal(v any) bool {
if v, ok := v.(BlankNode); ok {
return b.equal(v)
}
if v, ok := v.(*BlankNode); ok && v != nil {
return b.equal(*v)
}
return false
}
func (b BlankNode) String() string {
if b == "[]" {
return string(b)
}
return fmt.Sprintf("_:%s", string(b))
}
func (b BlankNode) equal(other BlankNode) bool {
return b == other
}
func (b BlankNode) object() {}
func (b BlankNode) subject() {}
// BlankNodePropertyList may appear in the subject or object position of a triple. That subject or object is a fresh RDF
// blank node. This blank node also serves as the subject of the triples produced by matching the predicateObjectList
// production embedded in a blankNodePropertyList.
type BlankNodePropertyList PredicateObjectList
func ParseBlankNodePropertyList(n *parser.Node) (BlankNodePropertyList, error) {
if n.Name != "BlankNodePropertyList" {
return nil, fmt.Errorf("blank node property list: unknown %s", n.Name)
}
pol, err := ParsePredicateObjectList(n.Children()[0])
if err != nil {
return nil, err
}
sort.Sort(pol)
return BlankNodePropertyList(pol), nil
}
func (b BlankNodePropertyList) Equal(v any) bool {
if v, ok := v.(BlankNodePropertyList); ok {
return b.equal(v)
}
if v, ok := v.(*BlankNodePropertyList); ok && v != nil {
return b.equal(*v)
}
return false
}
func (b BlankNodePropertyList) String() string {
var s string
s += "[ "
for i, po := range b {
if i > 0 {
s += " ; "
}
s += po.String()
}
s += " ]"
return s
}
func (b BlankNodePropertyList) equal(other BlankNodePropertyList) bool {
return PredicateObjectList(b).equal(PredicateObjectList(other))
}
func (b BlankNodePropertyList) object() {}
type BooleanLiteral bool
func (bl BooleanLiteral) Equal(v any) bool {
if v, ok := v.(BooleanLiteral); ok {
return bl == v
}
if v, ok := v.(*BooleanLiteral); ok && v != nil {
return bl == *v
}
return false
}
func (bl BooleanLiteral) String() string {
return fmt.Sprintf("%t", bl)
}
func (bl BooleanLiteral) literal() {}
func (bl BooleanLiteral) object() {}
// Collection represents a rdf:first/rdf:rest list structure with the sequence of objects of the rdf:first statements
// being the order of the terms enclosed by (). It appears in the subject or object position of a triple. The blank node
// at the head of the list is the subject or object of the containing triple.
type Collection []Object
func ParseCollection(n *parser.Node) (Collection, error) {
if n.Name != "Collection" {
return nil, fmt.Errorf("collection: unknown %s", n.Name)
}
var collection Collection
for _, n := range n.Children() {
object, err := ParseObject(n)
if err != nil {
return nil, err
}
collection = append(collection, object)
}
return collection, nil
}
func (c Collection) Equal(v any) bool {
if v, ok := v.(Collection); ok {
return c.equal(v, false)
}
if v, ok := v.(*Collection); ok && v != nil {
return c.equal(*v, false)
}
return false
}
func (c Collection) String() string {
var s string
s += "("
for i, o := range c {
if i > 0 {
s += " "
}
s += o.String()
}
s += ")"
return s
}
func (c Collection) equal(other Collection, checkBlankNode bool) bool {
if len(c) != len(other) {
return false
}
for i, o := range c {
switch o.(type) {
case BlankNode, *BlankNode:
if !checkBlankNode {
_, isBn := other[i].(BlankNode)
_, isBnPtr := other[i].(*BlankNode)
if !isBn && !isBnPtr {
return false
}
} else {
if !o.Equal(other[i]) {
return false
}
}
default:
if !o.Equal(other[i]) {
return false
}
}
}
return true
}
func (c Collection) normalizeBlankNodes(f func(b fmt.Stringer) *BlankNode) (n Collection) {
for _, v := range c {
switch o := v.(type) {
case BlankNode, *BlankNode:
n = append(n, f(o))
case BlankNodePropertyList:
n = append(n, BlankNodePropertyList(
PredicateObjectList(o).normalizeBlankNodes(f),
))
case Collection:
n = append(n, o.normalizeBlankNodes(f))
default:
n = append(n, o)
}
}
return
}
func (c Collection) object() {}
func (c Collection) subject() {}
type Directive interface {
directive()
Equal(v any) bool
fmt.Stringer
}
func ParseDirective(n *parser.Node) (Directive, error) {
if n.Name != "Directive" {
return nil, fmt.Errorf("directive: unknown %s", n.Name)
}
switch n = n.Children()[0]; n.Name {
case "Base":
return ParseBase(n)
case "Prefix":
return ParsePrefix(n)
default:
return nil, fmt.Errorf("directive: unknown: %s", n.Name)
}
}
type Document []Statement
func ParseDocument(doc string) (Document, error) {
if len(doc) == 0 {
return nil, nil
}
if !strings.HasSuffix(doc, "\n") {
doc += "\n"
}
p, err := grammar.NewParser([]rune(doc))
if err != nil {
return nil, err
}
n, err := p.Parse(op.And{grammar.Document, op.EOF{}})
if err != nil {
return nil, err
}
return parseDocument(n)
}
func parseDocument(n *parser.Node) (Document, error) {
if n.Name != "Document" {
return nil, fmt.Errorf("document: unknown %s", n.Name)
}
var document Document
for _, n := range n.Children() {
switch n.Name {
case "Directive":
d, err := ParseDirective(n)
if err != nil {
return nil, err
}
switch d := d.(type) {
case *Base:
document = append(document, d)
case *Prefix:
document = append(document, d)
default:
return nil, fmt.Errorf("document: unknown directive: %T", d)
}
case "Triples":
t, err := ParseTriples(n)
if err != nil {
return nil, err
}
document = append(document, t)
default:
return nil, fmt.Errorf("document: unknown: %s", n.Name)
}
}
sort.Sort(document)
return document, nil
}
func (d Document) Equal(other Document) bool {
if len(d) != len(other) {
return false
}
d, o := d.normalizeBlankNodes(), other.normalizeBlankNodes()
for i, s0 := range d {
s1 := o[i]
if !s0.Equal(s1) {
return false
}
}
return true
}
func (d Document) Len() int {
return len(d)
}
func (d Document) Less(i, j int) bool {
switch d0 := d[i].(type) {
case Triple, *Triple:
switch t1 := d[j].(type) {
case Triple, *Triple:
return d0.String() < t1.String()
default:
return false
}
case Prefix:
switch p1 := d[j].(type) {
case Prefix, *Prefix:
return d0.Less(p1)
default:
return false
}
case *Prefix:
switch p1 := d[j].(type) {
case Prefix, *Prefix:
return d0.Less(p1)
default:
return false
}
default:
return false
}
}
func (d Document) String() string {
var s string
for _, l := range d {
s += l.String()
s += "\n"
}
return s
}
func (d Document) SubjectMap() (map[string]*Triple, error) {
m := make(map[string]*Triple)
for _, t := range d {
switch t := t.(type) {
case *Triple:
var name string
switch t := t.Subject.(type) {
case *IRI:
name = t.Value
default:
return nil, fmt.Errorf("document: subject not an iri: %T", t)
}
if _, ok := m[name]; ok {
return nil, fmt.Errorf("document: duplicate subject: %s", t.Subject)
}
m[name] = t
}
}
return m, nil
}
func (d Document) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d Document) normalizeBlankNodes() (n Document) {
var i int
mapping := make(map[string]string)
f := func(b fmt.Stringer) *BlankNode {
if bn, ok := mapping[b.String()]; ok {
return (*BlankNode)(&bn)
}
bn := fmt.Sprintf("%d", i)
mapping[b.String()] = bn
i++
return (*BlankNode)(&bn)
}
for _, s := range d {
switch t := s.(type) {
case *Triple:
if t.Subject != nil {
subject := t.Subject
switch s := t.Subject.(type) {
case BlankNode, *BlankNode:
subject = f(s)
case Collection:
subject = s.normalizeBlankNodes(f)
case *Collection:
if s != nil {
subject = s.normalizeBlankNodes(f)
}
}
n = append(n, &Triple{
Subject: subject,
PredicateObjectList: t.PredicateObjectList.normalizeBlankNodes(f),
})
} else {
var predicateObjectList PredicateObjectList
if t.PredicateObjectList != nil {
predicateObjectList = t.PredicateObjectList.normalizeBlankNodes(f)
}
n = append(n, &Triple{
BlankNodePropertyList: BlankNodePropertyList(
PredicateObjectList(t.BlankNodePropertyList).normalizeBlankNodes(f),
),
PredicateObjectList: predicateObjectList,
})
}
default:
n = append(n, s)
}
}
return
}
// IRI can be written as a relative/absolute IRI or prefixed name.
type IRI struct {
Prefixed bool
Value string
}
func ParseIRI(n *parser.Node) (*IRI, error) {
if n.Name != "IRI" {
return nil, fmt.Errorf("iri: unknown %s", n.Name)
}
n = n.Children()[0]
var prefixed bool
switch n.Name {
case "PrefixedName":
prefixed = true
case "IRIReference":
default:
return nil, fmt.Errorf("iri: unknown: %s", n.Name)
}
return &IRI{
Prefixed: prefixed,
Value: n.Value(),
}, nil
}
func (i IRI) Equal(v any) bool {
if v, ok := v.(IRI); ok {
return i.equal(v)
}
if v, ok := v.(*IRI); ok && v != nil {
return i.equal(*v)
}
return false
}
func (i IRI) String() string {
if i.Prefixed {
return i.Value
}
return fmt.Sprintf("<%s>", i.Value)
}
func (i IRI) equal(other IRI) bool {
return i.Prefixed == other.Prefixed && i.Value == other.Value
}
func (i IRI) object() {}
func (i IRI) subject() {}
func (i IRI) verb() {}
// Literal is either StringLiteral, NumericLiteral or BooleanLiteral.
type Literal interface {
literal()
Object
Equal(v any) bool
fmt.Stringer
}
func ParseBooleanLiteral(n *parser.Node) (Literal, error) {
if n.Name != "BooleanLiteral" {
return nil, fmt.Errorf("boolean literal: unknown %s", n.Name)
}
switch n.Value() {
case "true":
b := BooleanLiteral(true)
return &b, nil
case "false":
b := BooleanLiteral(false)
return &b, nil
default:
return nil, fmt.Errorf("boolean literal: unknown: %s", n.Name)
}
}
func ParseDecimal(n *parser.Node) (Literal, error) {
if n.Name != "Decimal" {
return nil, fmt.Errorf("decimal: unknown %s", n.Name)
}
return &NumericLiteral{
Type: Decimal,
Value: n.Value(),
}, nil
}
func ParseDouble(n *parser.Node) (Literal, error) {
if n.Name != "Double" {
return nil, fmt.Errorf("double: unknown %s", n.Name)
}
return &NumericLiteral{
Type: Double,
Value: n.Value(),
}, nil
}
func ParseInteger(n *parser.Node) (Literal, error) {
if n.Name != "Integer" {
return nil, fmt.Errorf("integer: unknown %s", n.Name)
}
return &NumericLiteral{
Type: Integer,
Value: n.Value(),
}, nil
}
func ParseLiteral(n *parser.Node) (Literal, error) {
if n.Name != "Literal" {
return nil, fmt.Errorf("literal: unknown %s", n.Name)
}
switch n = n.Children()[0]; n.Name {
case "RDFLiteral":
return ParseRDFLiteral(n)
case "NumericLiteral":
return ParseNumericLiteral(n)
case "BooleanLiteral":
return ParseBooleanLiteral(n)
default:
return nil, fmt.Errorf("literal: unknown: %s", n.Name)
}
}
func ParseNumericLiteral(n *parser.Node) (Literal, error) {
if n.Name != "NumericLiteral" {
return nil, fmt.Errorf("numeric literal: unknown %s", n.Name)
}
switch n = n.Children()[0]; n.Name {
case "Decimal":
return ParseDecimal(n)
case "Double":
return ParseDouble(n)
case "Integer":
return ParseInteger(n)
default:
return nil, fmt.Errorf("numeric literal: unknown: %s", n.Name)
}
}
func ParseRDFLiteral(n *parser.Node) (Literal, error) {
if n.Name != "RDFLiteral" {
return nil, fmt.Errorf("rdf literal: unknown %s", n.Name)
}
v, err := ParseStringLiteral(n.Children()[0])
if err != nil {
return nil, err
}
if len(n.Children()) > 1 {
switch n := n.Children()[1]; n.Name {
case "LanguageTag":
v.LanguageTag = n.Value()
case "IRI":
iri, err := ParseIRI(n)
if err != nil {
return nil, err
}
v.DatatypeIRI = iri
default:
return nil, fmt.Errorf("rdf literal: unknown: %s", n.Name)
}
}
return v, nil
}
type NumericLiteral struct {
Type NumericType
Value string
}
func (nl NumericLiteral) Equal(v any) bool {
if v, ok := v.(NumericLiteral); ok {
return nl.equal(v)
}
if v, ok := v.(*NumericLiteral); ok && v != nil {
return nl.equal(*v)
}
return false
}
func (nl NumericLiteral) String() string {
return nl.Value
}
func (nl NumericLiteral) equal(other NumericLiteral) bool {
return nl.Type == other.Type && nl.Value == other.Value
}
func (nl NumericLiteral) literal() {}
func (nl NumericLiteral) object() {}
type NumericType int
const (
Decimal NumericType = iota
Double
Integer
)
type Object interface {
object()
Equal(v any) bool
fmt.Stringer
}
func ParseObject(n *parser.Node) (Object, error) {
if n.Name != "Object" {
return nil, fmt.Errorf("object: unknown %s", n.Name)
}
switch n = n.Children()[0]; n.Name {
case "IRI":
return ParseIRI(n)
case "BlankNode":
return ParseBlankNode(n)
case "Collection":
return ParseCollection(n)
case "BlankNodePropertyList":
return ParseBlankNodePropertyList(n)
case "Literal":
return ParseLiteral(n)
default:
return nil, fmt.Errorf("object: unknown: %s", n.Name)
}
}
// ObjectList matches a series of objects separated by ',' following a predicate. This expresses a series of RDF Triples
// with the corresponding subject and predicate and each object allocated to one triple.
type ObjectList Collection
func ParseObjectList(n *parser.Node) (ObjectList, error) {
if n.Name != "ObjectList" {
return nil, fmt.Errorf("object list: unknown %s", n.Name)
}
n.Name = "Collection"
c, err := ParseCollection(n)
if err != nil {
return nil, err
}
ol := ObjectList(c)
sort.Sort(ol)
return ol, nil
}
func (ol ObjectList) Equal(v any) bool {
if v, ok := v.(ObjectList); ok {
return ol.equal(v)
}
if v, ok := v.(*ObjectList); ok && v != nil {
return ol.equal(*v)
}
return false
}
func (ol ObjectList) Len() int {
return len(ol)
}
func (ol ObjectList) Less(i, j int) bool {
return ol[i].String() < ol[j].String()
}
func (ol ObjectList) String() string {
var s string
for i, o := range ol {
if i > 0 {
s += ", "
}
s += o.String()
}
return s
}
func (ol ObjectList) Swap(i, j int) {
ol[i], ol[j] = ol[j], ol[i]
}
func (ol ObjectList) equal(other ObjectList) bool {
return Collection(ol).Equal(Collection(other))
}
type PredicateObject struct {
Verb Verb
ObjectList ObjectList
}
func ParsePredicateObject(n *parser.Node) (*PredicateObject, error) {
if n.Name != "PredicateObject" {
return nil, fmt.Errorf("predicate object: unknown %s", n.Name)
}
v, err := ParseVerb(n.Children()[0])
if err != nil {
return nil, err
}
ol, err := ParseObjectList(n.Children()[1])
if err != nil {
return nil, err
}
return &PredicateObject{
Verb: v,
ObjectList: ol,
}, nil
}
func (po PredicateObject) Equal(v any) bool {
if v, ok := v.(PredicateObject); ok {
return po.equal(v)
}
if v, ok := v.(*PredicateObject); ok && v != nil {
return po.equal(*v)
}
return false
}
func (po PredicateObject) String() string {
var s string
s += po.Verb.String()
if po.ObjectList != nil {
s += fmt.Sprintf(" %s", po.ObjectList)
}
return s
}
func (po PredicateObject) equal(other PredicateObject) bool {
return po.Verb.Equal(other.Verb) && po.ObjectList.Equal(other.ObjectList)
}
// PredicateObjectList matches a series of predicates and objects, separated by ';', following a subject. This expresses
// a series of RDF Triples with that subject and each predicate and object allocated to one triple.
type PredicateObjectList []PredicateObject
func ParsePredicateObjectList(n *parser.Node) (PredicateObjectList, error) {
if n.Name != "PredicateObjectList" {
return nil, fmt.Errorf("predicate object list: unknown %s", n.Name)
}
var pol PredicateObjectList
for _, n := range n.Children() {
if n.Name == "PredicateObject" {
po, err := ParsePredicateObject(n)
if err != nil {
return nil, err
}
pol = append(pol, *po)
continue
} else {
for _, n := range n.Children() {
po, err := ParsePredicateObject(n)
if err != nil {
return nil, err
}
pol = append(pol, *po)
}
}
}
sort.Sort(pol)
return pol, nil
}
func (pol PredicateObjectList) Equal(v any) bool {
if v, ok := v.(PredicateObjectList); ok {
return pol.equal(v)
}
if v, ok := v.(*PredicateObjectList); ok && v != nil {
return pol.equal(*v)
}
return false
}
func (pol PredicateObjectList) Len() int {
return len(pol)
}
func (pol PredicateObjectList) Less(i, j int) bool {
return pol[i].String() < pol[j].String()
}
func (pol PredicateObjectList) String() string {
var s string
for i, po := range pol {
if i > 0 {
s += " ; "
}
s += po.String()
}
return s
}
func (pol PredicateObjectList) Swap(i, j int) {
pol[i], pol[j] = pol[j], pol[i]
}
func (pol PredicateObjectList) equal(other PredicateObjectList) bool {
if len(pol) != len(other) {
return false
}
for i, po := range pol {
if !po.Equal(other[i]) {
return false
}
}
return true
}
func (pol PredicateObjectList) normalizeBlankNodes(f func(b fmt.Stringer) *BlankNode) (n PredicateObjectList) {
for _, v := range pol {
n = append(n, PredicateObject{
Verb: v.Verb,
ObjectList: ObjectList(Collection(v.ObjectList).normalizeBlankNodes(f)),
})
}
return
}
type Prefix struct {
Name string
IRI string
}
func ParsePrefix(n *parser.Node) (*Prefix, error) {
if n.Name != "Prefix" {
return nil, fmt.Errorf("prefix: unknown %s", n.Name)
}
return &Prefix{
Name: n.Children()[0].Value(),
IRI: n.Children()[1].Value(),
}, nil
}
func (p Prefix) Equal(v any) bool {
if v, ok := v.(Prefix); ok {
return p.equal(v)
}
if v, ok := v.(*Prefix); ok && v != nil {
return p.equal(*v)
}
return false
}
func (p Prefix) Less(other any) bool {
if other, ok := other.(Prefix); ok {
return p.less(other)
}
if other, ok := other.(*Prefix); ok && other != nil {
return p.less(*other)
}
return false
}
func (p Prefix) String() string {
return fmt.Sprintf("@prefix %s <%s> .", p.Name, p.IRI)
}
func (p Prefix) directive() {}
func (p Prefix) equal(other Prefix) bool {
return p.Name == other.Name && p.IRI == other.IRI
}
func (p Prefix) less(other Prefix) bool {
if p.Name == other.Name {
return false
}
return p.String() < other.String()
}
func (p Prefix) statement() {}
type Statement interface {
statement()
Equal(v any) bool
fmt.Stringer
}
type StringLiteral struct {
Value string
Multiline bool
SingleQuote bool
LanguageTag string
DatatypeIRI *IRI
}
func ParseStringLiteral(n *parser.Node) (*StringLiteral, error) {
switch n.Name {
case "StringLiteral":
return &StringLiteral{
Value: n.Value(),
}, nil
case "StringLiteralSQ":
return &StringLiteral{
Value: n.Value(),
SingleQuote: true,
}, nil
case "StringLiteralLQ":
return &StringLiteral{
Value: n.Value(),
Multiline: true,
}, nil
case "StringLiteralLSQ":
return &StringLiteral{
Value: n.Value(),
SingleQuote: true,
Multiline: true,
}, nil
default:
return nil, fmt.Errorf("string literal: unknown %s", n.Name)
}
}
func (sl StringLiteral) Equal(v any) bool {
if v, ok := v.(StringLiteral); ok {
return sl.equal(v)
}
if v, ok := v.(*StringLiteral); ok && v != nil {
return sl.equal(*v)
}
return false
}
func (sl StringLiteral) String() string {
var s string
if sl.Multiline {
if sl.SingleQuote {
s = fmt.Sprintf(`'''%s'''`, sl.Value)
} else {
s = fmt.Sprintf(`"""%s"""`, sl.Value)
}
} else {