Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse subject and predicate literals in N3 #337

Closed
wants to merge 9 commits into from
38 changes: 36 additions & 2 deletions src/N3Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ export default class N3Parser {
case 'abbreviation':
this._predicate = this.ABBREVIATIONS[token.value];
break;
case 'literal':
if (!this._n3Mode)
return this._error('Unexpected literal', token);

if (token.prefix.length === 0) {
jeswr marked this conversation as resolved.
Show resolved Hide resolved
this._literalValue = token.value;
return this._completePredicateLiteral;
}
else
this._predicate = this._literal(token.value, this._namedNode(token.prefix));

break;
case '.':
case ']':
case '}':
Expand Down Expand Up @@ -547,8 +559,30 @@ export default class N3Parser {

// Completes a literal in subject position
_completeSubjectLiteral(token) {
this._subject = this._completeLiteral(token).literal;
return this._readPredicateOrNamedGraph;
const completed = this._completeLiteral(token);

this._subject = completed.literal;
// If the token was consumed, continue with the rest of the input
if (completed.token === null)
return this._readPredicateOrNamedGraph;
// Otherwise, consume the token now
else {
return this._readPredicateOrNamedGraph(completed.token);
}
jeswr marked this conversation as resolved.
Show resolved Hide resolved
}

// Completes a literal in subject position
jeswr marked this conversation as resolved.
Show resolved Hide resolved
_completePredicateLiteral(token) {
const completed = this._completeLiteral(token);

this._predicate = completed.literal;
// If the token was consumed, continue with the rest of the input
if (completed.token === null)
return this._readObject;
// Otherwise, consume the token now
else {
return this._readObject(completed.token);
}
}

// Completes a literal in object position
Expand Down
66 changes: 66 additions & 0 deletions test/N3Parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ describe('Parser', () => {
['noturn:a', 'noturn:b', '"x"^^urn:foo'],
['noturn:a', 'noturn:b', '"x"^^noturn:urn:foo']));

it('should not parse literals with datatype as predicate',
shouldNotParse('<greaterThan> "a"^^<c> "b"^^<c>.', 'Unexpected literal on line 1.'));

it('should not parse literals without datatype as predicate',
shouldNotParse('<greaterThan> "a" "b".', 'Unexpected literal on line 1.'));

it('should not parse a triple with a literal and a prefixed name type with an inexistent prefix',
shouldNotParse('<a> <b> "string"^^x:z.',
'Undefined prefix "x:" on line 1.', {
Expand Down Expand Up @@ -1756,12 +1762,72 @@ describe('Parser', () => {
['"1"^^http://www.w3.org/2001/XMLSchema#integer', 'greaterThan', '"0"^^http://www.w3.org/2001/XMLSchema#integer', '_:b0']
));


it('should parse literals with datatype as subject',
shouldParse(parser, '"a"^^<c> <greaterThan> <d>.',
['"a"^^http://example.org/c', 'greaterThan', 'd']
));


it('should parse literals with datatype as subject and object',
shouldParse(parser, '"a"^^<c> <greaterThan> "b"^^<c>.',
['"a"^^http://example.org/c', 'greaterThan', '"b"^^http://example.org/c']
));

it('should parse literals without datatype as subject and object',
shouldParse(parser, '"a" <greaterThan> "b".',
['"a"', 'greaterThan', '"b"']
));

it('should parse literals without datatype as subject',
shouldParse(parser, '"a" <greaterThan> <b>.',
['"a"', 'greaterThan', 'b']
));

it('should parse literals with datatype as predicate',
shouldParse(parser, '<greaterThan> "a"^^<c> "b"^^<c>.',
['greaterThan', '"a"^^http://example.org/c', '"b"^^http://example.org/c']
));

it('should parse literals without datatype as predicate',
shouldParse(parser, '<greaterThan> "a" "b".',
['greaterThan', '"a"', '"b"']
));

it('should parse subject predicate and object as integer',
jeswr marked this conversation as resolved.
Show resolved Hide resolved
shouldParse(parser, '1 1 1.',
['"1"^^http://www.w3.org/2001/XMLSchema#integer', '"1"^^http://www.w3.org/2001/XMLSchema#integer', '"1"^^http://www.w3.org/2001/XMLSchema#integer']
));

it('should parse literals with integer as predicate',
shouldParse(parser, '<greaterThan> 1 "b".',
['greaterThan', '"1"^^http://www.w3.org/2001/XMLSchema#integer', '"b"']
));

it('should parse literals with datatype as predicate in graph',
shouldParse(parser, '<x> <y> {<greaterThan> "a"^^<c> "b"^^<c>}.',
['x', 'y', '_:b0'],
['greaterThan', '"a"^^http://example.org/c', '"b"^^http://example.org/c', '_:b0']
));

it('should parse literals without datatype as predicate in graph',
shouldParse(parser, '<x> <y> {<greaterThan> "a" "b"}.',
['x', 'y', '_:b0'],
['greaterThan', '"a"', '"b"', '_:b0']
));

it('should parse literals with datatype as subject in graph',
shouldParse(parser, '<a> <b> {"a"^^<c> <greaterThan> "b"^^<c>}.',
['a', 'b', '_:b0'],
['"a"^^http://example.org/c', 'greaterThan', '"b"^^http://example.org/c', '_:b0']
));

it('should parse literals without datatype as subject in graph',
shouldParse(parser, '<a> <b> {"a" <greaterThan> "b"}.',
['a', 'b', '_:b0'],
['"a"', 'greaterThan', '"b"', '_:b0']
));

it('should parse literals with language as subject',
shouldParse(parser, '<a> <b> {"bonjour"@fr <sameAs> "hello"@en}.',
['a', 'b', '_:b0'],
Expand Down