eyemind 0.9.16 → 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/engine.js +12 -15
- package/src/le.js +33 -45
- package/src/messages.js +1 -1
- package/src/n3-writer.js +0 -4
- package/src/rdf-lexer.js +40 -645
- package/src/rdf-parser.js +47 -304
- package/src/rdf-prelude.js +0 -259
- package/src/rdf.js +4 -7
- package/src/shacl-expr.js +1 -1
- package/src/shacl.js +8 -19
- package/src/srl-analyze.js +0 -22
- package/src/srl-term.js +0 -53
- package/src/srl-tokenizer.js +16 -22
- package/src/version.js +1 -1
- package/src/rdf-rules.js +0 -106
package/src/rdf-prelude.js
CHANGED
|
@@ -11,22 +11,8 @@
|
|
|
11
11
|
// ===========================================================================
|
|
12
12
|
|
|
13
13
|
const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
14
|
-
const RDFS_NS = 'http://www.w3.org/2000/01/rdf-schema#';
|
|
15
|
-
const OWL_NS = 'http://www.w3.org/2002/07/owl#';
|
|
16
14
|
const XSD_NS = 'http://www.w3.org/2001/XMLSchema#';
|
|
17
|
-
const MATH_NS = 'http://www.w3.org/2000/10/swap/math#';
|
|
18
|
-
const TIME_NS = 'http://www.w3.org/2000/10/swap/time#';
|
|
19
|
-
const LIST_NS = 'http://www.w3.org/2000/10/swap/list#';
|
|
20
15
|
const LOG_NS = 'http://www.w3.org/2000/10/swap/log#';
|
|
21
|
-
const STRING_NS = 'http://www.w3.org/2000/10/swap/string#';
|
|
22
|
-
const DT_NS = 'https://eyereasoner.github.io/eyeling/datatype#';
|
|
23
|
-
const SKOLEM_NS = 'https://eyereasoner.github.io/.well-known/genid/';
|
|
24
|
-
|
|
25
|
-
// Private rule-lifting variable prefix used for blank nodes that appear in
|
|
26
|
-
// rule-body patterns. The prefix is intentionally not spellable in N3 input,
|
|
27
|
-
// but public extension APIs should present these variables with stable,
|
|
28
|
-
// ordinary names and translate them back internally.
|
|
29
|
-
const INTERNAL_BLANK_VAR_PREFIX = '\uE000eyeling_b';
|
|
30
16
|
|
|
31
17
|
function parseUriReferenceForResolution(uri) {
|
|
32
18
|
// RFC 3986 Appendix B-style component parser, with the scheme tightened to
|
|
@@ -373,13 +359,6 @@ class Literal extends Term {
|
|
|
373
359
|
}
|
|
374
360
|
}
|
|
375
361
|
|
|
376
|
-
class Var extends Term {
|
|
377
|
-
constructor(name) {
|
|
378
|
-
super();
|
|
379
|
-
this.name = name; // without leading '?'
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
362
|
class Blank extends Term {
|
|
384
363
|
constructor(label, blankNodeKind = 'labeled') {
|
|
385
364
|
super();
|
|
@@ -401,14 +380,6 @@ class ListTerm extends Term {
|
|
|
401
380
|
}
|
|
402
381
|
}
|
|
403
382
|
|
|
404
|
-
class OpenListTerm extends Term {
|
|
405
|
-
constructor(prefix, tailVar) {
|
|
406
|
-
super();
|
|
407
|
-
this.prefix = prefix; // Term[]
|
|
408
|
-
this.tailVar = tailVar; // string
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
383
|
class GraphTerm extends Term {
|
|
413
384
|
constructor(triples) {
|
|
414
385
|
super();
|
|
@@ -424,17 +395,6 @@ class Triple {
|
|
|
424
395
|
}
|
|
425
396
|
}
|
|
426
397
|
|
|
427
|
-
class Rule {
|
|
428
|
-
constructor(premise, conclusion, isForward, isFuse, headBlankLabels) {
|
|
429
|
-
this.premise = premise; // Triple[]
|
|
430
|
-
this.conclusion = conclusion; // Triple[]
|
|
431
|
-
this.isForward = isForward; // boolean
|
|
432
|
-
this.isFuse = isFuse; // boolean
|
|
433
|
-
// Set<string> of blank-node labels that occur explicitly in the rule head
|
|
434
|
-
this.headBlankLabels = headBlankLabels || new Set();
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
398
|
// ===========================================================================
|
|
439
399
|
// Term interning
|
|
440
400
|
// ===========================================================================
|
|
@@ -485,79 +445,18 @@ function internLiteral(value) {
|
|
|
485
445
|
// Special predicate helpers (kept here because PrefixEnv needs them)
|
|
486
446
|
// ===========================================================================
|
|
487
447
|
|
|
488
|
-
function isRdfTypePred(p) {
|
|
489
|
-
return p instanceof Iri && p.value === RDF_NS + 'type';
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
function isLogImplies(p) {
|
|
493
|
-
return p instanceof Iri && p.value === LOG_NS + 'implies';
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
function isLogImpliedBy(p) {
|
|
497
|
-
return p instanceof Iri && p.value === LOG_NS + 'impliedBy';
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
function isLogQuery(p) {
|
|
501
|
-
return p instanceof Iri && p.value === LOG_NS + 'query';
|
|
502
|
-
}
|
|
503
|
-
|
|
504
448
|
// ===========================================================================
|
|
505
449
|
// PREFIX ENVIRONMENT
|
|
506
450
|
// ===========================================================================
|
|
507
451
|
|
|
508
|
-
// Conservative check for whether a candidate local part can be safely serialized as a prefixed name.
|
|
509
|
-
// If false, we fall back to <IRI> to guarantee syntactically valid N3/Turtle output.
|
|
510
|
-
function isValidQNameLocal(local) {
|
|
511
|
-
if (typeof local !== 'string' || local.length === 0) return false;
|
|
512
|
-
// Disallow characters that would break PN_LOCAL unless escaped (we keep this conservative).
|
|
513
|
-
if (/[#:/?\s]/.test(local)) return false;
|
|
514
|
-
// Allow a safe ASCII subset.
|
|
515
|
-
if (/[^A-Za-z0-9._-]/.test(local)) return false;
|
|
516
|
-
// Avoid edge cases that typically require escaping.
|
|
517
|
-
if (local.endsWith('.')) return false;
|
|
518
|
-
if (/^[.-]/.test(local)) return false;
|
|
519
|
-
return true;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
452
|
class PrefixEnv {
|
|
523
453
|
constructor(map, baseIri) {
|
|
524
454
|
this.map = map || {}; // prefix -> IRI (including "" for @prefix :)
|
|
525
455
|
this.baseIri = baseIri || ''; // base IRI for resolving <relative>
|
|
526
456
|
}
|
|
527
457
|
|
|
528
|
-
static newDefault() {
|
|
529
|
-
const m = {};
|
|
530
|
-
m['rdf'] = RDF_NS;
|
|
531
|
-
m['rdfs'] = RDFS_NS;
|
|
532
|
-
m['xsd'] = XSD_NS;
|
|
533
|
-
m['log'] = LOG_NS;
|
|
534
|
-
m['math'] = MATH_NS;
|
|
535
|
-
m['string'] = STRING_NS;
|
|
536
|
-
m['list'] = LIST_NS;
|
|
537
|
-
m['time'] = TIME_NS;
|
|
538
|
-
m['dt'] = DT_NS;
|
|
539
|
-
m['genid'] = SKOLEM_NS;
|
|
540
|
-
m[''] = ''; // empty prefix default namespace
|
|
541
|
-
return new PrefixEnv(m, ''); // base IRI starts empty
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
// shrinkIri and prefixesUsedForOutput run once per IRI of output, and both used
|
|
545
|
-
// to rebuild Object.entries(this.map) on every call. These caches hold the
|
|
546
|
-
// entry list and the per-IRI answer instead; every write to the map goes
|
|
547
|
-
// through set(), so invalidating there is enough.
|
|
548
|
-
__invalidatePrefixCaches() {
|
|
549
|
-
this.__entries = null;
|
|
550
|
-
this.__shrinkCache = null;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
__nonEmptyEntries() {
|
|
554
|
-
if (!this.__entries) this.__entries = Object.entries(this.map).filter(([, base]) => !!base);
|
|
555
|
-
return this.__entries;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
458
|
set(pref, base) {
|
|
559
459
|
this.map[pref] = base;
|
|
560
|
-
this.__invalidatePrefixCaches();
|
|
561
460
|
}
|
|
562
461
|
|
|
563
462
|
setBase(baseIri) {
|
|
@@ -575,179 +474,21 @@ class PrefixEnv {
|
|
|
575
474
|
}
|
|
576
475
|
return q;
|
|
577
476
|
}
|
|
578
|
-
|
|
579
|
-
shrinkIri(iri) {
|
|
580
|
-
if (!this.__shrinkCache) this.__shrinkCache = new Map();
|
|
581
|
-
const cached = this.__shrinkCache.get(iri);
|
|
582
|
-
if (cached !== undefined) return cached;
|
|
583
|
-
const out = this.__shrinkIriUncached(iri);
|
|
584
|
-
this.__shrinkCache.set(iri, out);
|
|
585
|
-
return out;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
__shrinkIriUncached(iri) {
|
|
589
|
-
let best = null; // [prefix, local]
|
|
590
|
-
for (const [p, base] of this.__nonEmptyEntries()) {
|
|
591
|
-
if (iri.startsWith(base)) {
|
|
592
|
-
const local = iri.slice(base.length);
|
|
593
|
-
if (!local) continue;
|
|
594
|
-
// Only emit a QName when the local part is safe to serialize without escaping.
|
|
595
|
-
if (!isValidQNameLocal(local)) continue;
|
|
596
|
-
const cand = [p, local];
|
|
597
|
-
if (best === null || cand[1].length < best[1].length) best = cand;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
if (best === null) return null;
|
|
601
|
-
const [p, local] = best;
|
|
602
|
-
if (p === '') return `:${local}`;
|
|
603
|
-
return `${p}:${local}`;
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
prefixesUsedForOutput(triples) {
|
|
607
|
-
const used = new Set();
|
|
608
|
-
const entries = this.__nonEmptyEntries();
|
|
609
|
-
// One accumulator reused across every term, and stop the moment every prefix
|
|
610
|
-
// is accounted for: the answer cannot grow after that.
|
|
611
|
-
const iris = [];
|
|
612
|
-
for (const t of triples) {
|
|
613
|
-
if (used.size === entries.length) break;
|
|
614
|
-
iris.length = 0;
|
|
615
|
-
collectIrisInTerm(t.s, iris);
|
|
616
|
-
if (!isRdfTypePred(t.p)) {
|
|
617
|
-
collectIrisInTerm(t.p, iris);
|
|
618
|
-
}
|
|
619
|
-
collectIrisInTerm(t.o, iris);
|
|
620
|
-
for (const iri of iris) {
|
|
621
|
-
for (const [p, base] of entries) {
|
|
622
|
-
if (iri.startsWith(base)) used.add(p);
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
const v = [];
|
|
627
|
-
for (const p of used) {
|
|
628
|
-
if (Object.prototype.hasOwnProperty.call(this.map, p)) v.push([p, this.map[p]]);
|
|
629
|
-
}
|
|
630
|
-
v.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
631
|
-
return v;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
// out is optional so the old "returns a fresh array" contract still holds, but
|
|
636
|
-
// callers in a loop can pass one accumulator instead of allocating per term.
|
|
637
|
-
function collectIrisInTerm(t, out = []) {
|
|
638
|
-
if (t instanceof Iri) {
|
|
639
|
-
out.push(t.value);
|
|
640
|
-
} else if (t instanceof Literal) {
|
|
641
|
-
const [, dt] = literalParts(t.value);
|
|
642
|
-
if (dt) out.push(dt); // so rdf/xsd prefixes are emitted when only used in ^^...
|
|
643
|
-
} else if (t instanceof ListTerm) {
|
|
644
|
-
for (const x of t.elems) collectIrisInTerm(x, out);
|
|
645
|
-
} else if (t instanceof OpenListTerm) {
|
|
646
|
-
for (const x of t.prefix) collectIrisInTerm(x, out);
|
|
647
|
-
} else if (t instanceof GraphTerm) {
|
|
648
|
-
for (const tr of t.triples) {
|
|
649
|
-
collectIrisInTerm(tr.s, out);
|
|
650
|
-
collectIrisInTerm(tr.p, out);
|
|
651
|
-
collectIrisInTerm(tr.o, out);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
return out;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
function collectVarsInTerm(t, acc) {
|
|
658
|
-
if (t instanceof Var) {
|
|
659
|
-
acc.add(t.name);
|
|
660
|
-
} else if (t instanceof ListTerm) {
|
|
661
|
-
for (const x of t.elems) collectVarsInTerm(x, acc);
|
|
662
|
-
} else if (t instanceof OpenListTerm) {
|
|
663
|
-
for (const x of t.prefix) collectVarsInTerm(x, acc);
|
|
664
|
-
acc.add(t.tailVar);
|
|
665
|
-
} else if (t instanceof GraphTerm) {
|
|
666
|
-
for (const tr of t.triples) {
|
|
667
|
-
collectVarsInTerm(tr.s, acc);
|
|
668
|
-
collectVarsInTerm(tr.p, acc);
|
|
669
|
-
collectVarsInTerm(tr.o, acc);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
function collectBlankLabelsInTerm(t, acc) {
|
|
675
|
-
if (t instanceof Blank) {
|
|
676
|
-
acc.add(t.label);
|
|
677
|
-
} else if (t instanceof ListTerm) {
|
|
678
|
-
for (const x of t.elems) collectBlankLabelsInTerm(x, acc);
|
|
679
|
-
} else if (t instanceof OpenListTerm) {
|
|
680
|
-
for (const x of t.prefix) collectBlankLabelsInTerm(x, acc);
|
|
681
|
-
} else if (t instanceof GraphTerm) {
|
|
682
|
-
for (const tr of t.triples) {
|
|
683
|
-
collectBlankLabelsInTerm(tr.s, acc);
|
|
684
|
-
collectBlankLabelsInTerm(tr.p, acc);
|
|
685
|
-
collectBlankLabelsInTerm(tr.o, acc);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
function collectBlankLabelsInTriples(triples) {
|
|
691
|
-
const acc = new Set();
|
|
692
|
-
for (const tr of triples) {
|
|
693
|
-
collectBlankLabelsInTerm(tr.s, acc);
|
|
694
|
-
collectBlankLabelsInTerm(tr.p, acc);
|
|
695
|
-
collectBlankLabelsInTerm(tr.o, acc);
|
|
696
|
-
}
|
|
697
|
-
return acc;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
function annotateQuotedGraphTerm(graph) {
|
|
701
|
-
if (!(graph instanceof GraphTerm)) return graph;
|
|
702
|
-
const labels = collectBlankLabelsInTriples(graph.triples);
|
|
703
|
-
Object.defineProperty(graph, '__quotedLocalBlankLabels', {
|
|
704
|
-
value: labels,
|
|
705
|
-
enumerable: false,
|
|
706
|
-
writable: true,
|
|
707
|
-
configurable: true,
|
|
708
|
-
});
|
|
709
|
-
return graph;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
function copyQuotedGraphMetadata(src, dst) {
|
|
713
|
-
if (!(src instanceof GraphTerm) || !(dst instanceof GraphTerm)) return dst;
|
|
714
|
-
if (!Object.prototype.hasOwnProperty.call(src, '__quotedLocalBlankLabels')) return dst;
|
|
715
|
-
|
|
716
|
-
const labels = src.__quotedLocalBlankLabels;
|
|
717
|
-
Object.defineProperty(dst, '__quotedLocalBlankLabels', {
|
|
718
|
-
value: labels instanceof Set ? new Set(labels) : labels,
|
|
719
|
-
enumerable: false,
|
|
720
|
-
writable: true,
|
|
721
|
-
configurable: true,
|
|
722
|
-
});
|
|
723
|
-
return dst;
|
|
724
477
|
}
|
|
725
478
|
|
|
726
479
|
export {
|
|
727
480
|
RDF_NS,
|
|
728
|
-
OWL_NS,
|
|
729
481
|
LOG_NS,
|
|
730
|
-
INTERNAL_BLANK_VAR_PREFIX,
|
|
731
482
|
resolveIriRef,
|
|
732
483
|
literalParts,
|
|
733
|
-
Term,
|
|
734
484
|
Iri,
|
|
735
485
|
Literal,
|
|
736
|
-
Var,
|
|
737
486
|
Blank,
|
|
738
487
|
ListTerm,
|
|
739
|
-
OpenListTerm,
|
|
740
488
|
GraphTerm,
|
|
741
489
|
Triple,
|
|
742
|
-
Rule,
|
|
743
490
|
internIri,
|
|
744
491
|
internLiteral,
|
|
745
492
|
clearParserCaches,
|
|
746
|
-
isLogImplies,
|
|
747
|
-
isLogImpliedBy,
|
|
748
|
-
isLogQuery,
|
|
749
493
|
PrefixEnv,
|
|
750
|
-
collectBlankLabelsInTriples,
|
|
751
|
-
annotateQuotedGraphTerm,
|
|
752
|
-
copyQuotedGraphMetadata,
|
|
753
494
|
};
|
package/src/rdf.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// RDF 1.2 in and out of eyemind: Turtle, TriG, N-Triples and N-Quads.
|
|
2
2
|
//
|
|
3
3
|
// Documents are read by the RDF lexer and parser (rdf-lexer.js,
|
|
4
|
-
// rdf-parser.js
|
|
5
|
-
//
|
|
4
|
+
// rdf-parser.js and rdf-prelude.js), with the RDF 1.2 surface checks
|
|
5
|
+
// below. Each quad becomes an eyemind term
|
|
6
6
|
// rdf(Subject, Predicate, Object, Graph):
|
|
7
7
|
//
|
|
8
8
|
// IRI iri('https://example.org/a')
|
|
@@ -46,9 +46,7 @@ export function parseRdf(text, { format = 'turtle', base = null, scope = 'rdf' }
|
|
|
46
46
|
try {
|
|
47
47
|
checkRdfSyntax(text, format);
|
|
48
48
|
assertRdf12SurfaceSyntax(text, format);
|
|
49
|
-
const parser = new Parser(lex(text, {
|
|
50
|
-
// RDF declares every prefix it uses; N3's predeclared ones do not apply.
|
|
51
|
-
parser.prefixes.map = {};
|
|
49
|
+
const parser = new Parser(lex(text, { trig: format === 'trig' }));
|
|
52
50
|
if (base) parser.prefixes.setBase(base);
|
|
53
51
|
const state = { scope, lists: 0, out: [] };
|
|
54
52
|
if (format === 'n-quads') {
|
|
@@ -62,8 +60,7 @@ export function parseRdf(text, { format = 'turtle', base = null, scope = 'rdf' }
|
|
|
62
60
|
quad(s, p, o, g, state);
|
|
63
61
|
}
|
|
64
62
|
} else {
|
|
65
|
-
const [, triples
|
|
66
|
-
if (forward.length || backward.length || queries.length) throw new SyntaxError('an RDF document has no rules');
|
|
63
|
+
const [, triples] = parser.parseDocument();
|
|
67
64
|
for (const t of triples) {
|
|
68
65
|
if (format === 'trig' && t.p instanceof Iri && t.p.value === `${LOG_NS}nameOf` && t.o instanceof GraphTerm) {
|
|
69
66
|
const g = convert(t.s, 'graph', DEFAULT_GRAPH, state);
|
package/src/shacl-expr.js
CHANGED
|
@@ -16,7 +16,7 @@ const SHNEX = 'http://www.w3.org/ns/shacl-node-expr#';
|
|
|
16
16
|
export const SPARQL = 'http://www.w3.org/ns/sparql#';
|
|
17
17
|
const TRUE = `literal('true', datatype('${XSD}boolean'))`;
|
|
18
18
|
const FALSE = `literal('false', datatype('${XSD}boolean'))`;
|
|
19
|
-
const isIri = (t) => t instanceof Struct && t.name === 'iri';
|
|
19
|
+
export const isIri = (t) => t instanceof Struct && t.name === 'iri';
|
|
20
20
|
const isBlank = (t) => t instanceof Struct && t.name === 'bnode';
|
|
21
21
|
|
|
22
22
|
// sparql:<name> to its SRL function goal and the library definition it needs.
|
package/src/shacl.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { EyemindError, Struct } from './terms.js';
|
|
6
6
|
import { write } from './writer.js';
|
|
7
7
|
import { RDF_DATATYPES } from './rdf-values.js';
|
|
8
|
-
import { NODE_COMMON, nodeExpressions } from './shacl-expr.js';
|
|
8
|
+
import { NODE_COMMON, isIri, nodeExpressions } from './shacl-expr.js';
|
|
9
9
|
|
|
10
10
|
const SH = 'http://www.w3.org/ns/shacl#';
|
|
11
11
|
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
|
|
@@ -34,7 +34,6 @@ const literal = (t, datatype) => t instanceof Struct && t.name === 'literal' &&
|
|
|
34
34
|
const BOOL = (t) => literal(t, XSD + 'boolean') && ['true', 'false', '1', '0'].includes(t.args[0]);
|
|
35
35
|
// Only the boolean true activates a constraint; the suite reads "1" as not true.
|
|
36
36
|
const TRUE = (t) => literal(t, XSD + 'boolean') && t.args[0] === 'true';
|
|
37
|
-
const isIri = (t) => t instanceof Struct && t.name === 'iri';
|
|
38
37
|
const resource = (t) => t instanceof Struct && ['iri', 'bnode', 'triple'].includes(t.name);
|
|
39
38
|
const typeNames = (types) => types.map((t) => t.args?.[0]);
|
|
40
39
|
const TRUE_TERM = `literal('true', datatype('${XSD}boolean'))`;
|
|
@@ -362,8 +361,9 @@ export function compileShacl(quads, { disallows = SHACL_DISALLOWS, needs = new S
|
|
|
362
361
|
// The LS regex primitive uses ECMAScript Unicode syntax. Translate the
|
|
363
362
|
// portable SHACL flags at compilation, without executing input patterns.
|
|
364
363
|
const f = flags?.args[0] ?? '';
|
|
365
|
-
|
|
366
|
-
if (f.includes('
|
|
364
|
+
// x drops whitespace and s lets a dot match newlines, outside classes.
|
|
365
|
+
if (f.includes('x')) expression = rewritePattern(expression, (c) => /[ \t\r\n]/.test(c) ? '' : c);
|
|
366
|
+
if (f.includes('s')) expression = rewritePattern(expression, (c) => c === '.' ? '[\\s\\S]' : c);
|
|
367
367
|
if (f.includes('m')) error(s, 'uses sh:flags m, which is not supported by the LS regex primitive');
|
|
368
368
|
if (f.includes('i')) expression = caseInsensitivePattern(expression);
|
|
369
369
|
try { new RegExp(expression, 'u'); } catch { error(s, 'has an invalid or unsupported regular expression'); }
|
|
@@ -523,27 +523,16 @@ export function compileShacl(quads, { disallows = SHACL_DISALLOWS, needs = new S
|
|
|
523
523
|
}
|
|
524
524
|
|
|
525
525
|
// Flag translation is deliberately conservative: unsupported regex syntax is
|
|
526
|
-
// rejected rather than changing a constraint's meaning silently.
|
|
527
|
-
|
|
526
|
+
// rejected rather than changing a constraint's meaning silently. This
|
|
527
|
+
// rewrites each character of a pattern outside escapes and character classes.
|
|
528
|
+
function rewritePattern(pattern, rewrite) {
|
|
528
529
|
let out = '', escaped = false, inClass = false;
|
|
529
530
|
for (const c of pattern) {
|
|
530
531
|
if (escaped) { out += '\\' + c; escaped = false; continue; }
|
|
531
532
|
if (c === '\\') { escaped = true; continue; }
|
|
532
533
|
if (c === '[') inClass = true;
|
|
533
534
|
if (c === ']') inClass = false;
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
return out + (escaped ? '\\' : '');
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function dotAllPattern(pattern) {
|
|
540
|
-
let out = '', escaped = false, inClass = false;
|
|
541
|
-
for (const c of pattern) {
|
|
542
|
-
if (escaped) { out += '\\' + c; escaped = false; continue; }
|
|
543
|
-
if (c === '\\') { escaped = true; continue; }
|
|
544
|
-
if (c === '[') inClass = true;
|
|
545
|
-
if (c === ']') inClass = false;
|
|
546
|
-
out += c === '.' && !inClass ? '[\\s\\S]' : c;
|
|
535
|
+
out += inClass ? c : rewrite(c);
|
|
547
536
|
}
|
|
548
537
|
return out + (escaped ? '\\' : '');
|
|
549
538
|
}
|
package/src/srl-analyze.js
CHANGED
|
@@ -595,28 +595,6 @@ function boundVariables(clauses, initialBound = new Set()) {
|
|
|
595
595
|
return vars;
|
|
596
596
|
}
|
|
597
597
|
|
|
598
|
-
function bodyVariables(clauses) {
|
|
599
|
-
const vars = new Set();
|
|
600
|
-
for (const clause of clauses) {
|
|
601
|
-
if (clause.type === 'triple' || clause.type === 'path') collectTripleVars(clause.triple, vars);
|
|
602
|
-
if ((clause.type === 'set' || clause.type === 'bind')) {
|
|
603
|
-
vars.add(clause.variable);
|
|
604
|
-
for (const v of expressionVariables(clause.expr)) vars.add(v);
|
|
605
|
-
}
|
|
606
|
-
if (clause.type === 'filter') for (const v of expressionVariables(clause.expr)) vars.add(v);
|
|
607
|
-
if (clause.type === 'not') for (const v of bodyVariables(clause.body)) vars.add(v);
|
|
608
|
-
if (clause.type === 'group') {
|
|
609
|
-
for (const key of clause.keys) vars.add(key);
|
|
610
|
-
for (const aggregation of clause.aggregations) {
|
|
611
|
-
vars.add(aggregation.variable);
|
|
612
|
-
if (aggregation.function.argument !== '*') vars.add(aggregation.function.argument);
|
|
613
|
-
}
|
|
614
|
-
for (const v of bodyVariables(clause.pattern)) vars.add(v);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
return vars;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
598
|
function collectTripleVars(triple, vars) {
|
|
621
599
|
for (const term of [triple.s, triple.p, triple.o]) collectTermVars(term, vars);
|
|
622
600
|
}
|
package/src/srl-term.js
CHANGED
|
@@ -5,7 +5,6 @@ const RDF_FIRST = `${RDF_NS}first`;
|
|
|
5
5
|
const RDF_REST = `${RDF_NS}rest`;
|
|
6
6
|
const RDF_NIL = `${RDF_NS}nil`;
|
|
7
7
|
const RDF_REIFIES = `${RDF_NS}reifies`;
|
|
8
|
-
const XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
|
|
9
8
|
const XSD_BOOLEAN = 'http://www.w3.org/2001/XMLSchema#boolean';
|
|
10
9
|
const XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
|
|
11
10
|
const XSD_DECIMAL = 'http://www.w3.org/2001/XMLSchema#decimal';
|
|
@@ -64,21 +63,6 @@ function termKey(term) {
|
|
|
64
63
|
return JSON.stringify(term);
|
|
65
64
|
}
|
|
66
65
|
|
|
67
|
-
function cloneTerm(term) {
|
|
68
|
-
if (!term) return term;
|
|
69
|
-
if (term.type === 'triple') return tripleTerm(cloneTerm(term.s), cloneTerm(term.p), cloneTerm(term.o));
|
|
70
|
-
return { ...term };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function escapeString(value) {
|
|
74
|
-
return String(value)
|
|
75
|
-
.replace(/\\/g, '\\\\')
|
|
76
|
-
.replace(/\n/g, '\\n')
|
|
77
|
-
.replace(/\r/g, '\\r')
|
|
78
|
-
.replace(/\t/g, '\\t')
|
|
79
|
-
.replace(/"/g, '\\"');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
66
|
function compactIRI(value, prefixes = {}) {
|
|
83
67
|
if (value === RDF_TYPE) return 'a';
|
|
84
68
|
const entries = Object.entries(prefixes)
|
|
@@ -94,42 +78,6 @@ function compactIRI(value, prefixes = {}) {
|
|
|
94
78
|
return `<${value}>`;
|
|
95
79
|
}
|
|
96
80
|
|
|
97
|
-
// A number may drop its datatype only when the shorthand reads back as the
|
|
98
|
-
// same datatype: an INTEGER carries neither `.` nor exponent, a DECIMAL a
|
|
99
|
-
// `.`, a DOUBLE an exponent ([95]-[97]). `72` written bare for an
|
|
100
|
-
// xsd:double would come back as an xsd:integer, so such a literal keeps
|
|
101
|
-
// its datatype; a whole-numbered decimal gains the `.0` that makes it one.
|
|
102
|
-
function numericShorthand(value, datatype) {
|
|
103
|
-
const text = String(value);
|
|
104
|
-
const hasExponent = /[eE]/.test(text);
|
|
105
|
-
const hasDot = text.includes('.');
|
|
106
|
-
if (!datatype || datatype === XSD_INTEGER) return !hasDot && !hasExponent ? text : null;
|
|
107
|
-
if (datatype === XSD_DECIMAL) return hasExponent ? null : (hasDot ? text : `${text}.0`);
|
|
108
|
-
if (datatype === XSD_DOUBLE) return hasExponent ? text : null;
|
|
109
|
-
return null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function formatTerm(term, prefixes = {}) {
|
|
113
|
-
if (term.type === 'iri') return compactIRI(term.value, prefixes);
|
|
114
|
-
if (term.type === 'blank') return `_:${term.value}`;
|
|
115
|
-
if (term.type === 'var') return `?${term.value}`;
|
|
116
|
-
if (term.type === 'triple') return `<<(${formatTerm(term.s, prefixes)} ${formatTerm(term.p, prefixes)} ${formatTerm(term.o, prefixes)})>>`;
|
|
117
|
-
if (term.type === 'literal') {
|
|
118
|
-
const v = term.value;
|
|
119
|
-
if (typeof v === 'bigint' && !term.lang && (!term.datatype || term.datatype === XSD_INTEGER)) return String(v);
|
|
120
|
-
if (typeof v === 'number' && Number.isFinite(v) && !term.lang) {
|
|
121
|
-
const shorthand = numericShorthand(v, term.datatype);
|
|
122
|
-
if (shorthand !== null) return shorthand;
|
|
123
|
-
}
|
|
124
|
-
if (typeof v === 'boolean' && !term.lang && (!term.datatype || term.datatype === XSD_BOOLEAN)) return v ? 'true' : 'false';
|
|
125
|
-
const lexical = `"${escapeString(v)}"`;
|
|
126
|
-
if (term.lang) return `${lexical}@${term.lang}${term.langDir ? `--${term.langDir}` : ''}`;
|
|
127
|
-
if (term.datatype && term.datatype !== XSD_STRING) return `${lexical}^^${compactIRI(term.datatype, prefixes)}`;
|
|
128
|
-
return lexical;
|
|
129
|
-
}
|
|
130
|
-
return String(term.value ?? term);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
81
|
export {
|
|
134
82
|
termKey,
|
|
135
83
|
RDF_NS,
|
|
@@ -138,7 +86,6 @@ export {
|
|
|
138
86
|
RDF_REST,
|
|
139
87
|
RDF_NIL,
|
|
140
88
|
RDF_REIFIES,
|
|
141
|
-
XSD_STRING,
|
|
142
89
|
XSD_BOOLEAN,
|
|
143
90
|
XSD_INTEGER,
|
|
144
91
|
XSD_DECIMAL,
|
package/src/srl-tokenizer.js
CHANGED
|
@@ -76,21 +76,24 @@ function tokenize(source, filenameOrOptions = '<input>') {
|
|
|
76
76
|
return value;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
// The character of a \u or \U escape, whose hex digits follow.
|
|
80
|
+
function readHexEscape(esc, startLine, startColumn) {
|
|
81
|
+
const length = esc === 'u' ? 4 : 8;
|
|
82
|
+
let hex = '';
|
|
83
|
+
for (let j = 0; j < length; j += 1) {
|
|
84
|
+
if (!isHexCode(source.charCodeAt(i))) syntax(`Invalid \\${esc} escape`, startLine, startColumn);
|
|
85
|
+
hex += advance();
|
|
86
|
+
}
|
|
87
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
88
|
+
if (codePoint >= 0xD800 && codePoint <= 0xDFFF) syntax(`Invalid \\${esc} escape: surrogate code point`, startLine, startColumn);
|
|
89
|
+
try { return String.fromCodePoint(codePoint); }
|
|
90
|
+
catch { syntax(`Invalid \\${esc} escape`, startLine, startColumn); }
|
|
91
|
+
}
|
|
92
|
+
|
|
79
93
|
function readEscape(startLine, startColumn) {
|
|
80
94
|
advance(); // consume backslash
|
|
81
95
|
const esc = advance();
|
|
82
|
-
if (esc === 'u' || esc === 'U')
|
|
83
|
-
const length = esc === 'u' ? 4 : 8;
|
|
84
|
-
let hex = '';
|
|
85
|
-
for (let j = 0; j < length; j += 1) {
|
|
86
|
-
if (!isHexCode(source.charCodeAt(i))) syntax(`Invalid \\${esc} escape`, startLine, startColumn);
|
|
87
|
-
hex += advance();
|
|
88
|
-
}
|
|
89
|
-
const codePoint = Number.parseInt(hex, 16);
|
|
90
|
-
if (codePoint >= 0xD800 && codePoint <= 0xDFFF) syntax(`Invalid \\${esc} escape: surrogate code point`, startLine, startColumn);
|
|
91
|
-
try { return String.fromCodePoint(codePoint); }
|
|
92
|
-
catch { syntax(`Invalid \\${esc} escape`, startLine, startColumn); }
|
|
93
|
-
}
|
|
96
|
+
if (esc === 'u' || esc === 'U') return readHexEscape(esc, startLine, startColumn);
|
|
94
97
|
if (strictGrammar && !Object.hasOwn(escapeMap, esc)) syntax(`Invalid escape \\${esc}`, startLine, startColumn);
|
|
95
98
|
return escapeValue(esc);
|
|
96
99
|
}
|
|
@@ -100,16 +103,7 @@ function tokenize(source, filenameOrOptions = '<input>') {
|
|
|
100
103
|
advance();
|
|
101
104
|
const esc = advance();
|
|
102
105
|
if (esc !== 'u' && esc !== 'U') syntax(`Invalid IRI escape \\${esc}`, startLine, startColumn);
|
|
103
|
-
|
|
104
|
-
let hex = '';
|
|
105
|
-
for (let j = 0; j < length; j += 1) {
|
|
106
|
-
if (!isHexCode(source.charCodeAt(i))) syntax(`Invalid \\${esc} escape`, startLine, startColumn);
|
|
107
|
-
hex += advance();
|
|
108
|
-
}
|
|
109
|
-
const codePoint = Number.parseInt(hex, 16);
|
|
110
|
-
if (codePoint >= 0xD800 && codePoint <= 0xDFFF) syntax(`Invalid \\${esc} escape: surrogate code point`, startLine, startColumn);
|
|
111
|
-
try { return String.fromCodePoint(codePoint); }
|
|
112
|
-
catch { syntax(`Invalid \\${esc} escape`, startLine, startColumn); }
|
|
106
|
+
return readHexEscape(esc, startLine, startColumn);
|
|
113
107
|
}
|
|
114
108
|
const c = current();
|
|
115
109
|
if (strictGrammar && (/[\u0000-\u0020]/.test(c) || /[<>"{}|^`]/.test(c))) syntax(`Invalid character in IRI reference ${JSON.stringify(c)}`, startLine, startColumn);
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// The release this checkout is; ./release keeps it in step with package.json.
|
|
2
|
-
export const VERSION = '0.9.
|
|
2
|
+
export const VERSION = '0.9.18';
|