n3 2.10.1 → 2.10.3
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/README.md +10 -0
- package/browser/n3.esm.min.js +3 -3
- package/browser/n3.min.js +3 -3
- package/lib/N3Parser.js +11 -5
- package/lib/N3Writer.js +19 -3
- package/package.json +1 -1
- package/src/N3Parser.js +11 -5
- package/src/N3Writer.js +20 -4
package/lib/N3Parser.js
CHANGED
|
@@ -60,6 +60,8 @@ class N3Parser {
|
|
|
60
60
|
});
|
|
61
61
|
// Disable explicit quantifiers by default
|
|
62
62
|
this._explicitQuantifiers = !!options.explicitQuantifiers;
|
|
63
|
+
// Disable formula-only blank node scoping by default
|
|
64
|
+
this._formulaScopedBlankNodes = !!options.formulaScopedBlankNodes;
|
|
63
65
|
// Disable parsing of unsupported versions by default
|
|
64
66
|
this._parseUnsupportedVersions = !!options.parseUnsupportedVersions;
|
|
65
67
|
this._version = options.version;
|
|
@@ -128,11 +130,15 @@ class N3Parser {
|
|
|
128
130
|
// Every new scope resets the predicate direction
|
|
129
131
|
this._inversePredicate = false;
|
|
130
132
|
this._expectOf = false;
|
|
131
|
-
// In N3, blank nodes are scoped to a formula
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
133
|
+
// In N3, blank nodes and quantifiers are scoped to a formula;
|
|
134
|
+
// with `formulaScopedBlankNodes`, lists and blank node property lists
|
|
135
|
+
// share the enclosing scope instead of rescoping
|
|
136
|
+
if (!this._formulaScopedBlankNodes || type === 'formula') {
|
|
137
|
+
// Label the scope with the enclosing formula's blank node
|
|
138
|
+
// (using a dot as separator, as a blank node label cannot start with it)
|
|
139
|
+
this._prefixes._ = this._graph ? `${this._graph.value}.` : '.';
|
|
140
|
+
this._quantified = Object.create(this._quantified);
|
|
141
|
+
}
|
|
136
142
|
// A formula starts empty and must not inherit its parent's subject
|
|
137
143
|
if (type === 'formula') {
|
|
138
144
|
this._subject = null;
|
package/lib/N3Writer.js
CHANGED
|
@@ -135,7 +135,7 @@ class N3Writer {
|
|
|
135
135
|
|
|
136
136
|
// ### `quadToString` serializes a quad as a string
|
|
137
137
|
quadToString(subject, predicate, object, graph) {
|
|
138
|
-
return `${this._encodeSubject(subject)} ${this._encodeIriOrBlank(predicate)} ${this._encodeObject(object)}${graph && !(0, _N3Util.isDefaultGraph)(graph) ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
|
|
138
|
+
return `${this._encodeSubject(subject)} ${predicate.termType === 'Literal' ? this._encodeLiteral(predicate) : this._encodeIriOrBlank(predicate)} ${this._encodeObject(object)}${graph && !(0, _N3Util.isDefaultGraph)(graph) ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
// ### `quadsToString` serializes an array of quads as a string
|
|
@@ -147,7 +147,15 @@ class N3Writer {
|
|
|
147
147
|
|
|
148
148
|
// ### `_encodeSubject` represents a subject
|
|
149
149
|
_encodeSubject(entity) {
|
|
150
|
-
|
|
150
|
+
switch (entity.termType) {
|
|
151
|
+
case 'Quad':
|
|
152
|
+
return this._encodeQuad(entity);
|
|
153
|
+
// Literal subjects are only valid in N3
|
|
154
|
+
case 'Literal':
|
|
155
|
+
return this._encodeLiteral(entity);
|
|
156
|
+
default:
|
|
157
|
+
return this._encodeIriOrBlank(entity);
|
|
158
|
+
}
|
|
151
159
|
}
|
|
152
160
|
|
|
153
161
|
// ### `_encodeIriOrBlank` represents an IRI or blank node
|
|
@@ -214,7 +222,15 @@ class N3Writer {
|
|
|
214
222
|
|
|
215
223
|
// ### `_encodePredicate` represents a predicate
|
|
216
224
|
_encodePredicate(predicate) {
|
|
217
|
-
|
|
225
|
+
switch (predicate.termType) {
|
|
226
|
+
case 'NamedNode':
|
|
227
|
+
return predicate.value === rdf.type ? 'a' : this._encodeIriOrBlank(predicate);
|
|
228
|
+
// Literal predicates are only valid in N3
|
|
229
|
+
case 'Literal':
|
|
230
|
+
return this._encodeLiteral(predicate);
|
|
231
|
+
default:
|
|
232
|
+
return this._encodeIriOrBlank(predicate);
|
|
233
|
+
}
|
|
218
234
|
}
|
|
219
235
|
|
|
220
236
|
// ### `_encodeObject` represents an object
|
package/package.json
CHANGED
package/src/N3Parser.js
CHANGED
|
@@ -49,6 +49,8 @@ export default class N3Parser {
|
|
|
49
49
|
this._lexer = options.lexer || new N3Lexer({ lineMode: isLineMode, n3: isN3, isImpliedBy: this._isImpliedBy });
|
|
50
50
|
// Disable explicit quantifiers by default
|
|
51
51
|
this._explicitQuantifiers = !!options.explicitQuantifiers;
|
|
52
|
+
// Disable formula-only blank node scoping by default
|
|
53
|
+
this._formulaScopedBlankNodes = !!options.formulaScopedBlankNodes;
|
|
52
54
|
// Disable parsing of unsupported versions by default
|
|
53
55
|
this._parseUnsupportedVersions = !!options.parseUnsupportedVersions;
|
|
54
56
|
this._version = options.version;
|
|
@@ -111,11 +113,15 @@ export default class N3Parser {
|
|
|
111
113
|
// Every new scope resets the predicate direction
|
|
112
114
|
this._inversePredicate = false;
|
|
113
115
|
this._expectOf = false;
|
|
114
|
-
// In N3, blank nodes are scoped to a formula
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
// In N3, blank nodes and quantifiers are scoped to a formula;
|
|
117
|
+
// with `formulaScopedBlankNodes`, lists and blank node property lists
|
|
118
|
+
// share the enclosing scope instead of rescoping
|
|
119
|
+
if (!this._formulaScopedBlankNodes || type === 'formula') {
|
|
120
|
+
// Label the scope with the enclosing formula's blank node
|
|
121
|
+
// (using a dot as separator, as a blank node label cannot start with it)
|
|
122
|
+
this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
|
|
123
|
+
this._quantified = Object.create(this._quantified);
|
|
124
|
+
}
|
|
119
125
|
// A formula starts empty and must not inherit its parent's subject
|
|
120
126
|
if (type === 'formula') {
|
|
121
127
|
this._subject = null;
|
package/src/N3Writer.js
CHANGED
|
@@ -129,7 +129,8 @@ export default class N3Writer {
|
|
|
129
129
|
// ### `quadToString` serializes a quad as a string
|
|
130
130
|
quadToString(subject, predicate, object, graph) {
|
|
131
131
|
return `${this._encodeSubject(subject)} ${
|
|
132
|
-
|
|
132
|
+
predicate.termType === 'Literal' ?
|
|
133
|
+
this._encodeLiteral(predicate) : this._encodeIriOrBlank(predicate)} ${
|
|
133
134
|
this._encodeObject(object)
|
|
134
135
|
}${graph && !isDefaultGraph(graph) ? ` ${this._encodeIriOrBlank(graph)} .\n` : ' .\n'}`;
|
|
135
136
|
}
|
|
@@ -144,8 +145,15 @@ export default class N3Writer {
|
|
|
144
145
|
|
|
145
146
|
// ### `_encodeSubject` represents a subject
|
|
146
147
|
_encodeSubject(entity) {
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
switch (entity.termType) {
|
|
149
|
+
case 'Quad':
|
|
150
|
+
return this._encodeQuad(entity);
|
|
151
|
+
// Literal subjects are only valid in N3
|
|
152
|
+
case 'Literal':
|
|
153
|
+
return this._encodeLiteral(entity);
|
|
154
|
+
default:
|
|
155
|
+
return this._encodeIriOrBlank(entity);
|
|
156
|
+
}
|
|
149
157
|
}
|
|
150
158
|
|
|
151
159
|
// ### `_encodeIriOrBlank` represents an IRI or blank node
|
|
@@ -223,7 +231,15 @@ export default class N3Writer {
|
|
|
223
231
|
|
|
224
232
|
// ### `_encodePredicate` represents a predicate
|
|
225
233
|
_encodePredicate(predicate) {
|
|
226
|
-
|
|
234
|
+
switch (predicate.termType) {
|
|
235
|
+
case 'NamedNode':
|
|
236
|
+
return predicate.value === rdf.type ? 'a' : this._encodeIriOrBlank(predicate);
|
|
237
|
+
// Literal predicates are only valid in N3
|
|
238
|
+
case 'Literal':
|
|
239
|
+
return this._encodeLiteral(predicate);
|
|
240
|
+
default:
|
|
241
|
+
return this._encodeIriOrBlank(predicate);
|
|
242
|
+
}
|
|
227
243
|
}
|
|
228
244
|
|
|
229
245
|
// ### `_encodeObject` represents an object
|