n3 3.0.0-alpha.4 → 3.0.0-alpha.6
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 +56 -0
- package/browser/n3.esm.min.js +15 -16
- package/browser/n3.min.js +15 -16
- package/lib/N3Lexer.js +255 -160
- package/lib/N3Parser.js +70 -32
- package/lib/N3Reasoner.js +1 -1
- package/lib/N3Store.js +50 -6
- package/lib/N3StreamParser.js +4 -0
- package/lib/N3Writer.js +65 -19
- package/lib/Util.js +1 -1
- package/package.json +2 -29
- package/src/N3Lexer.js +269 -171
- package/src/N3Parser.js +81 -32
- package/src/N3Reasoner.js +1 -1
- package/src/N3Store.js +61 -9
- package/src/N3StreamParser.js +2 -0
- package/src/N3Writer.js +70 -23
- package/src/Util.js +1 -1
package/README.md
CHANGED
|
@@ -158,6 +158,20 @@ const quads = parser.parse('<a> <b> "hello"@en.', {
|
|
|
158
158
|
});
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
Packages that extend a format with their own directives can register their names with the `directives` option.
|
|
162
|
+
Between statements (outside graph blocks and formulas), a registered directive is accepted
|
|
163
|
+
as `@name .` in Turtle, TriG and N3, and as `NAME` (case-insensitive) in all formats, including N-Triples and N-Quads.
|
|
164
|
+
`onDirective(name)` receives each one in order with the quads, and `N3.StreamParser` emits them as `directive` events.
|
|
165
|
+
Names consist of letters only, and cannot be words that already have a meaning in the grammar, such as `prefix` or `a`.
|
|
166
|
+
|
|
167
|
+
```JavaScript
|
|
168
|
+
const parser = new N3.Parser({ format: 'N-Quads', directives: ['message'] });
|
|
169
|
+
parser.parse('<a> <b> <c> .\nMESSAGE\n<a> <b> <d> .\n', {
|
|
170
|
+
onQuad: (error, quad) => { /* … */ },
|
|
171
|
+
onDirective: name => { console.log('end of a message'); },
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
161
175
|
If no callbacks are provided, parsing happens synchronously returning an array of quads:
|
|
162
176
|
|
|
163
177
|
```JavaScript
|
|
@@ -234,6 +248,13 @@ of also rescoping blank node labels in lists and blank node property lists:
|
|
|
234
248
|
const parser = new N3.Parser({ format: 'N3', formulaScopedBlankNodes: false });
|
|
235
249
|
```
|
|
236
250
|
|
|
251
|
+
The parser rejects triple terms nested more than 1024 levels deep,
|
|
252
|
+
so that the resulting quads stay within what the store and writer can traverse.
|
|
253
|
+
The `maxTripleTermDepth` option changes this limit (`Infinity` removes it):
|
|
254
|
+
```JavaScript
|
|
255
|
+
const parser = new N3.Parser({ maxTripleTermDepth: 64 });
|
|
256
|
+
```
|
|
257
|
+
|
|
237
258
|
### From an RDF stream to quads
|
|
238
259
|
|
|
239
260
|
`N3.Parser` can parse [Node.js streams](http://nodejs.org/api/stream.html) as they grow,
|
|
@@ -335,6 +356,30 @@ A `baseIRI` argument makes the writer abbreviate IRIs relative to that base in T
|
|
|
335
356
|
const writer = new N3.Writer({ baseIRI: 'http://example.org/', writeBase: false });
|
|
336
357
|
```
|
|
337
358
|
|
|
359
|
+
A `version` argument writes an RDF 1.2 version directive at the top of the document,
|
|
360
|
+
as `@version "1.2".` in Turtle/TriG or `VERSION "1.2"` in N-Triples/N-Quads.
|
|
361
|
+
The constructor throws for a label that contains quotes, backslashes, control characters, or unpaired surrogates.
|
|
362
|
+
|
|
363
|
+
```JavaScript
|
|
364
|
+
const writer = new N3.Writer({ version: '1.2' });
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Packages that add their own syntax can extend `N3.Writer`.
|
|
368
|
+
`this._endStatement()` finishes the pending statement (and closes an open graph block),
|
|
369
|
+
`this._write(text, done)` writes text to the output,
|
|
370
|
+
and `this._lineMode` tells whether the output is N-Triples or N-Quads.
|
|
371
|
+
|
|
372
|
+
```JavaScript
|
|
373
|
+
class GroupWriter extends N3.Writer {
|
|
374
|
+
addGroup(quads, done) {
|
|
375
|
+
for (const quad of quads)
|
|
376
|
+
this.addQuad(quad);
|
|
377
|
+
this._endStatement();
|
|
378
|
+
this._write(this._lineMode ? 'GROUP\n' : '@group .\n', done);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
338
383
|
### From quads to an RDF stream
|
|
339
384
|
|
|
340
385
|
`N3.Writer` can also write quads to a Node.js stream through `addQuad`.
|
|
@@ -566,6 +611,17 @@ Both budgets are unbounded by default;
|
|
|
566
611
|
`reason()` throws when one is exceeded,
|
|
567
612
|
leaving any quads derived up to that point in the store.
|
|
568
613
|
|
|
614
|
+
## Extensions
|
|
615
|
+
The following packages build on N3.js to support formats or features beyond the W3C specifications listed under [Compatibility](#compatibility).
|
|
616
|
+
They are maintained separately from N3.js; please report issues with them in their own repositories.
|
|
617
|
+
|
|
618
|
+
| Package | Description |
|
|
619
|
+
| ------- | ----------- |
|
|
620
|
+
| [n3.js-messages](https://www.npmjs.com/package/n3.js-messages) | Parses and writes [RDF Messages](https://w3c-cg.github.io/rsp/spec/messages) (message-delimited Turtle, TriG, N-Triples and N-Quads). |
|
|
621
|
+
| [@jeswr/n3-provenance](https://github.com/jeswr/n3-provenance) | Experimental. Maps the terms of parsed quads back to their source positions in the input document. Terms that the parser generates, such as list nodes and reification scaffolding, have no position. It builds on the parser's `onToken` and `onTokenEnd` callbacks, but also subclasses private parser and index internals, so it is pinned to a specific N3.js revision. |
|
|
622
|
+
|
|
623
|
+
To list a package here, open a pull request that adds a row to this table.
|
|
624
|
+
|
|
569
625
|
## Compatibility
|
|
570
626
|
### Format specifications
|
|
571
627
|
The N3.js parser and writer is fully compatible with the following W3C specifications:
|