From b6488ce1e785275e63f6c2f2cce2a7da0d874e26 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:15:43 +1100 Subject: [PATCH 1/7] WIP: fix n3 paths --- src/N3Parser.js | 8 +++- test/N3Parser-test.js | 86 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/N3Parser.js b/src/N3Parser.js index 1a7d7449..76414367 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -439,7 +439,7 @@ export default class N3Parser { // Was this list the parent's subject? if (this._predicate === null) { // The next token is the predicate - next = this._readPredicate; + next = this._n3Mode ? this._getPathReader(this._readPredicateOrNamedGraph) : this._readPredicate; // No list tail if this was an empty list if (this._subject === this.RDF_NIL) return next; @@ -473,6 +473,10 @@ export default class N3Parser { this._saveContext('formula', this._graph, this._subject, this._predicate, this._graph = this._blankNode()); return this._readSubject; + case '!': + if (this._n3Mode) { + return this._getPathReader(this._readPredicateOrNamedGraph); + } default: if ((item = this._readEntity(token)) === undefined) return; @@ -497,7 +501,7 @@ export default class N3Parser { // If an item was read, add it to the list if (item !== null) { // In N3 mode, the item might be a path - if (this._n3Mode && (token.type === 'IRI' || token.type === 'prefixed')) { + if (this._n3Mode) { // Create a new context to add the item's path this._saveContext('item', this._graph, list, this.RDF_FIRST, item); this._subject = item, this._predicate = null; diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 80cb2466..ceb19197 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1733,6 +1733,92 @@ describe('Parser', () => { ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], ['_:b2', 'f:son', 'ex:joe'])); + it('should parse a ! path of length 2 as subject in a list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(:joe!fam:mother) a fam:Person.', + ['ex:joe', 'f:mother', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + it('should parse a !^ path of length 3 as subject in a list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(:joe!fam:mother^fam:father) a fam:Person.', + ['ex:joe', 'f:mother', '_:b1'], + ['_:b2', 'f:father', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + it('should parse a ! path of length 2 starting with an empty list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '()!fam:mother a fam:Person.', + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + it('should parse a ! path of length 2 starting with a non-empty list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '( :a )!fam:mother a fam:Person.', + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'ex:a'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b0', 'f:mother', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + it('should parse a ! path of length 2 starting with a non-empty list in another list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(( :a )!fam:mother 1) a fam:Person.', + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'ex:a'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b0', 'f:mother', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + it('should parse a ! path of length 2 starting with an empty list in another list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(()!fam:mother 1) a fam:Person.', + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + + it('should parse a ! path of length 2 starting with an empty list in another list as second element', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(1 ()!fam:mother) a fam:Person.', + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'] + )); + + + it('should parse a ! path of length 2 starting with an empty list in another list of one element', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '(()!fam:mother) a fam:Person.', + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + + + it('should parse a birthday rule', + shouldParse(parser, + '@prefix foaf: .' + + '@prefix math: .' + + '@prefix : .' + + '' + + '{' + + ' ?x :trueOnDate ?date.' + + '} <= {' + + ' ((?date ?s!foaf:birthday)!math:difference 31622400) math:integerQuotient ?age .' + + '} .')); + + it('should parse a ! path of length 2 as nested subject in a list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '((:joe!fam:mother) 1) a fam:Person.', + ['ex:joe', 'f:mother', '_:b2'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"1"^^http://www.w3.org/2001/XMLSchema#integer'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'] + )); + it('should parse a formula as list item', shouldParse(parser, ' ( { a . } ).', ['a', 'findAll', '_:b0'], From f21edee78cb598353e372d7f5298a860d1ffdb31 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Mon, 27 Feb 2023 16:19:55 +1100 Subject: [PATCH 2/7] chore: fix formatting --- test/N3Parser-test.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index ceb19197..f08ffe9d 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1809,15 +1809,14 @@ describe('Parser', () => { it('should parse a ! path of length 2 as nested subject in a list', shouldParse(parser, '@prefix : . @prefix fam: .' + '((:joe!fam:mother) 1) a fam:Person.', - ['ex:joe', 'f:mother', '_:b2'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"1"^^http://www.w3.org/2001/XMLSchema#integer'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'] - )); + ['ex:joe', 'f:mother', '_:b2'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"1"^^http://www.w3.org/2001/XMLSchema#integer'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'])); it('should parse a formula as list item', shouldParse(parser, ' ( { a . } ).', From 61daa42b72369e11a350ccc48bddb22ada6cc760 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Thu, 23 Mar 2023 22:17:11 +1100 Subject: [PATCH 3/7] chore: add n3-star path tests --- test/N3Parser-test.js | 294 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index f08ffe9d..9139a53d 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1872,6 +1872,300 @@ describe('Parser', () => { it('should not parse nested quads', shouldNotParse(parser, '<<_:a _:b >> "c" .', 'Expected >> to follow "_:.b" on line 1.')); + + for (const [elem, value] of [['()', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], [':joe', 'ex:joe'], ['<<:joe a :Person>>', '<<:joe a :Person>>']]) { + it(`should parse a ^ path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `(${elem}^fam:son ) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'f:son', value])); + + it(`should parse a ^ path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` (${elem}^fam:son ).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'f:son', value])); + + it(`should parse a ^ path in a single element list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `(${elem}^fam:son) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'f:son', value])); + + it(`should parse a ^ path in a single element list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` (${elem}^fam:son).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'f:son', value])); + + it(`should parse a ^ path in a list as subject with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `( ${elem}^fam:son) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'f:son', value])); + + it(`should parse a ^ path in a list as object with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ( ${elem}^fam:son).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'f:son', value])); + + it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `(${elem}!fam:son ) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b1'])); + + it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` (${elem}!fam:son ).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b1'])); + + it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `(${elem}!fam:son) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b1'])); + + it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` (${elem}!fam:son).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b1'])); + + it(`should parse a ! path in a list as subject with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `( ${elem}!fam:son) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b3'])); + + it(`should parse a ! path in a list as object with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ( ${elem}!fam:son).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b3'])); + + it(`should parse a ^ path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `((${elem}^fam:son) ) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b2', 'f:son', value])); + + it(`should parse a ^ path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ((${elem}^fam:son) ).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b2', 'f:son', value])); + + it(`should parse a ^ path in a single element list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `((${elem}^fam:son)) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b2', 'f:son', value])); + + it(`should parse a ^ path in a single element list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ((${elem}^fam:son)).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b2', 'f:son', value])); + + it(`should parse a ^ path in a list as subject with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `( (${elem}^fam:son)) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b4', 'f:son', value])); + + it(`should parse a ^ path in a list as object with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ( (${elem}^fam:son)).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b4', 'f:son', value])); + + it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `((${elem}!fam:son) ) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + [value, 'f:son', '_:b2'])); + + it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ((${elem}!fam:son) ).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + [value, 'f:son', '_:b2'])); + + it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `((${elem}!fam:son)) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + [value, 'f:son', '_:b2'])); + + it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ((${elem}!fam:son)).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + + [value, 'f:son', '_:b2'])); + + it(`should parse a ! path in a list as subject with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + `( (${elem}!fam:son)) a :List.`, + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b4'])); + + it(`should parse a ! path in a list as object with path as object - starting with ${elem}`, + shouldParse(parser, '@prefix : . @prefix fam: .' + + ` ( (${elem}!fam:son)).`, + ['l', 'is', '_:b0'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], + ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], + ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], + ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [value, 'f:son', '_:b4'])); + } }); describe('A Parser instance for the N3 format with the explicitQuantifiers option', () => { From 9838ec73f2c63ca5431ba8dfe9172ed3f959d18a Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Fri, 24 Mar 2023 11:44:08 +1100 Subject: [PATCH 4/7] chore: refactor list tests --- test/N3Parser-test.js | 348 +++++++----------------------------------- 1 file changed, 57 insertions(+), 291 deletions(-) diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 9139a53d..77f04f20 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1874,297 +1874,51 @@ describe('Parser', () => { 'Expected >> to follow "_:.b" on line 1.')); for (const [elem, value] of [['()', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], [':joe', 'ex:joe'], ['<<:joe a :Person>>', '<<:joe a :Person>>']]) { - it(`should parse a ^ path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `(${elem}^fam:son ) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'f:son', value])); - - it(`should parse a ^ path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` (${elem}^fam:son ).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'f:son', value])); - - it(`should parse a ^ path in a single element list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `(${elem}^fam:son) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'f:son', value])); - - it(`should parse a ^ path in a single element list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` (${elem}^fam:son).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'f:son', value])); - - it(`should parse a ^ path in a list as subject with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `( ${elem}^fam:son) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'f:son', value])); - - it(`should parse a ^ path in a list as object with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ( ${elem}^fam:son).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'f:son', value])); - - it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `(${elem}!fam:son ) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b1'])); - - it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` (${elem}!fam:son ).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b1'])); - - it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `(${elem}!fam:son) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b1'])); - - it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` (${elem}!fam:son).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b1'])); - - it(`should parse a ! path in a list as subject with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `( ${elem}!fam:son) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b3'])); - - it(`should parse a ! path in a list as object with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ( ${elem}!fam:son).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b3'])); - - it(`should parse a ^ path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `((${elem}^fam:son) ) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b2', 'f:son', value])); - - it(`should parse a ^ path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ((${elem}^fam:son) ).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b2', 'f:son', value])); - - it(`should parse a ^ path in a single element list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `((${elem}^fam:son)) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b2', 'f:son', value])); - - it(`should parse a ^ path in a single element list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ((${elem}^fam:son)).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b2', 'f:son', value])); - - it(`should parse a ^ path in a list as subject with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `( (${elem}^fam:son)) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b4', 'f:son', value])); - - it(`should parse a ^ path in a list as object with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ( (${elem}^fam:son)).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b4', 'f:son', value])); - - it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `((${elem}!fam:son) ) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - [value, 'f:son', '_:b2'])); - - it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ((${elem}!fam:son) ).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b4'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b4', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - [value, 'f:son', '_:b2'])); - - it(`should parse a ! path in a list as subject with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `((${elem}!fam:son)) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - [value, 'f:son', '_:b2'])); - - it(`should parse a ! path in a list as object with path as subject - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ((${elem}!fam:son)).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - - [value, 'f:son', '_:b2'])); - - it(`should parse a ! path in a list as subject with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - `( (${elem}!fam:son)) a :List.`, - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b4'])); - - it(`should parse a ! path in a list as object with path as object - starting with ${elem}`, - shouldParse(parser, '@prefix : . @prefix fam: .' + - ` ( (${elem}!fam:son)).`, - ['l', 'is', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'x'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'y'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b3'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b4'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - [value, 'f:son', '_:b4'])); + for (const pathType of ['!', '^']) { + // eslint-disable-next-line no-inner-declarations + function son(bnode) { + return pathType === '!' ? [value, 'f:son', `_:b${bnode}`] : [`_:b${bnode}`, 'f:son', value]; + } + + const prefixes = '@prefix : . @prefix fam: .'; + for (const [f, triple, listPos] of [ + [x => `${prefixes}(${x}) a :List .`, ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], 'subject'], + [x => `${prefixes} (${x}) .`, ['l', 'is', '_:b0'], 'object'], + ]) { + it(`should parse a ${pathType} path in a list as ${listPos} with path as subject - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, ...list(['_:b0', '_:b1'], ['_:b2', 'x'], ['_:b3', 'y']), son('1'))); + + it(`should parse a ${pathType} path in a single element list as ${listPos} with path as subject - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, ...list(['_:b0', '_:b1']), son('1'))); + + it(`should parse a ${pathType} path in a list as ${listPos} with path as object - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), son('3'))); + + it(`should parse a ${pathType} path in a list as ${listPos} with path as subject - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, + ...list(['_:b0', '_:b1'], ['_:b3', 'x'], ['_:b4', 'y']), + ...list(['_:b1', '_:b2']), + son('2'))); + + it(`should parse a ${pathType} path in a single element list as ${listPos} with path as subject - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, + ...list(['_:b0', '_:b1']), + ...list(['_:b1', '_:b2']), + son('2'))); + + it(`should parse a ${pathType} path in a list as ${listPos} with path as object - starting with ${elem}`, + shouldParse(parser, f(`${elem}${pathType}fam:son `), + triple, + ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), + ...list(['_:b3', '_:b4']), + son('4'))); + } + } } }); @@ -2764,3 +2518,15 @@ function itShouldResolve(baseIRI, relativeIri, expected) { }); }); } + +// creates an RDF list from the input +function list(...elems) { + const arr = []; + for (let i = 0; i < elems.length; i++) { + arr.push( + [elems[i][0], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', elems[i][1]], + [elems[i][0], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', i + 1 === elems.length ? 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil' : elems[i + 1][0]] + ); + } + return arr; +} From cf3a464e09b14e57aee0729b4382ee160a72468e Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Fri, 24 Mar 2023 12:11:38 +1100 Subject: [PATCH 5/7] chore: refactor n3parser tests --- test/N3Parser-test.js | 66 ++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 77f04f20..dcb3b5d6 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1880,43 +1880,37 @@ describe('Parser', () => { return pathType === '!' ? [value, 'f:son', `_:b${bnode}`] : [`_:b${bnode}`, 'f:son', value]; } - const prefixes = '@prefix : . @prefix fam: .'; - for (const [f, triple, listPos] of [ - [x => `${prefixes}(${x}) a :List .`, ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List'], 'subject'], - [x => `${prefixes} (${x}) .`, ['l', 'is', '_:b0'], 'object'], + for (const [f, triple] of [ + [x => `(${x}) a :List .`, ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List']], + [x => ` (${x}) .`, ['l', '_:b0', 'm']], + [x => ` (${x}) .`, ['l', 'is', '_:b0']], ]) { - it(`should parse a ${pathType} path in a list as ${listPos} with path as subject - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, ...list(['_:b0', '_:b1'], ['_:b2', 'x'], ['_:b3', 'y']), son('1'))); - - it(`should parse a ${pathType} path in a single element list as ${listPos} with path as subject - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, ...list(['_:b0', '_:b1']), son('1'))); - - it(`should parse a ${pathType} path in a list as ${listPos} with path as object - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), son('3'))); - - it(`should parse a ${pathType} path in a list as ${listPos} with path as subject - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, - ...list(['_:b0', '_:b1'], ['_:b3', 'x'], ['_:b4', 'y']), - ...list(['_:b1', '_:b2']), - son('2'))); - - it(`should parse a ${pathType} path in a single element list as ${listPos} with path as subject - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, - ...list(['_:b0', '_:b1']), - ...list(['_:b1', '_:b2']), - son('2'))); - - it(`should parse a ${pathType} path in a list as ${listPos} with path as object - starting with ${elem}`, - shouldParse(parser, f(`${elem}${pathType}fam:son `), - triple, - ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), - ...list(['_:b3', '_:b4']), - son('4'))); + // eslint-disable-next-line no-inner-declarations + function check(content, ...triples) { + it(`should parse [${f(content)}]`, + shouldParse(parser, `@prefix : . @prefix fam: .${f(content)}`, + triple, ...triples)); + } + + check(`${elem}${pathType}fam:son`, ...list(['_:b0', '_:b1']), son('1')); + check(`(${elem}${pathType}fam:son)`, ...list(['_:b0', '_:b1']), ...list(['_:b1', '_:b2']), son('2')); + + check(`${elem}${pathType}fam:son `, ...list(['_:b0', '_:b1'], ['_:b2', 'x'], ['_:b3', 'y']), son('1')); + check(` ${elem}${pathType}fam:son `, ...list(['_:b0', 'x'], ['_:b1', '_:b2'], ['_:b3', 'y']), son('2')); + check(` ${elem}${pathType}fam:son`, ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), son('3')); + + check(`(${elem}${pathType}fam:son) `, + ...list(['_:b0', '_:b1'], ['_:b3', 'x'], ['_:b4', 'y']), + ...list(['_:b1', '_:b2']), + son('2')); + check(` (${elem}${pathType}fam:son) `, + ...list(['_:b0', 'x'], ['_:b1', '_:b2'], ['_:b4', 'y']), + ...list(['_:b2', '_:b3']), + son('3')); + check(` (${elem}${pathType}fam:son)`, + ...list(['_:b0', 'x'], ['_:b1', 'y'], ['_:b2', '_:b3']), + ...list(['_:b3', '_:b4']), + son('4')); } } } From 1d7129750fcd34a25bc34e0f18895f3d54ba612c Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Fri, 24 Mar 2023 14:50:16 +1100 Subject: [PATCH 6/7] WIP: fix problems with empty list parsing --- src/N3Parser.js | 26 +++++++++++++++++++++----- test/N3Parser-test.js | 11 +++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/N3Parser.js b/src/N3Parser.js index 76414367..734be7b6 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -434,8 +434,20 @@ export default class N3Parser { this._restoreContext('list', token); // If this list is contained within a parent list, return the membership quad here. // This will be ` rdf:first .`. - if (stack.length !== 0 && stack[stack.length - 1].type === 'list') - this._emit(this._subject, this._predicate, this._object, this._graph); + if (stack.length !== 0 && stack[stack.length - 1].type === 'list') { + if (this._n3Mode) { + const { _subject, _predicate, _object, _graph } = this; + if (_object.termType !== "NamedNode" || _object.value !== this.RDF_NIL.value) { + this._emit(_object, this.RDF_REST, this.RDF_NIL, _graph); + } + return this._getPathReader(tk => { + this._emit(_subject, _predicate, this._object, _graph); + return this._readListItem(tk) + }); + } else { + this._emit(this._subject, this._predicate, this._object, this._graph); + } + } // Was this list the parent's subject? if (this._predicate === null) { // The next token is the predicate @@ -474,9 +486,13 @@ export default class N3Parser { this._graph = this._blankNode()); return this._readSubject; case '!': - if (this._n3Mode) { - return this._getPathReader(this._readPredicateOrNamedGraph); - } + case '^': + // Continue path + if (!this._n3Mode) + return this._error('Unexpected path', token); + + this._saveContext('item', this._graph, list, this.RDF_FIRST, item); + return this._getPathReader(this._readListItem); default: if ((item = this._readEntity(token)) === undefined) return; diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index dcb3b5d6..53db78c9 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1873,7 +1873,14 @@ describe('Parser', () => { shouldNotParse(parser, '<<_:a _:b >> "c" .', 'Expected >> to follow "_:.b" on line 1.')); - for (const [elem, value] of [['()', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], [':joe', 'ex:joe'], ['<<:joe a :Person>>', '<<:joe a :Person>>']]) { + for (const [elem, value] of [ + ['()', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['( )', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['( )', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ['', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + [':joe', 'ex:joe'], + // ['<<:joe a :Person>>', '<<:joe a :Person>>'] + ]) { for (const pathType of ['!', '^']) { // eslint-disable-next-line no-inner-declarations function son(bnode) { @@ -1882,7 +1889,7 @@ describe('Parser', () => { for (const [f, triple] of [ [x => `(${x}) a :List .`, ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'ex:List']], - [x => ` (${x}) .`, ['l', '_:b0', 'm']], + // [x => ` (${x}) .`, ['l', '_:b0', 'm']], [x => ` (${x}) .`, ['l', 'is', '_:b0']], ]) { // eslint-disable-next-line no-inner-declarations From da5993447b5c68273f3d0ce984f5900f6ea96c75 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Fri, 24 Mar 2023 20:14:01 +1100 Subject: [PATCH 7/7] chore: re-enable all tests and remove redundant cases --- src/N3Parser.js | 21 +++++--------- test/N3Parser-test.js | 67 +++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/N3Parser.js b/src/N3Parser.js index 734be7b6..201ae83c 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -436,15 +436,16 @@ export default class N3Parser { // This will be ` rdf:first .`. if (stack.length !== 0 && stack[stack.length - 1].type === 'list') { if (this._n3Mode) { - const { _subject, _predicate, _object, _graph } = this; - if (_object.termType !== "NamedNode" || _object.value !== this.RDF_NIL.value) { - this._emit(_object, this.RDF_REST, this.RDF_NIL, _graph); + const { _subject, _predicate, _graph } = this; + if (this._object !== this.RDF_NIL) { + this._emit(previousList, this.RDF_REST, this.RDF_NIL, _graph); } - return this._getPathReader(tk => { + return this._getPathReader(tk => { this._emit(_subject, _predicate, this._object, _graph); - return this._readListItem(tk) + return this._readListItem(tk); }); - } else { + } + else { this._emit(this._subject, this._predicate, this._object, this._graph); } } @@ -485,14 +486,6 @@ export default class N3Parser { this._saveContext('formula', this._graph, this._subject, this._predicate, this._graph = this._blankNode()); return this._readSubject; - case '!': - case '^': - // Continue path - if (!this._n3Mode) - return this._error('Unexpected path', token); - - this._saveContext('item', this._graph, list, this.RDF_FIRST, item); - return this._getPathReader(this._readListItem); default: if ((item = this._readEntity(token)) === undefined) return; diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 53db78c9..97f24b49 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -1759,40 +1759,48 @@ describe('Parser', () => { it('should parse a ! path of length 2 starting with a non-empty list', shouldParse(parser, '@prefix : . @prefix fam: .' + '( :a )!fam:mother a fam:Person.', - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'ex:a'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ...list(['_:b0', 'ex:a']), ['_:b0', 'f:mother', '_:b1'], ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); it('should parse a ! path of length 2 starting with a non-empty list in another list', shouldParse(parser, '@prefix : . @prefix fam: .' + '(( :a )!fam:mother 1) a fam:Person.', - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'ex:a'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b0', 'f:mother', '_:b1'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + ...list(['_:b0', '_:b2'], ['_:b3', '"1"^^http://www.w3.org/2001/XMLSchema#integer']), + ...list(['_:b1', 'ex:a']), + ['_:b1', 'f:mother', '_:b2'], + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); it('should parse a ! path of length 2 starting with an empty list in another list', shouldParse(parser, '@prefix : . @prefix fam: .' + '(()!fam:mother 1) a fam:Person.', - ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); - + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ...list(['_:b0', '_:b1'], ['_:b2', '"1"^^http://www.w3.org/2001/XMLSchema#integer']), + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b1'] + )); it('should parse a ! path of length 2 starting with an empty list in another list as second element', shouldParse(parser, '@prefix : . @prefix fam: .' + '(1 ()!fam:mother) a fam:Person.', - ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'] + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ...list(['_:b0', '"1"^^http://www.w3.org/2001/XMLSchema#integer'], ['_:b1', '_:b2']), + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b2'] )); - it('should parse a ! path of length 2 starting with an empty list in another list of one element', shouldParse(parser, '@prefix : . @prefix fam: .' + '(()!fam:mother) a fam:Person.', - ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'])); + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ...list(['_:b0', '_:b1']), + ['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'f:mother', '_:b1'])); + it('should parse a ! path of length 2 as nested subject in a list', + shouldParse(parser, '@prefix : . @prefix fam: .' + + '((:joe!fam:mother) 1) a fam:Person.', + ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], + ...list(['_:b0', '_:b1'], ['_:b3', '"1"^^http://www.w3.org/2001/XMLSchema#integer']), + ...list(['_:b1', '_:b2']), + ['ex:joe', 'f:mother', '_:b2'])); it('should parse a birthday rule', shouldParse(parser, @@ -1804,27 +1812,24 @@ describe('Parser', () => { ' ?x :trueOnDate ?date.' + '} <= {' + ' ((?date ?s!foaf:birthday)!math:difference 31622400) math:integerQuotient ?age .' + - '} .')); - - it('should parse a ! path of length 2 as nested subject in a list', - shouldParse(parser, '@prefix : . @prefix fam: .' + - '((:joe!fam:mother) 1) a fam:Person.', - ['ex:joe', 'f:mother', '_:b2'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b1'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b3'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'f:Person'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '_:b2'], - ['_:b1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"1"^^http://www.w3.org/2001/XMLSchema#integer'], - ['_:b3', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'])); + '} .', + ['?x', 'http://example.org/trueOnDate', '?date', '_:b0'], + // eslint-disable-next-line no-warning-comments + // FIXME: when merging with https://github.com/rdfjs/N3.js/pull/327 + ['_:b1', 'http://www.w3.org/2000/10/swap/log#implies', '_:b0'], + ...[ + ...list(['_:b2', '_:b6'], ['_:b7', '"31622400"^^http://www.w3.org/2001/XMLSchema#integer']), + ['_:b2', 'http://www.w3.org/2000/10/swap/math#integerQuotient', '?age'], + ...list(['_:b3', '?date'], ['_:b4', '_:b5']), + ['?s', 'http://xmlns.com/foaf/0.1/birthday', '_:b5'], + ['_:b3', 'http://www.w3.org/2000/10/swap/math#difference', '_:b6'], + ].map(elem => [...elem, '_:b1']) + )); it('should parse a formula as list item', shouldParse(parser, ' ( { a . } ).', ['a', 'findAll', '_:b0'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'b'], - ['_:b0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', '_:b2'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', 'o'], - ['_:b2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'], + ...list(['_:b0', 'b'], ['_:b2', 'o']), ['b', 'something', 'foo', '_:b1'], ['b', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'type', '_:b1'] ));