eyemind 0.9.17 → 0.9.18
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 +5 -0
- package/package.json +1 -1
- package/src/messages.js +1 -1
- package/src/n3-writer.js +0 -4
- package/src/rdf-lexer.js +24 -516
- package/src/rdf-parser.js +33 -261
- package/src/rdf-prelude.js +0 -259
- package/src/rdf.js +4 -7
- package/src/srl-analyze.js +0 -22
- package/src/srl-term.js +0 -53
- package/src/version.js +1 -1
- package/src/rdf-rules.js +0 -106
package/LS-SPEC.md
CHANGED
package/README.md
CHANGED
|
@@ -331,6 +331,11 @@ ordered envelopes in the `eymsg:` vocabulary, with proofs;
|
|
|
331
331
|
standard input or HTTP(S) incrementally. See the
|
|
332
332
|
[message-boundaries](https://eyereasoner.github.io/eyemind/examples/deck/message-boundaries-n3)
|
|
333
333
|
and [message-alerts](https://eyereasoner.github.io/eyemind/examples/deck/message-alerts-n3) decks.
|
|
334
|
+
Every `npm test` scores the
|
|
335
|
+
[RDF Messages tests](https://w3c-cg.github.io/rsp/spec/messages-tests) as
|
|
336
|
+
coverage: all of the Turtle, TriG, N-Triples and N-Quads cases pass, a wrong
|
|
337
|
+
result fails the run, and the NDJSON-LD, RDF/XML and serialization cases are
|
|
338
|
+
counted as unsupported.
|
|
334
339
|
|
|
335
340
|
With an [RDF logic profile](#rdf-logic-profiles), `--stream-messages` treats
|
|
336
341
|
the other inputs as a background graph and writes, for each message, the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eyemind",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.18",
|
|
4
4
|
"description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3), SPARQL 1.2 RL (SRL) and Logical English (LE) interoperability.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/messages.js
CHANGED
|
@@ -6,7 +6,7 @@ import { readSources } from './n3.js';
|
|
|
6
6
|
import { tripleToEyemind } from './n3-writer.js';
|
|
7
7
|
import { EyemindError } from './terms.js';
|
|
8
8
|
|
|
9
|
-
export const MESSAGE_NS = 'https://eyereasoner.github.io/
|
|
9
|
+
export const MESSAGE_NS = 'https://eyereasoner.github.io/eyemind/vocab/message#';
|
|
10
10
|
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
|
|
11
11
|
const LOG_NAME = 'http://www.w3.org/2000/10/swap/log#nameOf';
|
|
12
12
|
const GRAPH_CONTENT = 'https://eyereasoner.github.io/eyemind/graph#content';
|
package/src/n3-writer.js
CHANGED
|
@@ -60,10 +60,6 @@ export function writeN3(triples, { prefixes = new Map(), strings = 'auto', blank
|
|
|
60
60
|
return [...head, ...body].map((line) => `${line}\n`).join('');
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
export function eyemindToN3(text, options = {}) {
|
|
64
|
-
return writeN3(triplesFromEyemind(text).triples, options);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
63
|
// A parsed N3 term as the eyemind term n3-to-eyemind writes for it.
|
|
68
64
|
export function termToEyemind(t) {
|
|
69
65
|
const st = (name, ...args) => new Struct(name, args);
|
package/src/rdf-lexer.js
CHANGED
|
@@ -347,26 +347,8 @@ function assertValidStringLiteralValue(s, offset = null) {
|
|
|
347
347
|
// This keeps all downstream parsing/reasoning N3-only.
|
|
348
348
|
const LOG_NAME_OF_IRI = '<http://www.w3.org/2000/10/swap/log#nameOf>';
|
|
349
349
|
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 } = {}) {
|
|
350
|
+
|
|
351
|
+
function normalizeRdfCompatibility(inputText, { trig = false } = {}) {
|
|
370
352
|
let text = String(inputText ?? '');
|
|
371
353
|
|
|
372
354
|
// eyemind: ordinary N3 input does not need RDF/TriG
|
|
@@ -375,11 +357,10 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
375
357
|
// plausible top-level TriG named graph block.
|
|
376
358
|
const hasTripleTerms = text.includes('<<');
|
|
377
359
|
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
360
|
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
361
|
const hasAnnotationSyntax = /(?:^|\s)~|\{\|/.test(text);
|
|
381
362
|
|
|
382
|
-
if (!trig && !hasTripleTerms && !hasVersionDirective && !
|
|
363
|
+
if (!trig && !hasTripleTerms && !hasVersionDirective && !hasNamedGraphCandidate && !hasAnnotationSyntax) return text;
|
|
383
364
|
|
|
384
365
|
function isWordChar(ch) {
|
|
385
366
|
return ch != null && /[A-Za-z0-9_:-]/.test(ch);
|
|
@@ -659,192 +640,6 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
659
640
|
}
|
|
660
641
|
|
|
661
642
|
|
|
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
643
|
function stripVersionDirectives(s) {
|
|
849
644
|
// eyemind: a line in a long literal can look exactly like a directive.
|
|
850
645
|
// Only strip tokens outside literals, IRIs and comments.
|
|
@@ -1126,11 +921,6 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
1126
921
|
}
|
|
1127
922
|
|
|
1128
923
|
|
|
1129
|
-
function isOnlyWhitespaceAndComments(s) {
|
|
1130
|
-
return skipWsAndComments(s, 0) >= s.length;
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
924
|
function skipOldStyleDirective(s, at) {
|
|
1135
925
|
let i = at;
|
|
1136
926
|
while (i < s.length) {
|
|
@@ -1153,242 +943,10 @@ function normalizeRdfCompatibility(inputText, { trig = false, messages = true }
|
|
|
1153
943
|
return i;
|
|
1154
944
|
}
|
|
1155
945
|
|
|
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
946
|
if (hasTripleTerms) text = convertTripleTerms(text);
|
|
1389
947
|
// eyemind: annotations are read by the parser, after their triple.
|
|
1390
|
-
if (hasVersionDirective
|
|
1391
|
-
if (trig || hasVersionDirective ||
|
|
948
|
+
if (hasVersionDirective) text = stripVersionDirectives(text);
|
|
949
|
+
if (trig || hasVersionDirective || hasNamedGraphCandidate) text = normalizeNamedGraphs(text);
|
|
1392
950
|
return text;
|
|
1393
951
|
}
|
|
1394
952
|
|
|
@@ -1403,8 +961,7 @@ function isNumericLikeIdentifier(word) {
|
|
|
1403
961
|
}
|
|
1404
962
|
|
|
1405
963
|
function lex(inputText, opts = {}) {
|
|
1406
|
-
|
|
1407
|
-
if (rdf) inputText = normalizeRdfCompatibility(inputText, { trig: !!opts.trig, messages: opts.messages !== false });
|
|
964
|
+
inputText = normalizeRdfCompatibility(inputText, { trig: !!opts.trig });
|
|
1408
965
|
// Avoid copying large ASCII/BMP inputs into an Array. Array.from() is
|
|
1409
966
|
// only needed when the text contains surrogate pairs and we want the old
|
|
1410
967
|
// code-point iteration behavior for non-BMP characters.
|
|
@@ -1449,7 +1006,7 @@ function lex(inputText, opts = {}) {
|
|
|
1449
1006
|
|
|
1450
1007
|
// eyemind: in RDF a blank node label has no colon after its _: prefix,
|
|
1451
1008
|
// so _:s:p is _:s followed by :p.
|
|
1452
|
-
if (
|
|
1009
|
+
if (code === 58 && i > start + 1 && chars[start] === '_' && chars[start + 1] === ':') break;
|
|
1453
1010
|
|
|
1454
1011
|
// Common ASCII QName/identifier characters. Keep this branch inline so
|
|
1455
1012
|
// ordinary N3 files do not call through the full Unicode PN_CHARS predicate
|
|
@@ -1551,50 +1108,23 @@ function lex(inputText, opts = {}) {
|
|
|
1551
1108
|
continue;
|
|
1552
1109
|
}
|
|
1553
1110
|
|
|
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
1111
|
// 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
|
-
}
|
|
1112
|
+
// blocks {| ... |} are tokens of their own.
|
|
1113
|
+
const two = c + (peek(1) ?? '');
|
|
1114
|
+
const kind = { '<<': 'LtLt', '>>': 'GtGt', '{|': 'AnnotOpen', '|}': 'AnnotClose' }[two];
|
|
1115
|
+
if (kind) {
|
|
1116
|
+
tokens.push(new Token(kind, null, i));
|
|
1117
|
+
i += 2;
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
if (c === '~') {
|
|
1121
|
+
tokens.push(new Token('Tilde', null, i));
|
|
1122
|
+
i += 1;
|
|
1123
|
+
continue;
|
|
1583
1124
|
}
|
|
1584
1125
|
|
|
1126
|
+
// IRIREF <...>
|
|
1585
1127
|
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
1128
|
const start = i;
|
|
1599
1129
|
i++; // skip '<'
|
|
1600
1130
|
const iriChars = [];
|
|
@@ -1611,20 +1141,10 @@ function lex(inputText, opts = {}) {
|
|
|
1611
1141
|
continue;
|
|
1612
1142
|
}
|
|
1613
1143
|
|
|
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;
|
|
1144
|
+
// The datatype operator ^^
|
|
1145
|
+
if (c === '^' && peek(1) === '^') {
|
|
1146
|
+
tokens.push(new Token('HatHat', null, i));
|
|
1147
|
+
i += 2;
|
|
1628
1148
|
continue;
|
|
1629
1149
|
}
|
|
1630
1150
|
|
|
@@ -1845,18 +1365,6 @@ function lex(inputText, opts = {}) {
|
|
|
1845
1365
|
continue;
|
|
1846
1366
|
}
|
|
1847
1367
|
|
|
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
1368
|
// Directives: @prefix, @base (and language tags after string literals)
|
|
1861
1369
|
if (c === '@') {
|
|
1862
1370
|
const start = i;
|