Debian Codegraph @

test/N3Parser-test.js

raw
1 import { Parser, NamedNode, BlankNode, Quad, termFromId } from '../src/';
2
3 const BASE_IRI = 'http://example.org/';
4
5 // Reset blank node identifiers between tests
6 let blankId;
7 beforeEach(() => {
8 blankId = 0; // reset per-node ID
9 Parser._resetBlankNodePrefix(); // reset per-parser ID
10 });
11 Parser.prototype._blankNode = name => new BlankNode(name || `b${blankId++}`);
12
13 describe('Parser', () => {
14 describe('The Parser export', () => {
15 it('should be a function', () => {
16 Parser.should.be.a('function');
17 });
18
19 it('should be a Parser constructor', () => {
20 new Parser().should.be.an.instanceof(Parser);
21 });
22 });
23
24 describe('A Parser instance', () => {
25 it('should parse the empty string',
26 shouldParse(''
27 /* no triples */));
28
29 it('should parse a whitespace string',
30 shouldParse(' \t \n '
31 /* no triples */));
32
33 it('should parse a single triple',
34 shouldParse('<a> <b> <c>.',
35 ['a', 'b', 'c']));
36
37 it('should parse three triples',
38 shouldParse('<a> <b> <c>.\n<d> <e> <f>.\n<g> <h> <i>.',
39 ['a', 'b', 'c'],
40 ['d', 'e', 'f'],
41 ['g', 'h', 'i']));
42
43 it('should parse a triple with a literal',
44 shouldParse('<a> <b> "string".',
45 ['a', 'b', '"string"']));
46
47 it('should parse a triple with a numeric literal',
48 shouldParse('<a> <b> 3.0.',
49 ['a', 'b', '"3.0"^^http://www.w3.org/2001/XMLSchema#decimal']));
50
51 it('should parse a triple with an integer literal',
52 shouldParse('<a> <b> 3.',
53 ['a', 'b', '"3"^^http://www.w3.org/2001/XMLSchema#integer']));
54
55 it('should parse a triple with a floating point literal',
56 shouldParse('<a> <b> 1.3e2.',
57 ['a', 'b', '"1.3e2"^^http://www.w3.org/2001/XMLSchema#double']));
58
59 it('should parse a triple with a boolean literal',
60 shouldParse('<a> <b> true.',
61 ['a', 'b', '"true"^^http://www.w3.org/2001/XMLSchema#boolean']));
62
63 it('should parse a triple with a literal and a language code',
64 shouldParse('<a> <b> "string"@en.',
65 ['a', 'b', '"string"@en']));
66
67 it('should normalize language codes to lowercase',
68 shouldParse('<a> <b> "string"@EN.',
69 ['a', 'b', '"string"@en']));
70
71 it('should parse a triple with a literal and an IRI type',
72 shouldParse('<a> <b> "string"^^<type>.',
73 ['a', 'b', '"string"^^http://example.org/type']));
74
75 it('should parse a triple with a literal and a prefixed name type',
76 shouldParse('@prefix x: <urn:x:y#>. <a> <b> "string"^^x:z.',
77 ['a', 'b', '"string"^^urn:x:y#z']));
78
79 it('should differentiate between IRI and prefixed name types',
80 shouldParse('@prefix : <noturn:>. :a :b "x"^^<urn:foo>. :a :b "x"^^:urn:foo.',
81 ['noturn:a', 'noturn:b', '"x"^^urn:foo'],
82 ['noturn:a', 'noturn:b', '"x"^^noturn:urn:foo']));
83
84 it('should not parse a triple with a literal and a prefixed name type with an inexistent prefix',
85 shouldNotParse('<a> <b> "string"^^x:z.',
86 'Undefined prefix "x:" on line 1.', {
87 token: {
88 line: 1,
89 type: 'type',
90 value: 'z',
91 prefix: 'x',
92 start: 18,
93 end: 21,
94 },
95 line: 1,
96 previousToken: {
97 line: 1,
98 type: 'literal',
99 value: 'string',
100 prefix: '',
101 start: 8,
102 end: 16,
103 },
104 }));
105
106 it('should parse a triple with the "a" shorthand predicate',
107 shouldParse('<a> a <t>.',
108 ['a', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 't']));
109
110 it('should parse triples with prefixes',
111 shouldParse('@prefix : <#>.\n' +
112 '@prefix a: <a#>.\n' +
113 ':x a:a a:b.',
114 ['#x', 'a#a', 'a#b']));
115
116 it('should parse triples with the prefix "prefix"',
117 shouldParse('@prefix prefix: <http://prefix.cc/>.' +
118 'prefix:a prefix:b prefix:c.',
119 ['http://prefix.cc/a', 'http://prefix.cc/b', 'http://prefix.cc/c']));
120
121 it('should parse triples with the prefix "base"',
122 shouldParse('PREFIX base: <http://prefix.cc/>' +
123 'base:a base:b base:c.',
124 ['http://prefix.cc/a', 'http://prefix.cc/b', 'http://prefix.cc/c']));
125
126 it('should parse triples with the prefix "graph"',
127 shouldParse('PREFIX graph: <http://prefix.cc/>' +
128 'graph:a graph:b graph:c.',
129 ['http://prefix.cc/a', 'http://prefix.cc/b', 'http://prefix.cc/c']));
130
131 it('should not parse @PREFIX',
132 shouldNotParse('@PREFIX : <#>.',
133 'Expected entity but got @PREFIX on line 1.', {
134 token: {
135 line: 1,
136 type: '@PREFIX',
137 value: '',
138 prefix: '',
139 start: 0,
140 end: 7,
141 },
142 previousToken: undefined,
143 line: 1,
144 }));
145
146 it('should parse triples with prefixes and different punctuation',
147 shouldParse('@prefix : <#>.\n' +
148 '@prefix a: <a#>.\n' +
149 ':x a:a a:b;a:c a:d,a:e.',
150 ['#x', 'a#a', 'a#b'],
151 ['#x', 'a#c', 'a#d'],
152 ['#x', 'a#c', 'a#e']));
153
154 it('should not parse undefined empty prefix in subject',
155 shouldNotParse(':a ',
156 'Undefined prefix ":" on line 1.'));
157
158 it('should not parse undefined prefix in subject',
159 shouldNotParse('a:a ',
160 'Undefined prefix "a:" on line 1.'));
161
162 it('should not parse undefined prefix in predicate',
163 shouldNotParse('<a> b:c ',
164 'Undefined prefix "b:" on line 1.'));
165
166 it('should not parse undefined prefix in object',
167 shouldNotParse('<a> <b> c:d ',
168 'Undefined prefix "c:" on line 1.'));
169
170 it('should not parse undefined prefix in datatype',
171 shouldNotParse('<a> <b> "c"^^d:e ',
172 'Undefined prefix "d:" on line 1.'));
173
174 it('should parse triples with SPARQL prefixes',
175 shouldParse('PREFIX : <#>\n' +
176 'PrEfIX a: <a#> ' +
177 ':x a:a a:b.',
178 ['#x', 'a#a', 'a#b']));
179
180 it('should not parse prefix declarations without prefix',
181 shouldNotParse('@prefix <a> ',
182 'Expected prefix to follow @prefix on line 1.'));
183
184 it('should not parse prefix declarations without IRI',
185 shouldNotParse('@prefix : .',
186 'Expected IRI to follow prefix ":" on line 1.'));
187
188 it('should not parse prefix declarations without a dot',
189 shouldNotParse('@prefix : <a> ;',
190 'Expected declaration to end with a dot on line 1.'));
191
192 it('should parse statements with shared subjects',
193 shouldParse('<a> <b> <c>;\n<d> <e>.',
194 ['a', 'b', 'c'],
195 ['a', 'd', 'e']));
196
197 it('should parse statements with shared subjects and trailing semicolon',
198 shouldParse('<a> <b> <c>;\n<d> <e>;\n.',
199 ['a', 'b', 'c'],
200 ['a', 'd', 'e']));
201
202 it('should parse statements with shared subjects and multiple semicolons',
203 shouldParse('<a> <b> <c>;;\n<d> <e>.',
204 ['a', 'b', 'c'],
205 ['a', 'd', 'e']));
206
207 it('should parse statements with shared subjects and predicates',
208 shouldParse('<a> <b> <c>, <d>.',
209 ['a', 'b', 'c'],
210 ['a', 'b', 'd']));
211
212 it('should not accept ; without preceding predicate',
213 shouldNotParse('<a> <b> <c>. <x>; <y> <z>.',
214 'Expected predicate but got ; on line 1.'));
215
216 it('should not accept , without preceding object',
217 shouldNotParse('<a> <b> <c>. <x> <y>, <z>.',
218 'Expected entity but got , on line 1.'));
219
220 it('should parse diamonds',
221 shouldParse('<> <> <> <>.\n(<>) <> (<>) <>.',
222 [BASE_IRI, BASE_IRI, BASE_IRI, BASE_IRI],
223 ['_:b0', BASE_IRI, '_:b1', BASE_IRI],
224 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', BASE_IRI],
225 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
226 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', BASE_IRI],
227 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
228
229 it('should parse statements with named blank nodes',
230 shouldParse('_:a <b> _:c.',
231 ['_:b0_a', 'b', '_:b0_c']));
232
233 it('should not parse statements with a blank node as predicate',
234 shouldNotParse('PREFIX : <#>\n<a> [] <c>.',
235 'Disallowed blank node as predicate on line 2.', {
236 token: {
237 line: 2,
238 type: '[',
239 value: '',
240 prefix: '',
241 start: 4,
242 end: 5,
243 },
244 line: 2,
245 previousToken: {
246 line: 2,
247 type: 'IRI',
248 value: 'a',
249 prefix: '',
250 start: 0,
251 end: 4,
252 },
253 }));
254
255 it('should not parse statements with a blank node label as predicate',
256 shouldNotParse('PREFIX : <#>\n<a> _:b <c>.',
257 'Disallowed blank node as predicate on line 2.', {
258 token: {
259 line: 2,
260 type: 'blank',
261 value: 'b',
262 prefix: '_',
263 start: 4,
264 end: 8,
265 },
266 line: 2,
267 previousToken: {
268 line: 2,
269 type: 'IRI',
270 value: 'a',
271 prefix: '',
272 start: 0,
273 end: 4,
274 },
275 }));
276
277 it('should parse statements with empty blank nodes',
278 shouldParse('[] <b> [].',
279 ['_:b0', 'b', '_:b1']));
280
281 it('should parse statements with unnamed blank nodes in the subject',
282 shouldParse('[<a> <b>] <c> <d>.',
283 ['_:b0', 'c', 'd'],
284 ['_:b0', 'a', 'b']));
285
286 it('should parse statements with unnamed blank nodes in the object',
287 shouldParse('<a> <b> [<c> <d>].',
288 ['a', 'b', '_:b0'],
289 ['_:b0', 'c', 'd']));
290
291 it('should parse statements with unnamed blank nodes with a string object',
292 shouldParse('<a> <b> [<c> "x"].',
293 ['a', 'b', '_:b0'],
294 ['_:b0', 'c', '"x"']));
295
296 it('should not parse a blank node with missing subject',
297 shouldNotParse('<a> <b> [<c>].',
298 'Expected entity but got ] on line 1.'));
299
300 it('should not parse a blank node with only a semicolon',
301 shouldNotParse('<a> <b> [;].',
302 'Expected predicate but got ; on line 1.'));
303
304 it('should not parse a dangling blank node closing brace',
305 shouldNotParse('<a:a> <b:b> <c:c> ; ]',
306 'Unexpected ] on line 1.'));
307
308 it('should parse a blank node with a trailing semicolon',
309 shouldParse('<a> <b> [ <u> <v>; ].',
310 ['a', 'b', '_:b0'],
311 ['_:b0', 'u', 'v']));
312
313 it('should parse a blank node with multiple trailing semicolons',
314 shouldParse('<a> <b> [ <u> <v>;;; ].',
315 ['a', 'b', '_:b0'],
316 ['_:b0', 'u', 'v']));
317
318 it('should parse a multi-predicate blank node',
319 shouldParse('<a> <b> [ <u> <v>; <w> <z> ].',
320 ['a', 'b', '_:b0'],
321 ['_:b0', 'u', 'v'],
322 ['_:b0', 'w', 'z']));
323
324 it('should parse a multi-predicate blank node with multiple semicolons',
325 shouldParse('<a> <b> [ <u> <v>;;; <w> <z> ].',
326 ['a', 'b', '_:b0'],
327 ['_:b0', 'u', 'v'],
328 ['_:b0', 'w', 'z']));
329
330 it('should parse a multi-object blank node',
331 shouldParse('<a> <b> [ <u> <v>, <z> ].',
332 ['a', 'b', '_:b0'],
333 ['_:b0', 'u', 'v'],
334 ['_:b0', 'u', 'z']));
335
336 it('should parse a multi-statement blank node ending with a literal',
337 shouldParse('<a> <b> [ <u> <v>; <w> "z" ].',
338 ['a', 'b', '_:b0'],
339 ['_:b0', 'u', 'v'],
340 ['_:b0', 'w', '"z"']));
341
342 it('should parse a multi-statement blank node ending with a typed literal',
343 shouldParse('<a> <b> [ <u> <v>; <w> "z"^^<t> ].',
344 ['a', 'b', '_:b0'],
345 ['_:b0', 'u', 'v'],
346 ['_:b0', 'w', '"z"^^http://example.org/t']));
347
348 it('should parse a multi-statement blank node ending with a string with language',
349 shouldParse('<a> <b> [ <u> <v>; <w> "z"^^<t> ].',
350 ['a', 'b', '_:b0'],
351 ['_:b0', 'u', 'v'],
352 ['_:b0', 'w', '"z"^^http://example.org/t']));
353
354 it('should parse a multi-statement blank node with trailing semicolon',
355 shouldParse('<a> <b> [ <u> <v>; <w> <z>; ].',
356 ['a', 'b', '_:b0'],
357 ['_:b0', 'u', 'v'],
358 ['_:b0', 'w', 'z']));
359
360 it('should parse statements with nested blank nodes in the subject',
361 shouldParse('[<a> [<x> <y>]] <c> <d>.',
362 ['_:b0', 'c', 'd'],
363 ['_:b0', 'a', '_:b1'],
364 ['_:b1', 'x', 'y']));
365
366 it('should parse statements with nested blank nodes in the object',
367 shouldParse('<a> <b> [<c> [<d> <e>]].',
368 ['a', 'b', '_:b0'],
369 ['_:b0', 'c', '_:b1'],
370 ['_:b1', 'd', 'e']));
371
372 it('should reuse identifiers of blank nodes within and outside of graphs',
373 shouldParse('_:a <b> _:c. <g> { _:a <b> _:c }',
374 ['_:b0_a', 'b', '_:b0_c'],
375 ['_:b0_a', 'b', '_:b0_c', 'g']));
376
377 it('should not parse an invalid blank node',
378 shouldNotParse('[ <a> <b> .',
379 'Expected punctuation to follow "http://example.org/b" on line 1.'));
380
381 it('should parse a statements with only an anonymous node',
382 shouldParse('[<p> <o>].',
383 ['_:b0', 'p', 'o']));
384
385 it('should not parse a statement with only a blank anonymous node',
386 shouldNotParse('[].',
387 'Unexpected . on line 1.'));
388
389 it('should not parse an anonymous node with only an anonymous node inside',
390 shouldNotParse('[[<p> <o>]].',
391 'Disallowed blank node as predicate on line 1.'));
392
393 it('should parse statements with an empty list in the subject',
394 shouldParse('() <a> <b>.',
395 ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'a', 'b']));
396
397 it('should parse statements with an empty list in the object',
398 shouldParse('<a> <b> ().',
399 ['a', 'b', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
400
401 it('should parse statements with a single-element list in the subject',
402 shouldParse('(<x>) <a> <b>.',
403 ['_:b0', 'a', 'b'],
404 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
405 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
406
407 it('should parse statements with a single-element list in the object',
408 shouldParse('<a> <b> (<x>).',
409 ['a', 'b', '_:b0'],
410 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
411 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
412
413 it('should parse a list with a literal',
414 shouldParse('<a> <b> ("x").',
415 ['a', 'b', '_:b0'],
416 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"x"'],
417 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
418
419 it('should parse a list with a typed literal',
420 shouldParse('<a> <b> ("x"^^<y>).',
421 ['a', 'b', '_:b0'],
422 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"x"^^http://example.org/y'],
423 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
424
425 it('should parse a list with a language-tagged literal',
426 shouldParse('<a> <b> ("x"@en-GB).',
427 ['a', 'b', '_:b0'],
428 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"x"@en-gb'],
429 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
430
431 it('should parse statements with a multi-element list in the subject',
432 shouldParse('(<x> <y>) <a> <b>.',
433 ['_:b0', 'a', 'b'],
434 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
435 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
436 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
437 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
438
439 it('should parse statements with a multi-element list in the object',
440 shouldParse('<a> <b> (<x> <y>).',
441 ['a', 'b', '_:b0'],
442 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
443 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
444 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
445 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
446
447 it('should parse statements with a multi-element literal list in the object',
448 shouldParse('<a> <b> ("x" "y"@en-GB 1 "z"^^<t>).',
449 ['a', 'b', '_:b0'],
450 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"x"'],
451 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
452 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"y"@en-gb'],
453 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
454 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"1"^^http://www.w3.org/2001/XMLSchema#integer'],
455 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
456 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"z"^^http://example.org/t'],
457 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
458
459 it('should parse statements with prefixed names in lists',
460 shouldParse('@prefix a: <a#>. <a> <b> (a:x a:y).',
461 ['a', 'b', '_:b0'],
462 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a#x'],
463 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
464 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a#y'],
465 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
466
467 it('should not parse statements with undefined prefixes in lists',
468 shouldNotParse('<a> <b> (a:x a:y).',
469 'Undefined prefix "a:" on line 1.'));
470
471 it('should parse statements with blank nodes in lists',
472 shouldParse('<a> <b> (_:x _:y).',
473 ['a', 'b', '_:b0'],
474 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b0_x'],
475 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
476 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b0_y'],
477 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
478
479 it('should parse statements with a nested empty list',
480 shouldParse('<a> <b> (<x> ()).',
481 ['a', 'b', '_:b0'],
482 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
483 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
484 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
485 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
486
487 it('should parse statements with non-empty nested lists',
488 shouldParse('<a> <b> (<x> (<y>)).',
489 ['a', 'b', '_:b0'],
490 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
491 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
492 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'],
493 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
494 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
495 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
496
497 it('should parse statements with a list containing a blank node',
498 shouldParse('([]) <a> <b>.',
499 ['_:b0', 'a', 'b'],
500 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
501 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
502
503 it('should parse statements with a list containing multiple blank nodes',
504 shouldParse('([] [<x> <y>]) <a> <b>.',
505 ['_:b0', 'a', 'b'],
506 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
507 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
508 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'],
509 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
510 ['_:b3', 'x', 'y']));
511
512 it('should parse statements with a blank node containing a list',
513 shouldParse('[<a> (<b>)] <c> <d>.',
514 ['_:b0', 'c', 'd'],
515 ['_:b0', 'a', '_:b1'],
516 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'b'],
517 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
518
519 it('should not parse an invalid list',
520 shouldNotParse('<a> <b> (]).',
521 'Expected entity but got ] on line 1.'));
522
523 it('should resolve IRIs against @base',
524 shouldParse('@base <http://ex.org/>.\n' +
525 '<a> <b> <c>.\n' +
526 '@base <d/>.\n' +
527 '<e> <f> <g>.',
528 ['http://ex.org/a', 'http://ex.org/b', 'http://ex.org/c'],
529 ['http://ex.org/d/e', 'http://ex.org/d/f', 'http://ex.org/d/g']));
530
531 it('should not resolve IRIs against @BASE',
532 shouldNotParse('@BASE <http://ex.org/>.',
533 'Expected entity but got @BASE on line 1.'));
534
535 it('should resolve IRIs against SPARQL base',
536 shouldParse('BASE <http://ex.org/>\n' +
537 '<a> <b> <c>. ' +
538 'BASE <d/> ' +
539 '<e> <f> <g>.',
540 ['http://ex.org/a', 'http://ex.org/b', 'http://ex.org/c'],
541 ['http://ex.org/d/e', 'http://ex.org/d/f', 'http://ex.org/d/g']));
542
543 it('should resolve IRIs against a @base with query string',
544 shouldParse('@base <http://ex.org/?foo>.\n' +
545 '<> <b> <c>.\n' +
546 '@base <d/?bar>.\n' +
547 '<> <f> <g>.',
548 ['http://ex.org/?foo', 'http://ex.org/b', 'http://ex.org/c'],
549 ['http://ex.org/d/?bar', 'http://ex.org/d/f', 'http://ex.org/d/g']));
550
551 it('should resolve IRIs with query string against @base',
552 shouldParse('@base <http://ex.org/>.\n' +
553 '<?> <?a> <?a=b>.\n' +
554 '@base <d>.\n' +
555 '<?> <?a> <?a=b>.' +
556 '@base <?e>.\n' +
557 '<> <?a> <?a=b>.',
558 ['http://ex.org/?', 'http://ex.org/?a', 'http://ex.org/?a=b'],
559 ['http://ex.org/d?', 'http://ex.org/d?a', 'http://ex.org/d?a=b'],
560 ['http://ex.org/d?e', 'http://ex.org/d?a', 'http://ex.org/d?a=b']));
561
562 it('should not resolve IRIs with colons',
563 shouldParse('@base <http://ex.org/>.\n' +
564 '<a> <b> <c>.\n' +
565 '<A:> <b:> <c:>.\n' +
566 '<a:a> <b:B> <C-D:c>.',
567 ['http://ex.org/a', 'http://ex.org/b', 'http://ex.org/c'],
568 ['A:', 'b:', 'c:'],
569 ['a:a', 'b:B', 'C-D:c']));
570
571 it('should not allow relative URIs with a colon in the first path segment',
572 shouldNotParse('<entity.beeldbank_leiden_person:A.E._Stuur.> <x:x> <x:x> .',
573 'Invalid IRI on line 1.'));
574
575 it('should not allow relative URIs with a colon in the first path segment as base',
576 shouldNotParse('@base <entity.beeldbank_leiden_person:A.E._Stuur.> .',
577 'Expected valid IRI to follow base declaration on line 1.'));
578
579 it('should resolve datatype IRIs against @base',
580 shouldParse('@base <http://ex.org/>.\n' +
581 '<a> <b> "c"^^<d>.\n' +
582 '@base <d/>.\n' +
583 '<e> <f> "g"^^<h>.',
584 ['http://ex.org/a', 'http://ex.org/b', '"c"^^http://ex.org/d'],
585 ['http://ex.org/d/e', 'http://ex.org/d/f', '"g"^^http://ex.org/d/h']));
586
587 it('should resolve IRIs against a base with a fragment',
588 shouldParse('@base <http://ex.org/foo#bar>.\n' +
589 '<a> <b> <#c>.\n',
590 ['http://ex.org/a', 'http://ex.org/b', 'http://ex.org/foo#c']));
591
592 it('should resolve IRIs with an empty fragment',
593 shouldParse('@base <http://ex.org/foo>.\n' +
594 '<#> <b#> <#c>.\n',
595 ['http://ex.org/foo#', 'http://ex.org/b#', 'http://ex.org/foo#c']));
596
597 it('should not resolve prefixed names',
598 shouldParse('PREFIX ex: <http://ex.org/a/bb/ccc/../>\n' +
599 'ex:a ex:b ex:c .',
600 ['http://ex.org/a/bb/ccc/../a', 'http://ex.org/a/bb/ccc/../b', 'http://ex.org/a/bb/ccc/../c']));
601
602 it('should parse an empty default graph',
603 shouldParse('{}'));
604
605 it('should parse a one-triple default graph ending without a dot',
606 shouldParse('{<a> <b> <c>}',
607 ['a', 'b', 'c']));
608
609 it('should parse a one-triple default graph ending with a dot',
610 shouldParse('{<a> <b> <c>.}',
611 ['a', 'b', 'c']));
612
613 it('should parse a three-triple default graph ending without a dot',
614 shouldParse('{<a> <b> <c>;<d> <e>,<f>}',
615 ['a', 'b', 'c'],
616 ['a', 'd', 'e'],
617 ['a', 'd', 'f']));
618
619 it('should parse a three-triple default graph ending with a dot',
620 shouldParse('{<a> <b> <c>;<d> <e>,<f>.}',
621 ['a', 'b', 'c'],
622 ['a', 'd', 'e'],
623 ['a', 'd', 'f']));
624
625 it('should parse a three-triple default graph ending with a semicolon',
626 shouldParse('{<a> <b> <c>;<d> <e>,<f>;}',
627 ['a', 'b', 'c'],
628 ['a', 'd', 'e'],
629 ['a', 'd', 'f']));
630
631 it('should parse a default graph with a blank node ending with a dot',
632 shouldParse('{ [<p> <o>]. }',
633 ['_:b0', 'p', 'o']));
634
635 it('should parse a default graph with a blank node ending without a dot',
636 shouldParse('{ [<p> <o>] }',
637 ['_:b0', 'p', 'o']));
638
639 it('should parse an empty named graph with an IRI',
640 shouldParse('<g>{}'));
641
642 it('should parse a one-triple named graph with an IRI ending without a dot',
643 shouldParse('<g> {<a> <b> <c>}',
644 ['a', 'b', 'c', 'g']));
645
646 it('should parse a one-triple named graph with an IRI ending with a dot',
647 shouldParse('<g>{<a> <b> <c>.}',
648 ['a', 'b', 'c', 'g']));
649
650 it('should parse a three-triple named graph with an IRI ending without a dot',
651 shouldParse('<g> {<a> <b> <c>;<d> <e>,<f>}',
652 ['a', 'b', 'c', 'g'],
653 ['a', 'd', 'e', 'g'],
654 ['a', 'd', 'f', 'g']));
655
656 it('should parse a three-triple named graph with an IRI ending with a dot',
657 shouldParse('<g>{<a> <b> <c>;<d> <e>,<f>.}',
658 ['a', 'b', 'c', 'g'],
659 ['a', 'd', 'e', 'g'],
660 ['a', 'd', 'f', 'g']));
661
662 it('should parse an empty named graph with a prefixed name',
663 shouldParse('@prefix g: <g#>.\ng:h {}'));
664
665 it('should parse a one-triple named graph with a prefixed name ending without a dot',
666 shouldParse('@prefix g: <g#>.\ng:h {<a> <b> <c>}',
667 ['a', 'b', 'c', 'g#h']));
668
669 it('should parse a one-triple named graph with a prefixed name ending with a dot',
670 shouldParse('@prefix g: <g#>.\ng:h{<a> <b> <c>.}',
671 ['a', 'b', 'c', 'g#h']));
672
673 it('should parse a three-triple named graph with a prefixed name ending without a dot',
674 shouldParse('@prefix g: <g#>.\ng:h {<a> <b> <c>;<d> <e>,<f>}',
675 ['a', 'b', 'c', 'g#h'],
676 ['a', 'd', 'e', 'g#h'],
677 ['a', 'd', 'f', 'g#h']));
678
679 it('should parse a three-triple named graph with a prefixed name ending with a dot',
680 shouldParse('@prefix g: <g#>.\ng:h{<a> <b> <c>;<d> <e>,<f>.}',
681 ['a', 'b', 'c', 'g#h'],
682 ['a', 'd', 'e', 'g#h'],
683 ['a', 'd', 'f', 'g#h']));
684
685 it('should parse a named graph with a blank node ending with a dot',
686 shouldParse('<g> { [<p> <o>]. }',
687 ['_:b0', 'p', 'o', 'g']));
688
689 it('should parse a named graph with a blank node ending without a dot',
690 shouldParse('<g> { [<p> <o>] }',
691 ['_:b0', 'p', 'o', 'g']));
692
693 it('should parse an empty anonymous graph',
694 shouldParse('[] {}'));
695
696 it('should parse a one-triple anonymous graph ending without a dot',
697 shouldParse('[] {<a> <b> <c>}',
698 ['a', 'b', 'c', '_:b0']));
699
700 it('should parse a one-triple anonymous graph ending with a dot',
701 shouldParse('[]{<a> <b> <c>.}',
702 ['a', 'b', 'c', '_:b0']));
703
704 it('should parse a three-triple anonymous graph ending without a dot',
705 shouldParse('[] {<a> <b> <c>;<d> <e>,<f>}',
706 ['a', 'b', 'c', '_:b0'],
707 ['a', 'd', 'e', '_:b0'],
708 ['a', 'd', 'f', '_:b0']));
709
710 it('should parse a three-triple anonymous graph ending with a dot',
711 shouldParse('[]{<a> <b> <c>;<d> <e>,<f>.}',
712 ['a', 'b', 'c', '_:b0'],
713 ['a', 'd', 'e', '_:b0'],
714 ['a', 'd', 'f', '_:b0']));
715
716 it('should parse an empty named graph with an IRI and the GRAPH keyword',
717 shouldParse('GRAPH <g> {}'));
718
719 it('should parse an empty named graph with a prefixed name and the GRAPH keyword',
720 shouldParse('@prefix g: <g#>.\nGRAPH g:h {}'));
721
722 it('should parse an empty anonymous graph and the GRAPH keyword',
723 shouldParse('GRAPH [] {}'));
724
725 it('should parse a one-triple named graph with an IRI and the GRAPH keyword',
726 shouldParse('GRAPH <g> {<a> <b> <c>}',
727 ['a', 'b', 'c', 'g']));
728
729 it('should parse a one-triple named graph with a prefixed name and the GRAPH keyword',
730 shouldParse('@prefix g: <g#>.\nGRAPH g:h {<a> <b> <c>}',
731 ['a', 'b', 'c', 'g#h']));
732
733 it('should parse a one-triple anonymous graph and the GRAPH keyword',
734 shouldParse('GRAPH [] {<a> <b> <c>}',
735 ['a', 'b', 'c', '_:b0']));
736
737 it('should parse a graph with 8-bit unicode escape sequences',
738 shouldParse('<\\U0001d400> {\n<\\U0001d400> <\\U0001d400> "\\U0001d400"^^<\\U0001d400>\n}\n',
739 ['\ud835\udC00', '\ud835\udc00', '"\ud835\udc00"^^http://example.org/\ud835\udc00', '\ud835\udc00']));
740
741 it('should not parse a single closing brace',
742 shouldNotParse('}',
743 'Unexpected graph closing on line 1.'));
744
745 it('should not parse a single opening brace',
746 shouldNotParse('{',
747 'Expected entity but got eof on line 1.'));
748
749 it('should not parse a superfluous closing brace ',
750 shouldNotParse('{}}',
751 'Unexpected graph closing on line 1.'));
752
753 it('should not parse a graph with only a dot',
754 shouldNotParse('{.}',
755 'Expected entity but got . on line 1.'));
756
757 it('should not parse a graph with only a semicolon',
758 shouldNotParse('{;}',
759 'Expected entity but got ; on line 1.'));
760
761 it('should not parse an unclosed graph',
762 shouldNotParse('{<a> <b> <c>.',
763 'Unclosed graph on line 1.'));
764
765 it('should not parse a named graph with a list node as label',
766 shouldNotParse('() {}',
767 'Expected entity but got { on line 1.'));
768
769 it('should not parse a named graph with a non-empty blank node as label',
770 shouldNotParse('[<a> <b>] {}',
771 'Expected entity but got { on line 1.'));
772
773 it('should not parse a named graph with the GRAPH keyword and a non-empty blank node as label',
774 shouldNotParse('GRAPH [<a> <b>] {}',
775 'Invalid graph label on line 1.'));
776
777 it('should not parse a triple after the GRAPH keyword',
778 shouldNotParse('GRAPH <a> <b> <c>.',
779 'Expected graph but got IRI on line 1.'));
780
781 it('should not parse repeated GRAPH keywords',
782 shouldNotParse('GRAPH GRAPH <g> {}',
783 'Invalid graph label on line 1.'));
784
785 it('should parse a quad with 4 IRIs',
786 shouldParse('<a> <b> <c> <g>.',
787 ['a', 'b', 'c', 'g']));
788
789 it('should parse a quad with 4 prefixed names',
790 shouldParse('@prefix p: <p#>.\np:a p:b p:c p:g.',
791 ['p#a', 'p#b', 'p#c', 'p#g']));
792
793 it('should not parse a quad with an undefined prefix',
794 shouldNotParse('<a> <b> <c> p:g.',
795 'Undefined prefix "p:" on line 1.'));
796
797 it('should parse a quad with 3 IRIs and a literal',
798 shouldParse('<a> <b> "c"^^<d> <g>.',
799 ['a', 'b', '"c"^^http://example.org/d', 'g']));
800
801 it('should parse a quad with 2 blank nodes and a literal',
802 shouldParse('_:a <b> "c"^^<d> _:g.',
803 ['_:b0_a', 'b', '"c"^^http://example.org/d', '_:b0_g']));
804
805 it('should not parse a quad in a graph',
806 shouldNotParse('{<a> <b> <c> <g>.}',
807 'Expected punctuation to follow "http://example.org/c" on line 1.'));
808
809 it('should not parse a quad with different punctuation',
810 shouldNotParse('<a> <b> <c> <g>;',
811 'Expected dot to follow quad on line 1.'));
812
813 it('should not parse base declarations without IRI',
814 shouldNotParse('@base a: ',
815 'Expected valid IRI to follow base declaration on line 1.'));
816
817 it('should not parse improperly nested parentheses and brackets',
818 shouldNotParse('<a> <b> [<c> (<d>]).',
819 'Expected entity but got ] on line 1.'));
820
821 it('should not parse improperly nested square brackets',
822 shouldNotParse('<a> <b> [<c> <d>]].',
823 'Expected entity but got ] on line 1.'));
824
825 it('should error when an object is not there',
826 shouldNotParse('<a> <b>.',
827 'Expected entity but got . on line 1.'));
828
829 it('should error when a dot is not there',
830 shouldNotParse('<a> <b> <c>',
831 'Expected entity but got eof on line 1.'));
832
833 it('should error with an abbreviation in the subject',
834 shouldNotParse('a <a> <a>.',
835 'Expected entity but got abbreviation on line 1.'));
836
837 it('should error with an abbreviation in the object',
838 shouldNotParse('<a> <a> a .',
839 'Expected entity but got abbreviation on line 1.'));
840
841 it('should error if punctuation follows a subject',
842 shouldNotParse('<a> .',
843 'Unexpected . on line 1.'));
844
845 it('should error if an unexpected token follows a subject',
846 shouldNotParse('<a> @',
847 'Unexpected "@" on line 1.'), {
848 token: {
849 line: 1,
850 type: '@PREFIX',
851 value: '',
852 prefix: '',
853 },
854 previousToken: undefined,
855 line: 1,
856 });
857
858 it('should not error if there is no triple callback', () => {
859 new Parser().parse('');
860 });
861
862 it('should return prefixes through a callback', done => {
863 const prefixes = {};
864 new Parser().parse('@prefix a: <http://a.org/#>. a:a a:b a:c. @prefix b: <http://b.org/#>.',
865 tripleCallback, prefixCallback);
866
867 function tripleCallback(error, triple) {
868 expect(error).not.to.exist;
869 if (!triple) {
870 Object.keys(prefixes).should.have.length(2);
871 expect(prefixes).to.have.property('a');
872 expect(prefixes.a).to.deep.equal(new NamedNode('http://a.org/#'));
873 expect(prefixes).to.have.property('b');
874 expect(prefixes.b).to.deep.equal(new NamedNode('http://b.org/#'));
875 done();
876 }
877 }
878
879 function prefixCallback(prefix, iri) {
880 expect(prefix).to.exist;
881 expect(iri).to.exist;
882 prefixes[prefix] = iri;
883 }
884 });
885
886 it('should return prefixes through a callback without triple callback', done => {
887 const prefixes = {};
888 new Parser().parse('@prefix a: <IRIa>. a:a a:b a:c. @prefix b: <IRIb>.',
889 null, prefixCallback);
890
891 function prefixCallback(prefix, iri) {
892 expect(prefix).to.exist;
893 expect(iri).to.exist;
894 prefixes[prefix] = iri;
895 if (Object.keys(prefixes).length === 2)
896 done();
897 }
898 });
899
900 it('should return prefixes at the last triple callback', done => {
901 new Parser({ baseIRI: BASE_IRI })
902 .parse('@prefix a: <IRIa>. a:a a:b a:c. @prefix b: <IRIb>.', tripleCallback);
903
904 function tripleCallback(error, triple, prefixes) {
905 expect(error).not.to.exist;
906 if (triple)
907 expect(prefixes).not.to.exist;
908 else {
909 expect(prefixes).to.exist;
910 Object.keys(prefixes).should.have.length(2);
911 expect(prefixes).to.have.property('a', 'http://example.org/IRIa');
912 expect(prefixes).to.have.property('b', 'http://example.org/IRIb');
913 done();
914 }
915 }
916 });
917
918 it('should parse a string synchronously if no callback is given', () => {
919 const triples = new Parser().parse('@prefix a: <urn:a:>. a:a a:b a:c.');
920 triples.should.deep.equal([
921 new Quad(termFromId('urn:a:a'), termFromId('urn:a:b'),
922 termFromId('urn:a:c'), termFromId('')),
923 ]);
924 });
925
926 it('should throw on syntax errors if no callback is given', () => {
927 (function () { new Parser().parse('<a> bar <c>'); })
928 .should.throw('Unexpected "bar" on line 1.').with.property('context').with.property('line', 1);
929 });
930
931 it('should throw on grammar errors if no callback is given', () => {
932 (function () { new Parser().parse('<a> <b> <c>'); })
933 .should.throw('Expected entity but got eof on line 1');
934 });
935
936 it('should parse an RDF* triple with a triple with iris as subject correctly', () => {
937 shouldParse('<<<a> <b> <c>>> <b> <c>.',
938 [['a', 'b', 'c'], 'b', 'c']);
939 });
940
941 it('should not parse an RDF* triple with a triple as predicate',
942 shouldNotParse('<a> <<<b> <c> <d>>> <e>',
943 'Expected entity but got << on line 1.'));
944
945 it('should parse an RDF* triple with a triple with blanknodes as subject correctly',
946 shouldParse('<<_:a <b> _:c>> <b> <c>.',
947 [['_:b0_a', 'b', '_:b0_c'], 'b', 'c']));
948
949 it('should parse an RDF* triple with a triple with blanknodes and literals as subject correctly',
950 shouldParse('<<_:a <b> "c"^^<d>>> <b> <c>.',
951 [['_:b0_a', 'b', '"c"^^http://example.org/d'], 'b', 'c']));
952
953 it('should parse an RDF* triple with a triple as object correctly',
954 shouldParse('<a> <b> <<<a> <b> <c>>>.',
955 ['a', 'b', ['a', 'b', 'c']]));
956
957 it('should parse an RDF* triple with a triple as object correctly',
958 shouldParse('<a> <b> <<_:a <b> _:c>>.',
959 ['a', 'b', ['_:b0_a', 'b', '_:b0_c']]));
960
961 it('should parse an RDF* triple with a triple as object correctly',
962 shouldParse('<a> <b> <<_:a <b> "c"^^<d>>>.',
963 ['a', 'b', ['_:b0_a', 'b', '"c"^^http://example.org/d']]));
964
965 it('should parse nested triples correctly',
966 shouldParse('<<<<<a> <b> <c>>> <f> <g>>> <d> <e>.',
967 [[['a', 'b', 'c'], 'f', 'g'], 'd', 'e']));
968 it('should parse nested triples correctly',
969 shouldParse('<d> <e> <<<f> <g> <<<a> <b> <c>>>>>.',
970 ['d', 'e', ['f', 'g', ['a', 'b', 'c']]]));
971 it('should parse nested triples correctly',
972 shouldParse('<<<f> <g> <<<a> <b> <c>>>>> <d> <e>.',
973 [['f', 'g', ['a', 'b', 'c']], 'd', 'e']));
974 it('should parse nested triples correctly',
975 shouldParse('<d> <e> <<<<<a> <b> <c>>> <f> <g>>>.',
976 ['d', 'e', [['a', 'b', 'c'], 'f', 'g']]));
977
978 it('should not parse nested RDF* statements that are partially closed',
979 shouldNotParse('<d> <e> <<<<<a> <b> <c>>> <f> <g>.',
980 'Expected entity but got . on line 1.'
981 ));
982
983 it('should not parse partially closed nested RDF* statements',
984 shouldNotParse('<d> <e> <<<<<a> <b> <c> <f> <g>>>.',
985 'Expected >> but got IRI on line 1.'
986 ));
987
988 it('should not parse nested RDF* statements with too many closing tags',
989 shouldNotParse('<d> <e> <<<<<a> <b> <c>>>>> <f> <g>>>.',
990 'Expected entity but got >> on line 1.'
991 ));
992
993 it('should not parse nested RDF* statements with too many closing tags',
994 shouldNotParse('<d> <e> <<<<<a> <b> <c>>> <f> <g>>>>>.',
995 'Expected entity but got >> on line 1.'
996 ));
997
998 it('should not parse RDF* statements with too many closing tags',
999 shouldNotParse('<a> <b> <c>>>.',
1000 'Expected entity but got >> on line 1.'
1001 ));
1002
1003 it('should not parse incomplete RDF* statements',
1004 shouldNotParse('<d> <e> <<<a> <b>>>.',
1005 'Expected entity but got >> on line 1.'
1006 ));
1007
1008 it('should not parse incomplete RDF* statements',
1009 shouldNotParse('<<<a> <b>>> <d> <e>.',
1010 'Expected entity but got >> on line 1.'
1011 ));
1012
1013 it('should not parse incorrectly nested RDF* statements',
1014 shouldNotParse('>> <<',
1015 'Expected entity but got >> on line 1.'
1016 ));
1017
1018 it('should not parse a nested triple on its own',
1019 shouldNotParse('<<<a> <b> <c>>>.',
1020 'Unexpected . on line 1.'
1021 ));
1022
1023 it('should parse an RDF* quad',
1024 shouldParse('<<<a> <b> <c> <d>>> <a> <b> .',
1025 [['a', 'b', 'c', 'd'], 'a', 'b']));
1026
1027 it('should not parse a malformed RDF* quad',
1028 shouldNotParse('<<<a> <b> <c> <d> <e>>> <a> <b> .',
1029 'Expected >> but got IRI on line 1.'));
1030
1031 it('should parse statements with a shared RDF* subject',
1032 shouldParse('<<<a> <b> <c>>> <b> <c>;\n<d> <c>.',
1033 [['a', 'b', 'c'], 'b', 'c'],
1034 [['a', 'b', 'c'], 'd', 'c']));
1035
1036 it('should parse statements with a shared RDF* subject',
1037 shouldParse('<<<a> <b> <c>>> <b> <c>;\n<d> <<<a> <b> <c>>>.',
1038 [['a', 'b', 'c'], 'b', 'c'],
1039 [['a', 'b', 'c'], 'd', ['a', 'b', 'c']]));
1040
1041 it('should put nested triples in the default graph',
1042 shouldParse('<a> <b> <c> <g>.\n<<<a> <b> <c>>> <d> <e>.',
1043 ['a', 'b', 'c', 'g'],
1044 [['a', 'b', 'c'], 'd', 'e']));
1045 });
1046
1047 describe('An Parser instance without document IRI', () => {
1048 function parser() { return new Parser(); }
1049
1050 it('should keep relative IRIs',
1051 shouldParse(parser,
1052 '@prefix : <#>.\n' +
1053 '<a> <b> <c> <g>.\n' +
1054 ':d :e :f :g.',
1055 [termFromId('a'), termFromId('b'), termFromId('c'), termFromId('g')],
1056 [termFromId('#d'), termFromId('#e'), termFromId('#f'), termFromId('#g')]));
1057
1058 it('should keep empty IRIs',
1059 shouldParse(parser,
1060 '@prefix : <>.\n' +
1061 '<> <> <> <>.\n' +
1062 ': : : :.',
1063 [new NamedNode(''), new NamedNode(''), new NamedNode(''), new NamedNode('')],
1064 [new NamedNode(''), new NamedNode(''), new NamedNode(''), new NamedNode('')]));
1065 });
1066
1067 describe('An Parser instance with a document IRI', () => {
1068 function parser() { return new Parser({ baseIRI: 'http://ex.org/x/yy/zzz/f.ttl' }); }
1069
1070 it('should resolve IRIs against the document IRI',
1071 shouldParse(parser,
1072 '@prefix : <#>.\n' +
1073 '<a> <b> <c> <g>.\n' +
1074 ':d :e :f :g.',
1075 ['http://ex.org/x/yy/zzz/a', 'http://ex.org/x/yy/zzz/b', 'http://ex.org/x/yy/zzz/c', 'http://ex.org/x/yy/zzz/g'],
1076 ['http://ex.org/x/yy/zzz/f.ttl#d', 'http://ex.org/x/yy/zzz/f.ttl#e', 'http://ex.org/x/yy/zzz/f.ttl#f', 'http://ex.org/x/yy/zzz/f.ttl#g']));
1077
1078 it('should resolve IRIs with a trailing slash against the document IRI',
1079 shouldParse(parser,
1080 '</a> </a/b> </a/b/c>.\n',
1081 ['http://ex.org/a', 'http://ex.org/a/b', 'http://ex.org/a/b/c']));
1082
1083 it('should resolve IRIs starting with ./ against the document IRI',
1084 shouldParse(parser,
1085 '<./a> <./a/b> <./a/b/c>.\n',
1086 ['http://ex.org/x/yy/zzz/a', 'http://ex.org/x/yy/zzz/a/b', 'http://ex.org/x/yy/zzz/a/b/c']));
1087
1088 it('should resolve IRIs starting with multiple ./ sequences against the document IRI',
1089 shouldParse(parser,
1090 '<./././a> <./././././a/b> <././././././a/b/c>.\n',
1091 ['http://ex.org/x/yy/zzz/a', 'http://ex.org/x/yy/zzz/a/b', 'http://ex.org/x/yy/zzz/a/b/c']));
1092
1093 it('should resolve IRIs starting with ../ against the document IRI',
1094 shouldParse(parser,
1095 '<../a> <../a/b> <../a/b/c>.\n',
1096 ['http://ex.org/x/yy/a', 'http://ex.org/x/yy/a/b', 'http://ex.org/x/yy/a/b/c']));
1097
1098 it('should resolve IRIs starting multiple ../ sequences against the document IRI',
1099 shouldParse(parser,
1100 '<../../a> <../../../a/b> <../../../../../../../../a/b/c>.\n',
1101 ['http://ex.org/x/a', 'http://ex.org/a/b', 'http://ex.org/a/b/c']));
1102
1103 it('should resolve IRIs starting with mixes of ./ and ../ sequences against the document IRI',
1104 shouldParse(parser,
1105 '<.././a> <./.././a/b> <./.././.././a/b/c>.\n',
1106 ['http://ex.org/x/yy/a', 'http://ex.org/x/yy/a/b', 'http://ex.org/x/a/b/c']));
1107
1108 it('should resolve IRIs starting with .x, ..x, or .../ against the document IRI',
1109 shouldParse(parser,
1110 '<.x/a> <..x/a/b> <.../a/b/c>.\n',
1111 ['http://ex.org/x/yy/zzz/.x/a', 'http://ex.org/x/yy/zzz/..x/a/b', 'http://ex.org/x/yy/zzz/.../a/b/c']));
1112
1113 it('should resolve datatype IRIs against the document IRI',
1114 shouldParse(parser,
1115 '<a> <b> "c"^^<d>.',
1116 ['http://ex.org/x/yy/zzz/a', 'http://ex.org/x/yy/zzz/b', '"c"^^http://ex.org/x/yy/zzz/d']));
1117
1118 it('should resolve IRIs in lists against the document IRI',
1119 shouldParse(parser,
1120 '(<a> <b>) <p> (<c> <d>).',
1121 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'http://ex.org/x/yy/zzz/a'],
1122 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
1123 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'http://ex.org/x/yy/zzz/b'],
1124 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1125 ['_:b0', 'http://ex.org/x/yy/zzz/p', '_:b2'],
1126 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'http://ex.org/x/yy/zzz/c'],
1127 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
1128 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'http://ex.org/x/yy/zzz/d'],
1129 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
1130
1131 it('should respect @base statements',
1132 shouldParse(parser,
1133 '<a> <b> <c>.\n' +
1134 '@base <http://ex.org/x/>.\n' +
1135 '<e> <f> <g>.\n' +
1136 '@base <d/>.\n' +
1137 '<h> <i> <j>.\n' +
1138 '@base </e/>.\n' +
1139 '<k> <l> <m>.',
1140 ['http://ex.org/x/yy/zzz/a', 'http://ex.org/x/yy/zzz/b', 'http://ex.org/x/yy/zzz/c'],
1141 ['http://ex.org/x/e', 'http://ex.org/x/f', 'http://ex.org/x/g'],
1142 ['http://ex.org/x/d/h', 'http://ex.org/x/d/i', 'http://ex.org/x/d/j'],
1143 ['http://ex.org/e/k', 'http://ex.org/e/l', 'http://ex.org/e/m']));
1144 });
1145
1146 describe('A Parser instance with a blank node prefix', () => {
1147 function parser() { return new Parser({ baseIRI: BASE_IRI, blankNodePrefix: '_:blank' }); }
1148
1149 it('should use the given prefix for blank nodes',
1150 shouldParse(parser,
1151 '_:a <b> _:c.\n',
1152 ['_:blanka', 'b', '_:blankc']));
1153 });
1154
1155 describe('A Parser instance with an empty blank node prefix', () => {
1156 function parser() { return new Parser({ baseIRI: BASE_IRI, blankNodePrefix: '' }); }
1157
1158 it('should not use a prefix for blank nodes',
1159 shouldParse(parser,
1160 '_:a <b> _:c.\n',
1161 ['_:a', 'b', '_:c']));
1162 });
1163
1164 describe('A Parser instance with a non-string format', () => {
1165 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 1 }); }
1166
1167 it('should parse a single triple',
1168 shouldParse(parser, '<a> <b> <c>.', ['a', 'b', 'c']));
1169
1170 it('should parse a graph',
1171 shouldParse(parser, '{<a> <b> <c>}', ['a', 'b', 'c']));
1172 });
1173
1174 describe('A Parser instance for the Turtle format', () => {
1175 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'Turtle' }); }
1176
1177 it('should parse a single triple',
1178 shouldParse(parser, '<a> <b> <c>.', ['a', 'b', 'c']));
1179
1180 it('should not parse a default graph',
1181 shouldNotParse(parser, '{}', 'Unexpected graph on line 1.'));
1182
1183 it('should not parse a named graph',
1184 shouldNotParse(parser, '<g> {}', 'Expected entity but got { on line 1.'));
1185
1186 it('should not parse a named graph with the GRAPH keyword',
1187 shouldNotParse(parser, 'GRAPH <g> {}', 'Expected entity but got GRAPH on line 1.'));
1188
1189 it('should not parse a quad',
1190 shouldNotParse(parser, '<a> <b> <c> <d>.', 'Expected punctuation to follow "http://example.org/c" on line 1.'));
1191
1192 it('should not parse a variable',
1193 shouldNotParse(parser, '?a ?b ?c.', 'Unexpected "?a" on line 1.'));
1194
1195 it('should not parse an equality statement',
1196 shouldNotParse(parser, '<a> = <b>.', 'Unexpected "=" on line 1.'));
1197
1198 it('should not parse a right implication statement',
1199 shouldNotParse(parser, '<a> => <b>.', 'Unexpected "=>" on line 1.'));
1200
1201 it('should not parse a left implication statement',
1202 shouldNotParse(parser, '<a> <= <b>.', 'Unexpected "<=" on line 1.'));
1203
1204 it('should not parse a formula as object',
1205 shouldNotParse(parser, '<a> <b> {}.', 'Unexpected graph on line 1.'));
1206
1207 it('should not parse @forSome',
1208 shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));
1209
1210 it('should not parse @forAll',
1211 shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
1212
1213 it('should not parse a formula as list item',
1214 shouldNotParse(parser, '( <a> { <b> <c> <d> } <e> ).',
1215 'Unexpected graph on line 1.'));
1216
1217 it('should not parse a literal as subject',
1218 shouldNotParse(parser, '1 <a> <b>.',
1219 'Unexpected literal on line 1.'));
1220
1221 it('should not parse RDF* in the subject position',
1222 shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1223 'Unexpected RDF* syntax on line 1.'));
1224
1225 it('should not parse RDF* in the object position',
1226 shouldNotParse(parser, '<a> <b> <<a> <b> <c>>>.',
1227 'Unexpected RDF* syntax on line 1.'));
1228 });
1229
1230 describe('A Parser instance for the TurtleStar format', () => {
1231 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'TurtleStar' }); }
1232
1233 it('should parse RDF*',
1234 shouldParse(parser,
1235 '<<<a> <b> <c>>> <b> <c> .',
1236 [['a', 'b', 'c'], 'b', 'c']));
1237
1238 it('should not parse nested quads',
1239 shouldNotParse(parser, '<<_:a <http://ex.org/b> _:b <http://ex.org/b>>> <http://ex.org/b> "c" .',
1240 'Expected >> to follow "_:b0_b" on line 1.'));
1241 });
1242
1243 describe('A Parser instance for the TriG format', () => {
1244 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'TriG' }); }
1245
1246 it('should parse a single triple',
1247 shouldParse(parser, '<a> <b> <c>.', ['a', 'b', 'c']));
1248
1249 it('should parse a default graph',
1250 shouldParse(parser, '{}'));
1251
1252 it('should parse a named graph',
1253 shouldParse(parser, '<g> {}'));
1254
1255 it('should parse a named graph with the GRAPH keyword',
1256 shouldParse(parser, 'GRAPH <g> {}'));
1257
1258 it('should not parse a quad',
1259 shouldNotParse(parser, '<a> <b> <c> <d>.', 'Expected punctuation to follow "http://example.org/c" on line 1.'));
1260
1261 it('should not parse a variable',
1262 shouldNotParse(parser, '?a ?b ?c.', 'Unexpected "?a" on line 1.'));
1263
1264 it('should not parse an equality statement',
1265 shouldNotParse(parser, '<a> = <b>.', 'Unexpected "=" on line 1.'));
1266
1267 it('should not parse a right implication statement',
1268 shouldNotParse(parser, '<a> => <b>.', 'Unexpected "=>" on line 1.'));
1269
1270 it('should not parse a left implication statement',
1271 shouldNotParse(parser, '<a> <= <b>.', 'Unexpected "<=" on line 1.'));
1272
1273 it('should not parse a formula as object',
1274 shouldNotParse(parser, '<a> <b> {}.', 'Unexpected graph on line 1.'));
1275
1276 it('should not parse @forSome',
1277 shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));
1278
1279 it('should not parse @forAll',
1280 shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
1281
1282 it('should not parse RDF* in the subject position',
1283 shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1284 'Unexpected RDF* syntax on line 1.'));
1285
1286 it('should not parse RDF* in the object position',
1287 shouldNotParse(parser, '<a> <b> <<<a> <b> <c>>>.',
1288 'Unexpected RDF* syntax on line 1.'));
1289 });
1290
1291 describe('A Parser instance for the TriGStar format', () => {
1292 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'TriGStar' }); }
1293
1294 it('should parse RDF*',
1295 shouldParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1296 [['a', 'b', 'c'], 'a', 'b']));
1297
1298 it('should not parse nested quads',
1299 shouldNotParse(parser, '<<_:a <http://ex.org/b> _:b <http://ex.org/b>>> <http://ex.org/b> "c" .',
1300 'Expected >> to follow "_:b0_b" on line 1.'));
1301 });
1302
1303 describe('A Parser instance for the N-Triples format', () => {
1304 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N-Triples' }); }
1305
1306 it('should parse a single triple',
1307 shouldParse(parser, '_:a <http://ex.org/b> "c".',
1308 ['_:b0_a', 'http://ex.org/b', '"c"']));
1309
1310 it('should parse a single triple starting with Bom',
1311 shouldParse(parser, '\ufeff_:a <http://ex.org/b> "c".',
1312 ['_:b0_a', 'http://ex.org/b', '"c"']));
1313
1314 it('should not parse a single quad',
1315 shouldNotParse(parser, '_:a <http://ex.org/b> "c" <http://ex.org/g>.',
1316 'Expected punctuation to follow ""c"" on line 1.'));
1317
1318 it('should not parse relative IRIs',
1319 shouldNotParse(parser, '<a> <b> <c>.', 'Invalid IRI on line 1.'));
1320
1321 it('should not parse a prefix declaration',
1322 shouldNotParse(parser, '@prefix : <p#>.', 'Unexpected "@prefix" on line 1.'));
1323
1324 it('should not parse apostrophe literals',
1325 shouldNotParse(parser, "_:a <http://ex.org/b> 'c'.",
1326 "Unexpected \"'c'.\" on line 1."));
1327
1328 it('should not parse triple-quoted literals',
1329 shouldNotParse(parser, '_:a <http://ex.org/b> """c""".',
1330 'Unexpected """"c"""." on line 1.'));
1331
1332 it('should not parse triple-apostrophe literals',
1333 shouldNotParse(parser, "_:a <http://ex.org/b> '''c'''.",
1334 "Unexpected \"'''c'''.\" on line 1."));
1335
1336 it('should not parse a variable',
1337 shouldNotParse(parser, '?a ?b ?c.', 'Unexpected "?a" on line 1.'));
1338
1339 it('should not parse an equality statement',
1340 shouldNotParse(parser, '<urn:a:a> = <urn:b:b>.', 'Unexpected "=" on line 1.'));
1341
1342 it('should not parse a right implication statement',
1343 shouldNotParse(parser, '<urn:a:a> => <urn:b:b>.', 'Unexpected "=>" on line 1.'));
1344
1345 it('should not parse a left implication statement',
1346 shouldNotParse(parser, '<urn:a:a> <= <urn:b:b>.', 'Unexpected "<=" on line 1.'));
1347
1348 it('should not parse a formula as object',
1349 shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{}." on line 1.'));
1350
1351 it('should not parse @forSome',
1352 shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));
1353
1354 it('should not parse @forAll',
1355 shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
1356
1357 it('should not parse RDF* in the subject position',
1358 shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1359 'Unexpected RDF* syntax on line 1.'));
1360
1361 it('should not parse RDF* in the object position',
1362 shouldNotParse(parser, '<http://ex.org/a> <http://ex.org/b> <<<a> <b> <c>>>.',
1363 'Unexpected RDF* syntax on line 1.'));
1364 });
1365
1366 describe('A Parser instance for the N-TriplesStar format', () => {
1367 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N-TriplesStar' }); }
1368
1369 it('should parse RDF*',
1370 shouldParse(parser, '<<_:a <http://example.org/b> _:c>> <http://example.org/a> _:b .',
1371 [['_:b0_a', 'b', '_:b0_c'], 'a', '_:b0_b']));
1372
1373 it('should not parse nested quads',
1374 shouldNotParse(parser, '<<_:a <http://ex.org/b> _:b <http://ex.org/b>>> <http://ex.org/b> "c" .',
1375 'Expected >> to follow "_:b0_b" on line 1.'));
1376 });
1377
1378 describe('A Parser instance for the N-Quads format', () => {
1379 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N-Quads' }); }
1380
1381 it('should parse a single triple',
1382 shouldParse(parser, '_:a <http://ex.org/b> "c".',
1383 ['_:b0_a', 'http://ex.org/b', '"c"']));
1384
1385 it('should parse a single quad',
1386 shouldParse(parser, '_:a <http://ex.org/b> "c" <http://ex.org/g>.',
1387 ['_:b0_a', 'http://ex.org/b', '"c"', 'http://ex.org/g']));
1388
1389 it('should not parse relative IRIs',
1390 shouldNotParse(parser, '<a> <b> <c>.', 'Invalid IRI on line 1.'));
1391
1392 it('should not parse a prefix declaration',
1393 shouldNotParse(parser, '@prefix : <p#>.', 'Unexpected "@prefix" on line 1.'));
1394
1395 it('should not parse a variable',
1396 shouldNotParse(parser, '?a ?b ?c.', 'Unexpected "?a" on line 1.'));
1397
1398 it('should not parse an equality statement',
1399 shouldNotParse(parser, '<urn:a:a> = <urn:b:b>.', 'Unexpected "=" on line 1.'));
1400
1401 it('should not parse a right implication statement',
1402 shouldNotParse(parser, '<urn:a:a> => <urn:b:b>.', 'Unexpected "=>" on line 1.'));
1403
1404 it('should not parse a left implication statement',
1405 shouldNotParse(parser, '<urn:a:a> <= <urn:b:b>.', 'Unexpected "<=" on line 1.'));
1406
1407 it('should not parse a formula as object',
1408 shouldNotParse(parser, '<urn:a:a> <urn:b:b> {}.', 'Unexpected "{}." on line 1.'));
1409
1410 it('should not parse @forSome',
1411 shouldNotParse(parser, '@forSome <x>.', 'Unexpected "@forSome" on line 1.'));
1412
1413 it('should not parse @forAll',
1414 shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
1415
1416 it('should not parse RDF* in the subject position',
1417 shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1418 'Unexpected RDF* syntax on line 1.'));
1419
1420 it('should not parse RDF* in the object position',
1421 shouldNotParse(parser, '_:a <http://ex.org/b> <<<a> <b> <c>>>.',
1422 'Unexpected RDF* syntax on line 1.'));
1423 });
1424
1425 describe('A Parser instance for the N-QuadsStar format', () => {
1426 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N-QuadsStar' }); }
1427
1428 it('should parse RDF*',
1429 shouldParse(parser, '<<_:a <http://example.org/b> _:c>> <http://example.org/a> _:c .',
1430 [['_:b0_a', 'b', '_:b0_c'], 'a', '_:b0_c']));
1431 });
1432
1433 describe('A Parser instance for the N3 format', () => {
1434 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N3' }); }
1435
1436 it('should parse a single triple',
1437 shouldParse(parser, '<a> <b> <c>.', ['a', 'b', 'c']));
1438
1439 it('should not parse a default graph',
1440 shouldNotParse(parser, '{}', 'Expected entity but got eof on line 1.'));
1441
1442 it('should not parse a named graph',
1443 shouldNotParse(parser, '<g> {}', 'Expected entity but got { on line 1.'));
1444
1445 it('should not parse a named graph with the GRAPH keyword',
1446 shouldNotParse(parser, 'GRAPH <g> {}', 'Expected entity but got GRAPH on line 1.'));
1447
1448 it('should not parse a quad',
1449 shouldNotParse(parser, '<a> <b> <c> <d>.', 'Expected punctuation to follow "http://example.org/c" on line 1.'));
1450
1451 it('allows a blank node in predicate position',
1452 shouldParse(parser, '<a> [] <c>.', ['a', '_:b0', 'c']));
1453
1454 it('allows a blank node label in predicate position',
1455 shouldParse(parser, '<a> _:b <c>.', ['a', '_:b0_b', 'c']));
1456
1457 it('allows a blank node with properties in predicate position',
1458 shouldParse(parser, '<a> [<p> <o>] <c>.',
1459 ['a', '_:b0', 'c'],
1460 ['_:b0', 'p', 'o']));
1461
1462 it('should parse a variable',
1463 shouldParse(parser, '?a ?b ?c.', ['?a', '?b', '?c']));
1464
1465 it('should parse a simple equality',
1466 shouldParse(parser, '<a> = <b>.',
1467 ['a', 'http://www.w3.org/2002/07/owl#sameAs', 'b']));
1468
1469 it('should parse a simple right implication',
1470 shouldParse(parser, '<a> => <b>.',
1471 ['a', 'http://www.w3.org/2000/10/swap/log#implies', 'b']));
1472
1473 it('should parse a simple left implication',
1474 shouldParse(parser, '<a> <= <b>.',
1475 ['b', 'http://www.w3.org/2000/10/swap/log#implies', 'a']));
1476
1477 it('should parse a right implication between one-triple graphs',
1478 shouldParse(parser, '{ ?a ?b <c>. } => { <d> <e> ?a }.',
1479 ['_:b0', 'http://www.w3.org/2000/10/swap/log#implies', '_:b1'],
1480 ['?a', '?b', 'c', '_:b0'],
1481 ['d', 'e', '?a', '_:b1']));
1482
1483 it('should parse a right implication between two-triple graphs',
1484 shouldParse(parser, '{ ?a ?b <c>. <d> <e> <f>. } => { <d> <e> ?a, <f> }.',
1485 ['_:b0', 'http://www.w3.org/2000/10/swap/log#implies', '_:b1'],
1486 ['?a', '?b', 'c', '_:b0'],
1487 ['d', 'e', 'f', '_:b0'],
1488 ['d', 'e', '?a', '_:b1'],
1489 ['d', 'e', 'f', '_:b1']));
1490
1491 it('should parse a left implication between one-triple graphs',
1492 shouldParse(parser, '{ ?a ?b <c>. } <= { <d> <e> ?a }.',
1493 ['_:b1', 'http://www.w3.org/2000/10/swap/log#implies', '_:b0'],
1494 ['?a', '?b', 'c', '_:b0'],
1495 ['d', 'e', '?a', '_:b1']));
1496
1497 it('should parse a left implication between two-triple graphs',
1498 shouldParse(parser, '{ ?a ?b <c>. <d> <e> <f>. } <= { <d> <e> ?a, <f> }.',
1499 ['_:b1', 'http://www.w3.org/2000/10/swap/log#implies', '_:b0'],
1500 ['?a', '?b', 'c', '_:b0'],
1501 ['d', 'e', 'f', '_:b0'],
1502 ['d', 'e', '?a', '_:b1'],
1503 ['d', 'e', 'f', '_:b1']));
1504
1505 it('should parse an equality of one-triple graphs',
1506 shouldParse(parser, '{ ?a ?b <c>. } = { <d> <e> ?a }.',
1507 ['_:b0', 'http://www.w3.org/2002/07/owl#sameAs', '_:b1'],
1508 ['?a', '?b', 'c', '_:b0'],
1509 ['d', 'e', '?a', '_:b1']));
1510
1511 it('should parse an equality of two-triple graphs',
1512 shouldParse(parser, '{ ?a ?b <c>. <d> <e> <f>. } = { <d> <e> ?a, <f> }.',
1513 ['_:b0', 'http://www.w3.org/2002/07/owl#sameAs', '_:b1'],
1514 ['?a', '?b', 'c', '_:b0'],
1515 ['d', 'e', 'f', '_:b0'],
1516 ['d', 'e', '?a', '_:b1'],
1517 ['d', 'e', 'f', '_:b1']));
1518
1519 it('should parse nested implication graphs',
1520 shouldParse(parser, '{ { ?a ?b ?c }<={ ?d ?e ?f }. } <= { { ?g ?h ?i } => { ?j ?k ?l } }.',
1521 ['_:b3', 'http://www.w3.org/2000/10/swap/log#implies', '_:b0'],
1522 ['_:b2', 'http://www.w3.org/2000/10/swap/log#implies', '_:b1', '_:b0'],
1523 ['?a', '?b', '?c', '_:b1'],
1524 ['?d', '?e', '?f', '_:b2'],
1525 ['_:b4', 'http://www.w3.org/2000/10/swap/log#implies', '_:b5', '_:b3'],
1526 ['?g', '?h', '?i', '_:b4'],
1527 ['?j', '?k', '?l', '_:b5']));
1528
1529 it('should not reuse identifiers of blank nodes within and outside of formulas',
1530 shouldParse(parser, '_:a _:b _:c. { _:a _:b _:c } => { { _:a _:b _:c } => { _:a _:b _:c } }.',
1531 ['_:b0_a', '_:b0_b', '_:b0_c'],
1532 ['_:b0', 'http://www.w3.org/2000/10/swap/log#implies', '_:b1', ''],
1533 ['_:b0.a', '_:b0.b', '_:b0.c', '_:b0'],
1534 ['_:b2', 'http://www.w3.org/2000/10/swap/log#implies', '_:b3', '_:b1'],
1535 ['_:b2.a', '_:b2.b', '_:b2.c', '_:b2'],
1536 ['_:b3.a', '_:b3.b', '_:b3.c', '_:b3']));
1537
1538 it('should parse a @forSome statement',
1539 shouldParse(parser, '@forSome <x>. <x> <x> <x>.',
1540 ['_:b0', '_:b0', '_:b0']));
1541
1542 it('should parse a @forSome statement with multiple entities',
1543 shouldParse(parser, '@prefix a: <a:>. @base <b:>. @forSome a:x, <y>, a:z. a:x <y> a:z.',
1544 ['_:b0', '_:b1', '_:b2']));
1545
1546 it('should not parse a @forSome statement with an invalid prefix',
1547 shouldNotParse(parser, '@forSome a:b.',
1548 'Undefined prefix "a:" on line 1.'));
1549
1550 it('should not parse a @forSome statement with a blank node',
1551 shouldNotParse(parser, '@forSome _:a.',
1552 'Unexpected blank on line 1.'));
1553
1554 it('should not parse a @forSome statement with a variable',
1555 shouldNotParse(parser, '@forSome ?a.',
1556 'Unexpected var on line 1.'));
1557
1558 it('should correctly scope @forSome statements',
1559 shouldParse(parser, '@forSome <x>. <x> <x> { @forSome <x>. <x> <x> <x>. }. <x> <x> <x>.',
1560 ['_:b0', '_:b0', '_:b1'],
1561 ['_:b2', '_:b2', '_:b2', '_:b1'],
1562 ['_:b0', '_:b0', '_:b0']));
1563
1564 it('should parse a @forAll statement',
1565 shouldParse(parser, '@forAll <x>. <x> <x> <x>.',
1566 ['?b0', '?b0', '?b0']));
1567
1568 it('should parse a @forAll statement with multiple entities',
1569 shouldParse(parser, '@prefix a: <a:>. @base <b:>. @forAll a:x, <y>, a:z. a:x <y> a:z.',
1570 ['?b0', '?b1', '?b2']));
1571
1572 it('should not parse a @forAll statement with an invalid prefix',
1573 shouldNotParse(parser, '@forAll a:b.',
1574 'Undefined prefix "a:" on line 1.'));
1575
1576 it('should not parse a @forAll statement with a blank node',
1577 shouldNotParse(parser, '@forAll _:a.',
1578 'Unexpected blank on line 1.'));
1579
1580 it('should not parse a @forAll statement with a variable',
1581 shouldNotParse(parser, '@forAll ?a.',
1582 'Unexpected var on line 1.'));
1583
1584 it('should correctly scope @forAll statements',
1585 shouldParse(parser, '@forAll <x>. <x> <x> { @forAll <x>. <x> <x> <x>. }. <x> <x> <x>.',
1586 ['?b0', '?b0', '_:b1'],
1587 ['?b2', '?b2', '?b2', '_:b1'],
1588 ['?b0', '?b0', '?b0']));
1589
1590 it('should parse a ! path of length 2 as subject',
1591 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1592 ':joe!fam:mother a fam:Person.',
1593 ['ex:joe', 'f:mother', '_:b0'],
1594 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1595
1596 it('should parse a ! path of length 4 as subject',
1597 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>. @prefix loc: <l:>.' +
1598 ':joe!fam:mother!loc:office!loc:zip loc:code 1234.',
1599 ['ex:joe', 'f:mother', '_:b0'],
1600 ['_:b0', 'l:office', '_:b1'],
1601 ['_:b1', 'l:zip', '_:b2'],
1602 ['_:b2', 'l:code', '"1234"^^http://www.w3.org/2001/XMLSchema#integer']));
1603
1604 it('should parse a ! path of length 2 as object',
1605 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1606 '<x> <is> :joe!fam:mother.',
1607 ['x', 'is', '_:b0'],
1608 ['ex:joe', 'f:mother', '_:b0']));
1609
1610 it('should parse a ! path of length 4 as object',
1611 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>. @prefix loc: <l:>.' +
1612 '<x> <is> :joe!fam:mother!loc:office!loc:zip.',
1613 ['x', 'is', '_:b2'],
1614 ['ex:joe', 'f:mother', '_:b0'],
1615 ['_:b0', 'l:office', '_:b1'],
1616 ['_:b1', 'l:zip', '_:b2']));
1617
1618 it('should parse a ^ path of length 2 as subject',
1619 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1620 ':joe^fam:son a fam:Person.',
1621 ['_:b0', 'f:son', 'ex:joe'],
1622 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1623
1624 it('should parse a ^ path of length 4 as subject',
1625 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1626 ':joe^fam:son^fam:sister^fam:mother a fam:Person.',
1627 ['_:b0', 'f:son', 'ex:joe'],
1628 ['_:b1', 'f:sister', '_:b0'],
1629 ['_:b2', 'f:mother', '_:b1'],
1630 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1631
1632 it('should parse a ^ path of length 2 as object',
1633 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1634 '<x> <is> :joe^fam:son.',
1635 ['x', 'is', '_:b0'],
1636 ['_:b0', 'f:son', 'ex:joe']));
1637
1638 it('should parse a ^ path of length 4 as object',
1639 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1640 '<x> <is> :joe^fam:son^fam:sister^fam:mother.',
1641 ['x', 'is', '_:b2'],
1642 ['_:b0', 'f:son', 'ex:joe'],
1643 ['_:b1', 'f:sister', '_:b0'],
1644 ['_:b2', 'f:mother', '_:b1']));
1645
1646 it('should parse mixed !/^ paths as subject',
1647 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1648 ':joe!fam:mother^fam:mother a fam:Person.',
1649 ['ex:joe', 'f:mother', '_:b0'],
1650 ['_:b1', 'f:mother', '_:b0'],
1651 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1652
1653 it('should parse mixed !/^ paths as object',
1654 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1655 '<x> <is> :joe!fam:mother^fam:mother.',
1656 ['x', 'is', '_:b1'],
1657 ['ex:joe', 'f:mother', '_:b0'],
1658 ['_:b1', 'f:mother', '_:b0']));
1659
1660 it('should parse a ! path in a blank node as subject',
1661 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1662 '[fam:knows :joe!fam:mother] a fam:Person.',
1663 ['_:b0', 'f:knows', '_:b1'],
1664 ['ex:joe', 'f:mother', '_:b1'],
1665 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1666
1667 it('should parse a ! path in a blank node as object',
1668 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1669 '<x> <is> [fam:knows :joe!fam:mother].',
1670 ['x', 'is', '_:b0'],
1671 ['_:b0', 'f:knows', '_:b1'],
1672 ['ex:joe', 'f:mother', '_:b1']));
1673
1674 it('should parse a ^ path in a blank node as subject',
1675 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1676 '[fam:knows :joe^fam:son] a fam:Person.',
1677 ['_:b0', 'f:knows', '_:b1'],
1678 ['_:b1', 'f:son', 'ex:joe'],
1679 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person']));
1680
1681 it('should parse a ^ path in a blank node as object',
1682 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1683 '<x> <is> [fam:knows :joe^fam:son].',
1684 ['x', 'is', '_:b0'],
1685 ['_:b0', 'f:knows', '_:b1'],
1686 ['_:b1', 'f:son', 'ex:joe']));
1687
1688 it('should parse a ! path in a list as subject',
1689 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1690 '(<x> :joe!fam:mother <y>) a :List.',
1691 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'],
1692 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
1693 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
1694 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'],
1695 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
1696 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
1697 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1698 ['ex:joe', 'f:mother', '_:b2']));
1699
1700 it('should parse a ! path in a list as object',
1701 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1702 '<l> <is> (<x> :joe!fam:mother <y>).',
1703 ['l', 'is', '_:b0'],
1704 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
1705 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
1706 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'],
1707 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
1708 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
1709 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1710 ['ex:joe', 'f:mother', '_:b2']));
1711
1712 it('should parse a ^ path in a list as subject',
1713 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1714 '(<x> :joe^fam:son <y>) a :List.',
1715 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'],
1716 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
1717 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
1718 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'],
1719 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
1720 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
1721 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1722 ['_:b2', 'f:son', 'ex:joe']));
1723
1724 it('should parse a ^ path in a list as object',
1725 shouldParse(parser, '@prefix : <ex:>. @prefix fam: <f:>.' +
1726 '<l> <is> (<x> :joe^fam:son <y>).',
1727 ['l', 'is', '_:b0'],
1728 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'],
1729 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
1730 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'],
1731 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'],
1732 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
1733 ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1734 ['_:b2', 'f:son', 'ex:joe']));
1735
1736 it('should parse a formula as list item',
1737 shouldParse(parser, '<a> <findAll> ( <b> { <b> a <type>. <b> <something> <foo> } <o> ).',
1738 ['a', 'findAll', '_:b0'],
1739 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'b'],
1740 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
1741 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'o'],
1742 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
1743 ['b', 'something', 'foo', '_:b1'],
1744 ['b', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'type', '_:b1']
1745 ));
1746
1747 it('should not parse an invalid ! path',
1748 shouldNotParse(parser, '<a>!"invalid" ', 'Expected entity but got literal on line 1.'));
1749
1750 it('should not parse an invalid ^ path',
1751 shouldNotParse(parser, '<a>^"invalid" ', 'Expected entity but got literal on line 1.'));
1752
1753 it('should parse literal as subject',
1754 shouldParse(parser, '<a> <b> {1 <greaterThan> 0}.',
1755 ['a', 'b', '_:b0'],
1756 ['"1"^^http://www.w3.org/2001/XMLSchema#integer', 'greaterThan', '"0"^^http://www.w3.org/2001/XMLSchema#integer', '_:b0']
1757 ));
1758
1759 it('should parse literals with datatype as subject',
1760 shouldParse(parser, '<a> <b> {"a"^^<c> <greaterThan> "b"^^<c>}.',
1761 ['a', 'b', '_:b0'],
1762 ['"a"^^http://example.org/c', 'greaterThan', '"b"^^http://example.org/c', '_:b0']
1763 ));
1764
1765 it('should parse literals with language as subject',
1766 shouldParse(parser, '<a> <b> {"bonjour"@fr <sameAs> "hello"@en}.',
1767 ['a', 'b', '_:b0'],
1768 ['"bonjour"@fr', 'sameAs', '"hello"@en', '_:b0']
1769 ));
1770
1771 it('should not parse RDF* in the subject position',
1772 shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1773 'Unexpected RDF* syntax on line 1.'));
1774
1775 it('should not parse RDF* in the object position',
1776 shouldNotParse(parser, '<a> <b> <<<a> <b> <c>>>.',
1777 'Unexpected RDF* syntax on line 1.'));
1778 });
1779
1780 describe('A Parser instance for the N3Star format', () => {
1781 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N3Star' }); }
1782
1783 it('should parse RDF*',
1784 shouldParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1785 [['a', 'b', 'c'], 'a', 'b']));
1786
1787 it('should not parse nested quads',
1788 shouldNotParse(parser, '<<_:a <http://ex.org/b> _:b <http://ex.org/b>>> <http://ex.org/b> "c" .',
1789 'Expected >> to follow "_:.b" on line 1.'));
1790 });
1791
1792 describe('A Parser instance for the N3 format with the explicitQuantifiers option', () => {
1793 function parser() { return new Parser({ baseIRI: BASE_IRI, format: 'N3', explicitQuantifiers: true }); }
1794
1795 it('should parse a @forSome statement',
1796 shouldParse(parser, '@forSome <x>. <x> <x> <x>.',
1797 ['', 'http://www.w3.org/2000/10/swap/reify#forSome', '_:b0', 'urn:n3:quantifiers'],
1798 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1799 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1800 ['x', 'x', 'x']));
1801
1802 it('should parse a @forSome statement with multiple entities',
1803 shouldParse(parser, '@prefix a: <a:>. @base <b:>. @forSome a:x, <y>, a:z. a:x <y> a:z.',
1804 ['', 'http://www.w3.org/2000/10/swap/reify#forSome', '_:b0', 'urn:n3:quantifiers'],
1805 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a:x', 'urn:n3:quantifiers'],
1806 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1', 'urn:n3:quantifiers'],
1807 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'b:y', 'urn:n3:quantifiers'],
1808 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2', 'urn:n3:quantifiers'],
1809 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a:z', 'urn:n3:quantifiers'],
1810 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1811 ['a:x', 'b:y', 'a:z']));
1812
1813 it('should correctly scope @forSome statements',
1814 shouldParse(parser, '@forSome <x>. <x> <x> { @forSome <x>. <x> <x> <x>. }. <x> <x> <x>.',
1815 ['', 'http://www.w3.org/2000/10/swap/reify#forSome', '_:b0', 'urn:n3:quantifiers'],
1816 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1817 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1818 ['x', 'x', '_:b1'],
1819 ['_:b1', 'http://www.w3.org/2000/10/swap/reify#forSome', '_:b2', 'urn:n3:quantifiers'],
1820 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1821 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1822 ['x', 'x', 'x', '_:b1'],
1823 ['x', 'x', 'x']));
1824
1825 it('should parse a @forAll statement',
1826 shouldParse(parser, '@forAll <x>. <x> <x> <x>.',
1827 ['', 'http://www.w3.org/2000/10/swap/reify#forAll', '_:b0', 'urn:n3:quantifiers'],
1828 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1829 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1830 ['x', 'x', 'x']));
1831
1832 it('should parse a @forAll statement with multiple entities',
1833 shouldParse(parser, '@prefix a: <a:>. @base <b:>. @forAll a:x, <y>, a:z. a:x <y> a:z.',
1834 ['', 'http://www.w3.org/2000/10/swap/reify#forAll', '_:b0', 'urn:n3:quantifiers'],
1835 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a:x', 'urn:n3:quantifiers'],
1836 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1', 'urn:n3:quantifiers'],
1837 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'b:y', 'urn:n3:quantifiers'],
1838 ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2', 'urn:n3:quantifiers'],
1839 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'a:z', 'urn:n3:quantifiers'],
1840 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1841 ['a:x', 'b:y', 'a:z']));
1842
1843 it('should correctly scope @forAll statements',
1844 shouldParse(parser, '@forAll <x>. <x> <x> { @forAll <x>. <x> <x> <x>. }. <x> <x> <x>.',
1845 ['', 'http://www.w3.org/2000/10/swap/reify#forAll', '_:b0', 'urn:n3:quantifiers'],
1846 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1847 ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1848 ['x', 'x', '_:b1'],
1849 ['_:b1', 'http://www.w3.org/2000/10/swap/reify#forAll', '_:b2', 'urn:n3:quantifiers'],
1850 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x', 'urn:n3:quantifiers'],
1851 ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'urn:n3:quantifiers'],
1852 ['x', 'x', 'x', '_:b1'],
1853 ['x', 'x', 'x']));
1854 });
1855
1856 describe('A Parser instance with a custom DataFactory', () => {
1857 const factory = {};
1858 let parser;
1859 before(() => {
1860 factory.quad = function (s, p, o, g) { return { s: s, p: p, o: o, g: g }; };
1861 ['namedNode', 'blankNode', 'literal', 'variable', 'defaultGraph'].forEach(f => {
1862 factory[f] = function (n) { return n ? `${f[0]}-${n}` : f; };
1863 });
1864 parser = new Parser({ baseIRI: BASE_IRI, format: 'n3', factory: factory });
1865 });
1866
1867 it('should use the custom factory', () => {
1868 parser.parse('<a> ?b 1, _:d.').should.deep.equal([
1869 { s: 'n-http://example.org/a', p: 'v-b', o: 'l-1', g: 'defaultGraph' },
1870 { s: 'n-http://example.org/a', p: 'v-b', o: 'b-b0_d', g: 'defaultGraph' },
1871 ]);
1872 });
1873 });
1874
1875 describe('IRI resolution', () => {
1876 describe('RFC3986 normal examples', () => {
1877 itShouldResolve('http://a/bb/ccc/d;p?q', 'g:h', 'g:h');
1878 itShouldResolve('http://a/bb/ccc/d;p?q', 'g', 'http://a/bb/ccc/g');
1879 itShouldResolve('http://a/bb/ccc/d;p?q', './g', 'http://a/bb/ccc/g');
1880 itShouldResolve('http://a/bb/ccc/d;p?q', 'g/', 'http://a/bb/ccc/g/');
1881 itShouldResolve('http://a/bb/ccc/d;p?q', '/g', 'http://a/g');
1882 itShouldResolve('http://a/bb/ccc/d;p?q', '//g', 'http://g');
1883 itShouldResolve('http://a/bb/ccc/d;p?q', '?y', 'http://a/bb/ccc/d;p?y');
1884 itShouldResolve('http://a/bb/ccc/d;p?q', 'g?y', 'http://a/bb/ccc/g?y');
1885 itShouldResolve('http://a/bb/ccc/d;p?q', '#s', 'http://a/bb/ccc/d;p?q#s');
1886 itShouldResolve('http://a/bb/ccc/d;p?q', 'g#s', 'http://a/bb/ccc/g#s');
1887 itShouldResolve('http://a/bb/ccc/d;p?q', 'g?y#s', 'http://a/bb/ccc/g?y#s');
1888 itShouldResolve('http://a/bb/ccc/d;p?q', ';x', 'http://a/bb/ccc/;x');
1889 itShouldResolve('http://a/bb/ccc/d;p?q', 'g;x', 'http://a/bb/ccc/g;x');
1890 itShouldResolve('http://a/bb/ccc/d;p?q', 'g;x?y#s', 'http://a/bb/ccc/g;x?y#s');
1891 itShouldResolve('http://a/bb/ccc/d;p?q', '', 'http://a/bb/ccc/d;p?q');
1892 itShouldResolve('http://a/bb/ccc/d;p?q', '.', 'http://a/bb/ccc/');
1893 itShouldResolve('http://a/bb/ccc/d;p?q', './', 'http://a/bb/ccc/');
1894 itShouldResolve('http://a/bb/ccc/d;p?q', '..', 'http://a/bb/');
1895 itShouldResolve('http://a/bb/ccc/d;p?q', '../', 'http://a/bb/');
1896 itShouldResolve('http://a/bb/ccc/d;p?q', '../g', 'http://a/bb/g');
1897 itShouldResolve('http://a/bb/ccc/d;p?q', '../..', 'http://a/');
1898 itShouldResolve('http://a/bb/ccc/d;p?q', '../../', 'http://a/');
1899 itShouldResolve('http://a/bb/ccc/d;p?q', '../../g', 'http://a/g');
1900 });
1901
1902 describe('RFC3986 abnormal examples', () => {
1903 itShouldResolve('http://a/bb/ccc/d;p?q', '../../../g', 'http://a/g');
1904 itShouldResolve('http://a/bb/ccc/d;p?q', '../../../../g', 'http://a/g');
1905 itShouldResolve('http://a/bb/ccc/d;p?q', '/./g', 'http://a/g');
1906 itShouldResolve('http://a/bb/ccc/d;p?q', '/../g', 'http://a/g');
1907 itShouldResolve('http://a/bb/ccc/d;p?q', 'g.', 'http://a/bb/ccc/g.');
1908 itShouldResolve('http://a/bb/ccc/d;p?q', '.g', 'http://a/bb/ccc/.g');
1909 itShouldResolve('http://a/bb/ccc/d;p?q', 'g..', 'http://a/bb/ccc/g..');
1910 itShouldResolve('http://a/bb/ccc/d;p?q', '..g', 'http://a/bb/ccc/..g');
1911 itShouldResolve('http://a/bb/ccc/d;p?q', './../g', 'http://a/bb/g');
1912 itShouldResolve('http://a/bb/ccc/d;p?q', './g/.', 'http://a/bb/ccc/g/');
1913 itShouldResolve('http://a/bb/ccc/d;p?q', 'g/./h', 'http://a/bb/ccc/g/h');
1914 itShouldResolve('http://a/bb/ccc/d;p?q', 'g/../h', 'http://a/bb/ccc/h');
1915 itShouldResolve('http://a/bb/ccc/d;p?q', 'g;x=1/./y', 'http://a/bb/ccc/g;x=1/y');
1916 itShouldResolve('http://a/bb/ccc/d;p?q', 'g;x=1/../y', 'http://a/bb/ccc/y');
1917 itShouldResolve('http://a/bb/ccc/d;p?q', 'g?y/./x', 'http://a/bb/ccc/g?y/./x');
1918 itShouldResolve('http://a/bb/ccc/d;p?q', 'g?y/../x', 'http://a/bb/ccc/g?y/../x');
1919 itShouldResolve('http://a/bb/ccc/d;p?q', 'g#s/./x', 'http://a/bb/ccc/g#s/./x');
1920 itShouldResolve('http://a/bb/ccc/d;p?q', 'g#s/../x', 'http://a/bb/ccc/g#s/../x');
1921 itShouldResolve('http://a/bb/ccc/d;p?q', 'http:g', 'http:g');
1922 });
1923
1924 describe('RFC3986 normal examples with trailing slash in base IRI', () => {
1925 itShouldResolve('http://a/bb/ccc/d/', 'g:h', 'g:h');
1926 itShouldResolve('http://a/bb/ccc/d/', 'g', 'http://a/bb/ccc/d/g');
1927 itShouldResolve('http://a/bb/ccc/d/', './g', 'http://a/bb/ccc/d/g');
1928 itShouldResolve('http://a/bb/ccc/d/', 'g/', 'http://a/bb/ccc/d/g/');
1929 itShouldResolve('http://a/bb/ccc/d/', '/g', 'http://a/g');
1930 itShouldResolve('http://a/bb/ccc/d/', '//g', 'http://g');
1931 itShouldResolve('http://a/bb/ccc/d/', '?y', 'http://a/bb/ccc/d/?y');
1932 itShouldResolve('http://a/bb/ccc/d/', 'g?y', 'http://a/bb/ccc/d/g?y');
1933 itShouldResolve('http://a/bb/ccc/d/', '#s', 'http://a/bb/ccc/d/#s');
1934 itShouldResolve('http://a/bb/ccc/d/', 'g#s', 'http://a/bb/ccc/d/g#s');
1935 itShouldResolve('http://a/bb/ccc/d/', 'g?y#s', 'http://a/bb/ccc/d/g?y#s');
1936 itShouldResolve('http://a/bb/ccc/d/', ';x', 'http://a/bb/ccc/d/;x');
1937 itShouldResolve('http://a/bb/ccc/d/', 'g;x', 'http://a/bb/ccc/d/g;x');
1938 itShouldResolve('http://a/bb/ccc/d/', 'g;x?y#s', 'http://a/bb/ccc/d/g;x?y#s');
1939 itShouldResolve('http://a/bb/ccc/d/', '', 'http://a/bb/ccc/d/');
1940 itShouldResolve('http://a/bb/ccc/d/', '.', 'http://a/bb/ccc/d/');
1941 itShouldResolve('http://a/bb/ccc/d/', './', 'http://a/bb/ccc/d/');
1942 itShouldResolve('http://a/bb/ccc/d/', '..', 'http://a/bb/ccc/');
1943 itShouldResolve('http://a/bb/ccc/d/', '../', 'http://a/bb/ccc/');
1944 itShouldResolve('http://a/bb/ccc/d/', '../g', 'http://a/bb/ccc/g');
1945 itShouldResolve('http://a/bb/ccc/d/', '../..', 'http://a/bb/');
1946 itShouldResolve('http://a/bb/ccc/d/', '../../', 'http://a/bb/');
1947 itShouldResolve('http://a/bb/ccc/d/', '../../g', 'http://a/bb/g');
1948 });
1949
1950 describe('RFC3986 abnormal examples with trailing slash in base IRI', () => {
1951 itShouldResolve('http://a/bb/ccc/d/', '../../../g', 'http://a/g');
1952 itShouldResolve('http://a/bb/ccc/d/', '../../../../g', 'http://a/g');
1953 itShouldResolve('http://a/bb/ccc/d/', '/./g', 'http://a/g');
1954 itShouldResolve('http://a/bb/ccc/d/', '/../g', 'http://a/g');
1955 itShouldResolve('http://a/bb/ccc/d/', 'g.', 'http://a/bb/ccc/d/g.');
1956 itShouldResolve('http://a/bb/ccc/d/', '.g', 'http://a/bb/ccc/d/.g');
1957 itShouldResolve('http://a/bb/ccc/d/', 'g..', 'http://a/bb/ccc/d/g..');
1958 itShouldResolve('http://a/bb/ccc/d/', '..g', 'http://a/bb/ccc/d/..g');
1959 itShouldResolve('http://a/bb/ccc/d/', './../g', 'http://a/bb/ccc/g');
1960 itShouldResolve('http://a/bb/ccc/d/', './g/.', 'http://a/bb/ccc/d/g/');
1961 itShouldResolve('http://a/bb/ccc/d/', 'g/./h', 'http://a/bb/ccc/d/g/h');
1962 itShouldResolve('http://a/bb/ccc/d/', 'g/../h', 'http://a/bb/ccc/d/h');
1963 itShouldResolve('http://a/bb/ccc/d/', 'g;x=1/./y', 'http://a/bb/ccc/d/g;x=1/y');
1964 itShouldResolve('http://a/bb/ccc/d/', 'g;x=1/../y', 'http://a/bb/ccc/d/y');
1965 itShouldResolve('http://a/bb/ccc/d/', 'g?y/./x', 'http://a/bb/ccc/d/g?y/./x');
1966 itShouldResolve('http://a/bb/ccc/d/', 'g?y/../x', 'http://a/bb/ccc/d/g?y/../x');
1967 itShouldResolve('http://a/bb/ccc/d/', 'g#s/./x', 'http://a/bb/ccc/d/g#s/./x');
1968 itShouldResolve('http://a/bb/ccc/d/', 'g#s/../x', 'http://a/bb/ccc/d/g#s/../x');
1969 itShouldResolve('http://a/bb/ccc/d/', 'http:g', 'http:g');
1970 });
1971
1972 describe('RFC3986 normal examples with /. in the base IRI', () => {
1973 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g:h', 'g:h');
1974 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g', 'http://a/bb/ccc/g');
1975 itShouldResolve('http://a/bb/ccc/./d;p?q', './g', 'http://a/bb/ccc/g');
1976 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g/', 'http://a/bb/ccc/g/');
1977 itShouldResolve('http://a/bb/ccc/./d;p?q', '/g', 'http://a/g');
1978 itShouldResolve('http://a/bb/ccc/./d;p?q', '//g', 'http://g');
1979 itShouldResolve('http://a/bb/ccc/./d;p?q', '?y', 'http://a/bb/ccc/./d;p?y');
1980 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g?y', 'http://a/bb/ccc/g?y');
1981 itShouldResolve('http://a/bb/ccc/./d;p?q', '#s', 'http://a/bb/ccc/./d;p?q#s');
1982 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g#s', 'http://a/bb/ccc/g#s');
1983 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g?y#s', 'http://a/bb/ccc/g?y#s');
1984 itShouldResolve('http://a/bb/ccc/./d;p?q', ';x', 'http://a/bb/ccc/;x');
1985 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g;x', 'http://a/bb/ccc/g;x');
1986 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g;x?y#s', 'http://a/bb/ccc/g;x?y#s');
1987 itShouldResolve('http://a/bb/ccc/./d;p?q', '', 'http://a/bb/ccc/./d;p?q');
1988 itShouldResolve('http://a/bb/ccc/./d;p?q', '.', 'http://a/bb/ccc/');
1989 itShouldResolve('http://a/bb/ccc/./d;p?q', './', 'http://a/bb/ccc/');
1990 itShouldResolve('http://a/bb/ccc/./d;p?q', '..', 'http://a/bb/');
1991 itShouldResolve('http://a/bb/ccc/./d;p?q', '../', 'http://a/bb/');
1992 itShouldResolve('http://a/bb/ccc/./d;p?q', '../g', 'http://a/bb/g');
1993 itShouldResolve('http://a/bb/ccc/./d;p?q', '../..', 'http://a/');
1994 itShouldResolve('http://a/bb/ccc/./d;p?q', '../../', 'http://a/');
1995 itShouldResolve('http://a/bb/ccc/./d;p?q', '../../g', 'http://a/g');
1996 });
1997
1998 describe('RFC3986 abnormal examples with /. in the base IRI', () => {
1999 itShouldResolve('http://a/bb/ccc/./d;p?q', '../../../g', 'http://a/g');
2000 itShouldResolve('http://a/bb/ccc/./d;p?q', '../../../../g', 'http://a/g');
2001 itShouldResolve('http://a/bb/ccc/./d;p?q', '/./g', 'http://a/g');
2002 itShouldResolve('http://a/bb/ccc/./d;p?q', '/../g', 'http://a/g');
2003 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g.', 'http://a/bb/ccc/g.');
2004 itShouldResolve('http://a/bb/ccc/./d;p?q', '.g', 'http://a/bb/ccc/.g');
2005 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g..', 'http://a/bb/ccc/g..');
2006 itShouldResolve('http://a/bb/ccc/./d;p?q', '..g', 'http://a/bb/ccc/..g');
2007 itShouldResolve('http://a/bb/ccc/./d;p?q', './../g', 'http://a/bb/g');
2008 itShouldResolve('http://a/bb/ccc/./d;p?q', './g/.', 'http://a/bb/ccc/g/');
2009 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g/./h', 'http://a/bb/ccc/g/h');
2010 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g/../h', 'http://a/bb/ccc/h');
2011 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g;x=1/./y', 'http://a/bb/ccc/g;x=1/y');
2012 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g;x=1/../y', 'http://a/bb/ccc/y');
2013 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g?y/./x', 'http://a/bb/ccc/g?y/./x');
2014 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g?y/../x', 'http://a/bb/ccc/g?y/../x');
2015 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g#s/./x', 'http://a/bb/ccc/g#s/./x');
2016 itShouldResolve('http://a/bb/ccc/./d;p?q', 'g#s/../x', 'http://a/bb/ccc/g#s/../x');
2017 itShouldResolve('http://a/bb/ccc/./d;p?q', 'http:g', 'http:g');
2018 });
2019
2020 describe('RFC3986 normal examples with /.. in the base IRI', () => {
2021 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g:h', 'g:h');
2022 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g', 'http://a/bb/g');
2023 itShouldResolve('http://a/bb/ccc/../d;p?q', './g', 'http://a/bb/g');
2024 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g/', 'http://a/bb/g/');
2025 itShouldResolve('http://a/bb/ccc/../d;p?q', '/g', 'http://a/g');
2026 itShouldResolve('http://a/bb/ccc/../d;p?q', '//g', 'http://g');
2027 itShouldResolve('http://a/bb/ccc/../d;p?q', '?y', 'http://a/bb/ccc/../d;p?y');
2028 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g?y', 'http://a/bb/g?y');
2029 itShouldResolve('http://a/bb/ccc/../d;p?q', '#s', 'http://a/bb/ccc/../d;p?q#s');
2030 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g#s', 'http://a/bb/g#s');
2031 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g?y#s', 'http://a/bb/g?y#s');
2032 itShouldResolve('http://a/bb/ccc/../d;p?q', ';x', 'http://a/bb/;x');
2033 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g;x', 'http://a/bb/g;x');
2034 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g;x?y#s', 'http://a/bb/g;x?y#s');
2035 itShouldResolve('http://a/bb/ccc/../d;p?q', '', 'http://a/bb/ccc/../d;p?q');
2036 itShouldResolve('http://a/bb/ccc/../d;p?q', '.', 'http://a/bb/');
2037 itShouldResolve('http://a/bb/ccc/../d;p?q', './', 'http://a/bb/');
2038 itShouldResolve('http://a/bb/ccc/../d;p?q', '..', 'http://a/');
2039 itShouldResolve('http://a/bb/ccc/../d;p?q', '../', 'http://a/');
2040 itShouldResolve('http://a/bb/ccc/../d;p?q', '../g', 'http://a/g');
2041 itShouldResolve('http://a/bb/ccc/../d;p?q', '../..', 'http://a/');
2042 itShouldResolve('http://a/bb/ccc/../d;p?q', '../../', 'http://a/');
2043 itShouldResolve('http://a/bb/ccc/../d;p?q', '../../g', 'http://a/g');
2044 });
2045
2046 describe('RFC3986 abnormal examples with /.. in the base IRI', () => {
2047 itShouldResolve('http://a/bb/ccc/../d;p?q', '../../../g', 'http://a/g');
2048 itShouldResolve('http://a/bb/ccc/../d;p?q', '../../../../g', 'http://a/g');
2049 itShouldResolve('http://a/bb/ccc/../d;p?q', '/./g', 'http://a/g');
2050 itShouldResolve('http://a/bb/ccc/../d;p?q', '/../g', 'http://a/g');
2051 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g.', 'http://a/bb/g.');
2052 itShouldResolve('http://a/bb/ccc/../d;p?q', '.g', 'http://a/bb/.g');
2053 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g..', 'http://a/bb/g..');
2054 itShouldResolve('http://a/bb/ccc/../d;p?q', '..g', 'http://a/bb/..g');
2055 itShouldResolve('http://a/bb/ccc/../d;p?q', './../g', 'http://a/g');
2056 itShouldResolve('http://a/bb/ccc/../d;p?q', './g/.', 'http://a/bb/g/');
2057 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g/./h', 'http://a/bb/g/h');
2058 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g/../h', 'http://a/bb/h');
2059 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g;x=1/./y', 'http://a/bb/g;x=1/y');
2060 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g;x=1/../y', 'http://a/bb/y');
2061 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g?y/./x', 'http://a/bb/g?y/./x');
2062 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g?y/../x', 'http://a/bb/g?y/../x');
2063 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g#s/./x', 'http://a/bb/g#s/./x');
2064 itShouldResolve('http://a/bb/ccc/../d;p?q', 'g#s/../x', 'http://a/bb/g#s/../x');
2065 itShouldResolve('http://a/bb/ccc/../d;p?q', 'http:g', 'http:g');
2066 });
2067
2068 describe('RFC3986 normal examples with trailing /. in the base IRI', () => {
2069 itShouldResolve('http://a/bb/ccc/.', 'g:h', 'g:h');
2070 itShouldResolve('http://a/bb/ccc/.', 'g', 'http://a/bb/ccc/g');
2071 itShouldResolve('http://a/bb/ccc/.', './g', 'http://a/bb/ccc/g');
2072 itShouldResolve('http://a/bb/ccc/.', 'g/', 'http://a/bb/ccc/g/');
2073 itShouldResolve('http://a/bb/ccc/.', '/g', 'http://a/g');
2074 itShouldResolve('http://a/bb/ccc/.', '//g', 'http://g');
2075 itShouldResolve('http://a/bb/ccc/.', '?y', 'http://a/bb/ccc/.?y');
2076 itShouldResolve('http://a/bb/ccc/.', 'g?y', 'http://a/bb/ccc/g?y');
2077 itShouldResolve('http://a/bb/ccc/.', '#s', 'http://a/bb/ccc/.#s');
2078 itShouldResolve('http://a/bb/ccc/.', 'g#s', 'http://a/bb/ccc/g#s');
2079 itShouldResolve('http://a/bb/ccc/.', 'g?y#s', 'http://a/bb/ccc/g?y#s');
2080 itShouldResolve('http://a/bb/ccc/.', ';x', 'http://a/bb/ccc/;x');
2081 itShouldResolve('http://a/bb/ccc/.', 'g;x', 'http://a/bb/ccc/g;x');
2082 itShouldResolve('http://a/bb/ccc/.', 'g;x?y#s', 'http://a/bb/ccc/g;x?y#s');
2083 itShouldResolve('http://a/bb/ccc/.', '', 'http://a/bb/ccc/.');
2084 itShouldResolve('http://a/bb/ccc/.', '.', 'http://a/bb/ccc/');
2085 itShouldResolve('http://a/bb/ccc/.', './', 'http://a/bb/ccc/');
2086 itShouldResolve('http://a/bb/ccc/.', '..', 'http://a/bb/');
2087 itShouldResolve('http://a/bb/ccc/.', '../', 'http://a/bb/');
2088 itShouldResolve('http://a/bb/ccc/.', '../g', 'http://a/bb/g');
2089 itShouldResolve('http://a/bb/ccc/.', '../..', 'http://a/');
2090 itShouldResolve('http://a/bb/ccc/.', '../../', 'http://a/');
2091 itShouldResolve('http://a/bb/ccc/.', '../../g', 'http://a/g');
2092 });
2093
2094 describe('RFC3986 abnormal examples with trailing /. in the base IRI', () => {
2095 itShouldResolve('http://a/bb/ccc/.', '../../../g', 'http://a/g');
2096 itShouldResolve('http://a/bb/ccc/.', '../../../../g', 'http://a/g');
2097 itShouldResolve('http://a/bb/ccc/.', '/./g', 'http://a/g');
2098 itShouldResolve('http://a/bb/ccc/.', '/../g', 'http://a/g');
2099 itShouldResolve('http://a/bb/ccc/.', 'g.', 'http://a/bb/ccc/g.');
2100 itShouldResolve('http://a/bb/ccc/.', '.g', 'http://a/bb/ccc/.g');
2101 itShouldResolve('http://a/bb/ccc/.', 'g..', 'http://a/bb/ccc/g..');
2102 itShouldResolve('http://a/bb/ccc/.', '..g', 'http://a/bb/ccc/..g');
2103 itShouldResolve('http://a/bb/ccc/.', './../g', 'http://a/bb/g');
2104 itShouldResolve('http://a/bb/ccc/.', './g/.', 'http://a/bb/ccc/g/');
2105 itShouldResolve('http://a/bb/ccc/.', 'g/./h', 'http://a/bb/ccc/g/h');
2106 itShouldResolve('http://a/bb/ccc/.', 'g/../h', 'http://a/bb/ccc/h');
2107 itShouldResolve('http://a/bb/ccc/.', 'g;x=1/./y', 'http://a/bb/ccc/g;x=1/y');
2108 itShouldResolve('http://a/bb/ccc/.', 'g;x=1/../y', 'http://a/bb/ccc/y');
2109 itShouldResolve('http://a/bb/ccc/.', 'g?y/./x', 'http://a/bb/ccc/g?y/./x');
2110 itShouldResolve('http://a/bb/ccc/.', 'g?y/../x', 'http://a/bb/ccc/g?y/../x');
2111 itShouldResolve('http://a/bb/ccc/.', 'g#s/./x', 'http://a/bb/ccc/g#s/./x');
2112 itShouldResolve('http://a/bb/ccc/.', 'g#s/../x', 'http://a/bb/ccc/g#s/../x');
2113 itShouldResolve('http://a/bb/ccc/.', 'http:g', 'http:g');
2114 });
2115
2116 describe('RFC3986 normal examples with trailing /.. in the base IRI', () => {
2117 itShouldResolve('http://a/bb/ccc/..', 'g:h', 'g:h');
2118 itShouldResolve('http://a/bb/ccc/..', 'g', 'http://a/bb/ccc/g');
2119 itShouldResolve('http://a/bb/ccc/..', './g', 'http://a/bb/ccc/g');
2120 itShouldResolve('http://a/bb/ccc/..', 'g/', 'http://a/bb/ccc/g/');
2121 itShouldResolve('http://a/bb/ccc/..', '/g', 'http://a/g');
2122 itShouldResolve('http://a/bb/ccc/..', '//g', 'http://g');
2123 itShouldResolve('http://a/bb/ccc/..', '?y', 'http://a/bb/ccc/..?y');
2124 itShouldResolve('http://a/bb/ccc/..', 'g?y', 'http://a/bb/ccc/g?y');
2125 itShouldResolve('http://a/bb/ccc/..', '#s', 'http://a/bb/ccc/..#s');
2126 itShouldResolve('http://a/bb/ccc/..', 'g#s', 'http://a/bb/ccc/g#s');
2127 itShouldResolve('http://a/bb/ccc/..', 'g?y#s', 'http://a/bb/ccc/g?y#s');
2128 itShouldResolve('http://a/bb/ccc/..', ';x', 'http://a/bb/ccc/;x');
2129 itShouldResolve('http://a/bb/ccc/..', 'g;x', 'http://a/bb/ccc/g;x');
2130 itShouldResolve('http://a/bb/ccc/..', 'g;x?y#s', 'http://a/bb/ccc/g;x?y#s');
2131 itShouldResolve('http://a/bb/ccc/..', '', 'http://a/bb/ccc/..');
2132 itShouldResolve('http://a/bb/ccc/..', '.', 'http://a/bb/ccc/');
2133 itShouldResolve('http://a/bb/ccc/..', './', 'http://a/bb/ccc/');
2134 itShouldResolve('http://a/bb/ccc/..', '..', 'http://a/bb/');
2135 itShouldResolve('http://a/bb/ccc/..', '../', 'http://a/bb/');
2136 itShouldResolve('http://a/bb/ccc/..', '../g', 'http://a/bb/g');
2137 itShouldResolve('http://a/bb/ccc/..', '../..', 'http://a/');
2138 itShouldResolve('http://a/bb/ccc/..', '../../', 'http://a/');
2139 itShouldResolve('http://a/bb/ccc/..', '../../g', 'http://a/g');
2140 });
2141
2142 describe('RFC3986 abnormal examples with trailing /.. in the base IRI', () => {
2143 itShouldResolve('http://a/bb/ccc/..', '../../../g', 'http://a/g');
2144 itShouldResolve('http://a/bb/ccc/..', '../../../../g', 'http://a/g');
2145 itShouldResolve('http://a/bb/ccc/..', '/./g', 'http://a/g');
2146 itShouldResolve('http://a/bb/ccc/..', '/../g', 'http://a/g');
2147 itShouldResolve('http://a/bb/ccc/..', 'g.', 'http://a/bb/ccc/g.');
2148 itShouldResolve('http://a/bb/ccc/..', '.g', 'http://a/bb/ccc/.g');
2149 itShouldResolve('http://a/bb/ccc/..', 'g..', 'http://a/bb/ccc/g..');
2150 itShouldResolve('http://a/bb/ccc/..', '..g', 'http://a/bb/ccc/..g');
2151 itShouldResolve('http://a/bb/ccc/..', './../g', 'http://a/bb/g');
2152 itShouldResolve('http://a/bb/ccc/..', './g/.', 'http://a/bb/ccc/g/');
2153 itShouldResolve('http://a/bb/ccc/..', 'g/./h', 'http://a/bb/ccc/g/h');
2154 itShouldResolve('http://a/bb/ccc/..', 'g/../h', 'http://a/bb/ccc/h');
2155 itShouldResolve('http://a/bb/ccc/..', 'g;x=1/./y', 'http://a/bb/ccc/g;x=1/y');
2156 itShouldResolve('http://a/bb/ccc/..', 'g;x=1/../y', 'http://a/bb/ccc/y');
2157 itShouldResolve('http://a/bb/ccc/..', 'g?y/./x', 'http://a/bb/ccc/g?y/./x');
2158 itShouldResolve('http://a/bb/ccc/..', 'g?y/../x', 'http://a/bb/ccc/g?y/../x');
2159 itShouldResolve('http://a/bb/ccc/..', 'g#s/./x', 'http://a/bb/ccc/g#s/./x');
2160 itShouldResolve('http://a/bb/ccc/..', 'g#s/../x', 'http://a/bb/ccc/g#s/../x');
2161 itShouldResolve('http://a/bb/ccc/..', 'http:g', 'http:g');
2162 });
2163
2164 describe('RFC3986 normal examples with fragment in base IRI', () => {
2165 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g:h', 'g:h');
2166 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g', 'http://a/bb/ccc/g');
2167 itShouldResolve('http://a/bb/ccc/d;p?q#f', './g', 'http://a/bb/ccc/g');
2168 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g/', 'http://a/bb/ccc/g/');
2169 itShouldResolve('http://a/bb/ccc/d;p?q#f', '/g', 'http://a/g');
2170 itShouldResolve('http://a/bb/ccc/d;p?q#f', '//g', 'http://g');
2171 itShouldResolve('http://a/bb/ccc/d;p?q#f', '?y', 'http://a/bb/ccc/d;p?y');
2172 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g?y', 'http://a/bb/ccc/g?y');
2173 itShouldResolve('http://a/bb/ccc/d;p?q#f', '#s', 'http://a/bb/ccc/d;p?q#s');
2174 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g#s', 'http://a/bb/ccc/g#s');
2175 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g?y#s', 'http://a/bb/ccc/g?y#s');
2176 itShouldResolve('http://a/bb/ccc/d;p?q#f', ';x', 'http://a/bb/ccc/;x');
2177 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g;x', 'http://a/bb/ccc/g;x');
2178 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g;x?y#s', 'http://a/bb/ccc/g;x?y#s');
2179 itShouldResolve('http://a/bb/ccc/d;p?q#f', '', 'http://a/bb/ccc/d;p?q');
2180 itShouldResolve('http://a/bb/ccc/d;p?q#f', '.', 'http://a/bb/ccc/');
2181 itShouldResolve('http://a/bb/ccc/d;p?q#f', './', 'http://a/bb/ccc/');
2182 itShouldResolve('http://a/bb/ccc/d;p?q#f', '..', 'http://a/bb/');
2183 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../', 'http://a/bb/');
2184 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../g', 'http://a/bb/g');
2185 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../..', 'http://a/');
2186 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../../', 'http://a/');
2187 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../../g', 'http://a/g');
2188 });
2189
2190 describe('RFC3986 abnormal examples with fragment in base IRI', () => {
2191 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../../../g', 'http://a/g');
2192 itShouldResolve('http://a/bb/ccc/d;p?q#f', '../../../../g', 'http://a/g');
2193 itShouldResolve('http://a/bb/ccc/d;p?q#f', '/./g', 'http://a/g');
2194 itShouldResolve('http://a/bb/ccc/d;p?q#f', '/../g', 'http://a/g');
2195 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g.', 'http://a/bb/ccc/g.');
2196 itShouldResolve('http://a/bb/ccc/d;p?q#f', '.g', 'http://a/bb/ccc/.g');
2197 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g..', 'http://a/bb/ccc/g..');
2198 itShouldResolve('http://a/bb/ccc/d;p?q#f', '..g', 'http://a/bb/ccc/..g');
2199 itShouldResolve('http://a/bb/ccc/d;p?q#f', './../g', 'http://a/bb/g');
2200 itShouldResolve('http://a/bb/ccc/d;p?q#f', './g/.', 'http://a/bb/ccc/g/');
2201 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g/./h', 'http://a/bb/ccc/g/h');
2202 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g/../h', 'http://a/bb/ccc/h');
2203 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g;x=1/./y', 'http://a/bb/ccc/g;x=1/y');
2204 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g;x=1/../y', 'http://a/bb/ccc/y');
2205 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g?y/./x', 'http://a/bb/ccc/g?y/./x');
2206 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g?y/../x', 'http://a/bb/ccc/g?y/../x');
2207 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g#s/./x', 'http://a/bb/ccc/g#s/./x');
2208 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'g#s/../x', 'http://a/bb/ccc/g#s/../x');
2209 itShouldResolve('http://a/bb/ccc/d;p?q#f', 'http:g', 'http:g');
2210 });
2211
2212 describe('RFC3986 normal examples with file path', () => {
2213 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g:h', 'g:h');
2214 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g', 'file:///a/bb/ccc/g');
2215 itShouldResolve('file:///a/bb/ccc/d;p?q', './g', 'file:///a/bb/ccc/g');
2216 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g/', 'file:///a/bb/ccc/g/');
2217 itShouldResolve('file:///a/bb/ccc/d;p?q', '/g', 'file:///g');
2218 itShouldResolve('file:///a/bb/ccc/d;p?q', '//g', 'file://g');
2219 itShouldResolve('file:///a/bb/ccc/d;p?q', '?y', 'file:///a/bb/ccc/d;p?y');
2220 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g?y', 'file:///a/bb/ccc/g?y');
2221 itShouldResolve('file:///a/bb/ccc/d;p?q', '#s', 'file:///a/bb/ccc/d;p?q#s');
2222 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g#s', 'file:///a/bb/ccc/g#s');
2223 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g?y#s', 'file:///a/bb/ccc/g?y#s');
2224 itShouldResolve('file:///a/bb/ccc/d;p?q', ';x', 'file:///a/bb/ccc/;x');
2225 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g;x', 'file:///a/bb/ccc/g;x');
2226 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g;x?y#s', 'file:///a/bb/ccc/g;x?y#s');
2227 itShouldResolve('file:///a/bb/ccc/d;p?q', '', 'file:///a/bb/ccc/d;p?q');
2228 itShouldResolve('file:///a/bb/ccc/d;p?q', '.', 'file:///a/bb/ccc/');
2229 itShouldResolve('file:///a/bb/ccc/d;p?q', './', 'file:///a/bb/ccc/');
2230 itShouldResolve('file:///a/bb/ccc/d;p?q', '..', 'file:///a/bb/');
2231 itShouldResolve('file:///a/bb/ccc/d;p?q', '../', 'file:///a/bb/');
2232 itShouldResolve('file:///a/bb/ccc/d;p?q', '../g', 'file:///a/bb/g');
2233 itShouldResolve('file:///a/bb/ccc/d;p?q', '../..', 'file:///a/');
2234 itShouldResolve('file:///a/bb/ccc/d;p?q', '../../', 'file:///a/');
2235 itShouldResolve('file:///a/bb/ccc/d;p?q', '../../g', 'file:///a/g');
2236 });
2237
2238 describe('RFC3986 abnormal examples with file path', () => {
2239 itShouldResolve('file:///a/bb/ccc/d;p?q', '../../../g', 'file:///g');
2240 itShouldResolve('file:///a/bb/ccc/d;p?q', '../../../../g', 'file:///g');
2241 itShouldResolve('file:///a/bb/ccc/d;p?q', '/./g', 'file:///g');
2242 itShouldResolve('file:///a/bb/ccc/d;p?q', '/../g', 'file:///g');
2243 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g.', 'file:///a/bb/ccc/g.');
2244 itShouldResolve('file:///a/bb/ccc/d;p?q', '.g', 'file:///a/bb/ccc/.g');
2245 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g..', 'file:///a/bb/ccc/g..');
2246 itShouldResolve('file:///a/bb/ccc/d;p?q', '..g', 'file:///a/bb/ccc/..g');
2247 itShouldResolve('file:///a/bb/ccc/d;p?q', './../g', 'file:///a/bb/g');
2248 itShouldResolve('file:///a/bb/ccc/d;p?q', './g/.', 'file:///a/bb/ccc/g/');
2249 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g/./h', 'file:///a/bb/ccc/g/h');
2250 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g/../h', 'file:///a/bb/ccc/h');
2251 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g;x=1/./y', 'file:///a/bb/ccc/g;x=1/y');
2252 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g;x=1/../y', 'file:///a/bb/ccc/y');
2253 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g?y/./x', 'file:///a/bb/ccc/g?y/./x');
2254 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g?y/../x', 'file:///a/bb/ccc/g?y/../x');
2255 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g#s/./x', 'file:///a/bb/ccc/g#s/./x');
2256 itShouldResolve('file:///a/bb/ccc/d;p?q', 'g#s/../x', 'file:///a/bb/ccc/g#s/../x');
2257 itShouldResolve('file:///a/bb/ccc/d;p?q', 'http:g', 'http:g');
2258 });
2259
2260 describe('additional cases', () => {
2261 // relative paths ending with '.'
2262 itShouldResolve('http://abc/', '.', 'http://abc/');
2263 itShouldResolve('http://abc/def/ghi', '.', 'http://abc/def/');
2264 itShouldResolve('http://abc/def/ghi', '.?a=b', 'http://abc/def/?a=b');
2265 itShouldResolve('http://abc/def/ghi', '.#a=b', 'http://abc/def/#a=b');
2266
2267 // relative paths ending with '..'
2268 itShouldResolve('http://abc/', '..', 'http://abc/');
2269 itShouldResolve('http://abc/def/ghi', '..', 'http://abc/');
2270 itShouldResolve('http://abc/def/ghi', '..?a=b', 'http://abc/?a=b');
2271 itShouldResolve('http://abc/def/ghi', '..#a=b', 'http://abc/#a=b');
2272
2273 // base path with empty subpaths (double slashes)
2274 itShouldResolve('http://ab//de//ghi', 'xyz', 'http://ab//de//xyz');
2275 itShouldResolve('http://ab//de//ghi', './xyz', 'http://ab//de//xyz');
2276 itShouldResolve('http://ab//de//ghi', '../xyz', 'http://ab//de/xyz');
2277
2278 // base path with colon (possible confusion with scheme)
2279 itShouldResolve('http://abc/d:f/ghi', 'xyz', 'http://abc/d:f/xyz');
2280 itShouldResolve('http://abc/d:f/ghi', './xyz', 'http://abc/d:f/xyz');
2281 itShouldResolve('http://abc/d:f/ghi', '../xyz', 'http://abc/xyz');
2282
2283 // base path consisting of '..' and/or '../' sequences
2284 itShouldResolve('./', 'abc', '/abc');
2285 itShouldResolve('../', 'abc', '/abc');
2286 itShouldResolve('./././', '././abc', '/abc');
2287 itShouldResolve('../../../', '../../abc', '/abc');
2288 itShouldResolve('.../././', '././abc', '.../abc');
2289
2290 // base path without authority
2291 itShouldResolve('a:b:c/', 'def/../', 'a:b:c/');
2292 itShouldResolve('a:b:c', '/def', 'a:/def');
2293 itShouldResolve('a:b/c', '/def', 'a:/def');
2294 itShouldResolve('a:', '/.', 'a:/');
2295 itShouldResolve('a:', '/..', 'a:/');
2296
2297 // base path with slashes in query string
2298 itShouldResolve('http://abc/def/ghi?q=xx/yyy/z', 'jjj', 'http://abc/def/jjj');
2299 itShouldResolve('http://abc/def/ghi?q=xx/y?y/z', 'jjj', 'http://abc/def/jjj');
2300 });
2301 });
2302 });
2303
2304 function shouldParse(parser, input) {
2305 const expected = Array.prototype.slice.call(arguments, 1);
2306 // Shift parameters as necessary
2307 if (parser.call)
2308 expected.shift();
2309 else
2310 input = parser, parser = Parser;
2311
2312 return function (done) {
2313 const results = [];
2314 const items = expected.map(mapToQuad);
2315 new parser({ baseIRI: BASE_IRI }).parse(input, (error, triple) => {
2316 expect(error).not.to.exist;
2317 if (triple)
2318 results.push(triple);
2319 else
2320 toSortedJSON(results).should.equal(toSortedJSON(items)), done();
2321 });
2322 };
2323 }
2324
2325 function mapToQuad(item) {
2326 item = item.map(t => {
2327 // don't touch if it's already an object
2328 if (typeof t === 'object')
2329 // recursively map content if it's an array
2330 return (t instanceof Array) ? mapToQuad(t) : t;
2331 // Append base to relative IRIs
2332 else if (!/^$|^["?]|:/.test(t))
2333 t = BASE_IRI + t;
2334 return termFromId(t);
2335 });
2336 return new Quad(item[0], item[1], item[2], item[3]);
2337 }
2338
2339 function toSortedJSON(triples) {
2340 triples = triples.map(t => {
2341 return JSON.stringify([
2342 t.subject.toJSON(), t.predicate.toJSON(), t.object.toJSON(), t.graph.toJSON(),
2343 ]);
2344 });
2345 triples.sort();
2346 return `[\n ${triples.join('\n ')}\n]`;
2347 }
2348
2349 function shouldNotParse(parser, input, expectedError, expectedContext) {
2350 // Shift parameters if necessary
2351 if (!parser.call)
2352 expectedContext = expectedError, expectedError = input, input = parser, parser = Parser;
2353
2354 return function (done) {
2355 new parser({ baseIRI: BASE_IRI }).parse(input, (error, triple) => {
2356 if (error) {
2357 expect(triple).not.to.exist;
2358 error.should.be.an.instanceof(Error);
2359 error.message.should.eql(expectedError);
2360 if (expectedContext) error.context.should.deep.equal(expectedContext);
2361 done();
2362 }
2363 else if (!triple)
2364 done(new Error(`Expected error ${expectedError}`));
2365 });
2366 };
2367 }
2368
2369 function itShouldResolve(baseIRI, relativeIri, expected) {
2370 let result;
2371 describe(`resolving <${relativeIri}> against <${baseIRI}>`, () => {
2372 before(done => {
2373 try {
2374 const doc = `<urn:ex:s> <urn:ex:p> <${relativeIri}>.`;
2375 new Parser({ baseIRI }).parse(doc, (error, triple) => {
2376 if (done)
2377 result = triple, done(error);
2378 done = null;
2379 });
2380 }
2381 catch (error) { done(error); }
2382 });
2383 it(`should result in ${expected}`, () => {
2384 expect(result.object.value).to.equal(expected);
2385 });
2386 });
2387 }