@traqula/rules-sparql-1-1 0.0.1-alpha.176 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/lib/RoundTripTypes.d.ts +379 -0
  2. package/lib/RoundTripTypes.js +2 -0
  3. package/lib/RoundTripTypes.js.map +1 -0
  4. package/lib/Sparql11types.d.ts +18 -245
  5. package/lib/Sparql11types.js.map +1 -1
  6. package/lib/SparqlParser.d.ts +13 -4
  7. package/lib/SparqlParser.js +7 -4
  8. package/lib/SparqlParser.js.map +1 -1
  9. package/lib/expressionHelpers.d.ts +8 -19
  10. package/lib/expressionHelpers.js +40 -103
  11. package/lib/expressionHelpers.js.map +1 -1
  12. package/lib/factory.d.ts +447 -0
  13. package/lib/factory.js +105 -0
  14. package/lib/factory.js.map +1 -0
  15. package/lib/factoryMixins/ContextFactory.d.ts +38 -0
  16. package/lib/factoryMixins/ContextFactory.js +30 -0
  17. package/lib/factoryMixins/ContextFactory.js.map +1 -0
  18. package/lib/factoryMixins/ExpressionFactory.d.ts +60 -0
  19. package/lib/factoryMixins/ExpressionFactory.js +83 -0
  20. package/lib/factoryMixins/ExpressionFactory.js.map +1 -0
  21. package/lib/factoryMixins/GraphRefFactory.d.ts +43 -0
  22. package/lib/factoryMixins/GraphRefFactory.js +51 -0
  23. package/lib/factoryMixins/GraphRefFactory.js.map +1 -0
  24. package/lib/factoryMixins/PathFactory.d.ts +51 -0
  25. package/lib/factoryMixins/PathFactory.js +68 -0
  26. package/lib/factoryMixins/PathFactory.js.map +1 -0
  27. package/lib/factoryMixins/Patternfactory.d.ts +55 -0
  28. package/lib/factoryMixins/Patternfactory.js +98 -0
  29. package/lib/factoryMixins/Patternfactory.js.map +1 -0
  30. package/lib/factoryMixins/QueryFactory.d.ts +41 -0
  31. package/lib/factoryMixins/QueryFactory.js +43 -0
  32. package/lib/factoryMixins/QueryFactory.js.map +1 -0
  33. package/lib/factoryMixins/SolutionModifiersFactory.d.ts +43 -0
  34. package/lib/factoryMixins/SolutionModifiersFactory.js +55 -0
  35. package/lib/factoryMixins/SolutionModifiersFactory.js.map +1 -0
  36. package/lib/factoryMixins/TermFactory.d.ts +76 -0
  37. package/lib/factoryMixins/TermFactory.js +84 -0
  38. package/lib/factoryMixins/TermFactory.js.map +1 -0
  39. package/lib/factoryMixins/UpdateOperationFactory.d.ts +68 -0
  40. package/lib/factoryMixins/UpdateOperationFactory.js +126 -0
  41. package/lib/factoryMixins/UpdateOperationFactory.js.map +1 -0
  42. package/lib/factoryMixins/mixins.d.ts +14 -0
  43. package/lib/factoryMixins/mixins.js +20 -0
  44. package/lib/factoryMixins/mixins.js.map +1 -0
  45. package/lib/grammar/builtIn.d.ts +59 -59
  46. package/lib/grammar/builtIn.js +50 -57
  47. package/lib/grammar/builtIn.js.map +1 -1
  48. package/lib/grammar/dataSetClause.d.ts +22 -9
  49. package/lib/grammar/dataSetClause.js +57 -14
  50. package/lib/grammar/dataSetClause.js.map +1 -1
  51. package/lib/grammar/expression.d.ts +12 -16
  52. package/lib/grammar/expression.js +224 -296
  53. package/lib/grammar/expression.js.map +1 -1
  54. package/lib/grammar/general.d.ts +8 -8
  55. package/lib/grammar/general.js +59 -94
  56. package/lib/grammar/general.js.map +1 -1
  57. package/lib/grammar/literals.d.ts +16 -13
  58. package/lib/grammar/literals.js +161 -144
  59. package/lib/grammar/literals.js.map +1 -1
  60. package/lib/grammar/propertyPaths.d.ts +16 -12
  61. package/lib/grammar/propertyPaths.js +100 -129
  62. package/lib/grammar/propertyPaths.js.map +1 -1
  63. package/lib/grammar/queryUnit/queryUnit.d.ts +13 -17
  64. package/lib/grammar/queryUnit/queryUnit.js +195 -329
  65. package/lib/grammar/queryUnit/queryUnit.js.map +1 -1
  66. package/lib/grammar/solutionModifier.d.ts +12 -11
  67. package/lib/grammar/solutionModifier.js +125 -109
  68. package/lib/grammar/solutionModifier.js.map +1 -1
  69. package/lib/grammar/tripleBlock.d.ts +24 -33
  70. package/lib/grammar/tripleBlock.js +172 -126
  71. package/lib/grammar/tripleBlock.js.map +1 -1
  72. package/lib/grammar/updateUnit/updateUnit.d.ts +28 -33
  73. package/lib/grammar/updateUnit/updateUnit.js +282 -367
  74. package/lib/grammar/updateUnit/updateUnit.js.map +1 -1
  75. package/lib/grammar/whereClause.d.ts +20 -19
  76. package/lib/grammar/whereClause.js +232 -245
  77. package/lib/grammar/whereClause.js.map +1 -1
  78. package/lib/grammar-helpers/utils.d.ts +0 -4
  79. package/lib/grammar-helpers/utils.js +0 -37
  80. package/lib/grammar-helpers/utils.js.map +1 -1
  81. package/lib/index.cjs +3061 -2438
  82. package/lib/index.d.ts +4 -2
  83. package/lib/index.js +4 -2
  84. package/lib/index.js.map +1 -1
  85. package/lib/lexer/lexer.d.ts +4 -3
  86. package/lib/lexer/lexer.js +11 -7
  87. package/lib/lexer/lexer.js.map +1 -1
  88. package/lib/lexer/terminals.d.ts +0 -34
  89. package/lib/lexer/terminals.js +35 -37
  90. package/lib/lexer/terminals.js.map +1 -1
  91. package/lib/utils.d.ts +0 -3
  92. package/lib/utils.js +0 -6
  93. package/lib/utils.js.map +1 -1
  94. package/lib/validation/validators.d.ts +14 -0
  95. package/lib/validation/validators.js +235 -0
  96. package/lib/validation/validators.js.map +1 -0
  97. package/package.json +6 -8
  98. package/lib/Wildcard.d.ts +0 -9
  99. package/lib/Wildcard.js +0 -19
  100. package/lib/Wildcard.js.map +0 -1
@@ -1,12 +1,11 @@
1
1
  import * as l from '../../lexer';
2
- import { Wildcard } from '../../Wildcard';
3
- import { datasetClause } from '../dataSetClause';
2
+ import { queryIsGood } from '../../validation/validators';
3
+ import { datasetClauseStar } from '../dataSetClause';
4
4
  import { expression } from '../expression';
5
5
  import { prologue, var_, varOrIri, varOrTerm } from '../general';
6
- import { iri } from '../literals';
7
6
  import { solutionModifier } from '../solutionModifier';
8
7
  import { triplesBlock, triplesTemplate } from '../tripleBlock';
9
- import { dataBlock, groupGraphPattern, inlineDataFull, whereClause } from '../whereClause';
8
+ import { inlineData, whereClause } from '../whereClause';
10
9
  /**
11
10
  * [[1]](https://www.w3.org/TR/sparql11-query/#rQueryUnit)
12
11
  */
@@ -19,9 +18,9 @@ export const queryUnit = {
19
18
  */
20
19
  export const query = {
21
20
  name: 'query',
22
- impl: ({ ACTION, SUBRULE, OR }) => () => {
21
+ impl: ({ ACTION, SUBRULE, OR }) => (C) => {
23
22
  const prologueValues = SUBRULE(prologue, undefined);
24
- const queryType = OR([
23
+ const subType = OR([
25
24
  { ALT: () => SUBRULE(selectQuery, undefined) },
26
25
  { ALT: () => SUBRULE(constructQuery, undefined) },
27
26
  { ALT: () => SUBRULE(describeQuery, undefined) },
@@ -29,187 +28,64 @@ export const query = {
29
28
  ]);
30
29
  const values = SUBRULE(valuesClause, undefined);
31
30
  return ACTION(() => ({
32
- ...prologueValues,
33
- ...queryType,
31
+ context: prologueValues,
32
+ ...subType,
34
33
  type: 'query',
35
34
  ...(values && { values }),
35
+ loc: C.factory.sourceLocation(prologueValues.at(0), subType, values),
36
36
  }));
37
37
  },
38
- gImpl: ({ SUBRULE }) => (ast) => {
39
- const prologueString = SUBRULE(prologue, ast, undefined);
40
- let queryString = '';
41
- switch (ast.queryType) {
42
- case 'SELECT':
43
- queryString = SUBRULE(selectQuery, ast, undefined);
44
- break;
45
- case 'CONSTRUCT':
46
- queryString = SUBRULE(constructQuery, ast, undefined);
47
- break;
48
- case 'DESCRIBE':
49
- queryString = SUBRULE(describeQuery, ast, undefined);
50
- break;
51
- case 'ASK':
52
- queryString = SUBRULE(askQuery, ast, undefined);
53
- break;
38
+ gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
39
+ SUBRULE(prologue, ast.context, undefined);
40
+ if (F.isQuerySelect(ast)) {
41
+ SUBRULE(selectQuery, ast, undefined);
54
42
  }
55
- const valuesString = ast.values ? SUBRULE(valuesClause, ast.values, undefined) : '';
56
- return [prologueString, queryString, valuesString].filter(Boolean).join(' ');
57
- },
58
- };
59
- function extractFromOfDataSetClauses(ACTION, MANY, SUBRULE) {
60
- const datasetClauses = [];
61
- MANY(() => {
62
- datasetClauses.push(SUBRULE(datasetClause, undefined));
63
- });
64
- return ACTION(() => {
65
- const from = {
66
- default: [],
67
- named: [],
68
- };
69
- for (const datasetClause of datasetClauses) {
70
- if (datasetClause.type === 'default') {
71
- from.default.push(datasetClause.value);
72
- }
73
- else {
74
- from.named.push(datasetClause.value);
75
- }
76
- }
77
- return (from.default.length === 0 && from.named.length === 0) ? undefined : from;
78
- });
79
- }
80
- /**
81
- * Get all 'aggregate' rules from an expression
82
- */
83
- function getAggregatesOfExpression(expression) {
84
- if ('type' in expression) {
85
- if (expression.type === 'aggregate') {
86
- return [expression];
43
+ else if (F.isQueryConstruct(ast)) {
44
+ SUBRULE(constructQuery, ast, undefined);
87
45
  }
88
- if (expression.type === 'operation') {
89
- const aggregates = [];
90
- for (const arg of expression.args) {
91
- aggregates.push(...getAggregatesOfExpression(arg));
92
- }
93
- return aggregates;
94
- }
95
- }
96
- return [];
97
- }
98
- /**
99
- * Return the id of an expression
100
- */
101
- function getExpressionId(expression) {
102
- // Check if grouping
103
- if ('variable' in expression && expression.variable) {
104
- return expression.variable.value;
105
- }
106
- if ('value' in expression) {
107
- return expression.value;
108
- }
109
- return 'value' in expression.expression ? expression.expression.value : undefined;
110
- }
111
- /**
112
- * Get all variables used in an expression
113
- */
114
- function getVariablesFromExpression(expression) {
115
- const variables = new Set();
116
- const visitExpression = (expr) => {
117
- if (!expr) {
118
- return;
46
+ else if (F.isQueryDescribe(ast)) {
47
+ SUBRULE(describeQuery, ast, undefined);
119
48
  }
120
- if ('termType' in expr && expr.termType === 'Variable') {
121
- variables.add(expr);
49
+ else if (F.isQueryAsk(ast)) {
50
+ SUBRULE(askQuery, ast, undefined);
122
51
  }
123
- else if ('type' in expr && expr.type === 'operation') {
124
- for (const rec of expr.args) {
125
- visitExpression(rec);
126
- }
52
+ if (ast.values) {
53
+ SUBRULE(inlineData, ast.values, undefined);
127
54
  }
128
- };
129
- visitExpression(expression);
130
- return variables;
131
- }
55
+ },
56
+ };
132
57
  /**
133
58
  * [[7]](https://www.w3.org/TR/sparql11-query/#rSelectQuery)
134
59
  */
135
60
  export const selectQuery = {
136
61
  name: 'selectQuery',
137
- impl: ({ ACTION, SUBRULE, MANY }) => (C) => {
62
+ impl: ({ ACTION, SUBRULE }) => (C) => {
138
63
  const selectVal = SUBRULE(selectClause, undefined);
139
- const from = extractFromOfDataSetClauses(ACTION, MANY, SUBRULE);
64
+ const from = SUBRULE(datasetClauseStar, undefined);
140
65
  const where = SUBRULE(whereClause, undefined);
141
- const modifier = SUBRULE(solutionModifier, undefined);
142
- ACTION(() => {
143
- if (selectVal.variables.length === 1 && selectVal.variables[0] instanceof Wildcard) {
144
- if (modifier.group !== undefined) {
145
- throw new Error('GROUP BY not allowed with wildcard');
146
- }
147
- return;
148
- }
149
- const variables = selectVal.variables;
150
- // Check for projection of ungrouped variable
151
- // Check can be skipped in case of wildcard select.
152
- if (!C.skipValidation) {
153
- const hasCountAggregate = variables.flatMap(varVal => 'termType' in varVal ? [] : getAggregatesOfExpression(varVal.expression)).some(agg => agg.aggregation === 'count' && !(agg.expression instanceof Wildcard));
154
- if (hasCountAggregate || modifier.group) {
155
- // We have to check whether
156
- // 1. Variables used in projection are usable given the group by clause
157
- // 2. A selectCount will create an implicit group by clause.
158
- for (const selectVar of variables) {
159
- if ('termType' in selectVar) {
160
- if (!modifier.group || !modifier.group.map(groupvar => getExpressionId(groupvar))
161
- .includes((getExpressionId(selectVar)))) {
162
- throw new Error('Variable not allowed in projection');
163
- }
164
- }
165
- else if (getAggregatesOfExpression(selectVar.expression).length === 0) {
166
- const usedvars = getVariablesFromExpression(selectVar.expression);
167
- for (const usedvar of usedvars) {
168
- if (!modifier.group || !modifier.group.map || !modifier.group.map(groupVar => getExpressionId(groupVar))
169
- .includes(getExpressionId(usedvar))) {
170
- throw new Error(`Use of ungrouped variable in projection of operation (?${getExpressionId(usedvar)})`);
171
- }
172
- }
173
- }
174
- }
175
- }
176
- }
177
- // Check if id of each AS-selected column is not yet bound by subquery
178
- const subqueries = where.filter(pattern => pattern.type === 'query');
179
- if (subqueries.length > 0) {
180
- const selectedVarIds = [];
181
- for (const selectedVar of variables) {
182
- if ('variable' in selectedVar) {
183
- selectedVarIds.push(selectedVar.variable.value);
184
- }
185
- }
186
- const vars = subqueries.flatMap(sub => sub.variables)
187
- .map(v => 'value' in v ? v.value : v.variable.value);
188
- const subqueryIds = new Set(vars);
189
- for (const selectedVarId of selectedVarIds) {
190
- if (subqueryIds.has(selectedVarId)) {
191
- throw new Error(`Target id of 'AS' (?${selectedVarId}) already used in subquery`);
192
- }
193
- }
194
- }
66
+ const modifiers = SUBRULE(solutionModifier, undefined);
67
+ return ACTION(() => {
68
+ const ret = {
69
+ subType: 'select',
70
+ where: where.val,
71
+ solutionModifiers: modifiers,
72
+ datasets: from,
73
+ ...selectVal.val,
74
+ loc: C.factory.sourceLocation(selectVal, where, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset),
75
+ };
76
+ queryIsGood(ret);
77
+ return ret;
195
78
  });
196
- return {
197
- ...selectVal,
198
- queryType: 'SELECT',
199
- ...(from && { from }),
200
- where,
201
- ...modifier,
202
- };
203
79
  },
204
- gImpl: ({ SUBRULE }) => (ast) => {
205
- const selectString = SUBRULE(selectClause, ast, undefined);
206
- const fromDefaultString = ast.from?.default.map(clause => `FROM ${SUBRULE(iri, clause, undefined)}`).join(' ') ?? '';
207
- const fromNamedString = ast.from?.named.map(clause => `FROM NAMED ${SUBRULE(iri, clause, undefined)}`).join(' ') ?? '';
208
- const whereString = ast.where ?
209
- `WHERE ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.where }, undefined)}` :
210
- '';
211
- const modifierString = SUBRULE(solutionModifier, ast, undefined);
212
- return [selectString, fromDefaultString, fromNamedString, whereString, modifierString].filter(Boolean).join(' ');
80
+ gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
81
+ SUBRULE(selectClause, F.wrap({
82
+ variables: ast.variables,
83
+ distinct: ast.distinct,
84
+ reduced: ast.reduced,
85
+ }, F.sourceLocation(...ast.variables)), undefined);
86
+ SUBRULE(datasetClauseStar, ast.datasets, undefined);
87
+ SUBRULE(whereClause, F.wrap(ast.where, ast.where.loc), undefined);
88
+ SUBRULE(solutionModifier, ast.solutionModifiers, undefined);
213
89
  },
214
90
  };
215
91
  /**
@@ -217,104 +93,117 @@ export const selectQuery = {
217
93
  */
218
94
  export const subSelect = {
219
95
  name: 'subSelect',
220
- impl: ({ ACTION, SUBRULE }) => () => {
221
- const clause = SUBRULE(selectClause, undefined);
96
+ impl: ({ ACTION, SUBRULE }) => (C) => {
97
+ const selectVal = SUBRULE(selectClause, undefined);
222
98
  const where = SUBRULE(whereClause, undefined);
223
99
  const modifiers = SUBRULE(solutionModifier, undefined);
224
100
  const values = SUBRULE(valuesClause, undefined);
225
- return ACTION(() => ({
226
- ...modifiers,
227
- ...clause,
228
- type: 'query',
229
- queryType: 'SELECT',
230
- where,
101
+ return ACTION(() => C.factory.querySelect({
102
+ where: where.val,
103
+ datasets: C.factory.datasetClauses([], C.factory.sourceLocation()),
104
+ context: [],
105
+ solutionModifiers: modifiers,
106
+ ...selectVal.val,
231
107
  ...(values && { values }),
232
- }));
108
+ }, C.factory.sourceLocation(selectVal, where, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset, values)));
233
109
  },
234
110
  };
111
+ /**
112
+ * [[9]](https://www.w3.org/TR/sparql11-query/#rSelectClause)
113
+ */
235
114
  export const selectClause = {
236
115
  name: 'selectClause',
237
- impl: ({ ACTION, AT_LEAST_ONE, SUBRULE, CONSUME, SUBRULE1, SUBRULE2, OPTION, OR1, OR2, OR3 }) => (C) => {
238
- CONSUME(l.select);
116
+ impl: ({ ACTION, AT_LEAST_ONE, SUBRULE1, SUBRULE2, CONSUME, OPTION, OR1, OR2, OR3, }) => (C) => {
117
+ const select = CONSUME(l.select);
239
118
  const couldParseAgg = ACTION(() => C.parseMode.has('canParseAggregate') || !C.parseMode.add('canParseAggregate'));
240
- const distinctOrReduced = OPTION(() => OR1([
119
+ const distinctAndReduced = OPTION(() => OR1([
241
120
  { ALT: () => {
242
121
  CONSUME(l.distinct);
243
- return { distinct: true };
122
+ return [true, false];
244
123
  } },
245
124
  { ALT: () => {
246
125
  CONSUME(l.reduced);
247
- return { reduced: true };
126
+ return [false, true];
248
127
  } },
249
- ]));
250
- const variables = OR2([
128
+ ])) ?? [false, false];
129
+ const distRed = ACTION(() => {
130
+ const [distinct, reduced] = distinctAndReduced;
131
+ return {
132
+ ...(distinct && { distinct }),
133
+ ...(reduced && { reduced }),
134
+ };
135
+ });
136
+ let last;
137
+ const val = OR2([
251
138
  { ALT: () => {
252
- CONSUME(l.symbols.star);
253
- return [new Wildcard()];
139
+ const star = CONSUME(l.symbols.star);
140
+ return ACTION(() => {
141
+ last = star;
142
+ return { variables: [C.factory.wildcard(C.factory.sourceLocation(star))], ...distRed };
143
+ });
254
144
  } },
255
145
  { ALT: () => {
256
146
  const usedVars = [];
257
- const result = [];
147
+ const variables = [];
258
148
  AT_LEAST_ONE(() => OR3([
259
149
  { ALT: () => {
260
150
  const raw = SUBRULE1(var_, undefined);
261
151
  ACTION(() => {
262
- if (usedVars.some(v => v.equals(raw))) {
152
+ if (usedVars.some(v => v.value === raw.value)) {
263
153
  throw new Error(`Variable ${raw.value} used more than once in SELECT clause`);
264
154
  }
265
155
  usedVars.push(raw);
266
- result.push(raw);
156
+ variables.push(raw);
157
+ last = raw;
267
158
  });
268
159
  } },
269
160
  { ALT: () => {
270
- CONSUME(l.symbols.LParen);
271
- const expr = SUBRULE(expression, undefined);
161
+ const open = CONSUME(l.symbols.LParen);
162
+ const expr = SUBRULE1(expression, undefined);
272
163
  CONSUME(l.as);
273
164
  const variable = SUBRULE2(var_, undefined);
274
- CONSUME(l.symbols.RParen);
165
+ const close = CONSUME(l.symbols.RParen);
275
166
  ACTION(() => {
276
- if (usedVars.some(v => v.equals(variable))) {
167
+ last = close;
168
+ if (usedVars.some(v => v.value === variable.value)) {
277
169
  throw new Error(`Variable ${variable.value} used more than once in SELECT clause`);
278
170
  }
279
171
  usedVars.push(variable);
280
- result.push({
281
- expression: expr,
282
- variable,
283
- });
172
+ variables.push(C.factory.patternBind(expr, variable, C.factory.sourceLocation(open, last)));
284
173
  });
285
174
  } },
286
175
  ]));
287
- return result;
176
+ return { variables, ...distRed };
288
177
  } },
289
178
  ]);
290
179
  ACTION(() => !couldParseAgg && C.parseMode.delete('canParseAggregate'));
291
- return ACTION(() => ({
292
- ...distinctOrReduced,
293
- variables,
294
- }));
180
+ return ACTION(() => C.factory.wrap(val, C.factory.sourceLocation(select, last)));
295
181
  },
296
- gImpl: ({ SUBRULE }) => (ast) => {
297
- const builder = ['SELECT'];
298
- if (ast.distinct) {
299
- builder.push('DISTINCT');
300
- }
301
- else if (ast.reduced) {
302
- builder.push('REDUCED');
303
- }
304
- if (ast.variables.length === 1 && ast.variables[0] instanceof Wildcard) {
305
- builder.push('*');
306
- }
307
- else {
308
- for (const variable of ast.variables) {
309
- if ('expression' in variable) {
310
- builder.push(`( ${SUBRULE(expression, variable.expression, undefined)} AS ${SUBRULE(var_, variable.variable, undefined)} )`);
311
- }
312
- else {
313
- builder.push(SUBRULE(var_, variable, undefined));
314
- }
182
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
183
+ F.printFilter(ast, () => {
184
+ PRINT_WORD('SELECT');
185
+ if (ast.val.distinct) {
186
+ PRINT_WORD('DISTINCT');
187
+ }
188
+ else if (ast.val.reduced) {
189
+ PRINT_WORD('REDUCED');
190
+ }
191
+ });
192
+ for (const variable of ast.val.variables) {
193
+ if (F.isWildcard(variable)) {
194
+ F.printFilter(ast, () => PRINT_WORD('*'));
195
+ }
196
+ else if (F.isTerm(variable)) {
197
+ SUBRULE(var_, variable, undefined);
198
+ }
199
+ else {
200
+ F.printFilter(ast, () => PRINT_WORD('('));
201
+ SUBRULE(expression, variable.expression, undefined);
202
+ F.printFilter(ast, () => PRINT_WORD('AS'));
203
+ SUBRULE(var_, variable.variable, undefined);
204
+ F.printFilter(ast, () => PRINT_WORD(')'));
315
205
  }
316
206
  }
317
- return builder.join(' ');
318
207
  },
319
208
  };
320
209
  /**
@@ -322,59 +211,56 @@ export const selectClause = {
322
211
  */
323
212
  export const constructQuery = {
324
213
  name: 'constructQuery',
325
- impl: ({ ACTION, SUBRULE, CONSUME, SUBRULE1, SUBRULE2, MANY1, MANY2, OPTION, OR }) => () => {
326
- CONSUME(l.construct);
214
+ impl: ({ ACTION, SUBRULE1, SUBRULE2, CONSUME, OR }) => (C) => {
215
+ const construct = CONSUME(l.construct);
327
216
  return OR([
328
- {
329
- ALT: () => {
330
- const template = SUBRULE(constructTemplate, undefined);
331
- const from = extractFromOfDataSetClauses(ACTION, MANY1, SUBRULE1);
332
- const where = SUBRULE(whereClause, undefined);
217
+ { ALT: () => {
218
+ const template = SUBRULE1(constructTemplate, undefined);
219
+ const from = SUBRULE1(datasetClauseStar, undefined);
220
+ const where = SUBRULE1(whereClause, undefined);
333
221
  const modifiers = SUBRULE1(solutionModifier, undefined);
334
222
  return ACTION(() => ({
335
- ...modifiers,
336
- queryType: 'CONSTRUCT',
337
- template,
338
- from,
339
- where,
223
+ subType: 'construct',
224
+ template: template.val,
225
+ datasets: from,
226
+ where: where.val,
227
+ solutionModifiers: modifiers,
228
+ loc: C.factory.sourceLocation(construct, where, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset),
340
229
  }));
341
- },
342
- },
343
- {
344
- ALT: () => {
345
- const from = extractFromOfDataSetClauses(ACTION, MANY2, SUBRULE2);
230
+ } },
231
+ { ALT: () => {
232
+ const from = SUBRULE2(datasetClauseStar, undefined);
346
233
  CONSUME(l.where);
347
- CONSUME(l.symbols.LCurly);
348
- const template = OPTION(() => SUBRULE(triplesTemplate, undefined));
349
- CONSUME(l.symbols.RCurly);
234
+ // ConstructTemplate is same as '{' TriplesTemplate? '}'
235
+ const template = SUBRULE2(constructTemplate, undefined);
350
236
  const modifiers = SUBRULE2(solutionModifier, undefined);
351
- const where = template ?
352
- [{
353
- type: 'bgp',
354
- triples: template,
355
- }] :
356
- [];
357
237
  return ACTION(() => ({
358
- ...modifiers,
359
- queryType: 'CONSTRUCT',
360
- from,
361
- template,
362
- where,
238
+ subType: 'construct',
239
+ template: template.val,
240
+ datasets: from,
241
+ where: C.factory.patternGroup([template.val], C.factory.sourceLocation()),
242
+ solutionModifiers: modifiers,
243
+ loc: C.factory.sourceLocation(construct, template, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset),
363
244
  }));
364
- },
365
- },
245
+ } },
366
246
  ]);
367
247
  },
368
- gImpl: ({ SUBRULE }) => (ast) => {
369
- const constructString = SUBRULE(constructTemplate, ast.template, undefined);
370
- const fromDefaultString = ast.from?.default.map(clause => `FROM ${SUBRULE(iri, clause, undefined)}`).join(' ') ?? '';
371
- const fromNamedString = ast.from?.named.map(clause => `FROM NAMED ${SUBRULE(iri, clause, undefined)}`).join(' ') ?? '';
372
- const whereString = ast.where ?
373
- `WHERE ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.where }, undefined)}` :
374
- '';
375
- const modifierString = SUBRULE(solutionModifier, ast, undefined);
376
- return ['CONSTRUCT', constructString, fromDefaultString, fromNamedString, whereString, modifierString]
377
- .filter(Boolean).join(' ');
248
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
249
+ F.printFilter(ast, () => PRINT_WORD('CONSTRUCT'));
250
+ if (!F.isSourceLocationNoMaterialize(ast.where.loc)) {
251
+ // You are NOT in second case construct
252
+ F.printFilter(ast, () => PRINT_WORD('{'));
253
+ SUBRULE(triplesBlock, ast.template, undefined);
254
+ F.printFilter(ast, () => PRINT_WORD('}'));
255
+ }
256
+ SUBRULE(datasetClauseStar, ast.datasets, undefined);
257
+ if (F.isSourceLocationNoMaterialize(ast.where.loc)) {
258
+ SUBRULE(whereClause, F.wrap(F.patternGroup([ast.template], ast.template.loc), ast.template.loc), undefined);
259
+ }
260
+ else {
261
+ SUBRULE(whereClause, F.wrap(ast.where, ast.where.loc), undefined);
262
+ }
263
+ SUBRULE(solutionModifier, ast.solutionModifiers, undefined);
378
264
  },
379
265
  };
380
266
  /**
@@ -382,51 +268,48 @@ export const constructQuery = {
382
268
  */
383
269
  export const describeQuery = {
384
270
  name: 'describeQuery',
385
- impl: ({ ACTION, AT_LEAST_ONE, SUBRULE, CONSUME, MANY, OPTION, OR }) => () => {
386
- CONSUME(l.describe);
271
+ impl: ({ ACTION, AT_LEAST_ONE, SUBRULE1, CONSUME, OPTION, OR }) => (C) => {
272
+ const describe = CONSUME(l.describe);
387
273
  const variables = OR([
388
274
  { ALT: () => {
389
275
  const variables = [];
390
276
  AT_LEAST_ONE(() => {
391
- variables.push(SUBRULE(varOrIri, undefined));
277
+ variables.push(SUBRULE1(varOrIri, undefined));
392
278
  });
393
279
  return variables;
394
280
  } },
395
281
  { ALT: () => {
396
- CONSUME(l.symbols.star);
397
- return [new Wildcard()];
282
+ const star = CONSUME(l.symbols.star);
283
+ return [ACTION(() => C.factory.wildcard(C.factory.sourceLocation(star)))];
398
284
  } },
399
285
  ]);
400
- const from = extractFromOfDataSetClauses(ACTION, MANY, SUBRULE);
401
- const where = OPTION(() => SUBRULE(whereClause, undefined));
402
- const modifiers = SUBRULE(solutionModifier, undefined);
286
+ const from = SUBRULE1(datasetClauseStar, undefined);
287
+ const where = OPTION(() => SUBRULE1(whereClause, undefined));
288
+ const modifiers = SUBRULE1(solutionModifier, undefined);
403
289
  return ACTION(() => ({
404
- ...modifiers,
405
- queryType: 'DESCRIBE',
290
+ subType: 'describe',
406
291
  variables,
407
- from,
408
- where,
292
+ datasets: from,
293
+ ...(where && { where: where.val }),
294
+ solutionModifiers: modifiers,
295
+ loc: C.factory.sourceLocation(describe, ...variables, from, where, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset),
409
296
  }));
410
297
  },
411
- gImpl: ({ SUBRULE }) => (ast) => {
412
- const builder = ['DESCRIBE'];
413
- for (const variable of ast.variables) {
414
- if (variable.termType === 'Wildcard') {
415
- builder.push('*');
416
- }
417
- else {
418
- builder.push(SUBRULE(varOrTerm, variable, undefined));
419
- }
298
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
299
+ F.printFilter(ast, () => PRINT_WORD('DESCRIBE'));
300
+ if (F.isWildcard(ast.variables[0])) {
301
+ F.printFilter(ast, () => PRINT_WORD('*'));
420
302
  }
421
- if (ast.from) {
422
- builder.push(ast.from.default.map(clause => `FROM ${SUBRULE(iri, clause, undefined)}`).join(' '));
423
- builder.push(ast.from.named.map(clause => `FROM NAMED ${SUBRULE(iri, clause, undefined)}`).join(' '));
303
+ else {
304
+ for (const variable of ast.variables) {
305
+ SUBRULE(varOrTerm, variable, undefined);
306
+ }
424
307
  }
308
+ SUBRULE(datasetClauseStar, ast.datasets, undefined);
425
309
  if (ast.where) {
426
- builder.push(`WHERE ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.where }, undefined)}`);
310
+ SUBRULE(whereClause, F.wrap(ast.where, ast.loc), undefined);
427
311
  }
428
- builder.push(SUBRULE(solutionModifier, ast, undefined));
429
- return builder.join(' ');
312
+ SUBRULE(solutionModifier, ast.solutionModifiers, undefined);
430
313
  },
431
314
  };
432
315
  /**
@@ -434,29 +317,24 @@ export const describeQuery = {
434
317
  */
435
318
  export const askQuery = {
436
319
  name: 'askQuery',
437
- impl: ({ ACTION, SUBRULE, CONSUME, MANY }) => () => {
438
- CONSUME(l.ask);
439
- const from = extractFromOfDataSetClauses(ACTION, MANY, SUBRULE);
320
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
321
+ const ask = CONSUME(l.ask);
322
+ const from = SUBRULE(datasetClauseStar, undefined);
440
323
  const where = SUBRULE(whereClause, undefined);
441
324
  const modifiers = SUBRULE(solutionModifier, undefined);
442
325
  return ACTION(() => ({
443
- ...modifiers,
444
- queryType: 'ASK',
445
- from,
446
- where,
326
+ subType: 'ask',
327
+ datasets: from,
328
+ where: where.val,
329
+ solutionModifiers: modifiers,
330
+ loc: C.factory.sourceLocation(ask, from, where, modifiers.group, modifiers.having, modifiers.order, modifiers.limitOffset),
447
331
  }));
448
332
  },
449
- gImpl: ({ SUBRULE }) => (ast) => {
450
- const builder = ['ASK'];
451
- if (ast.from) {
452
- builder.push(ast.from.default.map(clause => `FROM ${SUBRULE(iri, clause, undefined)}`).join(' '));
453
- builder.push(ast.from.named.map(clause => `FROM NAMED ${SUBRULE(iri, clause, undefined)}`).join(' '));
454
- }
455
- if (ast.where) {
456
- builder.push(`WHERE ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.where }, undefined)}`);
457
- }
458
- builder.push(SUBRULE(solutionModifier, ast, undefined));
459
- return builder.join(' ');
333
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
334
+ F.printFilter(ast, () => PRINT_WORD('ASK'));
335
+ SUBRULE(datasetClauseStar, ast.datasets, undefined);
336
+ SUBRULE(whereClause, F.wrap(ast.where, ast.loc), undefined);
337
+ SUBRULE(solutionModifier, ast.solutionModifiers, undefined);
460
338
  },
461
339
  };
462
340
  /**
@@ -464,31 +342,19 @@ export const askQuery = {
464
342
  */
465
343
  export const valuesClause = {
466
344
  name: 'valuesClause',
467
- impl: ({ SUBRULE, CONSUME, OPTION }) => () => OPTION(() => {
468
- CONSUME(l.values);
469
- return SUBRULE(dataBlock, undefined);
470
- }),
471
- gImpl: ({ SUBRULE }) => (ast) => {
472
- if (ast) {
473
- return `${SUBRULE(inlineDataFull, ast, undefined)}`;
474
- }
475
- return '';
476
- },
345
+ impl: ({ OPTION, SUBRULE }) => () => OPTION(() => SUBRULE(inlineData, undefined)),
477
346
  };
478
347
  /**
479
348
  * [[73]](https://www.w3.org/TR/sparql11-query/#ConstructTemplate)
480
349
  */
481
350
  export const constructTemplate = {
482
351
  name: 'constructTemplate',
483
- impl: ({ SUBRULE, CONSUME, OPTION }) => () => {
484
- CONSUME(l.symbols.LCurly);
485
- const triples = OPTION(() => SUBRULE(constructTriples, undefined));
486
- CONSUME(l.symbols.RCurly);
487
- return triples;
352
+ impl: ({ ACTION, SUBRULE1, CONSUME, OPTION }) => (C) => {
353
+ const open = CONSUME(l.symbols.LCurly);
354
+ const triples = OPTION(() => SUBRULE1(constructTriples, undefined));
355
+ const close = CONSUME(l.symbols.RCurly);
356
+ return ACTION(() => C.factory.wrap(triples ?? C.factory.patternBgp([], C.factory.sourceLocation()), C.factory.sourceLocation(open, close)));
488
357
  },
489
- gImpl: ({ SUBRULE }) => ast => `{
490
- ${ast ? SUBRULE(triplesBlock, { type: 'bgp', triples: ast }, undefined) : ''}
491
- }`,
492
358
  };
493
359
  /**
494
360
  * [[12]](https://www.w3.org/TR/sparql11-query/#rConstructTriples)