Skip to content

Commit 18826de

Browse files
authored
fix: correctly handle named graphs in lists
* chore: add test cases for named nodes in lists * chore: add more graph in list test cases * fix: correctly handle named graphs in lists
1 parent be1bcd9 commit 18826de

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

src/N3Parser.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,8 @@ export default class N3Parser {
418418
}
419419

420420
// ### `_readListItem` reads items from a list
421-
_readListItem(token) {
422-
let item = null, // The item of the list
423-
list = null, // The list itself
421+
_readListItem(token, item = null /* The list item */) {
422+
let list = null, // The list itself
424423
next = this._readListItem; // The next function to execute
425424
const previousList = this._subject, // The previous list that contains this list
426425
stack = this._contextStack, // The stack of parent contexts
@@ -510,7 +509,7 @@ export default class N3Parser {
510509
this._graph = null;
511510
break;
512511
default:
513-
if ((item = this._readEntity(token)) === undefined)
512+
if (item === null && (item = this._readEntity(token)) === undefined)
514513
return;
515514
}
516515

@@ -613,12 +612,20 @@ export default class N3Parser {
613612
if (token.type !== '}')
614613
return this._readPunctuation(token);
615614

615+
const graph = this._graph;
616+
616617
// Store the last quad of the formula
617618
if (this._subject !== null)
618-
this._emit(this._subject, this._predicate, this._object, this._graph);
619+
this._emit(this._subject, this._predicate, this._object, graph);
619620

620621
// Restore the parent context containing this formula
621622
this._restoreContext('formula', token);
623+
624+
// If the formula was in a list context, continue reading the list
625+
if (this._contextStack.length > 0 && this._contextStack[this._contextStack.length - 1].type === 'list') {
626+
return this._readListItem(token, graph);
627+
}
628+
622629
// If the formula was the subject, continue reading the predicate.
623630
// If the formula was the object, read punctuation.
624631
return this._object === null ? this._readPredicate : this._getContextEndReader();

test/N3Parser-test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,48 @@ describe('Parser', () => {
18641864
shouldParse(parser, '@forSome <x>. <x> <x> <x>.',
18651865
['_:b0', '_:b0', '_:b0']));
18661866

1867+
describe('should parse a named graph in a list',
1868+
shouldParse(parser, '<s> <p> ({<a> <b> <c>}) .',
1869+
['s', 'p', '_:b1'],
1870+
...list(['_:b1', '_:b0']),
1871+
['a', 'b', 'c', '_:b0']
1872+
));
1873+
1874+
describe('should parse a named graph as the second element in a list',
1875+
shouldParse(parser, '<s> <p> (<x> {<a> <b> <c>}) .',
1876+
['s', 'p', '_:b0'],
1877+
...list(['_:b0', 'x'], ['_:b2', '_:b1']),
1878+
['a', 'b', 'c', '_:b1']
1879+
));
1880+
1881+
describe('should parse a named graph as the second element in a list of 3 elements',
1882+
shouldParse(parser, '<s> <p> (<x> {<a> <b> <c>} <y>) .',
1883+
['s', 'p', '_:b0'],
1884+
...list(['_:b0', 'x'], ['_:b2', '_:b1'], ['_:b3', 'y']),
1885+
['a', 'b', 'c', '_:b1']
1886+
));
1887+
1888+
describe('should parse a named graph in a subject list',
1889+
shouldParse(parser, '({<a> <b> <c>}) <p> <o> .',
1890+
['_:b1', 'p', 'o'],
1891+
...list(['_:b1', '_:b0']),
1892+
['a', 'b', 'c', '_:b0']
1893+
));
1894+
1895+
describe('should parse a named graph as the second element in a subject list',
1896+
shouldParse(parser, '(<x> {<a> <b> <c>}) <p> <o> .',
1897+
['_:b0', 'p', 'o'],
1898+
...list(['_:b0', 'x'], ['_:b2', '_:b1']),
1899+
['a', 'b', 'c', '_:b1']
1900+
));
1901+
1902+
describe('should parse a named graph as the second element in a subject list with 3 elements',
1903+
shouldParse(parser, '(<x> {<a> <b> <c>} <y>) <p> <o> .',
1904+
['_:b0', 'p', 'o'],
1905+
...list(['_:b0', 'x'], ['_:b2', '_:b1'], ['_:b3', 'y']),
1906+
['a', 'b', 'c', '_:b1']
1907+
));
1908+
18671909
describe('should parse a @forSome statement with multiple entities',
18681910
shouldParse(parser, '@prefix a: <a:>. @base <b:>. @forSome a:x, <y>, a:z. a:x <y> a:z.',
18691911
['_:b0', '_:b1', '_:b2']));
@@ -2154,7 +2196,7 @@ describe('Parser', () => {
21542196
describe('should parse a formula as list item',
21552197
shouldParse(parser, '<a> <findAll> ( <b> { <b> a <type>. <b> <something> <foo> } <o> ).',
21562198
['a', 'findAll', '_:b0'],
2157-
...list(['_:b0', 'b'], ['_:b2', 'o']),
2199+
...list(['_:b0', 'b'], ['_:b2', '_:b1'], ['_:b3', 'o']),
21582200
['b', 'something', 'foo', '_:b1'],
21592201
['b', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'type', '_:b1']
21602202
));

0 commit comments

Comments
 (0)