Skip to content

Commit

Permalink
Update parser to RDF/JS.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Apr 15, 2017
1 parent 19bf2bd commit 3458f14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
47 changes: 26 additions & 21 deletions lib/N3Parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// **N3Parser** parses N3 documents.
var N3Lexer = require('./N3Lexer');
var N3Lexer = require('./N3Lexer'),
Datatype = require('./Datatypes');
var Term = Datatype.Term,
Quad = Datatype.Quad;

var RDF_PREFIX = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
RDF_NIL = RDF_PREFIX + 'nil',
Expand Down Expand Up @@ -332,7 +335,7 @@ N3Parser.prototype = {

// Store blank node triple
if (this._subject !== null)
this._triple(this._subject, this._predicate, this._object, this._graph);
this._quad(this._subject, this._predicate, this._object, this._graph);

// Restore the parent context containing this blank node
var empty = this._predicate === null;
Expand Down Expand Up @@ -385,7 +388,7 @@ N3Parser.prototype = {
// If this list is contained within a parent list, return the membership triple here.
// This will be `<parent list element> rdf:first <this list>.`.
if (stack.length !== 0 && stack[stack.length - 1].type === 'list')
this._triple(this._subject, this._predicate, this._object, this._graph);
this._quad(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
Expand Down Expand Up @@ -428,7 +431,7 @@ N3Parser.prototype = {
}
else {
// Continue the previous list with the current list
this._triple(prevList, RDF_REST, list, this._graph);
this._quad(prevList, RDF_REST, list, this._graph);
}
// Add the item's value
if (item !== null) {
Expand All @@ -442,7 +445,7 @@ N3Parser.prototype = {
}
// Output the item if it is complete
if (itemComplete)
this._triple(list, RDF_FIRST, item, this._graph);
this._quad(list, RDF_FIRST, item, this._graph);
// Otherwise, save it for completion
else
this._object = item;
Expand Down Expand Up @@ -479,7 +482,7 @@ N3Parser.prototype = {
// If this literal was part of a list, write the item
// (we could also check the context stack, but passing in a flag is faster)
if (listItem)
this._triple(this._subject, RDF_FIRST, this._object, this._graph);
this._quad(this._subject, RDF_FIRST, this._object, this._graph);
// Continue with the rest of the input
if (suffix)
return this._getContextEndReader();
Expand All @@ -496,7 +499,7 @@ N3Parser.prototype = {

// Store the last triple of the formula
if (this._subject !== null)
this._triple(this._subject, this._predicate, this._object, this._graph);
this._quad(this._subject, this._predicate, this._object, this._graph);

// Restore the parent context containing this formula
this._restoreContext();
Expand Down Expand Up @@ -543,9 +546,9 @@ N3Parser.prototype = {
if (subject !== null) {
var predicate = this._predicate, object = this._object;
if (!inversePredicate)
this._triple(subject, predicate, object, graph);
this._quad(subject, predicate, object, graph);
else
this._triple(object, predicate, subject, graph);
this._quad(object, predicate, subject, graph);
}
return next;
},
Expand All @@ -566,7 +569,7 @@ N3Parser.prototype = {
return this._error('Expected punctuation to follow "' + this._object + '"', token);
}
// A triple has been completed now, so return it
this._triple(this._subject, this._predicate, this._object, this._graph);
this._quad(this._subject, this._predicate, this._object, this._graph);
return next;
},

Expand Down Expand Up @@ -657,14 +660,14 @@ N3Parser.prototype = {
else {
// If this is the first item, start a new quantifier list
if (this._subject === null)
this._triple(this._graph || '', this._predicate,
this._quad(this._graph || '', this._predicate,
this._subject = '_:b' + blankNodeCount++, QUANTIFIERS_GRAPH);
// Otherwise, continue the previous list
else
this._triple(this._subject, RDF_REST,
this._quad(this._subject, RDF_REST,
this._subject = '_:b' + blankNodeCount++, QUANTIFIERS_GRAPH);
// Output the list item
this._triple(this._subject, RDF_FIRST, entity, QUANTIFIERS_GRAPH);
this._quad(this._subject, RDF_FIRST, entity, QUANTIFIERS_GRAPH);
}
return this._readQuantifierPunctuation;
},
Expand All @@ -678,7 +681,7 @@ N3Parser.prototype = {
else {
// With explicit quantifiers, close the quantifier list
if (this._explicitQuantifiers) {
this._triple(this._subject, RDF_REST, RDF_NIL, QUANTIFIERS_GRAPH);
this._quad(this._subject, RDF_REST, RDF_NIL, QUANTIFIERS_GRAPH);
this._subject = null;
}
// Read a dot
Expand Down Expand Up @@ -710,7 +713,7 @@ N3Parser.prototype = {
// Switch back to the context of the list
this._restoreContext();
// Output the list item
this._triple(this._subject, RDF_FIRST, item, this._graph);
this._quad(this._subject, RDF_FIRST, item, this._graph);
}
return this._afterPath(token);
}
Expand All @@ -729,7 +732,7 @@ N3Parser.prototype = {
else
subject = this._object, this._object = object;
// Emit the path's current triple and read its next section
this._triple(subject, predicate, object, this._graph);
this._quad(subject, predicate, object, this._graph);
return this._readPath;
},

Expand All @@ -746,7 +749,7 @@ N3Parser.prototype = {
else
object = this._object, this._object = subject;
// Emit the path's current triple and read its next section
this._triple(subject, predicate, object, this._graph);
this._quad(subject, predicate, object, this._graph);
return this._readPath;
},

Expand All @@ -766,10 +769,12 @@ N3Parser.prototype = {
}
},

// ### `_triple` emits a triple through the callback
_triple: function (subject, predicate, object, graph) {
this._callback(null,
{ subject: subject, predicate: predicate, object: object, graph: graph || '' });
// ### `_quad` emits a triple through the callback
_quad: function (subject, predicate, object, graph) {
this._callback(null, new Quad(
Term.fromId(subject), Term.fromId(predicate),
Term.fromId(object), Term.fromId(graph || '')
));
},

// ### `_error` emits an error message through the callback
Expand Down
21 changes: 17 additions & 4 deletions test/N3Parser-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var N3Parser = require('../N3').Parser;

var Term = require('../N3').Term,
Quad = require('../N3').Quad;

describe('N3Parser', function () {
describe('The N3Parser module', function () {
it('should be a function', function () {
Expand Down Expand Up @@ -794,7 +797,10 @@ describe('N3Parser', function () {

it('should parse a string synchronously if no callback is given', function () {
var triples = new N3Parser().parse('@prefix a: <urn:a:>. a:a a:b a:c.');
triples.should.deep.equal([{ subject: 'urn:a:a', predicate: 'urn:a:b', object: 'urn:a:c', graph: '' }]);
triples.should.deep.equal([
new Quad(Term.fromId('urn:a:a'), Term.fromId('urn:a:b'),
Term.fromId('urn:a:c'), Term.fromId('')),
]);
});

it('should throw on syntax errors if no callback is given', function () {
Expand Down Expand Up @@ -1855,7 +1861,10 @@ function shouldParse(createParser, input) {
return function (done) {
var results = [];
var items = expected.map(function (item) {
return { subject: item[0], predicate: item[1], object: item[2], graph: item[3] || '' };
return new Quad(
Term.fromId(item[0]), Term.fromId(item[1]),
Term.fromId(item[2]), Term.fromId(item[3] || '')
);
});
N3Parser._resetBlankNodeIds();
createParser().parse(input, function (error, triple) {
Expand All @@ -1869,7 +1878,11 @@ function shouldParse(createParser, input) {
}

function toSortedJSON(triples) {
triples = triples.map(JSON.stringify);
triples = triples.map(function (t) {
return JSON.stringify([
t.subject.id, t.predicate.id, t.object.id, t.graph.id,
]);
});
triples.sort();
return '[\n ' + triples.join('\n ') + '\n]';
}
Expand Down Expand Up @@ -1908,7 +1921,7 @@ function itShouldResolve(baseIri, relativeIri, expected) {
catch (error) { done(error); }
});
it('should result in ' + expected, function () {
expect(result.object).to.equal(expected);
expect(result.object.value).to.equal(expected);
});
});
}

0 comments on commit 3458f14

Please sign in to comment.