-
Notifications
You must be signed in to change notification settings - Fork 67
/
sparql.js
52 lines (49 loc) · 1.54 KB
/
sparql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { Parser } = require('./lib/SparqlParser');
const { Generator } = require('./lib/SparqlGenerator');
const { Wildcard } = require('./lib/Wildcard');
const { DataFactory } = require('rdf-data-factory');
/**
* Creates a SPARQL parser with the given pre-defined prefixes and base IRI
* @param options {
* prefixes?: { [prefix: string]: string },
* baseIRI?: string,
* factory?: import('rdf-js').DataFactory,
* sparqlStar?: boolean,
* skipValidation?: boolean,
* skipUngroupedVariableCheck?: boolean
* }
*/
function _Parser({
prefixes,
baseIRI,
factory,
pathOnly,
sparqlStar,
skipValidation,
skipUngroupedVariableCheck,
} = {}) {
// Create a copy of the prefixes
const prefixesCopy = {};
for (const prefix in prefixes ?? {})
prefixesCopy[prefix] = prefixes[prefix];
// Create a new parser with the given prefixes
// (Workaround for https://github.com/zaach/jison/issues/241)
const parser = new Parser();
parser.parse = function () {
Parser.base = baseIRI || '';
Parser.prefixes = Object.create(prefixesCopy);
Parser.factory = factory || new DataFactory();
Parser.sparqlStar = Boolean(sparqlStar);
Parser.pathOnly = Boolean(pathOnly);
// We keep skipUngroupedVariableCheck for compatibility reasons.
Parser.skipValidation = Boolean(skipValidation) || Boolean(skipUngroupedVariableCheck)
return Parser.prototype.parse.apply(parser, arguments);
};
parser._resetBlanks = Parser._resetBlanks;
return parser;
}
module.exports = {
Parser: _Parser,
Generator,
Wildcard,
};