forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n3parser.js
1562 lines (1453 loc) · 48.7 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
/**
*
* UTF-8 data encode / decode
* http://www.webtoolkit.info/
*
**/
$rdf.N3Parser = function () {
function hexify(str) { // also used in parser
return encodeURI(str);
}
var Utf8 = {
// public method for url encoding
encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// public method for url decoding
decode : function (utftext) {
var string = "";
var i = 0;
while ( i < utftext.length ) {
var c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
string += String.fromCharCode(((c & 31) << 6)
| (utftext.charCodeAt(i+1) & 63));
i += 2;
}
else {
string += String.fromCharCode(((c & 15) << 12)
| ((utftext.charCodeAt(i+1) & 63) << 6)
| (utftext.charCodeAt(i+2) & 63));
i += 3;
}
}
return string;
}
}// Things we need to define to make converted pythn code work in js
// environment of $rdf
var RDFSink_forSomeSym = "http://www.w3.org/2000/10/swap/log#forSome";
var RDFSink_forAllSym = "http://www.w3.org/2000/10/swap/log#forAll";
var Logic_NS = "http://www.w3.org/2000/10/swap/log#";
// pyjs seems to reference runtime library which I didn't find
var pyjslib_Tuple = function(theList) { return theList };
var pyjslib_List = function(theList) { return theList };
var pyjslib_Dict = function(listOfPairs) {
if (listOfPairs.length > 0)
throw "missing.js: oops nnonempty dict not imp";
return [];
}
var pyjslib_len = function(s) { return s.length }
var pyjslib_slice = function(str, i, j) {
if (typeof str.slice == 'undefined')
throw '@@ mising.js: No .slice function for '+str+' of type '+(typeof str)
if ((typeof j == 'undefined') || (j ==null)) return str.slice(i);
return str.slice(i, j) // @ exactly the same spec?
}
var StopIteration = Error('dummy error stop iteration');
var pyjslib_Iterator = function(theList) {
this.last = 0;
this.li = theList;
this.next = function() {
if (this.last == this.li.length) throw StopIteration;
return this.li[this.last++];
}
return this;
};
var ord = function(str) {
return str.charCodeAt(0)
}
var string_find = function(str, s) {
return str.indexOf(s)
}
var assertFudge = function(condition, desc) {
if (condition) return;
if (desc) throw "python Assertion failed: "+desc;
throw "(python) Assertion failed.";
}
var stringFromCharCode = function(uesc) {
return String.fromCharCode(uesc);
}
String.prototype.encode = function(encoding) {
if (encoding != 'utf-8') throw "UTF8_converter: can only do utf-8"
return Utf8.encode(this);
}
String.prototype.decode = function(encoding) {
if (encoding != 'utf-8') throw "UTF8_converter: can only do utf-8"
//return Utf8.decode(this);
return this;
}
var uripath_join = function(base, given) {
return $rdf.Util.uri.join(given, base) // sad but true
}
var becauseSubexpression = null; // No reason needed
var diag_tracking = 0;
var diag_chatty_flag = 0;
var diag_progress = function(str) { /*$rdf.log.debug(str);*/ }
// why_BecauseOfData = function(doc, reason) { return doc };
var RDF_type_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
var DAML_sameAs_URI = "http://www.w3.org/2002/07/owl#sameAs";
/*
function SyntaxError(details) {
return new __SyntaxError(details);
}
*/
function __SyntaxError(details) {
this.details = details
}
/*
$Id: n3parser.js 14561 2008-02-23 06:37:26Z kennyluck $
HAND EDITED FOR CONVERSION TO JAVASCRIPT
This module implements a Nptation3 parser, and the final
part of a notation3 serializer.
See also:
Notation 3
http://www.w3.org/DesignIssues/Notation3
Closed World Machine - and RDF Processor
http://www.w3.org/2000/10/swap/cwm
To DO: See also "@@" in comments
- Clean up interfaces
______________________________________________
Module originally by Dan Connolly, includeing notation3
parser and RDF generator. TimBL added RDF stream model
and N3 generation, replaced stream model with use
of common store/formula API. Yosi Scharf developped
the module, including tests and test harness.
*/
var ADDED_HASH = "#";
var LOG_implies_URI = "http://www.w3.org/2000/10/swap/log#implies";
var INTEGER_DATATYPE = "http://www.w3.org/2001/XMLSchema#integer";
var FLOAT_DATATYPE = "http://www.w3.org/2001/XMLSchema#double";
var DECIMAL_DATATYPE = "http://www.w3.org/2001/XMLSchema#decimal";
var DATE_DATATYPE = "http://www.w3.org/2001/XMLSchema#date";
var DATETIME_DATATYPE = "http://www.w3.org/2001/XMLSchema#dateTime";
var BOOLEAN_DATATYPE = "http://www.w3.org/2001/XMLSchema#boolean";
var option_noregen = 0;
var _notQNameChars = "\t\r\n !\"#$%&'()*.,+/;<=>?@[\\]^`{|}~";
var _notNameChars = ( _notQNameChars + ":" ) ;
var _rdfns = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
var N3CommentCharacter = "#";
var eol = new RegExp("^[ \\t]*(#[^\\n]*)?\\r?\\n", 'g');
var eof = new RegExp("^[ \\t]*(#[^\\n]*)?$", 'g');
var ws = new RegExp("^[ \\t]*", 'g');
var signed_integer = new RegExp("^[-+]?[0-9]+", 'g');
var number_syntax = new RegExp("^([-+]?[0-9]+)(\\.[0-9]+)?(e[-+]?[0-9]+)?", 'g');
var datetime_syntax = new RegExp('^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9](T[0-9][0-9]:[0-9][0-9](:[0-9][0-9](\\.[0-9]*)?)?)?Z?');
var digitstring = new RegExp("^[0-9]+", 'g');
var interesting = new RegExp("[\\\\\\r\\n\\\"]", 'g');
var langcode = new RegExp("^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)?", 'g');
function SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why) {
return new __SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why);
}
function __SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why) {
if (typeof openFormula == 'undefined') openFormula=null;
if (typeof thisDoc == 'undefined') thisDoc="";
if (typeof baseURI == 'undefined') baseURI=null;
if (typeof genPrefix == 'undefined') genPrefix="";
if (typeof metaURI == 'undefined') metaURI=null;
if (typeof flags == 'undefined') flags="";
if (typeof why == 'undefined') why=null;
/*
note: namespace names should *not* end in #;
the # will get added during qname processing */
this._bindings = new pyjslib_Dict([]);
this._flags = flags;
if ((thisDoc != "")) {
assertFudge((thisDoc.indexOf(":") >= 0), ( "Document URI not absolute: " + thisDoc ) );
this._bindings[""] = ( ( thisDoc + "#" ) );
}
this._store = store;
if (genPrefix) {
store.setGenPrefix(genPrefix);
}
this._thisDoc = thisDoc;
this.source = store.sym(thisDoc);
this.lines = 0;
this.statementCount = 0;
this.startOfLine = 0;
this.previousLine = 0;
this._genPrefix = genPrefix;
this.keywords = new pyjslib_List(["a", "this", "bind", "has", "is", "of", "true", "false"]);
this.keywordsSet = 0;
this._anonymousNodes = new pyjslib_Dict([]);
this._variables = new pyjslib_Dict([]);
this._parentVariables = new pyjslib_Dict([]);
this._reason = why;
this._reason2 = null;
if (diag_tracking) {
this._reason2 = why_BecauseOfData(store.sym(thisDoc), this._reason);
}
if (baseURI) {
this._baseURI = baseURI;
}
else {
if (thisDoc) {
this._baseURI = thisDoc;
}
else {
this._baseURI = null;
}
}
assertFudge(!(this._baseURI) || (this._baseURI.indexOf(":") >= 0));
if (!(this._genPrefix)) {
if (this._thisDoc) {
this._genPrefix = ( this._thisDoc + "#_g" ) ;
}
else {
this._genPrefix = RDFSink_uniqueURI();
}
}
if ((openFormula == null)) {
if (this._thisDoc) {
this._formula = store.formula( ( thisDoc + "#_formula" ) );
}
else {
this._formula = store.formula();
}
}
else {
this._formula = openFormula;
}
this._context = this._formula;
this._parentContext = null;
}
__SinkParser.prototype.here = function(i) {
return ( ( ( ( this._genPrefix + "_L" ) + this.lines ) + "C" ) + ( ( i - this.startOfLine ) + 1 ) ) ;
};
__SinkParser.prototype.formula = function() {
return this._formula;
};
__SinkParser.prototype.loadStream = function(stream) {
return this.loadBuf(stream.read());
};
__SinkParser.prototype.loadBuf = function(buf) {
/*
Parses a buffer and returns its top level formula*/
this.startDoc();
this.feed(buf);
return this.endDoc();
};
__SinkParser.prototype.feed = function(octets) {
/*
Feed an octet stream tothe parser
if BadSyntax is raised, the string
passed in the exception object is the
remainder after any statements have been parsed.
So if there is more data to feed to the
parser, it should be straightforward to recover.*/
var str = octets.decode("utf-8");
var i = 0;
while ((i >= 0)) {
var j = this.skipSpace(str, i);
if ((j < 0)) {
return;
}
var i = this.directiveOrStatement(str, j);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "expected directive or statement");
}
}
};
__SinkParser.prototype.directiveOrStatement = function(str, h) {
var i = this.skipSpace(str, h);
if ((i < 0)) {
return i;
}
var j = this.directive(str, i);
if ((j >= 0)) {
return this.checkDot(str, j);
}
var j = this.statement(str, i);
if ((j >= 0)) {
return this.checkDot(str, j);
}
return j;
};
__SinkParser.prototype.tok = function(tok, str, i) {
/*
Check for keyword. Space must have been stripped on entry and
we must not be at end of file.*/
var whitespace = "\t\n\v\f\r ";
if ((pyjslib_slice(str, i, ( i + 1 ) ) == "@")) {
var i = ( i + 1 ) ;
}
else {
if (($rdf.Util.ArrayIndexOf(this.keywords,tok) < 0)) {
return -1;
}
}
var k = ( i + pyjslib_len(tok) ) ;
if ((pyjslib_slice(str, i, k) == tok) && (_notQNameChars.indexOf(str.charAt(k)) >= 0)) {
return k;
}
else {
return -1;
}
};
__SinkParser.prototype.directive = function(str, i) {
var j = this.skipSpace(str, i);
if ((j < 0)) {
return j;
}
var res = new pyjslib_List([]);
var j = this.tok("bind", str, i);
if ((j > 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "keyword bind is obsolete: use @prefix");
}
var j = this.tok("keywords", str, i);
if ((j > 0)) {
var i = this.commaSeparatedList(str, j, res, false);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "'@keywords' needs comma separated list of words");
}
this.setKeywords(pyjslib_slice(res, null, null));
if ((diag_chatty_flag > 80)) {
diag_progress("Keywords ", this.keywords);
}
return i;
}
var j = this.tok("forAll", str, i);
if ((j > 0)) {
var i = this.commaSeparatedList(str, j, res, true);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "Bad variable list after @forAll");
}
var __x = new pyjslib_Iterator(res);
try {
while (true) {
var x = __x.next();
if ($rdf.Util.ArrayIndexOf(this._variables,x) < 0 || ($rdf.Util.ArrayIndexOf(this._parentVariables,x) >= 0)) {
this._variables[x] = ( this._context.newUniversal(x));
}
}
} catch (e) {
if (e != StopIteration) {
throw e;
}
}
return i;
}
var j = this.tok("forSome", str, i);
if ((j > 0)) {
var i = this.commaSeparatedList(str, j, res, this.uri_ref2);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "Bad variable list after @forSome");
}
var __x = new pyjslib_Iterator(res);
try {
while (true) {
var x = __x.next();
this._context.declareExistential(x);
}
} catch (e) {
if (e != StopIteration) {
throw e;
}
}
return i;
}
var j = this.tok("prefix", str, i);
if ((j >= 0)) {
var t = new pyjslib_List([]);
var i = this.qname(str, j, t);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "expected qname after @prefix");
}
var j = this.uri_ref2(str, i, t);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected <uriref> after @prefix _qname_");
}
var ns = t[1].uri;
if (this._baseURI) {
var ns = uripath_join(this._baseURI, ns);
}
else {
assertFudge((ns.indexOf(":") >= 0), "With no base URI, cannot handle relative URI for NS");
}
assertFudge((ns.indexOf(":") >= 0));
this._bindings[t[0][0]] = ( ns);
this.bind(t[0][0], hexify(ns));
return j;
}
var j = this.tok("base", str, i);
if ((j >= 0)) {
var t = new pyjslib_List([]);
var i = this.uri_ref2(str, j, t);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "expected <uri> after @base ");
}
var ns = t[0].uri;
if (this._baseURI) {
var ns = uripath_join(this._baseURI, ns);
}
else {
throw BadSyntax(this._thisDoc, this.lines, str, j, ( ( "With no previous base URI, cannot use relative URI in @base <" + ns ) + ">" ) );
}
assertFudge((ns.indexOf(":") >= 0));
this._baseURI = ns;
return i;
}
return -1;
};
__SinkParser.prototype.bind = function(qn, uri) {
if ((qn == "")) {
}
else {
this._store.setPrefixForURI(qn, uri);
}
};
__SinkParser.prototype.setKeywords = function(k) {
/*
Takes a list of strings*/
if ((k == null)) {
this.keywordsSet = 0;
}
else {
this.keywords = k;
this.keywordsSet = 1;
}
};
__SinkParser.prototype.startDoc = function() {
};
__SinkParser.prototype.endDoc = function() {
/*
Signal end of document and stop parsing. returns formula*/
return this._formula;
};
__SinkParser.prototype.makeStatement = function(quad) {
quad[0].add(quad[2], quad[1], quad[3], this.source);
this.statementCount += 1;
};
__SinkParser.prototype.statement = function(str, i) {
var r = new pyjslib_List([]);
var i = this.object(str, i, r);
if ((i < 0)) {
return i;
}
var j = this.property_list(str, i, r[0]);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected propertylist");
}
return j;
};
__SinkParser.prototype.subject = function(str, i, res) {
return this.item(str, i, res);
};
__SinkParser.prototype.verb = function(str, i, res) {
/*
has _prop_
is _prop_ of
a
=
_prop_
>- prop ->
<- prop -<
_operator_*/
var j = this.skipSpace(str, i);
if ((j < 0)) {
return j;
}
var r = new pyjslib_List([]);
var j = this.tok("has", str, i);
if ((j >= 0)) {
var i = this.prop(str, j, r);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "expected property after 'has'");
}
res.push(new pyjslib_Tuple(["->", r[0]]));
return i;
}
var j = this.tok("is", str, i);
if ((j >= 0)) {
var i = this.prop(str, j, r);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "expected <property> after 'is'");
}
var j = this.skipSpace(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "End of file found, expected property after 'is'");
return j;
}
var i = j;
var j = this.tok("of", str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected 'of' after 'is' <prop>");
}
res.push(new pyjslib_Tuple(["<-", r[0]]));
return j;
}
var j = this.tok("a", str, i);
if ((j >= 0)) {
res.push(new pyjslib_Tuple(["->", this._store.sym(RDF_type_URI)]));
return j;
}
if ((pyjslib_slice(str, i, ( i + 2 ) ) == "<=")) {
res.push(new pyjslib_Tuple(["<-", this._store.sym( ( Logic_NS + "implies" ) )]));
return ( i + 2 ) ;
}
if ((pyjslib_slice(str, i, ( i + 1 ) ) == "=")) {
if ((pyjslib_slice(str, ( i + 1 ) , ( i + 2 ) ) == ">")) {
res.push(new pyjslib_Tuple(["->", this._store.sym( ( Logic_NS + "implies" ) )]));
return ( i + 2 ) ;
}
res.push(new pyjslib_Tuple(["->", this._store.sym(DAML_sameAs_URI)]));
return ( i + 1 ) ;
}
if ((pyjslib_slice(str, i, ( i + 2 ) ) == ":=")) {
res.push(new pyjslib_Tuple(["->", ( Logic_NS + "becomes" ) ]));
return ( i + 2 ) ;
}
var j = this.prop(str, i, r);
if ((j >= 0)) {
res.push(new pyjslib_Tuple(["->", r[0]]));
return j;
}
if ((pyjslib_slice(str, i, ( i + 2 ) ) == ">-") || (pyjslib_slice(str, i, ( i + 2 ) ) == "<-")) {
throw BadSyntax(this._thisDoc, this.lines, str, j, ">- ... -> syntax is obsolete.");
}
return -1;
};
__SinkParser.prototype.prop = function(str, i, res) {
return this.item(str, i, res);
};
__SinkParser.prototype.item = function(str, i, res) {
return this.path(str, i, res);
};
__SinkParser.prototype.blankNode = function(uri) {
return this._context.bnode(uri, this._reason2);
};
__SinkParser.prototype.path = function(str, i, res) {
/*
Parse the path production.
*/
var j = this.nodeOrLiteral(str, i, res);
if ((j < 0)) {
return j;
}
while (("!^.".indexOf(pyjslib_slice(str, j, ( j + 1 ) )) >= 0)) {
var ch = pyjslib_slice(str, j, ( j + 1 ) );
if ((ch == ".")) {
var ahead = pyjslib_slice(str, ( j + 1 ) , ( j + 2 ) );
if (!(ahead) || (_notNameChars.indexOf(ahead) >= 0) && (":?<[{(".indexOf(ahead) < 0)) {
break;
}
}
var subj = res.pop();
var obj = this.blankNode(this.here(j));
var j = this.node(str, ( j + 1 ) , res);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "EOF found in middle of path syntax");
}
var pred = res.pop();
if ((ch == "^")) {
this.makeStatement(new pyjslib_Tuple([this._context, pred, obj, subj]));
}
else {
this.makeStatement(new pyjslib_Tuple([this._context, pred, subj, obj]));
}
res.push(obj);
}
return j;
};
__SinkParser.prototype.anonymousNode = function(ln) {
/*
Remember or generate a term for one of these _: anonymous nodes*/
var term = this._anonymousNodes[ln];
if (term) {
return term;
}
var term = this._store.bnode(this._context, this._reason2);
this._anonymousNodes[ln] = ( term);
return term;
};
__SinkParser.prototype.node = function(str, i, res, subjectAlready) {
if (typeof subjectAlready == 'undefined') subjectAlready=null;
/*
Parse the <node> production.
Space is now skipped once at the beginning
instead of in multipe calls to self.skipSpace().
*/
var subj = subjectAlready;
var j = this.skipSpace(str, i);
if ((j < 0)) {
return j;
}
var i = j;
var ch = pyjslib_slice(str, i, ( i + 1 ) );
if ((ch == "[")) {
var bnodeID = this.here(i);
var j = this.skipSpace(str, ( i + 1 ) );
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF after '['");
}
if ((pyjslib_slice(str, j, ( j + 1 ) ) == "=")) {
var i = ( j + 1 ) ;
var objs = new pyjslib_List([]);
var j = this.objectList(str, i, objs);
if ((j >= 0)) {
var subj = objs[0];
if ((pyjslib_len(objs) > 1)) {
var __obj = new pyjslib_Iterator(objs);
try {
while (true) {
var obj = __obj.next();
this.makeStatement(new pyjslib_Tuple([this._context, this._store.sym(DAML_sameAs_URI), subj, obj]));
}
} catch (e) {
if (e != StopIteration) {
throw e;
}
}
}
var j = this.skipSpace(str, j);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF when objectList expected after [ = ");
}
if ((pyjslib_slice(str, j, ( j + 1 ) ) == ";")) {
var j = ( j + 1 ) ;
}
}
else {
throw BadSyntax(this._thisDoc, this.lines, str, i, "objectList expected after [= ");
}
}
if ((subj == null)) {
var subj = this.blankNode(bnodeID);
}
var i = this.property_list(str, j, subj);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "property_list expected");
}
var j = this.skipSpace(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF when ']' expected after [ <propertyList>");
}
if ((pyjslib_slice(str, j, ( j + 1 ) ) != "]")) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "']' expected");
}
res.push(subj);
return ( j + 1 ) ;
}
if ((ch == "{")) {
var ch2 = pyjslib_slice(str, ( i + 1 ) , ( i + 2 ) );
if ((ch2 == "$")) {
i += 1;
var j = ( i + 1 ) ;
var mylist = new pyjslib_List([]);
var first_run = true;
while (1) {
var i = this.skipSpace(str, j);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "needed '$}', found end.");
}
if ((pyjslib_slice(str, i, ( i + 2 ) ) == "$}")) {
var j = ( i + 2 ) ;
break;
}
if (!(first_run)) {
if ((pyjslib_slice(str, i, ( i + 1 ) ) == ",")) {
i += 1;
}
else {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected: ','");
}
}
else {
var first_run = false;
}
var item = new pyjslib_List([]);
var j = this.item(str, i, item);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected item in set or '$}'");
}
mylist.push(item[0]);
}
res.push(this._store.newSet(mylist, this._context));
return j;
}
else {
var j = ( i + 1 ) ;
var oldParentContext = this._parentContext;
this._parentContext = this._context;
var parentAnonymousNodes = this._anonymousNodes;
var grandParentVariables = this._parentVariables;
this._parentVariables = this._variables;
this._anonymousNodes = new pyjslib_Dict([]);
this._variables = this._variables.slice();
var reason2 = this._reason2;
this._reason2 = becauseSubexpression;
if ((subj == null)) {
var subj = this._store.formula();
}
this._context = subj;
while (1) {
var i = this.skipSpace(str, j);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "needed '}', found end.");
}
if ((pyjslib_slice(str, i, ( i + 1 ) ) == "}")) {
var j = ( i + 1 ) ;
break;
}
var j = this.directiveOrStatement(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected statement or '}'");
}
}
this._anonymousNodes = parentAnonymousNodes;
this._variables = this._parentVariables;
this._parentVariables = grandParentVariables;
this._context = this._parentContext;
this._reason2 = reason2;
this._parentContext = oldParentContext;
res.push(subj.close());
return j;
}
}
if ((ch == "(")) {
var thing_type = this._store.list;
var ch2 = pyjslib_slice(str, ( i + 1 ) , ( i + 2 ) );
if ((ch2 == "$")) {
var thing_type = this._store.newSet;
i += 1;
}
var j = ( i + 1 ) ;
var mylist = new pyjslib_List([]);
while (1) {
var i = this.skipSpace(str, j);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "needed ')', found end.");
}
if ((pyjslib_slice(str, i, ( i + 1 ) ) == ")")) {
var j = ( i + 1 ) ;
break;
}
var item = new pyjslib_List([]);
var j = this.item(str, i, item);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "expected item in list or ')'");
}
mylist.push(item[0]);
}
res.push(thing_type(mylist, this._context));
return j;
}
var j = this.tok("this", str, i);
if ((j >= 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "Keyword 'this' was ancient N3. Now use @forSome and @forAll keywords.");
res.push(this._context);
return j;
}
var j = this.tok("true", str, i);
if ((j >= 0)) {
res.push(true);
return j;
}
var j = this.tok("false", str, i);
if ((j >= 0)) {
res.push(false);
return j;
}
if ((subj == null)) {
var j = this.uri_ref2(str, i, res);
if ((j >= 0)) {
return j;
}
}
return -1;
};
__SinkParser.prototype.property_list = function(str, i, subj) {
/*
Parse property list
Leaves the terminating punctuation in the buffer
*/
while (1) {
var j = this.skipSpace(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF found when expected verb in property list");
return j;
}
if ((pyjslib_slice(str, j, ( j + 2 ) ) == ":-")) {
var i = ( j + 2 ) ;
var res = new pyjslib_List([]);
var j = this.node(str, i, res, subj);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "bad {} or () or [] node after :- ");
}
var i = j;
continue;
}
var i = j;
var v = new pyjslib_List([]);
var j = this.verb(str, i, v);
if ((j <= 0)) {
return i;
}
var objs = new pyjslib_List([]);
var i = this.objectList(str, j, objs);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "objectList expected");
}
var __obj = new pyjslib_Iterator(objs);
try {
while (true) {
var obj = __obj.next();
var pairFudge = v[0];
var dir = pairFudge[0];
var sym = pairFudge[1];
if ((dir == "->")) {
this.makeStatement(new pyjslib_Tuple([this._context, sym, subj, obj]));
}
else {
this.makeStatement(new pyjslib_Tuple([this._context, sym, obj, subj]));
}
}
} catch (e) {
if (e != StopIteration) {
throw e;
}
}
var j = this.skipSpace(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "EOF found in list of objects");
return j;
}
if ((pyjslib_slice(str, i, ( i + 1 ) ) != ";")) {
return i;
}
var i = ( i + 1 ) ;
}
};
__SinkParser.prototype.commaSeparatedList = function(str, j, res, ofUris) {
/*
return value: -1 bad syntax; >1 new position in str
res has things found appended
Used to use a final value of the function to be called, e.g. this.bareWord
but passing the function didn't work fo js converion pyjs
*/
var i = this.skipSpace(str, j);
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF found expecting comma sep list");
return i;
}
if ((str.charAt(i) == ".")) {
return j;
}
if (ofUris) {
var i = this.uri_ref2(str, i, res);
}
else {
var i = this.bareWord(str, i, res);
}
if ((i < 0)) {
return -1;
}
while (1) {
var j = this.skipSpace(str, i);
if ((j < 0)) {
return j;
}
var ch = pyjslib_slice(str, j, ( j + 1 ) );
if ((ch != ",")) {
if ((ch != ".")) {
return -1;
}
return j;
}
if (ofUris) {
var i = this.uri_ref2(str, ( j + 1 ) , res);
}
else {
var i = this.bareWord(str, ( j + 1 ) , res);
}
if ((i < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, i, "bad list content");
return i;
}
}
};
__SinkParser.prototype.objectList = function(str, i, res) {
var i = this.object(str, i, res);
if ((i < 0)) {
return -1;
}
while (1) {
var j = this.skipSpace(str, i);
if ((j < 0)) {
throw BadSyntax(this._thisDoc, this.lines, str, j, "EOF found after object");
return j;
}
if ((pyjslib_slice(str, j, ( j + 1 ) ) != ",")) {
return j;
}
var i = this.object(str, ( j + 1 ) , res);
if ((i < 0)) {
return i;