Skip to content

Commit f785a36

Browse files
committed
Review RDF* parsing support.
1 parent b56f356 commit f785a36

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

src/N3Parser.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,11 @@ export default class N3Parser {
232232

233233
break;
234234
case '<<':
235-
if (!this._supportsRDFStar) {
236-
this._error(`${token.type} is not allowed in this format`, token);
237-
}
238-
else {
239-
// Start a new nested triple
240-
this._saveContext('<<', this._graph, null, null, null);
241-
this._graph = null;
242-
// Read the subject
243-
return this._readSubject;
244-
}
235+
if (!this._supportsRDFStar)
236+
return this._error('Unexpected RDF* syntax', token);
237+
this._saveContext('<<', this._graph, null, null, null);
238+
this._graph = null;
239+
return this._readSubject;
245240
default:
246241
// Read the subject entity
247242
if ((this._subject = this._readEntity(token)) === undefined)
@@ -319,16 +314,11 @@ export default class N3Parser {
319314
this._graph = this._blankNode());
320315
return this._readSubject;
321316
case '<<':
322-
if (!this._supportsRDFStar) {
323-
this._error(`${token.type} is not allowed in this format`, token);
324-
}
325-
else {
326-
// Start a new nested triple
327-
this._saveContext('<<', this._graph, this._subject, this._predicate, null);
328-
this._graph = null;
329-
// Read the object
330-
return this._readSubject;
331-
}
317+
if (!this._supportsRDFStar)
318+
return this._error('Unexpected RDF* syntax', token);
319+
this._saveContext('<<', this._graph, this._subject, this._predicate, null);
320+
this._graph = null;
321+
return this._readSubject;
332322
default:
333323
// Read the object entity
334324
if ((this._object = this._readEntity(token)) === undefined)
@@ -831,34 +821,29 @@ export default class N3Parser {
831821
_readRDFStarTailOrGraph(token) {
832822
if (token.type !== '>>') {
833823
// An entity means this is a quad (only allowed if not already inside a graph)
834-
if (this._supportsQuads && this._graph === null && (this._graph = this._readEntity(token)) !== undefined) {
835-
// continue by reading '>>'
824+
if (this._supportsQuads && this._graph === null && (this._graph = this._readEntity(token)) !== undefined)
836825
return this._readRDFStarTail;
837-
}
838826
return this._error('Expected >> to follow "' + this._object.id + '"', token);
839827
}
840-
else {
841-
return this._readRDFStarTail(token);
842-
}
828+
return this._readRDFStarTail(token);
843829
}
844830

845831
// ### `_readRDFStarTail` reads the end of a nested RDF* triple
846832
_readRDFStarTail(token) {
847833
if (token.type !== '>>')
848834
return this._error(`Expected >> but got ${token.type}`, token);
849-
850-
// Get the triples value
851-
let value = this._quad(this._subject, this._predicate, this._object, this._graph || this.DEFAULTGRAPH);
852-
// Restore the parent context containing this formula
835+
// Read the quad and restore the previous context
836+
const quad = this._quad(this._subject, this._predicate, this._object,
837+
this._graph || this.DEFAULTGRAPH);
853838
this._restoreContext();
854839
// If the triple was the subject, continue by reading the predicate.
855840
if (this._subject === null) {
856-
this._subject = value;
841+
this._subject = quad;
857842
return this._readPredicate;
858843
}
844+
// If the triple was the object, read context end.
859845
else {
860-
// If the triple was the object, read context end.
861-
this._object = value;
846+
this._object = quad;
862847
return this._getContextEndReader();
863848
}
864849
}

test/N3Parser-test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,13 @@ describe('Parser', function () {
11821182
shouldNotParse(parser, '1 <a> <b>.',
11831183
'Unexpected literal on line 1.'));
11841184

1185-
it('should not parse RDF*',
1185+
it('should not parse RDF* in the subject position',
11861186
shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1187-
'<< is not allowed in this format on line 1.'));
1187+
'Unexpected RDF* syntax on line 1.'));
11881188

1189-
it('should not parse RDF*',
1189+
it('should not parse RDF* in the object position',
11901190
shouldNotParse(parser, '<a> <b> <<a> <b> <c>>>.',
1191-
'<< is not allowed in this format on line 1.'));
1191+
'Unexpected RDF* syntax on line 1.'));
11921192
});
11931193

11941194
describe('A Parser instance for the TurtleStar format', function () {
@@ -1243,13 +1243,13 @@ describe('Parser', function () {
12431243
it('should not parse @forAll',
12441244
shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
12451245

1246-
it('should not parse RDF*',
1246+
it('should not parse RDF* in the subject position',
12471247
shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1248-
'<< is not allowed in this format on line 1.'));
1248+
'Unexpected RDF* syntax on line 1.'));
12491249

1250-
it('should not parse RDF*',
1250+
it('should not parse RDF* in the object position',
12511251
shouldNotParse(parser, '<a> <b> <<<a> <b> <c>>>.',
1252-
'<< is not allowed in this format on line 1.'));
1252+
'Unexpected RDF* syntax on line 1.'));
12531253
});
12541254

12551255
describe('A Parser instance for the TriGStar format', function () {
@@ -1314,13 +1314,13 @@ describe('Parser', function () {
13141314
it('should not parse @forAll',
13151315
shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
13161316

1317-
it('should not parse RDF*',
1317+
it('should not parse RDF* in the subject position',
13181318
shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1319-
'<< is not allowed in this format on line 1.'));
1319+
'Unexpected RDF* syntax on line 1.'));
13201320

1321-
it('should not parse RDF*',
1321+
it('should not parse RDF* in the object position',
13221322
shouldNotParse(parser, '<http://ex.org/a> <http://ex.org/b> <<<a> <b> <c>>>.',
1323-
'<< is not allowed in this format on line 1.'));
1323+
'Unexpected RDF* syntax on line 1.'));
13241324
});
13251325

13261326
describe('A Parser instance for the N-TriplesStar format', function () {
@@ -1373,13 +1373,13 @@ describe('Parser', function () {
13731373
it('should not parse @forAll',
13741374
shouldNotParse(parser, '@forAll <x>.', 'Unexpected "@forAll" on line 1.'));
13751375

1376-
it('should not parse RDF*',
1376+
it('should not parse RDF* in the subject position',
13771377
shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1378-
'<< is not allowed in this format on line 1.'));
1378+
'Unexpected RDF* syntax on line 1.'));
13791379

1380-
it('should not parse RDF*',
1380+
it('should not parse RDF* in the object position',
13811381
shouldNotParse(parser, '_:a <http://ex.org/b> <<<a> <b> <c>>>.',
1382-
'<< is not allowed in this format on line 1.'));
1382+
'Unexpected RDF* syntax on line 1.'));
13831383
});
13841384

13851385
describe('A Parser instance for the N-QuadsStar format', function () {
@@ -1720,13 +1720,13 @@ describe('Parser', function () {
17201720
['"bonjour"@fr', 'sameAs', '"hello"@en', '_:b0']
17211721
));
17221722

1723-
it('should not parse RDF*',
1723+
it('should not parse RDF* in the subject position',
17241724
shouldNotParse(parser, '<<<a> <b> <c>>> <a> <b> .',
1725-
'<< is not allowed in this format on line 1.'));
1725+
'Unexpected RDF* syntax on line 1.'));
17261726

1727-
it('should not parse RDF*',
1727+
it('should not parse RDF* in the object position',
17281728
shouldNotParse(parser, '<a> <b> <<<a> <b> <c>>>.',
1729-
'<< is not allowed in this format on line 1.'));
1729+
'Unexpected RDF* syntax on line 1.'));
17301730
});
17311731

17321732
describe('A Parser instance for the N3Star format', function () {

0 commit comments

Comments
 (0)