From d5a26e501e77d5628ff0330e31578d5d57f8e4be Mon Sep 17 00:00:00 2001 From: smessie Date: Wed, 30 Oct 2024 12:54:47 +0100 Subject: [PATCH 1/4] fix: Correctly handle custom data factories by binding functions --- package-lock.json | 37 +++++-------------------------------- src/N3Parser.js | 10 +++++----- test/N3Parser-test.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index e49b165a..223bc97c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11088,21 +11088,6 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, - "node_modules/typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", - "dev": true, - "license": "Apache-2.0", - "peer": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, "node_modules/uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", @@ -11931,8 +11916,7 @@ "version": "7.21.0-placeholder-for-preset-env.2", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz", "integrity": "sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==", - "dev": true, - "requires": {} + "dev": true }, "@babel/plugin-syntax-async-generators": { "version": "7.8.4", @@ -13809,8 +13793,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "acorn-node": { "version": "1.8.2", @@ -15012,8 +14995,7 @@ "version": "1.5.3", "resolved": "https://registry.npmjs.org/dedent/-/dedent-1.5.3.tgz", "integrity": "sha512-NHQtfOOW68WD8lgypbLA5oT+Bt0xXJhiYvoR6SmmNXZfpzOGXwdKWmcwG8N7PwVVWV3eF/68nmD9BaJSsTBhyQ==", - "dev": true, - "requires": {} + "dev": true }, "deep-is": { "version": "0.1.4", @@ -17171,8 +17153,7 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", - "dev": true, - "requires": {} + "dev": true }, "jest-regex-util": { "version": "29.6.3", @@ -19785,8 +19766,7 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", "integrity": "sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==", - "dev": true, - "requires": {} + "dev": true }, "tsconfig-paths": { "version": "3.15.0", @@ -19902,13 +19882,6 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, - "typescript": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz", - "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==", - "dev": true, - "peer": true - }, "uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", diff --git a/src/N3Parser.js b/src/N3Parser.js index fc4af341..d1659f28 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -1083,12 +1083,12 @@ function noop() {} // Initializes the parser with the given data factory function initDataFactory(parser, factory) { // Set factory methods - const namedNode = factory.namedNode; + const namedNode = factory.namedNode.bind(factory); parser._namedNode = namedNode; - parser._blankNode = factory.blankNode; - parser._literal = factory.literal; - parser._variable = factory.variable; - parser._quad = factory.quad; + parser._blankNode = factory.blankNode.bind(factory); + parser._literal = factory.literal.bind(factory); + parser._variable = factory.variable.bind(factory); + parser._quad = factory.quad.bind(factory); parser.DEFAULTGRAPH = factory.defaultGraph(); // Set common named nodes diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 1d7cd6a4..2805e8ac 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -2592,6 +2592,49 @@ describe('Parser', () => { ), ])).toBe(true); }); + + it('should use the internal `this` state', () => { + const customFactory = { + blankI: 0, + ...rdfDataModel, + blankNode: function () { + return DF.blankNode(`b-custom-${this.blankI++}`); + }, + }; + + const parser = new Parser({ + baseIRI: BASE_IRI, + format: 'n3', + factory: customFactory, + }); + + const quads = parser.parse(` + @prefix : . + [ :friend [ + :name "Thomas" ; + ] + ] . + `); + + expect(quads.length).toBeGreaterThan(0); + + // The blank node should be named b-custom-0 and b-custom-1 + const bnode1 = DF.blankNode('b-custom-0'); + const bnode2 = DF.blankNode('b-custom-1'); + + expect(isomorphic(quads, [ + DF.quad( + bnode1, + DF.namedNode('http://example.com/friend'), + bnode2, + ), + DF.quad( + bnode2, + DF.namedNode('http://example.com/name'), + DF.literal('Thomas'), + ), + ])).toBe(true); + }); }); describe('A turtle parser instance with external data factory', () => { From f77b0675d31fb0bd422b552a521d1edc4114ce70 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:27:11 +0000 Subject: [PATCH 2/4] chore: only bind on non-native factories --- src/N3Parser.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/N3Parser.js b/src/N3Parser.js index d1659f28..903542c3 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -1083,13 +1083,24 @@ function noop() {} // Initializes the parser with the given data factory function initDataFactory(parser, factory) { // Set factory methods - const namedNode = factory.namedNode.bind(factory); - parser._namedNode = namedNode; - parser._blankNode = factory.blankNode.bind(factory); - parser._literal = factory.literal.bind(factory); - parser._variable = factory.variable.bind(factory); - parser._quad = factory.quad.bind(factory); - parser.DEFAULTGRAPH = factory.defaultGraph(); + let namedNode; + if (factory === N3DataFactory) { + namedNode = factory.namedNode; + parser._namedNode = namedNode; + parser._blankNode = factory.blankNode; + parser._literal = factory.literal; + parser._variable = factory.variable; + parser._quad = factory.quad; + parser.DEFAULTGRAPH = factory.defaultGraph(); + } else { + namedNode = factory.namedNode.bind(factory); + parser._namedNode = namedNode; + parser._blankNode = factory.blankNode.bind(factory); + parser._literal = factory.literal.bind(factory); + parser._variable = factory.variable.bind(factory); + parser._quad = factory.quad.bind(factory); + parser.DEFAULTGRAPH = factory.defaultGraph(); + } // Set common named nodes parser.RDF_FIRST = namedNode(namespaces.rdf.first); From a1ccc37a64c2132d93e596b3455b255a926e64ec Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:37:49 +0000 Subject: [PATCH 3/4] chore: use direct calls rather than binding --- package-lock.json | 37 ++++++++++++--- src/N3Parser.js | 102 +++++++++++++++++------------------------- test/N3Parser-test.js | 2 +- 3 files changed, 75 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index 223bc97c..1c6b537f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11088,6 +11088,21 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, + "node_modules/typescript": { + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", + "dev": true, + "license": "Apache-2.0", + "peer": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", @@ -11916,7 +11931,8 @@ "version": "7.21.0-placeholder-for-preset-env.2", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz", "integrity": "sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==", - "dev": true + "dev": true, + "requires": {} }, "@babel/plugin-syntax-async-generators": { "version": "7.8.4", @@ -13793,7 +13809,8 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true + "dev": true, + "requires": {} }, "acorn-node": { "version": "1.8.2", @@ -14995,7 +15012,8 @@ "version": "1.5.3", "resolved": "https://registry.npmjs.org/dedent/-/dedent-1.5.3.tgz", "integrity": "sha512-NHQtfOOW68WD8lgypbLA5oT+Bt0xXJhiYvoR6SmmNXZfpzOGXwdKWmcwG8N7PwVVWV3eF/68nmD9BaJSsTBhyQ==", - "dev": true + "dev": true, + "requires": {} }, "deep-is": { "version": "0.1.4", @@ -17153,7 +17171,8 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.3.tgz", "integrity": "sha512-+3NpwQEnRoIBtx4fyhblQDPgJI0H1IEIkX7ShLUjPGA7TtUTvI1oiKi3SR4oBR0hQhQR80l4WAe5RrXBwWMA8w==", - "dev": true + "dev": true, + "requires": {} }, "jest-regex-util": { "version": "29.6.3", @@ -19766,7 +19785,8 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.3.0.tgz", "integrity": "sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==", - "dev": true + "dev": true, + "requires": {} }, "tsconfig-paths": { "version": "3.15.0", @@ -19882,6 +19902,13 @@ "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", "dev": true }, + "typescript": { + "version": "5.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz", + "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==", + "dev": true, + "peer": true + }, "uglify-js": { "version": "3.19.3", "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz", diff --git a/src/N3Parser.js b/src/N3Parser.js index 903542c3..6328a28c 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -159,7 +159,7 @@ export default class N3Parser { const iri = this._resolveIRI(token.value); if (iri === null) return this._error('Invalid IRI', token); - value = this._namedNode(iri); + value = this._factory.namedNode(iri); break; // Read a prefixed name case 'type': @@ -167,15 +167,15 @@ export default class N3Parser { const prefix = this._prefixes[token.prefix]; if (prefix === undefined) return this._error(`Undefined prefix "${token.prefix}:"`, token); - value = this._namedNode(prefix + token.value); + value = this._factory.namedNode(prefix + token.value); break; // Read a blank node case 'blank': - value = this._blankNode(this._prefixes[token.prefix] + token.value); + value = this._factory.blankNode(this._prefixes[token.prefix] + token.value); break; // Read a variable case 'var': - value = this._variable(token.value.substr(1)); + value = this._factory.variable(token.value.substr(1)); break; // Everything else is not an entity default: @@ -194,7 +194,7 @@ export default class N3Parser { case '[': // Start a new quad with a new blank node as subject this._saveContext('blank', this._graph, - this._subject = this._blankNode(), null, null); + this._subject = this._factory.blankNode(), null, null); return this._readBlankNodeHead; case '(': // Start a new list @@ -206,7 +206,7 @@ export default class N3Parser { if (!this._n3Mode) return this._error('Unexpected graph', token); this._saveContext('formula', this._graph, - this._graph = this._blankNode(), null, null); + this._graph = this._factory.blankNode(), null, null); return this._readSubject; case '}': // No subject; the graph in which we are reading is closed instead @@ -216,14 +216,14 @@ export default class N3Parser { return this._error('Unexpected "@forSome"', token); this._subject = null; this._predicate = this.N3_FORSOME; - this._quantifier = this._blankNode; + this._quantifier = 'blankNode'; return this._readQuantifierList; case '@forAll': if (!this._n3Mode) return this._error('Unexpected "@forAll"', token); this._subject = null; this._predicate = this.N3_FORALL; - this._quantifier = this._variable; + this._quantifier = 'variable'; return this._readQuantifierList; case 'literal': if (!this._n3Mode) @@ -234,7 +234,7 @@ export default class N3Parser { return this._completeSubjectLiteral; } else - this._subject = this._literal(token.value, this._namedNode(token.prefix)); + this._subject = this._factory.literal(token.value, this._factory.namedNode(token.prefix)); break; case '<<': @@ -282,7 +282,7 @@ export default class N3Parser { if (this._n3Mode) { // Start a new quad with a new blank node as subject this._saveContext('blank', this._graph, this._subject, - this._subject = this._blankNode(), null); + this._subject = this._factory.blankNode(), null); return this._readBlankNodeHead; } case 'blank': @@ -307,12 +307,12 @@ export default class N3Parser { } // Pre-datatyped string literal (prefix stores the datatype) else - this._object = this._literal(token.value, this._namedNode(token.prefix)); + this._object = this._factory.literal(token.value, this._factory.namedNode(token.prefix)); break; case '[': // Start a new quad with a new blank node as subject this._saveContext('blank', this._graph, this._subject, this._predicate, - this._subject = this._blankNode()); + this._subject = this._factory.blankNode()); return this._readBlankNodeHead; case '(': // Start a new list @@ -324,7 +324,7 @@ export default class N3Parser { if (!this._n3Mode) return this._error('Unexpected graph', token); this._saveContext('formula', this._graph, this._subject, this._predicate, - this._graph = this._blankNode()); + this._graph = this._factory.blankNode()); return this._readSubject; case '<<': if (!this._supportsRDFStar) @@ -419,14 +419,14 @@ export default class N3Parser { case '[': // Stack the current list quad and start a new quad with a blank node as subject this._saveContext('blank', this._graph, - list = this._blankNode(), this.RDF_FIRST, - this._subject = item = this._blankNode()); + list = this._factory.blankNode(), this.RDF_FIRST, + this._subject = item = this._factory.blankNode()); next = this._readBlankNodeHead; break; case '(': // Stack the current list quad and start a new list this._saveContext('list', this._graph, - list = this._blankNode(), this.RDF_FIRST, this.RDF_NIL); + list = this._factory.blankNode(), this.RDF_FIRST, this.RDF_NIL); this._subject = null; break; case ')': @@ -462,7 +462,7 @@ export default class N3Parser { } // Pre-datatyped string literal (prefix stores the datatype) else { - item = this._literal(token.value, this._namedNode(token.prefix)); + item = this._factory.literal(token.value, this._factory.namedNode(token.prefix)); next = this._getContextEndReader(); } break; @@ -471,7 +471,7 @@ export default class N3Parser { if (!this._n3Mode) return this._error('Unexpected graph', token); this._saveContext('formula', this._graph, this._subject, this._predicate, - this._graph = this._blankNode()); + this._graph = this._factory.blankNode()); return this._readSubject; default: if ((item = this._readEntity(token)) === undefined) @@ -480,7 +480,7 @@ export default class N3Parser { // Create a new blank node if no item head was assigned yet if (list === null) - this._subject = list = this._blankNode(); + this._subject = list = this._factory.blankNode(); // Is this the first element of the list? if (previousList === null) { @@ -524,7 +524,7 @@ export default class N3Parser { // ### `_completeLiteral` completes a literal with an optional datatype or language _completeLiteral(token) { // Create a simple string literal by default - let literal = this._literal(this._literalValue); + let literal = this._factory.literal(this._literalValue); switch (token.type) { // Create a datatyped literal @@ -532,12 +532,12 @@ export default class N3Parser { case 'typeIRI': const datatype = this._readEntity(token); if (datatype === undefined) return; // No datatype means an error occurred - literal = this._literal(this._literalValue, datatype); + literal = this._factory.literal(this._literalValue, datatype); token = null; break; // Create a language-tagged string case 'langcode': - literal = this._literal(this._literalValue, token.value); + literal = this._factory.literal(this._literalValue, token.value); token = null; break; } @@ -620,7 +620,7 @@ export default class N3Parser { return this._error('Unexpected RDF-star syntax', token); // Continue using the last triple as quoted triple subject for the predicate-object pairs. const predicate = this._predicate, object = this._object; - this._subject = this._quad(subject, predicate, object, this.DEFAULTGRAPH); + this._subject = this._factory.quad(subject, predicate, object, this.DEFAULTGRAPH); next = this._readPredicate; break; // |} means that the current quoted triple in annotation syntax is finalized. @@ -721,7 +721,7 @@ export default class N3Parser { _readNamedGraphBlankLabel(token) { if (token.type !== ']') return this._error('Invalid graph label', token); - this._subject = this._blankNode(); + this._subject = this._factory.blankNode(); return this._readGraph; } @@ -751,17 +751,17 @@ export default class N3Parser { } // Without explicit quantifiers, map entities to a quantified entity if (!this._explicitQuantifiers) - this._quantified[entity.id] = this._quantifier(this._blankNode().value); + this._quantified[entity.id] = this._factory[this._quantifier](this._factory.blankNode().value); // With explicit quantifiers, output the reified quantifier else { // If this is the first item, start a new quantifier list if (this._subject === null) this._emit(this._graph || this.DEFAULTGRAPH, this._predicate, - this._subject = this._blankNode(), this.QUANTIFIERS_GRAPH); + this._subject = this._factory.blankNode(), this.QUANTIFIERS_GRAPH); // Otherwise, continue the previous list else this._emit(this._subject, this.RDF_REST, - this._subject = this._blankNode(), this.QUANTIFIERS_GRAPH); + this._subject = this._factory.blankNode(), this.QUANTIFIERS_GRAPH); // Output the list item this._emit(this._subject, this.RDF_FIRST, entity, this.QUANTIFIERS_GRAPH); } @@ -818,7 +818,7 @@ export default class N3Parser { // ### `_readForwardPath` reads a '!' path _readForwardPath(token) { let subject, predicate; - const object = this._blankNode(); + const object = this._factory.blankNode(); // The next token is the predicate if ((predicate = this._readEntity(token)) === undefined) return; @@ -835,7 +835,7 @@ export default class N3Parser { // ### `_readBackwardPath` reads a '^' path _readBackwardPath(token) { - const subject = this._blankNode(); + const subject = this._factory.blankNode(); let predicate, object; // The next token is the predicate if ((predicate = this._readEntity(token)) === undefined) @@ -867,7 +867,7 @@ export default class N3Parser { if (token.type !== '>>') return this._error(`Expected >> but got ${token.type}`, token); // Read the quad and restore the previous context - const quad = this._quad(this._subject, this._predicate, this._object, + const quad = this._factory.quad(this._subject, this._predicate, this._object, this._graph || this.DEFAULTGRAPH); this._restoreContext('<<', token); // If the triple was the subject, continue by reading the predicate. @@ -902,7 +902,7 @@ export default class N3Parser { // ### `_emit` sends a quad through the callback _emit(subject, predicate, object, graph) { - this._callback(null, this._quad(subject, predicate, object, graph || this.DEFAULTGRAPH)); + this._callback(null, this._factory.quad(subject, predicate, object, graph || this.DEFAULTGRAPH)); } // ### `_error` emits an error message through the callback @@ -1082,37 +1082,19 @@ function noop() {} // Initializes the parser with the given data factory function initDataFactory(parser, factory) { - // Set factory methods - let namedNode; - if (factory === N3DataFactory) { - namedNode = factory.namedNode; - parser._namedNode = namedNode; - parser._blankNode = factory.blankNode; - parser._literal = factory.literal; - parser._variable = factory.variable; - parser._quad = factory.quad; - parser.DEFAULTGRAPH = factory.defaultGraph(); - } else { - namedNode = factory.namedNode.bind(factory); - parser._namedNode = namedNode; - parser._blankNode = factory.blankNode.bind(factory); - parser._literal = factory.literal.bind(factory); - parser._variable = factory.variable.bind(factory); - parser._quad = factory.quad.bind(factory); - parser.DEFAULTGRAPH = factory.defaultGraph(); - } - + parser._factory = factory; // Set common named nodes - parser.RDF_FIRST = namedNode(namespaces.rdf.first); - parser.RDF_REST = namedNode(namespaces.rdf.rest); - parser.RDF_NIL = namedNode(namespaces.rdf.nil); - parser.N3_FORALL = namedNode(namespaces.r.forAll); - parser.N3_FORSOME = namedNode(namespaces.r.forSome); + parser.DEFAULTGRAPH = factory.defaultGraph(); + parser.RDF_FIRST = factory.namedNode(namespaces.rdf.first); + parser.RDF_REST = factory.namedNode(namespaces.rdf.rest); + parser.RDF_NIL = factory.namedNode(namespaces.rdf.nil); + parser.N3_FORALL = factory.namedNode(namespaces.r.forAll); + parser.N3_FORSOME = factory.namedNode(namespaces.r.forSome); parser.ABBREVIATIONS = { - 'a': namedNode(namespaces.rdf.type), - '=': namedNode(namespaces.owl.sameAs), - '>': namedNode(namespaces.log.implies), + 'a': factory.namedNode(namespaces.rdf.type), + '=': factory.namedNode(namespaces.owl.sameAs), + '>': factory.namedNode(namespaces.log.implies), }; - parser.QUANTIFIERS_GRAPH = namedNode('urn:n3:quantifiers'); + parser.QUANTIFIERS_GRAPH = factory.namedNode('urn:n3:quantifiers'); } initDataFactory(N3Parser.prototype, N3DataFactory); diff --git a/test/N3Parser-test.js b/test/N3Parser-test.js index 2805e8ac..961e8ad6 100644 --- a/test/N3Parser-test.js +++ b/test/N3Parser-test.js @@ -10,7 +10,7 @@ beforeEach(() => { blankId = 0; // reset per-node ID Parser._resetBlankNodePrefix(); // reset per-parser ID }); -Parser.prototype._blankNode = name => new BlankNode(name || `b${blankId++}`); +Parser.prototype._factory.blankNode = name => new BlankNode(name || `b${blankId++}`); describe('Parser', () => { describe('The Parser export', () => { From f93ab2e194b265a1cb1e771609f75f596cd0a16e Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sun, 24 Nov 2024 11:44:55 +0000 Subject: [PATCH 4/4] chore: fix comment placement --- src/N3Parser.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/N3Parser.js b/src/N3Parser.js index 6328a28c..33919405 100644 --- a/src/N3Parser.js +++ b/src/N3Parser.js @@ -1083,8 +1083,10 @@ function noop() {} // Initializes the parser with the given data factory function initDataFactory(parser, factory) { parser._factory = factory; - // Set common named nodes + parser.DEFAULTGRAPH = factory.defaultGraph(); + + // Set common named nodes parser.RDF_FIRST = factory.namedNode(namespaces.rdf.first); parser.RDF_REST = factory.namedNode(namespaces.rdf.rest); parser.RDF_NIL = factory.namedNode(namespaces.rdf.nil);