Skip to content

Commit

Permalink
Don't accept dot as punctuation in blank nodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 22, 2015
1 parent 816b138 commit 5a8f215
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
32 changes: 28 additions & 4 deletions lib/N3Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ N3Parser.prototype = {

// ### `_readPredicate` reads a triple's predicate.
_readPredicate: function (token) {
switch (token.type) {
var type = token.type;
switch (type) {
case 'IRI':
case 'abbreviation':
if (this._baseIRI === null || absoluteIRI.test(token.value))
Expand All @@ -165,9 +166,9 @@ N3Parser.prototype = {
case '}':
// Expected predicate didn't come, must have been trailing semicolon.
if (this._predicate === null)
return this._error('Unexpected ' + token.type, token);
return this._error('Unexpected ' + type, token);
this._subject = null;
return this._readBlankNodeTail(token);
return type === ']' ? this._readBlankNodeTail(token) : this._readPunctuation(token);
case ';':
// Extra semicolons can be safely ignored
return this._readPredicate;
Expand Down Expand Up @@ -240,7 +241,7 @@ N3Parser.prototype = {
// ### `_readBlankNodeTail` reads the end of a blank node.
_readBlankNodeTail: function (token) {
if (token.type !== ']')
return this._readPunctuation(token);
return this._readBlankNodePunctuation(token);

// Store blank node triple.
if (this._subject !== null)
Expand Down Expand Up @@ -449,6 +450,29 @@ N3Parser.prototype = {
return next;
},

// ### `_readBlankNodePunctuation` reads punctuation in a blank node
_readBlankNodePunctuation: function (token) {
var next;
switch (token.type) {
// Semicolon means the subject is shared; predicate and object are different.
case ';':
next = this._readPredicate;
break;
// Comma means both the subject and predicate are shared; the object is different.
case ',':
next = this._readObject;
break;
default:
return this._error('Expected punctuation to follow "' + this._object + '"', token);
}
// A triple has been completed now, so return it.
this._callback(null, { subject: this._subject,
predicate: this._predicate,
object: this._object,
graph: this._graph || '' });
return next;
},

// ### `_readQuadPunctuation` reads punctuation after a quad.
_readQuadPunctuation: function (token) {
if (token.type !== '.')
Expand Down
36 changes: 31 additions & 5 deletions test/N3Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,38 @@ describe('N3Parser', function () {
shouldNotParse('<a> <b> [<c>].',
'Expected object to follow "c" at line 1.'));

it('should parse a multi-statement blank node',
it('should not parse a blank node with only a semicolon',
shouldNotParse('<a> <b> [;].',
'Unexpected ] at line 1.'));

it('should parse a blank node with a trailing semicolon',
shouldParse('<a> <b> [ <u> <v>; ].',
['a', 'b', '_:b0'],
['_:b0', 'u', 'v']));

it('should parse a blank node with multiple trailing semicolons',
shouldParse('<a> <b> [ <u> <v>;;; ].',
['a', 'b', '_:b0'],
['_:b0', 'u', 'v']));

it('should parse a multi-predicate blank node',
shouldParse('<a> <b> [ <u> <v>; <w> <z> ].',
['a', 'b', '_:b0'],
['_:b0', 'u', 'v'],
['_:b0', 'w', 'z']));

it('should parse a multi-predicate blank node with multiple semicolons',
shouldParse('<a> <b> [ <u> <v>;;; <w> <z> ].',
['a', 'b', '_:b0'],
['_:b0', 'u', 'v'],
['_:b0', 'w', 'z']));

it('should parse a multi-object blank node',
shouldParse('<a> <b> [ <u> <v>, <z> ].',
['a', 'b', '_:b0'],
['_:b0', 'u', 'v'],
['_:b0', 'u', 'z']));

it('should parse a multi-statement blank node ending with a literal',
shouldParse('<a> <b> [ <u> <v>; <w> "z" ].',
['a', 'b', '_:b0'],
Expand All @@ -207,10 +233,6 @@ describe('N3Parser', function () {
['_:b0', 'u', 'v'],
['_:b0', 'w', '"z"^^t']));

it('should not parse a blank node with only a semicolon',
shouldNotParse('<a> <b> [;].',
'Unexpected ] at line 1.'));

it('should parse a multi-statement blank node with trailing semicolon',
shouldParse('<a> <b> [ <u> <v>; <w> <z>; ].',
['a', 'b', '_:b0'],
Expand All @@ -229,6 +251,10 @@ describe('N3Parser', function () {
['_:b0', 'c', '_:b1'],
['_:b1', 'd', 'e']));

it('should not parse an invalid blank node',
shouldNotParse('[ <a> <b> .',
'Expected punctuation to follow "b" at line 1.'));

it('should parse statements with an empty list in the subject',
shouldParse('() <a> <b>.',
['http://www.w3.org/1999/02/22-rdf-syntax-ns#nil', 'a', 'b']));
Expand Down

0 comments on commit 5a8f215

Please sign in to comment.