Skip to content

Commit 37ab09c

Browse files
committed
fix: support quoted triples in list
1 parent 2f7d6e8 commit 37ab09c

2 files changed

Lines changed: 172 additions & 10 deletions

File tree

src/N3Parser.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,16 @@ export default class N3Parser {
473473
this._saveContext('formula', this._graph, this._subject, this._predicate,
474474
this._graph = this._blankNode());
475475
return this._readSubject;
476+
case '<<':
477+
if (!this._supportsRDFStar)
478+
return this._error('Unexpected RDF* syntax', token);
479+
480+
this._saveContext('<<', this._graph, this._subject, null, null);
481+
return this._readSubject;
482+
case '>>':
483+
item = this._graph;
484+
this._graph = null;
485+
break;
476486
default:
477487
if ((item = this._readEntity(token)) === undefined)
478488
return;
@@ -854,6 +864,13 @@ export default class N3Parser {
854864
const quad = this._quad(this._subject, this._predicate, this._object,
855865
this._graph || this.DEFAULTGRAPH);
856866
this._restoreContext('<<', token);
867+
868+
// If the triple is in a list then return to reading the remaining elements
869+
if (this._contextStack.length > 0 && this._contextStack[this._contextStack.length - 1].type === 'list') {
870+
this._graph = quad;
871+
return this._readListItem(token);
872+
}
873+
857874
// If the triple was the subject, continue by reading the predicate.
858875
if (this._subject === null) {
859876
this._subject = quad;

test/N3Parser-test.js

Lines changed: 155 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,11 @@ describe('Parser', () => {
478478

479479
it('should parse a nested list',
480480
shouldParse('<a> <b> ( ( <c> ) ).',
481-
['a', 'b', '_:b0'],
482-
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
483-
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
484-
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'c'],
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-
481+
['a', 'b', '_:b0'],
482+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
483+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
484+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'c'],
485+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
488486

489487
it('should parse statements with a nested empty list',
490488
shouldParse('<a> <b> (<x> ()).',
@@ -504,13 +502,152 @@ describe('Parser', () => {
504502
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'],
505503
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
506504

507-
it('should parse statements with a list containing a blank node',
505+
it('should parse statements with a list containing a quoted triple',
506+
shouldParse('<a> <b> ( << <c> <d> <e> >> ) .',
507+
['a', 'b', '_:b0'],
508+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
509+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
510+
511+
it('should parse statements with a list containing a quoted triple and iri after',
512+
shouldParse('<a> <b> ( << <c> <d> <e> >> <f> ) .',
513+
['a', 'b', '_:b0'],
514+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
515+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
516+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'f'],
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 parse statements with a list containing a quoted triple and iri before',
520+
shouldParse('<a> <b> ( <f> << <c> <d> <e> >> ) .',
521+
['a', 'b', '_:b0'],
522+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'f'],
523+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
524+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
525+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
526+
527+
it('should parse statements with a list containing a 2 quoted triples',
528+
shouldParse('<a> <b> ( << <c> <d> <e> >> << <c1> <d1> <e1> >> ) .',
529+
['a', 'b', '_:b0'],
530+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
531+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
532+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c1', 'd1', 'e1']],
533+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
534+
535+
536+
it('should parse statements with a list containing a 3 quoted triples',
537+
shouldParse('<a> <b> ( << <c> <d> <e> >> << <c1> <d1> <e1> >> << <c2> <d2> <e2> >> ) .',
538+
['a', 'b', '_:b0'],
539+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
540+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
541+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c1', 'd1', 'e1']],
542+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
543+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c2', 'd2', 'e2']],
544+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
545+
546+
it('should parse statements with a list containing a 1 quoted triple and 2 iris',
547+
shouldParse('<a> <b> ( << <c> <d> <e> >> <h> <i> ) .',
548+
['a', 'b', '_:b0'],
549+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
550+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
551+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'h'],
552+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
553+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'i'],
554+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
555+
556+
it('should parse statements with a list containing a 1 quoted triple between 2 iris',
557+
shouldParse('<a> <b> ( <h> << <c> <d> <e> >> <i> ) .',
558+
['a', 'b', '_:b0'],
559+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'h'],
560+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
561+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
562+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
563+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'i'],
564+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
565+
566+
567+
it('should parse a nested list containing 1 quoted triple',
568+
shouldParse('<a> <b> ( ( << <c> <d> <e> >> ) ).',
569+
['a', 'b', '_:b0'],
570+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
571+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
572+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
573+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
574+
575+
it('should parse statements with a list containing a quoted triple with list as subject',
576+
shouldParse('( << <c> <d> <e> >> ) <a> <b> .',
577+
['_:b0', 'a', 'b'],
578+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
579+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
580+
581+
it('should parse statements with a list containing a quoted triple and iri after with list as subject',
582+
shouldParse('( << <c> <d> <e> >> <f> ) <a> <b> .',
583+
['_:b0', 'a', 'b'],
584+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
585+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
586+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'f'],
587+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
588+
589+
it('should parse statements with a list containing a quoted triple and iri before with list as subject',
590+
shouldParse('( <f> << <c> <d> <e> >> ) <a> <b> .',
591+
['_:b0', 'a', 'b'],
592+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'f'],
593+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
594+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
595+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
596+
597+
it('should parse statements with a list containing a 2 quoted triples with list as subject',
598+
shouldParse('( << <c> <d> <e> >> << <c1> <d1> <e1> >> ) <a> <b> .',
599+
['_:b0', 'a', 'b'],
600+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
601+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
602+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c1', 'd1', 'e1']],
603+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
604+
605+
606+
it('should parse statements with a list containing a 3 quoted triples with list as subject',
607+
shouldParse('( << <c> <d> <e> >> << <c1> <d1> <e1> >> << <c2> <d2> <e2> >> ) <a> <b> .',
608+
['_:b0', 'a', 'b'],
609+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
610+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
611+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c1', 'd1', 'e1']],
612+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
613+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c2', 'd2', 'e2']],
614+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
615+
616+
it('should parse statements with a list containing a 1 quoted triple and 2 iris with list as subject',
617+
shouldParse('( << <c> <d> <e> >> <h> <i> ) <a> <b> .',
618+
['_:b0', 'a', 'b'],
619+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
620+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
621+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'h'],
622+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
623+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'i'],
624+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
625+
626+
it('should parse statements with a list containing a 1 quoted triple between 2 iris with list as subject',
627+
shouldParse('( <h> << <c> <d> <e> >> <i> ) <a> <b> .',
628+
['_:b0', 'a', 'b'],
629+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'h'],
630+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'],
631+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
632+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'],
633+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'i'],
634+
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
635+
636+
it('should parse a nested list containing 1 quoted triple with list as subject',
637+
shouldParse('( ( << <c> <d> <e> >> ) ) <a> <b> .',
638+
['_:b0', 'a', 'b'],
639+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
640+
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
641+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', ['c', 'd', 'e']],
642+
['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
643+
644+
it('should parse statements with a list containing a blank node with list as subject',
508645
shouldParse('([]) <a> <b>.',
509646
['_:b0', 'a', 'b'],
510647
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
511648
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil']));
512649

513-
it('should parse statements with a list containing multiple blank nodes',
650+
it('should parse statements with a list containing multiple blank nodes with list as subject',
514651
shouldParse('([] [<x> <y>]) <a> <b>.',
515652
['_:b0', 'a', 'b'],
516653
['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'],
@@ -519,7 +656,7 @@ describe('Parser', () => {
519656
['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'],
520657
['_:b3', 'x', 'y']));
521658

522-
it('should parse statements with a blank node containing a list',
659+
it('should parse statements with a blank node containing a list with list as subject',
523660
shouldParse('[<a> (<b>)] <c> <d>.',
524661
['_:b0', 'c', 'd'],
525662
['_:b0', 'a', '_:b1'],
@@ -1235,6 +1372,14 @@ describe('Parser', () => {
12351372
it('should not parse RDF* in the object position',
12361373
shouldNotParse(parser, '<a> <b> <<a> <b> <c>>>.',
12371374
'Unexpected RDF* syntax on line 1.'));
1375+
1376+
it('should not parse RDF* in the object list',
1377+
shouldNotParse(parser, '<a> <b> ( <<a> <b> <c>>> ).',
1378+
'Unexpected RDF* syntax on line 1.'));
1379+
1380+
it('should not parse RDF* in the subject list',
1381+
shouldNotParse(parser, '( <<a> <b> <c>>> ) <a> <b>.',
1382+
'Unexpected RDF* syntax on line 1.'));
12381383
});
12391384

12401385
describe('A Parser instance for the TurtleStar format', () => {

0 commit comments

Comments
 (0)