n3 2.2.12 → 2.2.13

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/lib/N3Parser.js CHANGED
@@ -36,6 +36,9 @@ class N3Parser {
36
36
  this._supportsQuads = !(isTurtle || isTriG || isNTriples || isN3);
37
37
  // Whether the log:isImpliedBy predicate is supported
38
38
  this._isImpliedBy = options.isImpliedBy;
39
+ // Whether an empty formula is read as the boolean literal true,
40
+ // as in the N3 spec tests (opt-in until the next major version)
41
+ this._emptyFormulaAsTrue = !!options.emptyFormulaAsTrue;
39
42
  // Disable relative IRIs in N-Triples or N-Quads mode
40
43
  if (isLineMode) this._resolveRelativeIRI = iri => {
41
44
  return null;
@@ -92,7 +95,8 @@ class N3Parser {
92
95
  graph,
93
96
  inverse: n3Mode ? this._inversePredicate : false,
94
97
  blankPrefix: n3Mode ? this._prefixes._ : '',
95
- quantified: n3Mode ? this._quantified : null
98
+ quantified: n3Mode ? this._quantified : null,
99
+ emptyFormula: n3Mode ? this._emptyFormula : false
96
100
  });
97
101
  // The settings below only apply to N3 streams
98
102
  if (n3Mode) {
@@ -103,6 +107,13 @@ class N3Parser {
103
107
  this._prefixes._ = this._graph ? `${this._graph.value}.` : '.';
104
108
  // Quantifiers are scoped to a formula
105
109
  this._quantified = Object.create(this._quantified);
110
+ // A formula starts a new statement, so the subject of the
111
+ // enclosing statement must not leak into it,
112
+ // and it is empty until a statement has been read
113
+ if (type === 'formula') {
114
+ this._subject = null;
115
+ this._emptyFormula = true;
116
+ }
106
117
  }
107
118
  }
108
119
 
@@ -124,6 +135,7 @@ class N3Parser {
124
135
  this._inversePredicate = context.inverse;
125
136
  this._prefixes._ = context.blankPrefix;
126
137
  this._quantified = context.quantified;
138
+ this._emptyFormula = context.emptyFormula;
127
139
  }
128
140
  }
129
141
 
@@ -209,6 +221,8 @@ class N3Parser {
209
221
  // ### `_readSubject` reads a quad's subject
210
222
  _readSubject(token) {
211
223
  this._predicate = null;
224
+ // Any statement token means the enclosing formula is not empty
225
+ if (token.type !== '}') this._emptyFormula = false;
212
226
  switch (token.type) {
213
227
  case '[':
214
228
  // Start a new quad with a new blank node as subject
@@ -710,9 +724,18 @@ class N3Parser {
710
724
 
711
725
  // Store the last quad of the formula
712
726
  if (this._subject !== null) this._emit(this._subject, this._predicate, this._object, this._graph);
713
-
727
+ const formula = this._graph,
728
+ empty = this._emptyFormula;
714
729
  // Restore the parent context containing this formula
715
730
  this._restoreContext('formula', token);
731
+
732
+ // When the emptyFormulaAsTrue option is set, an empty formula
733
+ // is read as the boolean literal true, following the N3 spec tests
734
+ // and the direction discussed in https://github.com/w3c-cg/N3/issues/185
735
+ if (empty && this._emptyFormulaAsTrue) {
736
+ if (this._subject === formula) this._subject = this.N3_TRUE;else this._object = this.N3_TRUE;
737
+ }
738
+
716
739
  // If the formula was the subject, continue reading the predicate.
717
740
  // If the formula was the object, read punctuation.
718
741
  return this._object === null ? this._readPredicate : this._getContextEndReader();
@@ -1293,6 +1316,7 @@ class N3Parser {
1293
1316
  this._versionCallback = onVersion || noop;
1294
1317
  this._inversePredicate = false;
1295
1318
  this._quantified = Object.create(null);
1319
+ this._emptyFormula = false;
1296
1320
 
1297
1321
  // Parse synchronously if no quad callback is given
1298
1322
  if (!onQuad) {
@@ -1345,6 +1369,7 @@ function initDataFactory(parser, factory) {
1345
1369
  parser.RDF_REIFIES = factory.namedNode(_IRIs.default.rdf.reifies);
1346
1370
  parser.N3_FORALL = factory.namedNode(_IRIs.default.r.forAll);
1347
1371
  parser.N3_FORSOME = factory.namedNode(_IRIs.default.r.forSome);
1372
+ parser.N3_TRUE = factory.literal('true', factory.namedNode(_IRIs.default.xsd.boolean));
1348
1373
  parser.ABBREVIATIONS = {
1349
1374
  'a': factory.namedNode(_IRIs.default.rdf.type),
1350
1375
  '=': factory.namedNode(_IRIs.default.owl.sameAs),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.2.12",
3
+ "version": "2.2.13",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Parser.js CHANGED
@@ -29,6 +29,9 @@ export default class N3Parser {
29
29
  this._supportsQuads = !(isTurtle || isTriG || isNTriples || isN3);
30
30
  // Whether the log:isImpliedBy predicate is supported
31
31
  this._isImpliedBy = options.isImpliedBy;
32
+ // Whether an empty formula is read as the boolean literal true,
33
+ // as in the N3 spec tests (opt-in until the next major version)
34
+ this._emptyFormulaAsTrue = !!options.emptyFormulaAsTrue;
32
35
  // Disable relative IRIs in N-Triples or N-Quads mode
33
36
  if (isLineMode)
34
37
  this._resolveRelativeIRI = iri => { return null; };
@@ -82,6 +85,7 @@ export default class N3Parser {
82
85
  inverse: n3Mode ? this._inversePredicate : false,
83
86
  blankPrefix: n3Mode ? this._prefixes._ : '',
84
87
  quantified: n3Mode ? this._quantified : null,
88
+ emptyFormula: n3Mode ? this._emptyFormula : false,
85
89
  });
86
90
  // The settings below only apply to N3 streams
87
91
  if (n3Mode) {
@@ -92,6 +96,13 @@ export default class N3Parser {
92
96
  this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
93
97
  // Quantifiers are scoped to a formula
94
98
  this._quantified = Object.create(this._quantified);
99
+ // A formula starts a new statement, so the subject of the
100
+ // enclosing statement must not leak into it,
101
+ // and it is empty until a statement has been read
102
+ if (type === 'formula') {
103
+ this._subject = null;
104
+ this._emptyFormula = true;
105
+ }
95
106
  }
96
107
  }
97
108
 
@@ -114,6 +125,7 @@ export default class N3Parser {
114
125
  this._inversePredicate = context.inverse;
115
126
  this._prefixes._ = context.blankPrefix;
116
127
  this._quantified = context.quantified;
128
+ this._emptyFormula = context.emptyFormula;
117
129
  }
118
130
  }
119
131
 
@@ -205,6 +217,9 @@ export default class N3Parser {
205
217
  // ### `_readSubject` reads a quad's subject
206
218
  _readSubject(token) {
207
219
  this._predicate = null;
220
+ // Any statement token means the enclosing formula is not empty
221
+ if (token.type !== '}')
222
+ this._emptyFormula = false;
208
223
  switch (token.type) {
209
224
  case '[':
210
225
  // Start a new quad with a new blank node as subject
@@ -774,8 +789,20 @@ export default class N3Parser {
774
789
  if (this._subject !== null)
775
790
  this._emit(this._subject, this._predicate, this._object, this._graph);
776
791
 
792
+ const formula = this._graph, empty = this._emptyFormula;
777
793
  // Restore the parent context containing this formula
778
794
  this._restoreContext('formula', token);
795
+
796
+ // When the emptyFormulaAsTrue option is set, an empty formula
797
+ // is read as the boolean literal true, following the N3 spec tests
798
+ // and the direction discussed in https://github.com/w3c-cg/N3/issues/185
799
+ if (empty && this._emptyFormulaAsTrue) {
800
+ if (this._subject === formula)
801
+ this._subject = this.N3_TRUE;
802
+ else
803
+ this._object = this.N3_TRUE;
804
+ }
805
+
779
806
  // If the formula was the subject, continue reading the predicate.
780
807
  // If the formula was the object, read punctuation.
781
808
  return this._object === null ? this._readPredicate : this._getContextEndReader();
@@ -1392,6 +1419,7 @@ export default class N3Parser {
1392
1419
  this._versionCallback = onVersion || noop;
1393
1420
  this._inversePredicate = false;
1394
1421
  this._quantified = Object.create(null);
1422
+ this._emptyFormula = false;
1395
1423
 
1396
1424
  // Parse synchronously if no quad callback is given
1397
1425
  if (!onQuad) {
@@ -1451,6 +1479,7 @@ function initDataFactory(parser, factory) {
1451
1479
  parser.RDF_REIFIES = factory.namedNode(namespaces.rdf.reifies);
1452
1480
  parser.N3_FORALL = factory.namedNode(namespaces.r.forAll);
1453
1481
  parser.N3_FORSOME = factory.namedNode(namespaces.r.forSome);
1482
+ parser.N3_TRUE = factory.literal('true', factory.namedNode(namespaces.xsd.boolean));
1454
1483
  parser.ABBREVIATIONS = {
1455
1484
  'a': factory.namedNode(namespaces.rdf.type),
1456
1485
  '=': factory.namedNode(namespaces.owl.sameAs),