eyemind 0.9.17 → 0.9.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LS-SPEC.md +1 -1
- package/README.md +25 -9
- package/package.json +1 -1
- package/src/cli.js +7 -7
- package/src/formats.js +18 -6
- package/src/index.js +1 -1
- package/src/jsonld.js +906 -0
- package/src/messages.js +104 -4
- package/src/n3-writer.js +0 -4
- package/src/node-messages.js +2 -1
- package/src/rdf-lexer.js +35 -520
- package/src/rdf-parser.js +36 -261
- package/src/rdf-prelude.js +0 -259
- package/src/rdf-xml.js +363 -0
- package/src/rdf.js +96 -14
- package/src/srl-analyze.js +0 -22
- package/src/srl-term.js +0 -53
- package/src/version.js +1 -1
- package/src/xml.js +174 -0
- package/src/rdf-rules.js +0 -106
package/src/rdf-lexer.js
CHANGED
|
@@ -259,9 +259,11 @@ function decodeIriRefEscapes(s, offset = null) {
|
|
|
259
259
|
for (let i = 0; i < s.length; i++) {
|
|
260
260
|
const c = s[i];
|
|
261
261
|
if (c !== '\\') {
|
|
262
|
-
|
|
262
|
+
// eyemind: by code point, so a character beyond U+FFFF is no lone surrogate.
|
|
263
|
+
const cp = s.codePointAt(i);
|
|
263
264
|
assertValidIriRefCodePoint(cp, offset);
|
|
264
|
-
out +=
|
|
265
|
+
out += String.fromCodePoint(cp);
|
|
266
|
+
if (cp > 0xffff) i++;
|
|
265
267
|
continue;
|
|
266
268
|
}
|
|
267
269
|
|
|
@@ -347,26 +349,8 @@ function assertValidStringLiteralValue(s, offset = null) {
|
|
|
347
349
|
// This keeps all downstream parsing/reasoning N3-only.
|
|
348
350
|
const LOG_NAME_OF_IRI = '<http://www.w3.org/2000/10/swap/log#nameOf>';
|
|
349
351
|
const RDF_REIFIES_IRI = '<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies>';
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
const EYMSG_NS = 'https://eyereasoner.github.io/eyeling/vocab/message#';
|
|
353
|
-
const EYMSG = Object.freeze({
|
|
354
|
-
RDFMessageStream: `<${EYMSG_NS}RDFMessageStream>`,
|
|
355
|
-
MessageEnvelope: `<${EYMSG_NS}MessageEnvelope>`,
|
|
356
|
-
envelope: `<${EYMSG_NS}envelope>`,
|
|
357
|
-
firstEnvelope: `<${EYMSG_NS}firstEnvelope>`,
|
|
358
|
-
lastEnvelope: `<${EYMSG_NS}lastEnvelope>`,
|
|
359
|
-
orderedEnvelopes: `<${EYMSG_NS}orderedEnvelopes>`,
|
|
360
|
-
messageCount: `<${EYMSG_NS}messageCount>`,
|
|
361
|
-
offset: `<${EYMSG_NS}offset>`,
|
|
362
|
-
nextEnvelope: `<${EYMSG_NS}nextEnvelope>`,
|
|
363
|
-
payloadGraph: `<${EYMSG_NS}payloadGraph>`,
|
|
364
|
-
payloadKind: `<${EYMSG_NS}payloadKind>`,
|
|
365
|
-
empty: `<${EYMSG_NS}empty>`,
|
|
366
|
-
nonEmpty: `<${EYMSG_NS}nonEmpty>`,
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
function normalizeRdfCompatibility(inputText, { trig = false, messages = true } = {}) {
|
|
352
|
+
|
|
353
|
+
function normalizeRdfCompatibility(inputText, { trig = false } = {}) {
|
|
370
354
|
let text = String(inputText ?? '');
|
|
371
355
|
|
|
372
356
|
// eyemind: ordinary N3 input does not need RDF/TriG
|
|
@@ -375,11 +359,10 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
375
359
|
// plausible top-level TriG named graph block.
|
|
376
360
|
const hasTripleTerms = text.includes('<<');
|
|
377
361
|
const hasVersionDirective = /^\s*(?:@version|VERSION)\s+(["'])(?:1\.1|1\.2|1\.2-basic)\1\s*\.?\s*(?:#.*)?$/im.test(text);
|
|
378
|
-
const hasMessageVersionDirective = messages && /^\s*(?:@version|VERSION)\s+(["'])(?:1\.1|1\.2|1\.2-basic)-messages\1\s*\.?\s*(?:#.*)?$/im.test(text);
|
|
379
362
|
const hasNamedGraphCandidate = /(?:^|[.\r\n])\s*(?:GRAPH\s+)?(?:<[^>\r\n]*>|_:[A-Za-z][A-Za-z0-9_-]*|[A-Za-z][A-Za-z0-9_-]*:[^\s{};,.()[\]]*|:[^\s{};,.()[\]]+)\s*\{/m.test(text);
|
|
380
363
|
const hasAnnotationSyntax = /(?:^|\s)~|\{\|/.test(text);
|
|
381
364
|
|
|
382
|
-
if (!trig && !hasTripleTerms && !hasVersionDirective && !
|
|
365
|
+
if (!trig && !hasTripleTerms && !hasVersionDirective && !hasNamedGraphCandidate && !hasAnnotationSyntax) return text;
|
|
383
366
|
|
|
384
367
|
function isWordChar(ch) {
|
|
385
368
|
return ch != null && /[A-Za-z0-9_:-]/.test(ch);
|
|
@@ -659,192 +642,6 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
659
642
|
}
|
|
660
643
|
|
|
661
644
|
|
|
662
|
-
function convertAnnotations(s) {
|
|
663
|
-
let out = '';
|
|
664
|
-
let i = 0;
|
|
665
|
-
let statementStart = true;
|
|
666
|
-
let generatedBlank = 0;
|
|
667
|
-
|
|
668
|
-
function readBalancedDelimited(s, at, open, close) {
|
|
669
|
-
if (!s.startsWith(open, at)) return null;
|
|
670
|
-
let j = at + open.length;
|
|
671
|
-
let depth = 1;
|
|
672
|
-
while (j < s.length) {
|
|
673
|
-
const ch = s[j];
|
|
674
|
-
if (ch === '"' || ch === "'") {
|
|
675
|
-
j = readStringAt(s, j).end;
|
|
676
|
-
continue;
|
|
677
|
-
}
|
|
678
|
-
if (ch === '<' && !s.startsWith('<<', j)) {
|
|
679
|
-
j = readIriAt(s, j).end;
|
|
680
|
-
continue;
|
|
681
|
-
}
|
|
682
|
-
if (ch === '#') {
|
|
683
|
-
while (j < s.length && s[j] !== '\n' && s[j] !== '\r') j += 1;
|
|
684
|
-
continue;
|
|
685
|
-
}
|
|
686
|
-
if (s.startsWith(open, j)) {
|
|
687
|
-
depth += 1;
|
|
688
|
-
j += open.length;
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
if (s.startsWith(close, j)) {
|
|
692
|
-
depth -= 1;
|
|
693
|
-
j += close.length;
|
|
694
|
-
if (depth === 0) return { text: s.slice(at, j), inner: s.slice(at + open.length, j - close.length), end: j };
|
|
695
|
-
continue;
|
|
696
|
-
}
|
|
697
|
-
j += 1;
|
|
698
|
-
}
|
|
699
|
-
throw new N3SyntaxError(`Unterminated RDF annotation block, expected ${close}`);
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
function readTermLikeAt(s, at) {
|
|
703
|
-
const j = skipWsAndComments(s, at);
|
|
704
|
-
if (j >= s.length) return null;
|
|
705
|
-
if (s[j] === '<') return readIriAt(s, j);
|
|
706
|
-
if (s[j] === '"' || s[j] === "'") {
|
|
707
|
-
const str = readStringAt(s, j);
|
|
708
|
-
let end = str.end;
|
|
709
|
-
let text = str.text;
|
|
710
|
-
if (s.startsWith('^^', end)) {
|
|
711
|
-
const dt = readTermAt(s, end + 2);
|
|
712
|
-
if (dt) {
|
|
713
|
-
text += '^^' + dt.text;
|
|
714
|
-
end = dt.end;
|
|
715
|
-
}
|
|
716
|
-
} else if (s[end] === '@') {
|
|
717
|
-
let k = end + 1;
|
|
718
|
-
if (/[A-Za-z]/.test(s[k] || '')) {
|
|
719
|
-
while (k < s.length && /[A-Za-z0-9-]/.test(s[k])) k += 1;
|
|
720
|
-
text += s.slice(end, k);
|
|
721
|
-
end = k;
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
return { text, end };
|
|
725
|
-
}
|
|
726
|
-
if (s[j] === '{') return readBalancedBlock(s, j);
|
|
727
|
-
if (s[j] === '[') return readBalancedDelimited(s, j, '[', ']');
|
|
728
|
-
if (s[j] === '(') return readBalancedDelimited(s, j, '(', ')');
|
|
729
|
-
return readTermAt(s, j);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
function readAnnotationBlockAt(s, at) {
|
|
733
|
-
if (!s.startsWith('{|', at)) return null;
|
|
734
|
-
return readBalancedDelimited(s, at, '{|', '|}');
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
function tryReadAnnotatedTriple(at) {
|
|
738
|
-
const start = skipWsAndComments(s, at);
|
|
739
|
-
if (start >= s.length) return null;
|
|
740
|
-
if (s[start] === '@') return null;
|
|
741
|
-
if (startsWordAt(s, 'PREFIX', start) || startsWordAt(s, 'BASE', start) || startsWordAt(s, 'VERSION', start)) return null;
|
|
742
|
-
if (startsWordAt(s, 'GRAPH', start)) return null;
|
|
743
|
-
|
|
744
|
-
const subj = readTermLikeAt(s, start);
|
|
745
|
-
if (!subj) return null;
|
|
746
|
-
let j = skipWsAndComments(s, subj.end);
|
|
747
|
-
const pred = readTermLikeAt(s, j);
|
|
748
|
-
if (!pred) return null;
|
|
749
|
-
j = skipWsAndComments(s, pred.end);
|
|
750
|
-
const obj = readTermLikeAt(s, j);
|
|
751
|
-
if (!obj) return null;
|
|
752
|
-
j = skipWsAndComments(s, obj.end);
|
|
753
|
-
if (s[j] !== '~' && !s.startsWith('{|', j)) return null;
|
|
754
|
-
|
|
755
|
-
let reifier = '';
|
|
756
|
-
const annotationBlocks = [];
|
|
757
|
-
while (j < s.length) {
|
|
758
|
-
j = skipWsAndComments(s, j);
|
|
759
|
-
if (s[j] === '~') {
|
|
760
|
-
j += 1;
|
|
761
|
-
j = skipWsAndComments(s, j);
|
|
762
|
-
const term = readTermAt(s, j);
|
|
763
|
-
if (term) {
|
|
764
|
-
reifier = term.text;
|
|
765
|
-
j = term.end;
|
|
766
|
-
} else if (!reifier) {
|
|
767
|
-
reifier = `_:rdfAnnotation${++generatedBlank}`;
|
|
768
|
-
}
|
|
769
|
-
continue;
|
|
770
|
-
}
|
|
771
|
-
if (s.startsWith('{|', j)) {
|
|
772
|
-
const block = readAnnotationBlockAt(s, j);
|
|
773
|
-
const inner = block.inner.trim();
|
|
774
|
-
if (!inner) throw new N3SyntaxError('Empty RDF annotation block is not allowed');
|
|
775
|
-
if (!reifier) reifier = `_:rdfAnnotation${++generatedBlank}`;
|
|
776
|
-
annotationBlocks.push(inner);
|
|
777
|
-
j = block.end;
|
|
778
|
-
continue;
|
|
779
|
-
}
|
|
780
|
-
break;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
const after = skipWsAndComments(s, j);
|
|
784
|
-
if (!['.', ';', ',', '}'].includes(s[after])) return null;
|
|
785
|
-
if (!reifier && annotationBlocks.length === 0) return null;
|
|
786
|
-
|
|
787
|
-
const baseTriple = `${subj.text} ${pred.text} ${obj.text}`;
|
|
788
|
-
const graph = `{ ${baseTriple} }`;
|
|
789
|
-
const extra = [];
|
|
790
|
-
if (reifier) extra.push(`${reifier} ${RDF_REIFIES_IRI} ${graph} .`);
|
|
791
|
-
for (const inner of annotationBlocks) {
|
|
792
|
-
if (inner) extra.push(`${reifier} ${inner} .`);
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
let continuation = '';
|
|
796
|
-
if (s[after] === ';') continuation = `\n${subj.text} `;
|
|
797
|
-
else if (s[after] === ',') continuation = `\n${subj.text} ${pred.text} `;
|
|
798
|
-
|
|
799
|
-
return {
|
|
800
|
-
start,
|
|
801
|
-
end: s[after] === '}' ? after : after + 1,
|
|
802
|
-
text: `${baseTriple} .${extra.length ? '\n' + extra.join('\n') : ''}${continuation}`,
|
|
803
|
-
};
|
|
804
|
-
}
|
|
805
|
-
|
|
806
|
-
while (i < s.length) {
|
|
807
|
-
if (statementStart) {
|
|
808
|
-
const converted = tryReadAnnotatedTriple(i);
|
|
809
|
-
if (converted) {
|
|
810
|
-
out += s.slice(i, converted.start) + converted.text;
|
|
811
|
-
i = converted.end;
|
|
812
|
-
statementStart = true;
|
|
813
|
-
continue;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
const ch = s[i];
|
|
818
|
-
if (ch === '"' || ch === "'") {
|
|
819
|
-
const str = readStringAt(s, i);
|
|
820
|
-
out += str.text;
|
|
821
|
-
i = str.end;
|
|
822
|
-
continue;
|
|
823
|
-
}
|
|
824
|
-
if (ch === '<' && !s.startsWith('<<', i)) {
|
|
825
|
-
const iri = readIriAt(s, i);
|
|
826
|
-
out += iri.text;
|
|
827
|
-
i = iri.end;
|
|
828
|
-
continue;
|
|
829
|
-
}
|
|
830
|
-
if (ch === '#') {
|
|
831
|
-
while (i < s.length) {
|
|
832
|
-
const c = s[i++];
|
|
833
|
-
out += c;
|
|
834
|
-
if (c === '\n' || c === '\r') break;
|
|
835
|
-
}
|
|
836
|
-
statementStart = true;
|
|
837
|
-
continue;
|
|
838
|
-
}
|
|
839
|
-
out += ch;
|
|
840
|
-
if (ch === '.' || ch === '{' || ch === '}' || ch === '\n' || ch === '\r') statementStart = true;
|
|
841
|
-
else if (!/\s/.test(ch)) statementStart = false;
|
|
842
|
-
i += 1;
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
return out;
|
|
846
|
-
}
|
|
847
|
-
|
|
848
645
|
function stripVersionDirectives(s) {
|
|
849
646
|
// eyemind: a line in a long literal can look exactly like a directive.
|
|
850
647
|
// Only strip tokens outside literals, IRIs and comments.
|
|
@@ -1074,11 +871,16 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
1074
871
|
}
|
|
1075
872
|
}
|
|
1076
873
|
|
|
1077
|
-
// Not TriG named-graph syntax after all; copy the
|
|
1078
|
-
// continue as ordinary N3.
|
|
874
|
+
// Not TriG named-graph syntax after all; copy the term, or else the
|
|
875
|
+
// first character, and continue as ordinary N3.
|
|
876
|
+
// eyemind: the whole term, so a # in its IRI is no comment.
|
|
1079
877
|
if (graphKeyword) {
|
|
1080
878
|
out += 'GRAPH ';
|
|
1081
879
|
statementStart = false;
|
|
880
|
+
} else if (term && term.end > i) {
|
|
881
|
+
out += s.slice(i, term.end);
|
|
882
|
+
i = term.end;
|
|
883
|
+
statementStart = false;
|
|
1082
884
|
} else if (i < s.length) {
|
|
1083
885
|
const copied = s[i++];
|
|
1084
886
|
out += copied;
|
|
@@ -1126,11 +928,6 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
1126
928
|
}
|
|
1127
929
|
|
|
1128
930
|
|
|
1129
|
-
function isOnlyWhitespaceAndComments(s) {
|
|
1130
|
-
return skipWsAndComments(s, 0) >= s.length;
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
931
|
function skipOldStyleDirective(s, at) {
|
|
1135
932
|
let i = at;
|
|
1136
933
|
while (i < s.length) {
|
|
@@ -1153,242 +950,10 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
1153
950
|
return i;
|
|
1154
951
|
}
|
|
1155
952
|
|
|
1156
|
-
function stripDirectivesAndCommentsForEmptiness(s) {
|
|
1157
|
-
let out = '';
|
|
1158
|
-
let i = 0;
|
|
1159
|
-
let statementStart = true;
|
|
1160
|
-
while (i < s.length) {
|
|
1161
|
-
const ch = s[i];
|
|
1162
|
-
if (ch === '"' || ch === "'") {
|
|
1163
|
-
const str = readStringAt(s, i);
|
|
1164
|
-
out += str.text;
|
|
1165
|
-
i = str.end;
|
|
1166
|
-
statementStart = false;
|
|
1167
|
-
continue;
|
|
1168
|
-
}
|
|
1169
|
-
if (ch === '<') {
|
|
1170
|
-
const iri = readIriAt(s, i);
|
|
1171
|
-
out += iri.text;
|
|
1172
|
-
i = iri.end;
|
|
1173
|
-
statementStart = false;
|
|
1174
|
-
continue;
|
|
1175
|
-
}
|
|
1176
|
-
if (ch === '#') {
|
|
1177
|
-
while (i < s.length && s[i] !== '\n' && s[i] !== '\r') i += 1;
|
|
1178
|
-
statementStart = true;
|
|
1179
|
-
continue;
|
|
1180
|
-
}
|
|
1181
|
-
if (statementStart) {
|
|
1182
|
-
const start = skipWsAndComments(s, i);
|
|
1183
|
-
out += s.slice(i, start);
|
|
1184
|
-
i = start;
|
|
1185
|
-
if (startsWordAt(s, 'PREFIX', i) || startsWordAt(s, 'BASE', i) || startsWordAt(s, 'VERSION', i)) {
|
|
1186
|
-
while (i < s.length && s[i] !== '\n' && s[i] !== '\r') i += 1;
|
|
1187
|
-
statementStart = true;
|
|
1188
|
-
continue;
|
|
1189
|
-
}
|
|
1190
|
-
if (s[i] === '@') {
|
|
1191
|
-
const lower = s.slice(i, i + 9).toLowerCase();
|
|
1192
|
-
if (lower.startsWith('@prefix') || lower.startsWith('@base') || lower.startsWith('@version')) {
|
|
1193
|
-
i = skipOldStyleDirective(s, i);
|
|
1194
|
-
statementStart = true;
|
|
1195
|
-
continue;
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
}
|
|
1199
|
-
out += ch;
|
|
1200
|
-
if (ch === '.' || ch === '}' || ch === '\n' || ch === '\r') statementStart = true;
|
|
1201
|
-
else if (!/\s/.test(ch)) statementStart = false;
|
|
1202
|
-
i += 1;
|
|
1203
|
-
}
|
|
1204
|
-
return out;
|
|
1205
|
-
}
|
|
1206
|
-
|
|
1207
|
-
function simpleHashText(s) {
|
|
1208
|
-
let h = 0x811c9dc5;
|
|
1209
|
-
for (let i = 0; i < s.length; i += 1) {
|
|
1210
|
-
h ^= s.charCodeAt(i);
|
|
1211
|
-
h = Math.imul(h, 0x01000193) >>> 0;
|
|
1212
|
-
}
|
|
1213
|
-
return h.toString(16).padStart(8, '0');
|
|
1214
|
-
}
|
|
1215
|
-
|
|
1216
|
-
function rewriteMessageBlankLabels(s, messageIndex) {
|
|
1217
|
-
let out = '';
|
|
1218
|
-
let i = 0;
|
|
1219
|
-
const prefix = `_:eyeling_m${String(messageIndex).padStart(3, '0')}_`;
|
|
1220
|
-
while (i < s.length) {
|
|
1221
|
-
const ch = s[i];
|
|
1222
|
-
if (ch === '"' || ch === "'") {
|
|
1223
|
-
const str = readStringAt(s, i);
|
|
1224
|
-
out += str.text;
|
|
1225
|
-
i = str.end;
|
|
1226
|
-
continue;
|
|
1227
|
-
}
|
|
1228
|
-
if (ch === '<' && !s.startsWith('<<', i)) {
|
|
1229
|
-
const iri = readIriAt(s, i);
|
|
1230
|
-
out += iri.text;
|
|
1231
|
-
i = iri.end;
|
|
1232
|
-
continue;
|
|
1233
|
-
}
|
|
1234
|
-
if (ch === '#') {
|
|
1235
|
-
while (i < s.length) {
|
|
1236
|
-
const c = s[i++];
|
|
1237
|
-
out += c;
|
|
1238
|
-
if (c === '\n' || c === '\r') break;
|
|
1239
|
-
}
|
|
1240
|
-
continue;
|
|
1241
|
-
}
|
|
1242
|
-
if (s.startsWith('_:', i)) {
|
|
1243
|
-
let j = i + 2;
|
|
1244
|
-
while (j < s.length && !/\s/.test(s[j]) && !'{}[](),;.'.includes(s[j])) j += 1;
|
|
1245
|
-
const label = s.slice(i + 2, j);
|
|
1246
|
-
if (label) {
|
|
1247
|
-
out += prefix + label.replace(/[^A-Za-z0-9_]/g, '_');
|
|
1248
|
-
i = j;
|
|
1249
|
-
continue;
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
out += ch;
|
|
1253
|
-
i += 1;
|
|
1254
|
-
}
|
|
1255
|
-
return out;
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
function findMessageDirectiveAt(s, at) {
|
|
1259
|
-
if (startsWordAt(s, 'MESSAGE', at)) return { start: at, end: at + 'MESSAGE'.length };
|
|
1260
|
-
if (s.slice(at, at + 8).toLowerCase() === '@message' && !isWordChar(s[at + 8])) {
|
|
1261
|
-
let end = at + 8;
|
|
1262
|
-
end = skipWsAndComments(s, end);
|
|
1263
|
-
if (s[end] === '.') end += 1;
|
|
1264
|
-
return { start: at, end };
|
|
1265
|
-
}
|
|
1266
|
-
return null;
|
|
1267
|
-
}
|
|
1268
|
-
|
|
1269
|
-
function splitRdfMessageLog(s) {
|
|
1270
|
-
const chunks = [];
|
|
1271
|
-
let i = 0;
|
|
1272
|
-
let start = 0;
|
|
1273
|
-
let braceDepth = 0;
|
|
1274
|
-
let bracketDepth = 0;
|
|
1275
|
-
let parenDepth = 0;
|
|
1276
|
-
let statementStart = true;
|
|
1277
|
-
let sawDelimiter = false;
|
|
1278
|
-
|
|
1279
|
-
while (i < s.length) {
|
|
1280
|
-
const ch = s[i];
|
|
1281
|
-
if (ch === '"' || ch === "'") {
|
|
1282
|
-
i = readStringAt(s, i).end;
|
|
1283
|
-
statementStart = false;
|
|
1284
|
-
continue;
|
|
1285
|
-
}
|
|
1286
|
-
if (ch === '<' && !s.startsWith('<<', i)) {
|
|
1287
|
-
i = readIriAt(s, i).end;
|
|
1288
|
-
statementStart = false;
|
|
1289
|
-
continue;
|
|
1290
|
-
}
|
|
1291
|
-
if (ch === '#') {
|
|
1292
|
-
while (i < s.length && s[i] !== '\n' && s[i] !== '\r') i += 1;
|
|
1293
|
-
statementStart = true;
|
|
1294
|
-
continue;
|
|
1295
|
-
}
|
|
1296
|
-
if (statementStart && braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) {
|
|
1297
|
-
const termStart = skipWsAndComments(s, i);
|
|
1298
|
-
const msg = findMessageDirectiveAt(s, termStart);
|
|
1299
|
-
if (msg) {
|
|
1300
|
-
chunks.push(s.slice(start, termStart));
|
|
1301
|
-
start = msg.end;
|
|
1302
|
-
i = msg.end;
|
|
1303
|
-
statementStart = true;
|
|
1304
|
-
sawDelimiter = true;
|
|
1305
|
-
continue;
|
|
1306
|
-
}
|
|
1307
|
-
if (termStart !== i) {
|
|
1308
|
-
i = termStart;
|
|
1309
|
-
continue;
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
if (ch === '{') braceDepth += 1;
|
|
1313
|
-
else if (ch === '}' && braceDepth > 0) braceDepth -= 1;
|
|
1314
|
-
else if (ch === '[') bracketDepth += 1;
|
|
1315
|
-
else if (ch === ']' && bracketDepth > 0) bracketDepth -= 1;
|
|
1316
|
-
else if (ch === '(') parenDepth += 1;
|
|
1317
|
-
else if (ch === ')' && parenDepth > 0) parenDepth -= 1;
|
|
1318
|
-
|
|
1319
|
-
if (ch === '.' && braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) statementStart = true;
|
|
1320
|
-
else if (ch === '\n' || ch === '\r') statementStart = true;
|
|
1321
|
-
else if (!/\s/.test(ch)) statementStart = false;
|
|
1322
|
-
i += 1;
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
const tail = s.slice(start);
|
|
1326
|
-
if (!sawDelimiter || !isOnlyWhitespaceAndComments(tail)) chunks.push(tail);
|
|
1327
|
-
return chunks;
|
|
1328
|
-
}
|
|
1329
|
-
|
|
1330
|
-
function normalizeMessageChunk(chunk, messageIndex) {
|
|
1331
|
-
let body = String(chunk || '');
|
|
1332
|
-
if (hasTripleTerms || body.includes('<<')) body = convertTripleTerms(body);
|
|
1333
|
-
if (hasAnnotationSyntax || /(?:^|\s)~|\{\|/.test(body)) {
|
|
1334
|
-
// eyemind: annotations are read by the parser.
|
|
1335
|
-
}
|
|
1336
|
-
body = normalizeNamedGraphs(body);
|
|
1337
|
-
body = rewriteMessageBlankLabels(body, messageIndex);
|
|
1338
|
-
return body.trim();
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
function messageChunkHasRdf(body) {
|
|
1342
|
-
return !isOnlyWhitespaceAndComments(stripDirectivesAndCommentsForEmptiness(body));
|
|
1343
|
-
}
|
|
1344
|
-
|
|
1345
|
-
function normalizeRdfMessageLog(s) {
|
|
1346
|
-
const withoutVersion = stripVersionDirectives(s);
|
|
1347
|
-
const chunks = splitRdfMessageLog(withoutVersion);
|
|
1348
|
-
const hash = simpleHashText(s);
|
|
1349
|
-
const base = `urn:eyeling:message-log:${hash}`;
|
|
1350
|
-
const stream = `<${base}#stream>`;
|
|
1351
|
-
const envelopeIris = chunks.map((unused, idx) => `<${base}#m${String(idx + 1).padStart(3, '0')}>`);
|
|
1352
|
-
const payloadIris = chunks.map((unused, idx) => `<${base}#m${String(idx + 1).padStart(3, '0')}/payload>`);
|
|
1353
|
-
const out = [];
|
|
1354
|
-
|
|
1355
|
-
out.push(`${stream} ${RDF_TYPE_IRI} ${EYMSG.RDFMessageStream} .`);
|
|
1356
|
-
out.push(`${stream} ${EYMSG.messageCount} "${chunks.length}"^^${XSD_INTEGER_IRI} .`);
|
|
1357
|
-
if (envelopeIris.length) {
|
|
1358
|
-
out.push(`${stream} ${EYMSG.orderedEnvelopes} (${envelopeIris.join(' ')}) .`);
|
|
1359
|
-
out.push(`${stream} ${EYMSG.firstEnvelope} ${envelopeIris[0]} .`);
|
|
1360
|
-
out.push(`${stream} ${EYMSG.lastEnvelope} ${envelopeIris[envelopeIris.length - 1]} .`);
|
|
1361
|
-
}
|
|
1362
|
-
|
|
1363
|
-
for (let idx = 0; idx < chunks.length; idx += 1) {
|
|
1364
|
-
const n = idx + 1;
|
|
1365
|
-
const envelope = envelopeIris[idx];
|
|
1366
|
-
const payload = payloadIris[idx];
|
|
1367
|
-
const body = normalizeMessageChunk(chunks[idx], n);
|
|
1368
|
-
const hasBody = messageChunkHasRdf(body);
|
|
1369
|
-
|
|
1370
|
-
out.push(`${stream} ${EYMSG.envelope} ${envelope} .`);
|
|
1371
|
-
out.push(`${envelope} ${RDF_TYPE_IRI} ${EYMSG.MessageEnvelope} .`);
|
|
1372
|
-
out.push(`${envelope} ${EYMSG.offset} "${n}"^^${XSD_INTEGER_IRI} .`);
|
|
1373
|
-
out.push(`${envelope} ${EYMSG.payloadKind} ${hasBody ? EYMSG.nonEmpty : EYMSG.empty} .`);
|
|
1374
|
-
if (idx + 1 < envelopeIris.length) out.push(`${envelope} ${EYMSG.nextEnvelope} ${envelopeIris[idx + 1]} .`);
|
|
1375
|
-
if (hasBody) {
|
|
1376
|
-
out.push(`${envelope} ${EYMSG.payloadGraph} ${payload} .`);
|
|
1377
|
-
out.push(`${payload} ${LOG_NAME_OF_IRI} {`);
|
|
1378
|
-
out.push(body);
|
|
1379
|
-
out.push(`} .`);
|
|
1380
|
-
}
|
|
1381
|
-
}
|
|
1382
|
-
|
|
1383
|
-
return out.join('\n') + '\n';
|
|
1384
|
-
}
|
|
1385
|
-
|
|
1386
|
-
if (hasMessageVersionDirective) return normalizeRdfMessageLog(text);
|
|
1387
|
-
|
|
1388
953
|
if (hasTripleTerms) text = convertTripleTerms(text);
|
|
1389
954
|
// eyemind: annotations are read by the parser, after their triple.
|
|
1390
|
-
if (hasVersionDirective
|
|
1391
|
-
if (trig || hasVersionDirective ||
|
|
955
|
+
if (hasVersionDirective) text = stripVersionDirectives(text);
|
|
956
|
+
if (trig || hasVersionDirective || hasNamedGraphCandidate) text = normalizeNamedGraphs(text);
|
|
1392
957
|
return text;
|
|
1393
958
|
}
|
|
1394
959
|
|
|
@@ -1403,8 +968,7 @@ function isNumericLikeIdentifier(word) {
|
|
|
1403
968
|
}
|
|
1404
969
|
|
|
1405
970
|
function lex(inputText, opts = {}) {
|
|
1406
|
-
|
|
1407
|
-
if (rdf) inputText = normalizeRdfCompatibility(inputText, { trig: !!opts.trig, messages: opts.messages !== false });
|
|
971
|
+
inputText = normalizeRdfCompatibility(inputText, { trig: !!opts.trig });
|
|
1408
972
|
// Avoid copying large ASCII/BMP inputs into an Array. Array.from() is
|
|
1409
973
|
// only needed when the text contains surrogate pairs and we want the old
|
|
1410
974
|
// code-point iteration behavior for non-BMP characters.
|
|
@@ -1449,7 +1013,7 @@ function lex(inputText, opts = {}) {
|
|
|
1449
1013
|
|
|
1450
1014
|
// eyemind: in RDF a blank node label has no colon after its _: prefix,
|
|
1451
1015
|
// so _:s:p is _:s followed by :p.
|
|
1452
|
-
if (
|
|
1016
|
+
if (code === 58 && i > start + 1 && chars[start] === '_' && chars[start + 1] === ':') break;
|
|
1453
1017
|
|
|
1454
1018
|
// Common ASCII QName/identifier characters. Keep this branch inline so
|
|
1455
1019
|
// ordinary N3 files do not call through the full Unicode PN_CHARS predicate
|
|
@@ -1551,50 +1115,23 @@ function lex(inputText, opts = {}) {
|
|
|
1551
1115
|
continue;
|
|
1552
1116
|
}
|
|
1553
1117
|
|
|
1554
|
-
// 3) Two-character operators: => and <=
|
|
1555
|
-
if (c === '=') {
|
|
1556
|
-
if (peek(1) === '>') {
|
|
1557
|
-
tokens.push(new Token('OpImplies', null, i));
|
|
1558
|
-
i += 2;
|
|
1559
|
-
continue;
|
|
1560
|
-
} else {
|
|
1561
|
-
// N3 syntactic sugar: '=' means owl:sameAs
|
|
1562
|
-
tokens.push(new Token('Equals', null, i));
|
|
1563
|
-
i += 1;
|
|
1564
|
-
continue;
|
|
1565
|
-
}
|
|
1566
|
-
}
|
|
1567
|
-
|
|
1568
1118
|
// eyemind: RDF 1.2 reified triples << ... >>, reifiers ~ and annotation
|
|
1569
|
-
// blocks {| ... |} are tokens
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
continue;
|
|
1582
|
-
}
|
|
1119
|
+
// blocks {| ... |} are tokens of their own.
|
|
1120
|
+
const two = c + (peek(1) ?? '');
|
|
1121
|
+
const kind = { '<<': 'LtLt', '>>': 'GtGt', '{|': 'AnnotOpen', '|}': 'AnnotClose' }[two];
|
|
1122
|
+
if (kind) {
|
|
1123
|
+
tokens.push(new Token(kind, null, i));
|
|
1124
|
+
i += 2;
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
if (c === '~') {
|
|
1128
|
+
tokens.push(new Token('Tilde', null, i));
|
|
1129
|
+
i += 1;
|
|
1130
|
+
continue;
|
|
1583
1131
|
}
|
|
1584
1132
|
|
|
1133
|
+
// IRIREF <...>
|
|
1585
1134
|
if (c === '<') {
|
|
1586
|
-
if (peek(1) === '=') {
|
|
1587
|
-
tokens.push(new Token('OpImpliedBy', null, i));
|
|
1588
|
-
i += 2;
|
|
1589
|
-
continue;
|
|
1590
|
-
}
|
|
1591
|
-
// N3 predicate inversion: "<-" (swap subject/object for this predicate)
|
|
1592
|
-
if (peek(1) === '-') {
|
|
1593
|
-
tokens.push(new Token('OpPredInvert', null, i));
|
|
1594
|
-
i += 2;
|
|
1595
|
-
continue;
|
|
1596
|
-
}
|
|
1597
|
-
// Otherwise IRIREF <...>
|
|
1598
1135
|
const start = i;
|
|
1599
1136
|
i++; // skip '<'
|
|
1600
1137
|
const iriChars = [];
|
|
@@ -1611,20 +1148,10 @@ function lex(inputText, opts = {}) {
|
|
|
1611
1148
|
continue;
|
|
1612
1149
|
}
|
|
1613
1150
|
|
|
1614
|
-
//
|
|
1615
|
-
if (c === '
|
|
1616
|
-
tokens.push(new Token('
|
|
1617
|
-
i +=
|
|
1618
|
-
continue;
|
|
1619
|
-
}
|
|
1620
|
-
if (c === '^') {
|
|
1621
|
-
if (peek(1) === '^') {
|
|
1622
|
-
tokens.push(new Token('HatHat', null, i));
|
|
1623
|
-
i += 2;
|
|
1624
|
-
continue;
|
|
1625
|
-
}
|
|
1626
|
-
tokens.push(new Token('OpPathRev', null, i));
|
|
1627
|
-
i += 1;
|
|
1151
|
+
// The datatype operator ^^
|
|
1152
|
+
if (c === '^' && peek(1) === '^') {
|
|
1153
|
+
tokens.push(new Token('HatHat', null, i));
|
|
1154
|
+
i += 2;
|
|
1628
1155
|
continue;
|
|
1629
1156
|
}
|
|
1630
1157
|
|
|
@@ -1845,18 +1372,6 @@ function lex(inputText, opts = {}) {
|
|
|
1845
1372
|
continue;
|
|
1846
1373
|
}
|
|
1847
1374
|
|
|
1848
|
-
// Variable ?name
|
|
1849
|
-
if (c === '?') {
|
|
1850
|
-
const start = i;
|
|
1851
|
-
i++;
|
|
1852
|
-
const name = readIdentText(start);
|
|
1853
|
-
if (!name) {
|
|
1854
|
-
throw new N3SyntaxError("Expected variable name after '?'.", start);
|
|
1855
|
-
}
|
|
1856
|
-
tokens.push(new Token('Var', name, start));
|
|
1857
|
-
continue;
|
|
1858
|
-
}
|
|
1859
|
-
|
|
1860
1375
|
// Directives: @prefix, @base (and language tags after string literals)
|
|
1861
1376
|
if (c === '@') {
|
|
1862
1377
|
const start = i;
|