-
Notifications
You must be signed in to change notification settings - Fork 133
/
N3Parser.js
1107 lines (1028 loc) · 39.5 KB
/
N3Parser.js
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
// **N3Parser** parses N3 documents.
import N3Lexer from './N3Lexer';
import N3DataFactory from './N3DataFactory';
import namespaces from './IRIs';
let blankNodePrefix = 0;
// ## Constructor
export default class N3Parser {
constructor(options) {
this._contextStack = [];
this._graph = null;
// Set the document IRI
options = options || {};
this._setBase(options.baseIRI);
options.factory && initDataFactory(this, options.factory);
// Set supported features depending on the format
const format = (typeof options.format === 'string') ?
options.format.match(/\w*$/)[0].toLowerCase() : '',
isTurtle = /turtle/.test(format), isTriG = /trig/.test(format),
isNTriples = /triple/.test(format), isNQuads = /quad/.test(format),
isN3 = this._n3Mode = /n3/.test(format),
isLineMode = isNTriples || isNQuads;
if (!(this._supportsNamedGraphs = !(isTurtle || isN3)))
this._readPredicateOrNamedGraph = this._readPredicate;
// Support triples in other graphs
this._supportsQuads = !(isTurtle || isTriG || isNTriples || isN3);
// Support nesting of triples
this._supportsRDFStar = format === '' || /star|\*$/.test(format);
// Disable relative IRIs in N-Triples or N-Quads mode
if (isLineMode)
this._resolveRelativeIRI = iri => { return null; };
this._blankNodePrefix = typeof options.blankNodePrefix !== 'string' ? '' :
options.blankNodePrefix.replace(/^(?!_:)/, '_:');
this._lexer = options.lexer || new N3Lexer({ lineMode: isLineMode, n3: isN3 });
// Disable explicit quantifiers by default
this._explicitQuantifiers = !!options.explicitQuantifiers;
}
// ## Static class methods
// ### `_resetBlankNodePrefix` restarts blank node prefix identification
static _resetBlankNodePrefix() {
blankNodePrefix = 0;
}
// ## Private methods
// ### `_setBase` sets the base IRI to resolve relative IRIs
_setBase(baseIRI) {
if (!baseIRI) {
this._base = '';
this._basePath = '';
}
else {
// Remove fragment if present
const fragmentPos = baseIRI.indexOf('#');
if (fragmentPos >= 0)
baseIRI = baseIRI.substr(0, fragmentPos);
// Set base IRI and its components
this._base = baseIRI;
this._basePath = baseIRI.indexOf('/') < 0 ? baseIRI :
baseIRI.replace(/[^\/?]*(?:\?.*)?$/, '');
baseIRI = baseIRI.match(/^(?:([a-z][a-z0-9+.-]*:))?(?:\/\/[^\/]*)?/i);
this._baseRoot = baseIRI[0];
this._baseScheme = baseIRI[1];
}
}
// ### `_saveContext` stores the current parsing context
// when entering a new scope (list, blank node, formula)
_saveContext(type, graph, subject, predicate, object) {
const n3Mode = this._n3Mode;
this._contextStack.push({
type,
subject, predicate, object, graph,
inverse: n3Mode ? this._inversePredicate : false,
blankPrefix: n3Mode ? this._prefixes._ : '',
quantified: n3Mode ? this._quantified : null,
});
// The settings below only apply to N3 streams
if (n3Mode) {
// Every new scope resets the predicate direction
this._inversePredicate = false;
// In N3, blank nodes are scoped to a formula
// (using a dot as separator, as a blank node label cannot start with it)
this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
// Quantifiers are scoped to a formula
this._quantified = Object.create(this._quantified);
}
}
// ### `_restoreContext` restores the parent context
// when leaving a scope (list, blank node, formula)
_restoreContext(type, token) {
// Obtain the previous context
const context = this._contextStack.pop();
if (!context || context.type !== type)
return this._error(`Unexpected ${token.type}`, token);
// Restore the quad of the previous context
this._subject = context.subject;
this._predicate = context.predicate;
this._object = context.object;
this._graph = context.graph;
// Restore N3 context settings
if (this._n3Mode) {
this._inversePredicate = context.inverse;
this._prefixes._ = context.blankPrefix;
this._quantified = context.quantified;
}
}
// ### `_readInTopContext` reads a token when in the top context
_readInTopContext(token) {
switch (token.type) {
// If an EOF token arrives in the top context, signal that we're done
case 'eof':
if (this._graph !== null)
return this._error('Unclosed graph', token);
delete this._prefixes._;
return this._callback(null, null, this._prefixes);
// It could be a prefix declaration
case 'PREFIX':
this._sparqlStyle = true;
case '@prefix':
return this._readPrefix;
// It could be a base declaration
case 'BASE':
this._sparqlStyle = true;
case '@base':
return this._readBaseIRI;
// It could be a graph
case '{':
if (this._supportsNamedGraphs) {
this._graph = '';
this._subject = null;
return this._readSubject;
}
case 'GRAPH':
if (this._supportsNamedGraphs)
return this._readNamedGraphLabel;
// Otherwise, the next token must be a subject
default:
return this._readSubject(token);
}
}
// ### `_readEntity` reads an IRI, prefixed name, blank node, or variable
_readEntity(token, quantifier) {
let value;
switch (token.type) {
// Read a relative or absolute IRI
case 'IRI':
case 'typeIRI':
const iri = this._resolveIRI(token.value);
if (iri === null)
return this._error('Invalid IRI', token);
value = this._namedNode(iri);
break;
// Read a prefixed name
case 'type':
case 'prefixed':
const prefix = this._prefixes[token.prefix];
if (prefix === undefined)
return this._error(`Undefined prefix "${token.prefix}:"`, token);
value = this._namedNode(prefix + token.value);
break;
// Read a blank node
case 'blank':
value = this._blankNode(this._prefixes[token.prefix] + token.value);
break;
// Read a variable
case 'var':
value = this._variable(token.value.substr(1));
break;
// Everything else is not an entity
default:
return this._error(`Expected entity but got ${token.type}`, token);
}
// In N3 mode, replace the entity if it is quantified
if (!quantifier && this._n3Mode && (value.id in this._quantified))
value = this._quantified[value.id];
return value;
}
// ### `_readSubject` reads a quad's subject
_readSubject(token) {
this._predicate = null;
switch (token.type) {
case '[':
// Start a new quad with a new blank node as subject
this._saveContext('blank', this._graph,
this._subject = this._blankNode(), null, null);
return this._readBlankNodeHead;
case '(':
// Start a new list
this._saveContext('list', this._graph, this.RDF_NIL, null, null);
this._subject = null;
return this._readListItem;
case '{':
// Start a new formula
if (!this._n3Mode)
return this._error('Unexpected graph', token);
this._saveContext('formula', this._graph,
this._graph = this._blankNode(), null, null);
return this._readSubject;
case '}':
// No subject; the graph in which we are reading is closed instead
return this._readPunctuation(token);
case '@forSome':
if (!this._n3Mode)
return this._error('Unexpected "@forSome"', token);
this._subject = null;
this._predicate = this.N3_FORSOME;
this._quantifier = this._blankNode;
return this._readQuantifierList;
case '@forAll':
if (!this._n3Mode)
return this._error('Unexpected "@forAll"', token);
this._subject = null;
this._predicate = this.N3_FORALL;
this._quantifier = this._variable;
return this._readQuantifierList;
case 'literal':
if (!this._n3Mode)
return this._error('Unexpected literal', token);
if (token.prefix.length === 0) {
this._literalValue = token.value;
return this._completeSubjectLiteral;
}
else
this._subject = this._literal(token.value, this._namedNode(token.prefix));
break;
case '<<':
if (!this._supportsRDFStar)
return this._error('Unexpected RDF-star syntax', token);
this._saveContext('<<', this._graph, null, null, null);
this._graph = null;
return this._readSubject;
default:
// Read the subject entity
if ((this._subject = this._readEntity(token)) === undefined)
return;
// In N3 mode, the subject might be a path
if (this._n3Mode)
return this._getPathReader(this._readPredicateOrNamedGraph);
}
// The next token must be a predicate,
// or, if the subject was actually a graph IRI, a named graph
return this._readPredicateOrNamedGraph;
}
// ### `_readPredicate` reads a quad's predicate
_readPredicate(token) {
const type = token.type;
switch (type) {
case 'inverse':
this._inversePredicate = true;
case 'abbreviation':
this._predicate = this.ABBREVIATIONS[token.value];
break;
case '.':
case ']':
case '}':
// Expected predicate didn't come, must have been trailing semicolon
if (this._predicate === null)
return this._error(`Unexpected ${type}`, token);
this._subject = null;
return type === ']' ? this._readBlankNodeTail(token) : this._readPunctuation(token);
case ';':
// Additional semicolons can be safely ignored
return this._predicate !== null ? this._readPredicate :
this._error('Expected predicate but got ;', token);
case '[':
if (this._n3Mode) {
// Start a new quad with a new blank node as subject
this._saveContext('blank', this._graph, this._subject,
this._subject = this._blankNode(), null);
return this._readBlankNodeHead;
}
case 'blank':
if (!this._n3Mode)
return this._error('Disallowed blank node as predicate', token);
default:
if ((this._predicate = this._readEntity(token)) === undefined)
return;
}
// The next token must be an object
return this._readObject;
}
// ### `_readObject` reads a quad's object
_readObject(token) {
switch (token.type) {
case 'literal':
// Regular literal, can still get a datatype or language
if (token.prefix.length === 0) {
this._literalValue = token.value;
return this._readDataTypeOrLang;
}
// Pre-datatyped string literal (prefix stores the datatype)
else
this._object = this._literal(token.value, this._namedNode(token.prefix));
break;
case '[':
// Start a new quad with a new blank node as subject
this._saveContext('blank', this._graph, this._subject, this._predicate,
this._subject = this._blankNode());
return this._readBlankNodeHead;
case '(':
// Start a new list
this._saveContext('list', this._graph, this._subject, this._predicate, this.RDF_NIL);
this._subject = null;
return this._readListItem;
case '{':
// Start a new formula
if (!this._n3Mode)
return this._error('Unexpected graph', token);
this._saveContext('formula', this._graph, this._subject, this._predicate,
this._graph = this._blankNode());
return this._readSubject;
case '<<':
if (!this._supportsRDFStar)
return this._error('Unexpected RDF-star syntax', token);
this._saveContext('<<', this._graph, this._subject, this._predicate, null);
this._graph = null;
return this._readSubject;
default:
// Read the object entity
if ((this._object = this._readEntity(token)) === undefined)
return;
// In N3 mode, the object might be a path
if (this._n3Mode)
return this._getPathReader(this._getContextEndReader());
}
return this._getContextEndReader();
}
// ### `_readPredicateOrNamedGraph` reads a quad's predicate, or a named graph
_readPredicateOrNamedGraph(token) {
return token.type === '{' ? this._readGraph(token) : this._readPredicate(token);
}
// ### `_readGraph` reads a graph
_readGraph(token) {
if (token.type !== '{')
return this._error(`Expected graph but got ${token.type}`, token);
// The "subject" we read is actually the GRAPH's label
this._graph = this._subject, this._subject = null;
return this._readSubject;
}
// ### `_readBlankNodeHead` reads the head of a blank node
_readBlankNodeHead(token) {
if (token.type === ']') {
this._subject = null;
return this._readBlankNodeTail(token);
}
else {
this._predicate = null;
return this._readPredicate(token);
}
}
// ### `_readBlankNodeTail` reads the end of a blank node
_readBlankNodeTail(token) {
if (token.type !== ']')
return this._readBlankNodePunctuation(token);
// Store blank node quad
if (this._subject !== null)
this._emit(this._subject, this._predicate, this._object, this._graph);
// Restore the parent context containing this blank node
const empty = this._predicate === null;
this._restoreContext('blank', token);
// If the blank node was the object, restore previous context and read punctuation
if (this._object !== null)
return this._getContextEndReader();
// If the blank node was the predicate, continue reading the object
else if (this._predicate !== null)
return this._readObject;
// If the blank node was the subject, continue reading the predicate
else
// If the blank node was empty, it could be a named graph label
return empty ? this._readPredicateOrNamedGraph : this._readPredicateAfterBlank;
}
// ### `_readPredicateAfterBlank` reads a predicate after an anonymous blank node
_readPredicateAfterBlank(token) {
switch (token.type) {
case '.':
case '}':
// No predicate is coming if the triple is terminated here
this._subject = null;
return this._readPunctuation(token);
default:
return this._readPredicate(token);
}
}
// ### `_readListItem` reads items from a list
_readListItem(token) {
let item = null, // The item of the list
list = null, // The list itself
next = this._readListItem; // The next function to execute
const previousList = this._subject, // The previous list that contains this list
stack = this._contextStack, // The stack of parent contexts
parent = stack[stack.length - 1]; // The parent containing the current list
switch (token.type) {
case '[':
// Stack the current list quad and start a new quad with a blank node as subject
this._saveContext('blank', this._graph,
list = this._blankNode(), this.RDF_FIRST,
this._subject = item = this._blankNode());
next = this._readBlankNodeHead;
break;
case '(':
// Stack the current list quad and start a new list
this._saveContext('list', this._graph,
list = this._blankNode(), this.RDF_FIRST, this.RDF_NIL);
this._subject = null;
break;
case ')':
// Closing the list; restore the parent context
this._restoreContext('list', token);
// If this list is contained within a parent list, return the membership quad here.
// This will be `<parent list element> rdf:first <this list>.`.
if (stack.length !== 0 && stack[stack.length - 1].type === 'list')
this._emit(this._subject, this._predicate, this._object, this._graph);
// Was this list the parent's subject?
if (this._predicate === null) {
// The next token is the predicate
next = this._readPredicate;
// No list tail if this was an empty list
if (this._subject === this.RDF_NIL)
return next;
}
// The list was in the parent context's object
else {
next = this._getContextEndReader();
// No list tail if this was an empty list
if (this._object === this.RDF_NIL)
return next;
}
// Close the list by making the head nil
list = this.RDF_NIL;
break;
case 'literal':
// Regular literal, can still get a datatype or language
if (token.prefix.length === 0) {
this._literalValue = token.value;
next = this._readListItemDataTypeOrLang;
}
// Pre-datatyped string literal (prefix stores the datatype)
else {
item = this._literal(token.value, this._namedNode(token.prefix));
next = this._getContextEndReader();
}
break;
case '{':
// Start a new formula
if (!this._n3Mode)
return this._error('Unexpected graph', token);
this._saveContext('formula', this._graph, this._subject, this._predicate,
this._graph = this._blankNode());
return this._readSubject;
default:
if ((item = this._readEntity(token)) === undefined)
return;
}
// Create a new blank node if no item head was assigned yet
if (list === null)
this._subject = list = this._blankNode();
// Is this the first element of the list?
if (previousList === null) {
// This list is either the subject or the object of its parent
if (parent.predicate === null)
parent.subject = list;
else
parent.object = list;
}
else {
// Continue the previous list with the current list
this._emit(previousList, this.RDF_REST, list, this._graph);
}
// If an item was read, add it to the list
if (item !== null) {
// In N3 mode, the item might be a path
if (this._n3Mode && (token.type === 'IRI' || token.type === 'prefixed')) {
// Create a new context to add the item's path
this._saveContext('item', this._graph, list, this.RDF_FIRST, item);
this._subject = item, this._predicate = null;
// _readPath will restore the context and output the item
return this._getPathReader(this._readListItem);
}
// Output the item
this._emit(list, this.RDF_FIRST, item, this._graph);
}
return next;
}
// ### `_readDataTypeOrLang` reads an _optional_ datatype or language
_readDataTypeOrLang(token) {
return this._completeObjectLiteral(token, false);
}
// ### `_readListItemDataTypeOrLang` reads an _optional_ datatype or language in a list
_readListItemDataTypeOrLang(token) {
return this._completeObjectLiteral(token, true);
}
// ### `_completeLiteral` completes a literal with an optional datatype or language
_completeLiteral(token) {
// Create a simple string literal by default
let literal = this._literal(this._literalValue);
switch (token.type) {
// Create a datatyped literal
case 'type':
case 'typeIRI':
const datatype = this._readEntity(token);
if (datatype === undefined) return; // No datatype means an error occurred
literal = this._literal(this._literalValue, datatype);
token = null;
break;
// Create a language-tagged string
case 'langcode':
literal = this._literal(this._literalValue, token.value);
token = null;
break;
}
return { token, literal };
}
// Completes a literal in subject position
_completeSubjectLiteral(token) {
this._subject = this._completeLiteral(token).literal;
return this._readPredicateOrNamedGraph;
}
// Completes a literal in object position
_completeObjectLiteral(token, listItem) {
const completed = this._completeLiteral(token);
if (!completed)
return;
this._object = completed.literal;
// If this literal was part of a list, write the item
// (we could also check the context stack, but passing in a flag is faster)
if (listItem)
this._emit(this._subject, this.RDF_FIRST, this._object, this._graph);
// If the token was consumed, continue with the rest of the input
if (completed.token === null)
return this._getContextEndReader();
// Otherwise, consume the token now
else {
this._readCallback = this._getContextEndReader();
return this._readCallback(completed.token);
}
}
// ### `_readFormulaTail` reads the end of a formula
_readFormulaTail(token) {
if (token.type !== '}')
return this._readPunctuation(token);
// Store the last quad of the formula
if (this._subject !== null)
this._emit(this._subject, this._predicate, this._object, this._graph);
// Restore the parent context containing this formula
this._restoreContext('formula', token);
// If the formula was the subject, continue reading the predicate.
// If the formula was the object, read punctuation.
return this._object === null ? this._readPredicate : this._getContextEndReader();
}
// ### `_readPunctuation` reads punctuation between quads or quad parts
_readPunctuation(token) {
let next, graph = this._graph;
const subject = this._subject, inversePredicate = this._inversePredicate;
switch (token.type) {
// A closing brace ends a graph
case '}':
if (this._graph === null)
return this._error('Unexpected graph closing', token);
if (this._n3Mode)
return this._readFormulaTail(token);
this._graph = null;
// A dot just ends the statement, without sharing anything with the next
case '.':
this._subject = null;
next = this._contextStack.length ? this._readSubject : this._readInTopContext;
if (inversePredicate) this._inversePredicate = false;
break;
// Semicolon means the subject is shared; predicate and object are different
case ';':
next = this._readPredicate;
break;
// Comma means both the subject and predicate are shared; the object is different
case ',':
next = this._readObject;
break;
// {| means that the current triple is annotated with predicate-object pairs.
case '{|':
if (!this._supportsRDFStar)
return this._error('Unexpected RDF-star syntax', token);
// Continue using the last triple as quoted triple subject for the predicate-object pairs.
const predicate = this._predicate, object = this._object;
this._subject = this._quad(subject, predicate, object, this.DEFAULTGRAPH);
next = this._readPredicate;
break;
// |} means that the current quoted triple in annotation syntax is finalized.
case '|}':
if (this._subject.termType !== 'Quad')
return this._error('Unexpected asserted triple closing', token);
this._subject = null;
next = this._readPunctuation;
break;
default:
// An entity means this is a quad (only allowed if not already inside a graph)
if (this._supportsQuads && this._graph === null && (graph = this._readEntity(token)) !== undefined) {
next = this._readQuadPunctuation;
break;
}
return this._error(`Expected punctuation to follow "${this._object.id}"`, token);
}
// A quad has been completed now, so return it
if (subject !== null) {
const predicate = this._predicate, object = this._object;
if (!inversePredicate)
this._emit(subject, predicate, object, graph);
else
this._emit(object, predicate, subject, graph);
}
return next;
}
// ### `_readBlankNodePunctuation` reads punctuation in a blank node
_readBlankNodePunctuation(token) {
let next;
switch (token.type) {
// Semicolon means the subject is shared; predicate and object are different
case ';':
next = this._readPredicate;
break;
// Comma means both the subject and predicate are shared; the object is different
case ',':
next = this._readObject;
break;
default:
return this._error(`Expected punctuation to follow "${this._object.id}"`, token);
}
// A quad has been completed now, so return it
this._emit(this._subject, this._predicate, this._object, this._graph);
return next;
}
// ### `_readQuadPunctuation` reads punctuation after a quad
_readQuadPunctuation(token) {
if (token.type !== '.')
return this._error('Expected dot to follow quad', token);
return this._readInTopContext;
}
// ### `_readPrefix` reads the prefix of a prefix declaration
_readPrefix(token) {
if (token.type !== 'prefix')
return this._error('Expected prefix to follow @prefix', token);
this._prefix = token.value;
return this._readPrefixIRI;
}
// ### `_readPrefixIRI` reads the IRI of a prefix declaration
_readPrefixIRI(token) {
if (token.type !== 'IRI')
return this._error(`Expected IRI to follow prefix "${this._prefix}:"`, token);
const prefixNode = this._readEntity(token);
this._prefixes[this._prefix] = prefixNode.value;
this._prefixCallback(this._prefix, prefixNode);
return this._readDeclarationPunctuation;
}
// ### `_readBaseIRI` reads the IRI of a base declaration
_readBaseIRI(token) {
const iri = token.type === 'IRI' && this._resolveIRI(token.value);
if (!iri)
return this._error('Expected valid IRI to follow base declaration', token);
this._setBase(iri);
return this._readDeclarationPunctuation;
}
// ### `_readNamedGraphLabel` reads the label of a named graph
_readNamedGraphLabel(token) {
switch (token.type) {
case 'IRI':
case 'blank':
case 'prefixed':
return this._readSubject(token), this._readGraph;
case '[':
return this._readNamedGraphBlankLabel;
default:
return this._error('Invalid graph label', token);
}
}
// ### `_readNamedGraphLabel` reads a blank node label of a named graph
_readNamedGraphBlankLabel(token) {
if (token.type !== ']')
return this._error('Invalid graph label', token);
this._subject = this._blankNode();
return this._readGraph;
}
// ### `_readDeclarationPunctuation` reads the punctuation of a declaration
_readDeclarationPunctuation(token) {
// SPARQL-style declarations don't have punctuation
if (this._sparqlStyle) {
this._sparqlStyle = false;
return this._readInTopContext(token);
}
if (token.type !== '.')
return this._error('Expected declaration to end with a dot', token);
return this._readInTopContext;
}
// Reads a list of quantified symbols from a @forSome or @forAll statement
_readQuantifierList(token) {
let entity;
switch (token.type) {
case 'IRI':
case 'prefixed':
if ((entity = this._readEntity(token, true)) !== undefined)
break;
default:
return this._error(`Unexpected ${token.type}`, token);
}
// Without explicit quantifiers, map entities to a quantified entity
if (!this._explicitQuantifiers)
this._quantified[entity.id] = this._quantifier(this._blankNode().value);
// With explicit quantifiers, output the reified quantifier
else {
// If this is the first item, start a new quantifier list
if (this._subject === null)
this._emit(this._graph || this.DEFAULTGRAPH, this._predicate,
this._subject = this._blankNode(), this.QUANTIFIERS_GRAPH);
// Otherwise, continue the previous list
else
this._emit(this._subject, this.RDF_REST,
this._subject = this._blankNode(), this.QUANTIFIERS_GRAPH);
// Output the list item
this._emit(this._subject, this.RDF_FIRST, entity, this.QUANTIFIERS_GRAPH);
}
return this._readQuantifierPunctuation;
}
// Reads punctuation from a @forSome or @forAll statement
_readQuantifierPunctuation(token) {
// Read more quantifiers
if (token.type === ',')
return this._readQuantifierList;
// End of the quantifier list
else {
// With explicit quantifiers, close the quantifier list
if (this._explicitQuantifiers) {
this._emit(this._subject, this.RDF_REST, this.RDF_NIL, this.QUANTIFIERS_GRAPH);
this._subject = null;
}
// Read a dot
this._readCallback = this._getContextEndReader();
return this._readCallback(token);
}
}
// ### `_getPathReader` reads a potential path and then resumes with the given function
_getPathReader(afterPath) {
this._afterPath = afterPath;
return this._readPath;
}
// ### `_readPath` reads a potential path
_readPath(token) {
switch (token.type) {
// Forward path
case '!': return this._readForwardPath;
// Backward path
case '^': return this._readBackwardPath;
// Not a path; resume reading where we left off
default:
const stack = this._contextStack, parent = stack.length && stack[stack.length - 1];
// If we were reading a list item, we still need to output it
if (parent && parent.type === 'item') {
// The list item is the remaining subejct after reading the path
const item = this._subject;
// Switch back to the context of the list
this._restoreContext('item', token);
// Output the list item
this._emit(this._subject, this.RDF_FIRST, item, this._graph);
}
return this._afterPath(token);
}
}
// ### `_readForwardPath` reads a '!' path
_readForwardPath(token) {
let subject, predicate;
const object = this._blankNode();
// The next token is the predicate
if ((predicate = this._readEntity(token)) === undefined)
return;
// If we were reading a subject, replace the subject by the path's object
if (this._predicate === null)
subject = this._subject, this._subject = object;
// If we were reading an object, replace the subject by the path's object
else
subject = this._object, this._object = object;
// Emit the path's current quad and read its next section
this._emit(subject, predicate, object, this._graph);
return this._readPath;
}
// ### `_readBackwardPath` reads a '^' path
_readBackwardPath(token) {
const subject = this._blankNode();
let predicate, object;
// The next token is the predicate
if ((predicate = this._readEntity(token)) === undefined)
return;
// If we were reading a subject, replace the subject by the path's subject
if (this._predicate === null)
object = this._subject, this._subject = subject;
// If we were reading an object, replace the subject by the path's subject
else
object = this._object, this._object = subject;
// Emit the path's current quad and read its next section
this._emit(subject, predicate, object, this._graph);
return this._readPath;
}
// ### `_readRDFStarTailOrGraph` reads the graph of a nested RDF-star quad or the end of a nested RDF-star triple
_readRDFStarTailOrGraph(token) {
if (token.type !== '>>') {
// An entity means this is a quad (only allowed if not already inside a graph)
if (this._supportsQuads && this._graph === null && (this._graph = this._readEntity(token)) !== undefined)
return this._readRDFStarTail;
return this._error(`Expected >> to follow "${this._object.id}"`, token);
}
return this._readRDFStarTail(token);
}
// ### `_readRDFStarTail` reads the end of a nested RDF-star triple
_readRDFStarTail(token) {
if (token.type !== '>>')
return this._error(`Expected >> but got ${token.type}`, token);
// Read the quad and restore the previous context
const quad = this._quad(this._subject, this._predicate, this._object,
this._graph || this.DEFAULTGRAPH);
this._restoreContext('<<', token);
// If the triple was the subject, continue by reading the predicate.
if (this._subject === null) {
this._subject = quad;
return this._readPredicate;
}
// If the triple was the object, read context end.
else {
this._object = quad;
return this._getContextEndReader();
}
}
// ### `_getContextEndReader` gets the next reader function at the end of a context
_getContextEndReader() {
const contextStack = this._contextStack;
if (!contextStack.length)
return this._readPunctuation;
switch (contextStack[contextStack.length - 1].type) {
case 'blank':
return this._readBlankNodeTail;
case 'list':
return this._readListItem;
case 'formula':
return this._readFormulaTail;
case '<<':
return this._readRDFStarTailOrGraph;
}
}
// ### `_emit` sends a quad through the callback
_emit(subject, predicate, object, graph) {
this._callback(null, this._quad(subject, predicate, object, graph || this.DEFAULTGRAPH));
}
// ### `_error` emits an error message through the callback
_error(message, token) {
const err = new Error(`${message} on line ${token.line}.`);
err.context = {
token: token,
line: token.line,
previousToken: this._lexer.previousToken,
};
this._callback(err);
this._callback = noop;
}
// ### `_resolveIRI` resolves an IRI against the base path
_resolveIRI(iri) {
return /^[a-z][a-z0-9+.-]*:/i.test(iri) ? iri : this._resolveRelativeIRI(iri);
}
// ### `_resolveRelativeIRI` resolves an IRI against the base path,
// assuming that a base path has been set and that the IRI is indeed relative
_resolveRelativeIRI(iri) {
// An empty relative IRI indicates the base IRI
if (!iri.length)
return this._base;
// Decide resolving strategy based in the first character
switch (iri[0]) {
// Resolve relative fragment IRIs against the base IRI
case '#': return this._base + iri;
// Resolve relative query string IRIs by replacing the query string
case '?': return this._base.replace(/(?:\?.*)?$/, iri);
// Resolve root-relative IRIs at the root of the base IRI
case '/':
// Resolve scheme-relative IRIs to the scheme
return (iri[1] === '/' ? this._baseScheme : this._baseRoot) + this._removeDotSegments(iri);
// Resolve all other IRIs at the base IRI's path
default:
// Relative IRIs cannot contain a colon in the first path segment
return (/^[^/:]*:/.test(iri)) ? null : this._removeDotSegments(this._basePath + iri);
}
}
// ### `_removeDotSegments` resolves './' and '../' path segments in an IRI as per RFC3986
_removeDotSegments(iri) {
// Don't modify the IRI if it does not contain any dot segments
if (!/(^|\/)\.\.?($|[/#?])/.test(iri))
return iri;
// Start with an imaginary slash before the IRI in order to resolve trailing './' and '../'
const length = iri.length;
let result = '', i = -1, pathStart = -1, segmentStart = 0, next = '/';
while (i < length) {
switch (next) {
// The path starts with the first slash after the authority
case ':':
if (pathStart < 0) {
// Skip two slashes before the authority
if (iri[++i] === '/' && iri[++i] === '/')
// Skip to slash after the authority
while ((pathStart = i + 1) < length && iri[pathStart] !== '/')
i = pathStart;
}
break;
// Don't modify a query string or fragment
case '?':
case '#':
i = length;
break;
// Handle '/.' or '/..' path segments
case '/':
if (iri[i + 1] === '.') {
next = iri[++i + 1];
switch (next) {
// Remove a '/.' segment
case '/':
result += iri.substring(segmentStart, i - 1);
segmentStart = i + 1;
break;
// Remove a trailing '/.' segment
case undefined:
case '?':
case '#':
return result + iri.substring(segmentStart, i) + iri.substr(i + 1);
// Remove a '/..' segment
case '.':
next = iri[++i + 1];
if (next === undefined || next === '/' || next === '?' || next === '#') {
result += iri.substring(segmentStart, i - 2);
// Try to remove the parent path from result
if ((segmentStart = result.lastIndexOf('/')) >= pathStart)
result = result.substr(0, segmentStart);
// Remove a trailing '/..' segment
if (next !== '/')
return `${result}/${iri.substr(i + 1)}`;