WIP
This commit is contained in:
9
node_modules/sparqljs/LICENSE.md
generated
vendored
Normal file
9
node_modules/sparqljs/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# License
|
||||
The MIT License (MIT)
|
||||
Copyright ©2014 Ruben Verborgh
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
163
node_modules/sparqljs/README.md
generated
vendored
Normal file
163
node_modules/sparqljs/README.md
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
# SPARQL.js – A SPARQL 1.1 parser for JavaScript
|
||||
[](https://github.com/RubenVerborgh/SPARQL.js/actions)
|
||||
[](https://www.npmjs.com/package/sparqljs)
|
||||
[](https://zenodo.org/badge/latestdoi/22990236)
|
||||
|
||||
> [!warning]
|
||||
> SPARQL.js has been deprecated in favour of the [Traqula](https://github.com/comunica/traqula) modular parser, generator and transformer framework.
|
||||
> Traqula supports both parsing and generating SPARQL 1.1 and 1.2.
|
||||
> To migrate, follow the [migration guide on Traqula's repository](https://github.com/comunica/traqula/blob/main/docs/sparqlJSMigration.md).
|
||||
|
||||
The [SPARQL 1.1 Query Language](http://www.w3.org/TR/sparql11-query/) allows to query datasources of [RDF triples](http://www.w3.org/TR/rdf11-concepts/).
|
||||
SPARQL.js translates SPARQL into JSON and back,
|
||||
so you can parse and build SPARQL queries in your JavaScript applications.
|
||||
It also contains support for the [SPARQL*](https://blog.liu.se/olafhartig/2019/01/10/position-statement-rdf-star-and-sparql-star/) extension
|
||||
under the `sparqlStar` option.
|
||||
|
||||
It fully supports the [SPARQL 1.1 specification](http://www.w3.org/TR/sparql11-query/), including [property paths](http://www.w3.org/TR/sparql11-query/#propertypaths), [federation](http://www.w3.org/TR/sparql11-federated-query/), and [updates](http://www.w3.org/TR/sparql11-update/).
|
||||
|
||||
## Usage
|
||||
### Library
|
||||
```JavaScript
|
||||
// Parse a SPARQL query to a JSON object
|
||||
var SparqlParser = require('sparqljs').Parser;
|
||||
var parser = new SparqlParser();
|
||||
var parsedQuery = parser.parse(
|
||||
'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' +
|
||||
'SELECT * { ?mickey foaf:name "Mickey Mouse"@en; foaf:knows ?other. }');
|
||||
|
||||
// Regenerate a SPARQL query from a JSON object
|
||||
var SparqlGenerator = require('sparqljs').Generator;
|
||||
var generator = new SparqlGenerator({ /* prefixes, baseIRI, factory, sparqlStar */ });
|
||||
parsedQuery.variables = ['?mickey'];
|
||||
var generatedQuery = generator.stringify(parsedQuery);
|
||||
```
|
||||
Set `sparqlStar` to `true` to allow [SPARQL*](https://blog.liu.se/olafhartig/2019/01/10/position-statement-rdf-star-and-sparql-star/) syntax.
|
||||
|
||||
Set `pathOnly` to `true` to parse SPARQL paths such as `foaf:name/foaf:knows` rather than the full SPARQL Algebra.
|
||||
|
||||
### Validation
|
||||
|
||||
By default SPARQL.js throws on queries that are syntactically correct, but not allowed by the spec.
|
||||
Set `skipValidation` to `true` to skip validation.
|
||||
|
||||
```JavaScript
|
||||
// Parse a SPARQL query without validation.
|
||||
var SparqlParser = require('sparqljs').Parser;
|
||||
var parser = new SparqlParser({ skipValidation: true });
|
||||
var parsedQuery = parser.parse(
|
||||
'select (?x as ?xString)' +
|
||||
'(count(?y) as ?count)' +
|
||||
'{ ?x ?y ?z }');
|
||||
```
|
||||
|
||||
### Standalone
|
||||
```bash
|
||||
$ sparql-to-json --strict query.sparql
|
||||
```
|
||||
Parse [SPARQL*](https://blog.liu.se/olafhartig/2019/01/10/position-statement-rdf-star-and-sparql-star/) syntax by default.
|
||||
For pure [SPARQL 1.1](http://www.w3.org/TR/sparql11-query/), use the `--strict` flag.
|
||||
|
||||
## Representation
|
||||
Queries are represented in a JSON structure. The most easy way to get acquainted with this structure is to try the examples in the `queries` folder through `sparql-to-json`. All examples of the [SPARQL 1.1 specification](http://www.w3.org/TR/sparql11-query/) have been included, in case you wonder how a specific syntactical construct is represented.
|
||||
|
||||
Here is a simple query in SPARQL:
|
||||
```SPARQL
|
||||
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
|
||||
SELECT ?p ?c WHERE {
|
||||
?p a dbpedia-owl:Artist.
|
||||
?p dbpedia-owl:birthPlace ?c.
|
||||
?c <http://xmlns.com/foaf/0.1/name> "York"@en.
|
||||
}
|
||||
```
|
||||
|
||||
And here is the same query in JSON:
|
||||
```JSON
|
||||
{
|
||||
"queryType": "SELECT",
|
||||
"variables": [
|
||||
{
|
||||
"termType": "Variable",
|
||||
"value": "p"
|
||||
},
|
||||
{
|
||||
"termType": "Variable",
|
||||
"value": "c"
|
||||
}
|
||||
],
|
||||
"where": [
|
||||
{
|
||||
"type": "bgp",
|
||||
"triples": [
|
||||
{
|
||||
"subject": {
|
||||
"termType": "Variable",
|
||||
"value": "p"
|
||||
},
|
||||
"predicate": {
|
||||
"termType": "NamedNode",
|
||||
"value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
|
||||
},
|
||||
"object": {
|
||||
"termType": "NamedNode",
|
||||
"value": "http://dbpedia.org/ontology/Artist"
|
||||
}
|
||||
},
|
||||
{
|
||||
"subject": {
|
||||
"termType": "Variable",
|
||||
"value": "p"
|
||||
},
|
||||
"predicate": {
|
||||
"termType": "NamedNode",
|
||||
"value": "http://dbpedia.org/ontology/birthPlace"
|
||||
},
|
||||
"object": {
|
||||
"termType": "Variable",
|
||||
"value": "c"
|
||||
}
|
||||
},
|
||||
{
|
||||
"subject": {
|
||||
"termType": "Variable",
|
||||
"value": "c"
|
||||
},
|
||||
"predicate": {
|
||||
"termType": "NamedNode",
|
||||
"value": "http://xmlns.com/foaf/0.1/name"
|
||||
},
|
||||
"object": {
|
||||
"termType": "Literal",
|
||||
"value": "York",
|
||||
"language": "en",
|
||||
"datatype": {
|
||||
"termType": "NamedNode",
|
||||
"value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"type": "query",
|
||||
"prefixes": {
|
||||
"dbpedia-owl": "http://dbpedia.org/ontology/"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The representation of triples uses the [RDF/JS representation](http://rdf.js.org/).
|
||||
|
||||
## Installation
|
||||
```bash
|
||||
$ [sudo] npm [-g] install sparqljs
|
||||
```
|
||||
|
||||
# License, status and contributions
|
||||
The SPARQL.js library is copyrighted by [Ruben Verborgh](http://ruben.verborgh.org/)
|
||||
and released under the [MIT License](https://github.com/RubenVerborgh/SPARQL.js/blob/master/LICENSE.md).
|
||||
|
||||
[Contributions are welcome](https://github.com/RubenVerborgh/SPARQL.js/blob/master/CONTRIBUTING.md), and bug reports or pull requests are always helpful.
|
||||
|
||||
## Contributors
|
||||
- Thanks to [Tim Ermilov](https://github.com/yamalight) for [driving the SPARQL generator](https://github.com/RubenVerborgh/SPARQL.js/pull/9)
|
||||
20
node_modules/sparqljs/bin/json-to-sparql
generated
vendored
Executable file
20
node_modules/sparqljs/bin/json-to-sparql
generated
vendored
Executable file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// Parse arguments
|
||||
var args = process.argv.slice(2);
|
||||
if (args.length > 1) {
|
||||
console.error('usage: json-to-sparql query.json');
|
||||
return process.exit(1);
|
||||
}
|
||||
|
||||
var fs = require('fs'),
|
||||
SparqlGenerator = require('../sparql').Generator;
|
||||
|
||||
// Parse JSON and output stringified query
|
||||
var query = '', source = args[0] ? fs.createReadStream(args[0]) : process.stdin;
|
||||
source.setEncoding('utf8');
|
||||
source.on('data', function (data) { query += data; });
|
||||
source.on('end', function () {
|
||||
var sparqlText = new SparqlGenerator().stringify(JSON.parse(query));
|
||||
process.stdout.write(sparqlText + '\n');
|
||||
});
|
||||
27
node_modules/sparqljs/bin/sparql-to-json
generated
vendored
Executable file
27
node_modules/sparqljs/bin/sparql-to-json
generated
vendored
Executable file
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// Parse arguments
|
||||
var args = process.argv.slice(2);
|
||||
let strictMode = false;
|
||||
if (args.length > 1) {
|
||||
if (args[0] !== '--strict') {
|
||||
console.error('usage: sparql-to-json query.sparql');
|
||||
return process.exit(1);
|
||||
} else {
|
||||
strictMode = true;
|
||||
// Shift arguments
|
||||
args.shift();
|
||||
}
|
||||
}
|
||||
|
||||
var fs = require('fs'),
|
||||
SparqlParser = require('../sparql').Parser;
|
||||
|
||||
// Parse and display the query
|
||||
var query = '', source = args[0] ? fs.createReadStream(args[0]) : process.stdin;
|
||||
source.setEncoding('utf8');
|
||||
source.on('data', function (data) { query += data; });
|
||||
source.on('end', function () {
|
||||
var parseTree = new SparqlParser({ sparqlStar: !strictMode }).parse(query);
|
||||
process.stdout.write(JSON.stringify(parseTree, null, ' ') + '\n');
|
||||
});
|
||||
470
node_modules/sparqljs/lib/SparqlGenerator.js
generated
vendored
Normal file
470
node_modules/sparqljs/lib/SparqlGenerator.js
generated
vendored
Normal file
@@ -0,0 +1,470 @@
|
||||
var XSD_INTEGER = 'http://www.w3.org/2001/XMLSchema#integer';
|
||||
var XSD_STRING = 'http://www.w3.org/2001/XMLSchema#string';
|
||||
|
||||
function Generator(options) {
|
||||
this._options = options = options || {};
|
||||
|
||||
var prefixes = options.prefixes || {};
|
||||
this._prefixByIri = {};
|
||||
var prefixIris = [];
|
||||
for (var prefix in prefixes) {
|
||||
var iri = prefixes[prefix];
|
||||
if (isString(iri)) {
|
||||
this._prefixByIri[iri] = prefix;
|
||||
prefixIris.push(iri);
|
||||
}
|
||||
}
|
||||
var iriList = prefixIris.join('|').replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
|
||||
this._prefixRegex = new RegExp('^(' + iriList + ')([a-zA-Z][\\-_a-zA-Z0-9]*)$');
|
||||
this._usedPrefixes = {};
|
||||
this._sparqlStar = options.sparqlStar;
|
||||
this._indent = isString(options.indent) ? options.indent : ' ';
|
||||
this._newline = isString(options.newline) ? options.newline : '\n';
|
||||
this._explicitDatatype = Boolean(options.explicitDatatype);
|
||||
}
|
||||
|
||||
// Converts the parsed query object into a SPARQL query
|
||||
Generator.prototype.toQuery = function (q) {
|
||||
var query = '';
|
||||
|
||||
if (q.queryType)
|
||||
query += q.queryType.toUpperCase() + ' ';
|
||||
if (q.reduced)
|
||||
query += 'REDUCED ';
|
||||
if (q.distinct)
|
||||
query += 'DISTINCT ';
|
||||
|
||||
if (q.variables){
|
||||
query += mapJoin(q.variables, undefined, function (variable) {
|
||||
// String variable
|
||||
if (/^[?$]/.test(variable))
|
||||
return variable;
|
||||
// Entity object
|
||||
else if (isTerm(variable))
|
||||
return this.toEntity(variable);
|
||||
// Expression object
|
||||
else
|
||||
return '(' + this.toExpression(variable.expression) +
|
||||
' AS ' + variableToString(variable.variable) + ')';
|
||||
}, this) + ' ';
|
||||
}
|
||||
else if (q.template)
|
||||
query += this.group(q.template, true) + this._newline;
|
||||
|
||||
if (q.from)
|
||||
query += this.graphs('FROM ', q.from.default) + this.graphs('FROM NAMED ', q.from.named);
|
||||
if (q.where)
|
||||
query += 'WHERE ' + this.group(q.where, true) + this._newline;
|
||||
|
||||
if (q.updates)
|
||||
query += mapJoin(q.updates, ';' + this._newline, this.toUpdate, this);
|
||||
|
||||
if (q.group)
|
||||
query += 'GROUP BY ' + mapJoin(q.group, undefined, function (it) {
|
||||
var result = isTerm(it.expression)
|
||||
? this.toEntity(it.expression)
|
||||
: '(' + this.toExpression(it.expression) + ')';
|
||||
return it.variable ? '(' + result + ' AS ' + variableToString(it.variable) + ')' : result;
|
||||
}, this) + this._newline;
|
||||
if (q.having)
|
||||
query += 'HAVING (' + mapJoin(q.having, undefined, this.toExpression, this) + ')' + this._newline;
|
||||
if (q.order)
|
||||
query += 'ORDER BY ' + mapJoin(q.order, undefined, function (it) {
|
||||
var expr = '(' + this.toExpression(it.expression) + ')';
|
||||
return !it.descending ? expr : 'DESC ' + expr;
|
||||
}, this) + this._newline;
|
||||
|
||||
if (Number.isInteger(q.offset))
|
||||
query += 'OFFSET ' + q.offset + this._newline;
|
||||
if (Number.isInteger(q.limit))
|
||||
query += 'LIMIT ' + q.limit + this._newline;
|
||||
|
||||
if (q.values)
|
||||
query += this.values(q);
|
||||
|
||||
// stringify prefixes at the end to mark used ones
|
||||
query = this.baseAndPrefixes(q) + query;
|
||||
return query.trim();
|
||||
};
|
||||
|
||||
Generator.prototype.baseAndPrefixes = function (q) {
|
||||
var base = q.base ? ('BASE <' + q.base + '>' + this._newline) : '';
|
||||
var prefixes = '';
|
||||
for (var key in q.prefixes) {
|
||||
if (this._options.allPrefixes || this._usedPrefixes[key])
|
||||
prefixes += 'PREFIX ' + key + ': <' + q.prefixes[key] + '>' + this._newline;
|
||||
}
|
||||
return base + prefixes;
|
||||
};
|
||||
|
||||
// Converts the parsed SPARQL pattern into a SPARQL pattern
|
||||
Generator.prototype.toPattern = function (pattern) {
|
||||
var type = pattern.type || (pattern instanceof Array) && 'array' ||
|
||||
(pattern.subject && pattern.predicate && pattern.object ? 'triple' : '');
|
||||
if (!(type in this))
|
||||
throw new Error('Unknown entry type: ' + type);
|
||||
return this[type](pattern);
|
||||
};
|
||||
|
||||
Generator.prototype.triple = function (t) {
|
||||
return this.toEntity(t.subject) + ' ' + this.toEntity(t.predicate) + ' ' + this.toEntity(t.object) + '.';
|
||||
};
|
||||
|
||||
Generator.prototype.array = function (items) {
|
||||
return mapJoin(items, this._newline, this.toPattern, this);
|
||||
};
|
||||
|
||||
Generator.prototype.bgp = function (bgp) {
|
||||
return this.encodeTriples(bgp.triples);
|
||||
};
|
||||
|
||||
Generator.prototype.encodeTriples = function (triples) {
|
||||
if (!triples.length)
|
||||
return '';
|
||||
|
||||
var parts = [], subject = undefined, predicate = undefined;
|
||||
for (var i = 0; i < triples.length; i++) {
|
||||
var triple = triples[i];
|
||||
// Triple with different subject
|
||||
if (!equalTerms(triple.subject, subject)) {
|
||||
// Terminate previous triple
|
||||
if (subject)
|
||||
parts.push('.' + this._newline);
|
||||
subject = triple.subject;
|
||||
predicate = triple.predicate;
|
||||
parts.push(this.toEntity(subject), ' ', this.toEntity(predicate));
|
||||
}
|
||||
// Triple with same subject but different predicate
|
||||
else if (!equalTerms(triple.predicate, predicate)) {
|
||||
predicate = triple.predicate;
|
||||
parts.push(';' + this._newline, this._indent, this.toEntity(predicate));
|
||||
}
|
||||
// Triple with same subject and predicate
|
||||
else {
|
||||
parts.push(',');
|
||||
}
|
||||
parts.push(' ', this.toEntity(triple.object));
|
||||
}
|
||||
parts.push('.');
|
||||
|
||||
return parts.join('');
|
||||
}
|
||||
|
||||
Generator.prototype.graph = function (graph) {
|
||||
return 'GRAPH ' + this.toEntity(graph.name) + ' ' + this.group(graph);
|
||||
};
|
||||
|
||||
Generator.prototype.graphs = function (keyword, graphs) {
|
||||
return !graphs || graphs.length === 0 ? '' :
|
||||
mapJoin(graphs, '', function (g) { return keyword + this.toEntity(g) + this._newline; }, this)
|
||||
}
|
||||
|
||||
Generator.prototype.group = function (group, inline) {
|
||||
group = inline !== true ? this.array(group.patterns || group.triples)
|
||||
: this.toPattern(group.type !== 'group' ? group : group.patterns);
|
||||
return group.indexOf(this._newline) === -1 ? '{ ' + group + ' }' : '{' + this._newline + this.indent(group) + this._newline + '}';
|
||||
};
|
||||
|
||||
Generator.prototype.query = function (query) {
|
||||
return this.toQuery(query);
|
||||
};
|
||||
|
||||
Generator.prototype.filter = function (filter) {
|
||||
return 'FILTER(' + this.toExpression(filter.expression) + ')';
|
||||
};
|
||||
|
||||
Generator.prototype.bind = function (bind) {
|
||||
return 'BIND(' + this.toExpression(bind.expression) + ' AS ' + variableToString(bind.variable) + ')';
|
||||
};
|
||||
|
||||
Generator.prototype.optional = function (optional) {
|
||||
return 'OPTIONAL ' + this.group(optional);
|
||||
};
|
||||
|
||||
Generator.prototype.union = function (union) {
|
||||
return mapJoin(union.patterns, this._newline + 'UNION' + this._newline, function (p) { return this.group(p, true); }, this);
|
||||
};
|
||||
|
||||
Generator.prototype.minus = function (minus) {
|
||||
return 'MINUS ' + this.group(minus);
|
||||
};
|
||||
|
||||
Generator.prototype.values = function (valuesList) {
|
||||
// Gather unique keys
|
||||
var keys = Object.keys(valuesList.values.reduce(function (keyHash, values) {
|
||||
for (var key in values) keyHash[key] = true;
|
||||
return keyHash;
|
||||
}, {}));
|
||||
// Check whether simple syntax can be used
|
||||
var lparen, rparen;
|
||||
if (keys.length === 1) {
|
||||
lparen = rparen = '';
|
||||
} else {
|
||||
lparen = '(';
|
||||
rparen = ')';
|
||||
}
|
||||
// Create value rows
|
||||
return 'VALUES ' + lparen + keys.join(' ') + rparen + ' {' + this._newline +
|
||||
mapJoin(valuesList.values, this._newline, function (values) {
|
||||
return ' ' + lparen + mapJoin(keys, undefined, function (key) {
|
||||
return values[key] ? this.toEntity(values[key]) : 'UNDEF';
|
||||
}, this) + rparen;
|
||||
}, this) + this._newline + '}';
|
||||
};
|
||||
|
||||
Generator.prototype.service = function (service) {
|
||||
return 'SERVICE ' + (service.silent ? 'SILENT ' : '') + this.toEntity(service.name) + ' ' +
|
||||
this.group(service);
|
||||
};
|
||||
|
||||
// Converts the parsed expression object into a SPARQL expression
|
||||
Generator.prototype.toExpression = function (expr) {
|
||||
if (isTerm(expr)) {
|
||||
return this.toEntity(expr);
|
||||
}
|
||||
switch (expr.type.toLowerCase()) {
|
||||
case 'aggregate':
|
||||
return expr.aggregation.toUpperCase() +
|
||||
'(' + (expr.distinct ? 'DISTINCT ' : '') + this.toExpression(expr.expression) +
|
||||
(typeof expr.separator === 'string' ? '; SEPARATOR = ' + '"' + expr.separator.replace(escape, escapeReplacer) + '"' : '') + ')';
|
||||
case 'functioncall':
|
||||
return this.toEntity(expr.function) + '(' + mapJoin(expr.args, ', ', this.toExpression, this) + ')';
|
||||
case 'operation':
|
||||
var operator = expr.operator.toUpperCase(), args = expr.args || [];
|
||||
switch (expr.operator.toLowerCase()) {
|
||||
// Infix operators
|
||||
case '<':
|
||||
case '>':
|
||||
case '>=':
|
||||
case '<=':
|
||||
case '&&':
|
||||
case '||':
|
||||
case '=':
|
||||
case '!=':
|
||||
case '+':
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
return (isTerm(args[0]) ? this.toEntity(args[0]) : '(' + this.toExpression(args[0]) + ')') +
|
||||
' ' + operator + ' ' +
|
||||
(isTerm(args[1]) ? this.toEntity(args[1]) : '(' + this.toExpression(args[1]) + ')');
|
||||
// Unary operators
|
||||
case '!':
|
||||
return '!(' + this.toExpression(args[0]) + ')';
|
||||
case 'uplus':
|
||||
return '+(' + this.toExpression(args[0]) + ')';
|
||||
case 'uminus':
|
||||
return '-(' + this.toExpression(args[0]) + ')';
|
||||
// IN and NOT IN
|
||||
case 'notin':
|
||||
operator = 'NOT IN';
|
||||
case 'in':
|
||||
return this.toExpression(args[0]) + ' ' + operator +
|
||||
'(' + (isString(args[1]) ? args[1] : mapJoin(args[1], ', ', this.toExpression, this)) + ')';
|
||||
// EXISTS and NOT EXISTS
|
||||
case 'notexists':
|
||||
operator = 'NOT EXISTS';
|
||||
case 'exists':
|
||||
return operator + ' ' + this.group(args[0], true);
|
||||
// Other expressions
|
||||
default:
|
||||
return operator + '(' + mapJoin(args, ', ', this.toExpression, this) + ')';
|
||||
}
|
||||
default:
|
||||
throw new Error('Unknown expression type: ' + expr.type);
|
||||
}
|
||||
};
|
||||
|
||||
// Converts the parsed entity (or property path) into a SPARQL entity
|
||||
Generator.prototype.toEntity = function (value) {
|
||||
if (isTerm(value)) {
|
||||
switch (value.termType) {
|
||||
// variable, * selector, or blank node
|
||||
case 'Wildcard':
|
||||
return '*';
|
||||
case 'Variable':
|
||||
return variableToString(value);
|
||||
case 'BlankNode':
|
||||
return '_:' + value.value;
|
||||
// literal
|
||||
case 'Literal':
|
||||
var lexical = value.value || '', language = value.language || '', datatype = value.datatype;
|
||||
value = '"' + lexical.replace(escape, escapeReplacer) + '"';
|
||||
if (language){
|
||||
value += '@' + language;
|
||||
} else if (datatype) {
|
||||
// Abbreviate literals when possible
|
||||
if (!this._explicitDatatype) {
|
||||
switch (datatype.value) {
|
||||
case XSD_STRING:
|
||||
return value;
|
||||
case XSD_INTEGER:
|
||||
if (/^\d+$/.test(lexical))
|
||||
// Add space to avoid confusion with decimals in broken parsers
|
||||
return lexical + ' ';
|
||||
}
|
||||
}
|
||||
value += '^^' + this.encodeIRI(datatype.value);
|
||||
}
|
||||
return value;
|
||||
case 'Quad':
|
||||
if (!this._sparqlStar)
|
||||
throw new Error('SPARQL* support is not enabled');
|
||||
|
||||
if (value.graph && value.graph.termType !== "DefaultGraph") {
|
||||
return '<< GRAPH ' +
|
||||
this.toEntity(value.graph) +
|
||||
' { ' +
|
||||
this.toEntity(value.subject) + ' ' +
|
||||
this.toEntity(value.predicate) + ' ' +
|
||||
this.toEntity(value.object) +
|
||||
' } ' +
|
||||
' >>'
|
||||
}
|
||||
else {
|
||||
return (
|
||||
'<< ' +
|
||||
this.toEntity(value.subject) + ' ' +
|
||||
this.toEntity(value.predicate) + ' ' +
|
||||
this.toEntity(value.object) +
|
||||
' >>'
|
||||
);
|
||||
}
|
||||
// IRI
|
||||
default:
|
||||
return this.encodeIRI(value.value);
|
||||
}
|
||||
}
|
||||
// property path
|
||||
else {
|
||||
var items = value.items.map(this.toEntity, this), path = value.pathType;
|
||||
switch (path) {
|
||||
// prefix operator
|
||||
case '^':
|
||||
case '!':
|
||||
return path + items[0];
|
||||
// postfix operator
|
||||
case '*':
|
||||
case '+':
|
||||
case '?':
|
||||
return '(' + items[0] + path + ')';
|
||||
// infix operator
|
||||
default:
|
||||
return '(' + items.join(path) + ')';
|
||||
}
|
||||
}
|
||||
};
|
||||
var escape = /["\\\t\n\r\b\f]/g,
|
||||
escapeReplacer = function (c) { return escapeReplacements[c]; },
|
||||
escapeReplacements = { '\\': '\\\\', '"': '\\"', '\t': '\\t',
|
||||
'\n': '\\n', '\r': '\\r', '\b': '\\b', '\f': '\\f' };
|
||||
|
||||
// Represent the IRI, as a prefixed name when possible
|
||||
Generator.prototype.encodeIRI = function (iri) {
|
||||
var prefixMatch = this._prefixRegex.exec(iri);
|
||||
if (prefixMatch) {
|
||||
var prefix = this._prefixByIri[prefixMatch[1]];
|
||||
this._usedPrefixes[prefix] = true;
|
||||
return prefix + ':' + prefixMatch[2];
|
||||
}
|
||||
return '<' + iri + '>';
|
||||
};
|
||||
|
||||
// Converts the parsed update object into a SPARQL update clause
|
||||
Generator.prototype.toUpdate = function (update) {
|
||||
switch (update.type || update.updateType) {
|
||||
case 'load':
|
||||
return 'LOAD' + (update.source ? ' ' + this.toEntity(update.source) : '') +
|
||||
(update.destination ? ' INTO GRAPH ' + this.toEntity(update.destination) : '');
|
||||
case 'insert':
|
||||
return 'INSERT DATA ' + this.group(update.insert, true);
|
||||
case 'delete':
|
||||
return 'DELETE DATA ' + this.group(update.delete, true);
|
||||
case 'deletewhere':
|
||||
return 'DELETE WHERE ' + this.group(update.delete, true);
|
||||
case 'insertdelete':
|
||||
return (update.graph ? 'WITH ' + this.toEntity(update.graph) + this._newline : '') +
|
||||
(update.delete.length ? 'DELETE ' + this.group(update.delete, true) + this._newline : '') +
|
||||
(update.insert.length ? 'INSERT ' + this.group(update.insert, true) + this._newline : '') +
|
||||
(update.using ? this.graphs('USING ', update.using.default) : '') +
|
||||
(update.using ? this.graphs('USING NAMED ', update.using.named) : '') +
|
||||
'WHERE ' + this.group(update.where, true);
|
||||
case 'add':
|
||||
case 'copy':
|
||||
case 'move':
|
||||
return update.type.toUpperCase()+ ' ' + (update.silent ? 'SILENT ' : '') + (update.source.default ? 'DEFAULT' : this.toEntity(update.source.name)) +
|
||||
' TO ' + this.toEntity(update.destination.name);
|
||||
case 'create':
|
||||
case 'clear':
|
||||
case 'drop':
|
||||
return update.type.toUpperCase() + (update.silent ? ' SILENT ' : ' ') + (
|
||||
update.graph.default ? 'DEFAULT' :
|
||||
update.graph.named ? 'NAMED' :
|
||||
update.graph.all ? 'ALL' :
|
||||
('GRAPH ' + this.toEntity(update.graph.name))
|
||||
);
|
||||
default:
|
||||
throw new Error('Unknown update query type: ' + update.type);
|
||||
}
|
||||
};
|
||||
|
||||
// Indents each line of the string
|
||||
Generator.prototype.indent = function(text) { return text.replace(/^/gm, this._indent); }
|
||||
|
||||
function variableToString(variable){
|
||||
return '?' + variable.value;
|
||||
}
|
||||
|
||||
// Checks whether the object is a string
|
||||
function isString(object) { return typeof object === 'string'; }
|
||||
|
||||
// Checks whether the object is a Term
|
||||
function isTerm(object) {
|
||||
return typeof object.termType === 'string';
|
||||
}
|
||||
|
||||
// Checks whether term1 and term2 are equivalent without `.equals()` prototype method
|
||||
function equalTerms(term1, term2) {
|
||||
if (!term1 || !isTerm(term1)) { return false; }
|
||||
if (!term2 || !isTerm(term2)) { return false; }
|
||||
if (term1.termType !== term2.termType) { return false; }
|
||||
switch (term1.termType) {
|
||||
case 'Literal':
|
||||
return term1.value === term2.value
|
||||
&& term1.language === term2.language
|
||||
&& equalTerms(term1.datatype, term2.datatype);
|
||||
case 'Quad':
|
||||
return equalTerms(term1.subject, term2.subject)
|
||||
&& equalTerms(term1.predicate, term2.predicate)
|
||||
&& equalTerms(term1.object, term2.object)
|
||||
&& equalTerms(term1.graph, term2.graph);
|
||||
default:
|
||||
return term1.value === term2.value;
|
||||
}
|
||||
}
|
||||
|
||||
// Maps the array with the given function, and joins the results using the separator
|
||||
function mapJoin(array, sep, func, self) {
|
||||
return array.map(func, self).join(isString(sep) ? sep : ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param options {
|
||||
* allPrefixes: boolean,
|
||||
* indentation: string,
|
||||
* newline: string
|
||||
* }
|
||||
*/
|
||||
function _Generator(options = {}) {
|
||||
return {
|
||||
stringify: function (query) {
|
||||
var currentOptions = Object.create(options);
|
||||
currentOptions.prefixes = query.prefixes;
|
||||
return new Generator(currentOptions).toQuery(query);
|
||||
},
|
||||
createGenerator: function() { return new Generator(options); }
|
||||
};
|
||||
};
|
||||
module.exports = {
|
||||
Generator: _Generator,
|
||||
};
|
||||
1864
node_modules/sparqljs/lib/SparqlParser.js
generated
vendored
Normal file
1864
node_modules/sparqljs/lib/SparqlParser.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
27
node_modules/sparqljs/lib/Wildcard.js
generated
vendored
Normal file
27
node_modules/sparqljs/lib/Wildcard.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
// Wildcard constructor
|
||||
class Wildcard {
|
||||
constructor() {
|
||||
return WILDCARD || this;
|
||||
}
|
||||
|
||||
equals(other) {
|
||||
return other && (this.termType === other.termType);
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(Wildcard.prototype, 'value', {
|
||||
enumerable: true,
|
||||
value: '*',
|
||||
});
|
||||
|
||||
Object.defineProperty(Wildcard.prototype, 'termType', {
|
||||
enumerable: true,
|
||||
value: 'Wildcard',
|
||||
});
|
||||
|
||||
|
||||
// Wildcard singleton
|
||||
var WILDCARD = new Wildcard();
|
||||
|
||||
module.exports.Wildcard = Wildcard;
|
||||
65
node_modules/sparqljs/package.json
generated
vendored
Normal file
65
node_modules/sparqljs/package.json
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"name": "sparqljs",
|
||||
"version": "3.7.4",
|
||||
"description": "A parser for the SPARQL query language",
|
||||
"author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
|
||||
"keywords": [
|
||||
"sparql",
|
||||
"rdf",
|
||||
"query",
|
||||
"parser"
|
||||
],
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/RubenVerborgh/SPARQL.js.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/RubenVerborgh/SPARQL.js/issues"
|
||||
},
|
||||
"main": "sparql.js",
|
||||
"bin": {
|
||||
"sparqljs": "bin/sparql-to-json"
|
||||
},
|
||||
"files": [
|
||||
"sparql.js",
|
||||
"lib/*.js",
|
||||
"bin/*"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "jison lib/sparql.jison -p slr -m js -o lib/SparqlParser.js && echo 'module.exports=SparqlParser' >> lib/SparqlParser.js",
|
||||
"test": "mocha",
|
||||
"prepublishOnly": "npm run build",
|
||||
"spec:base": "rdf-test-suite spec/parser.js http://w3c.github.io/rdf-tests/sparql/sparql11/manifest-all.ttl -c .rdf-test-suite-cache/",
|
||||
"spec:query": "npm run spec:base -- -s http://www.w3.org/TR/sparql11-query/",
|
||||
"spec:update": "npm run spec:base -- -s http://www.w3.org/TR/sparql11-update/",
|
||||
"spec:star": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-star/tests/sparql/syntax/manifest.jsonld -i '{ \"sparqlStar\": true }' -c .rdf-test-suite-cache/",
|
||||
"spec:earl:query": "npm run spec:query --silent -- -o earl -p spec/earl-meta.json > spec/earl-query.ttl",
|
||||
"spec:earl:update": "npm run spec:update --silent -- -o earl -p spec/earl-meta.json > spec/earl-update.ttl",
|
||||
"spec:earl:star": "npm run spec:star --silent -- -o earl -p spec/earl-meta.json > spec/earl-star.ttl",
|
||||
"spec:all": "npm run spec:base && npm run spec:query && npm run spec:update && npm run spec:star",
|
||||
"spec:earl": "npm run spec:earl:query && npm run spec:earl:update && npm run spec:earl:star"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"rdf-data-factory": "^1.1.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"expect": "^24.9.0",
|
||||
"jison": "0.4.18",
|
||||
"mocha": "^10.1.0",
|
||||
"pre-commit": "^1.2.2",
|
||||
"rdf-test-suite": "^1.25.0"
|
||||
},
|
||||
"browser": {
|
||||
"_process": false,
|
||||
"fs": false,
|
||||
"path": false
|
||||
},
|
||||
"pre-commit": [
|
||||
"build",
|
||||
"test"
|
||||
]
|
||||
}
|
||||
52
node_modules/sparqljs/sparql.js
generated
vendored
Normal file
52
node_modules/sparqljs/sparql.js
generated
vendored
Normal file
@@ -0,0 +1,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,
|
||||
};
|
||||
Reference in New Issue
Block a user