@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,22 +1,24 @@
1
1
  import * as l from '../lexer';
2
- import { deGroupSingle, isVariable } from '../utils';
2
+ import { checkNote13 } from '../validation/validators';
3
3
  import { builtInCall } from './builtIn';
4
4
  import { argList, brackettedExpression, expression } from './expression';
5
- import { graphTerm, var_, varOrIri, varOrTerm } from './general';
5
+ import { var_, varOrIri, varOrTerm } from './general';
6
6
  import { booleanLiteral, iri, numericLiteral, rdfLiteral } from './literals';
7
- import { query, subSelect, valuesClause } from './queryUnit/queryUnit';
8
- import { triplesBlock } from './tripleBlock';
7
+ import { query, subSelect } from './queryUnit/queryUnit';
8
+ import { graphNodePath, triplesBlock } from './tripleBlock';
9
9
  /**
10
10
  * [[17]](https://www.w3.org/TR/sparql11-query/#rWhereClause)
11
11
  */
12
12
  export const whereClause = {
13
13
  name: 'whereClause',
14
- impl: ({ ACTION, SUBRULE, CONSUME, OPTION }) => () => {
15
- OPTION(() => {
16
- CONSUME(l.where);
17
- });
14
+ impl: ({ ACTION, SUBRULE, CONSUME, OPTION }) => (C) => {
15
+ const where = OPTION(() => CONSUME(l.where));
18
16
  const group = SUBRULE(groupGraphPattern, undefined);
19
- return ACTION(() => group.patterns);
17
+ return ACTION(() => C.factory.wrap(group, C.factory.sourceLocation(where, group)));
18
+ },
19
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
20
+ F.printFilter(ast, () => PRINT_WORD('WHERE'));
21
+ SUBRULE(groupGraphPattern, ast.val, undefined);
20
22
  },
21
23
  };
22
24
  /**
@@ -24,56 +26,49 @@ export const whereClause = {
24
26
  */
25
27
  export const groupGraphPattern = {
26
28
  name: 'groupGraphPattern',
27
- impl: ({ SUBRULE, CONSUME, OR }) => () => {
28
- CONSUME(l.symbols.LCurly);
29
+ impl: ({ ACTION, SUBRULE, CONSUME, OR }) => (C) => {
30
+ const open = CONSUME(l.symbols.LCurly);
29
31
  const patterns = OR([
30
32
  { ALT: () => [SUBRULE(subSelect, undefined)] },
31
33
  { ALT: () => SUBRULE(groupGraphPatternSub, undefined) },
32
34
  ]);
33
- CONSUME(l.symbols.RCurly);
34
- return {
35
- type: 'group',
36
- patterns,
37
- };
35
+ const close = CONSUME(l.symbols.RCurly);
36
+ return ACTION(() => C.factory.patternGroup(patterns, C.factory.sourceLocation(open, close)));
38
37
  },
39
- gImpl: ({ SUBRULE }) => (ast) => {
40
- const patterns = ast.patterns;
41
- const builder = ['{\n'];
42
- for (const pattern of patterns) {
43
- if ('queryType' in pattern) {
44
- builder.push(SUBRULE(query, { ...pattern, prefixes: {} }, undefined));
45
- }
46
- else if (pattern.type === 'bgp') {
47
- builder.push(SUBRULE(triplesBlock, pattern, undefined));
48
- }
49
- else {
50
- builder.push(SUBRULE(graphPatternNotTriples, pattern, undefined));
51
- }
38
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
39
+ F.printFilter(ast, () => PRINT_WORD('{'));
40
+ for (const pattern of ast.patterns) {
41
+ SUBRULE(generatePattern, pattern, undefined);
52
42
  }
53
- builder.push('\n}');
54
- return builder.join(' ');
43
+ F.printFilter(ast, () => PRINT_WORD('}'));
55
44
  },
56
45
  };
57
- function findBoundVarsFromGroupGraphPattern(pattern, boundedVars) {
58
- if ('triples' in pattern) {
59
- for (const triple of pattern.triples) {
60
- if (isVariable(triple.subject)) {
61
- boundedVars.add(triple.subject.value);
62
- }
63
- if (isVariable(triple.predicate)) {
64
- boundedVars.add(triple.predicate.value);
65
- }
66
- if (isVariable(triple.object)) {
67
- boundedVars.add(triple.object.value);
68
- }
46
+ export const generatePattern = {
47
+ name: 'generatePattern',
48
+ gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
49
+ if (ast.type === 'query') {
50
+ SUBRULE(query, F.querySelect({
51
+ context: [],
52
+ datasets: F.datasetClauses([], F.sourceLocation()),
53
+ where: ast.where,
54
+ variables: ast.variables,
55
+ solutionModifiers: ast.solutionModifiers,
56
+ values: ast.values,
57
+ distinct: ast.distinct,
58
+ reduced: ast.reduced,
59
+ }, ast.loc), undefined);
60
+ }
61
+ else if (ast.subType === 'group') {
62
+ SUBRULE(groupGraphPattern, ast, undefined);
63
+ }
64
+ else if (ast.subType === 'bgp') {
65
+ SUBRULE(triplesBlock, ast, undefined);
69
66
  }
70
- }
71
- else if ('patterns' in pattern) {
72
- for (const pat of pattern.patterns) {
73
- findBoundVarsFromGroupGraphPattern(pat, boundedVars);
67
+ else {
68
+ SUBRULE(graphPatternNotTriples, ast, undefined);
74
69
  }
75
- }
76
- }
70
+ },
71
+ };
77
72
  /**
78
73
  * [[54]](https://www.w3.org/TR/sparql11-query/#rGroupGraphPatternSub)
79
74
  */
@@ -82,43 +77,25 @@ export const groupGraphPatternSub = {
82
77
  impl: ({ ACTION, SUBRULE, CONSUME, MANY, SUBRULE1, SUBRULE2, OPTION1, OPTION2, OPTION3 }) => () => {
83
78
  const patterns = [];
84
79
  const bgpPattern = OPTION1(() => SUBRULE1(triplesBlock, undefined));
85
- ACTION(() => {
86
- if (bgpPattern) {
87
- patterns.push(bgpPattern);
88
- }
89
- });
80
+ if (bgpPattern) {
81
+ patterns.push(bgpPattern);
82
+ }
90
83
  MANY(() => {
91
84
  const notTriples = SUBRULE(graphPatternNotTriples, undefined);
92
85
  patterns.push(notTriples);
93
86
  OPTION2(() => CONSUME(l.symbols.dot));
94
87
  const moreTriples = OPTION3(() => SUBRULE2(triplesBlock, undefined));
95
- ACTION(() => {
96
- if (moreTriples) {
97
- patterns.push(moreTriples);
98
- }
99
- });
100
- });
101
- // Check note 13 of the spec.
102
- // TODO: currently optimized for case bind is present.
103
- // Since every iteration, even when no bind is present, we walk the tree collecting variables.
104
- // optimize either by: checking whether bind is present, or by keeping track of variables and passing them through
105
- ACTION(() => {
106
- const boundedVars = new Set();
107
- for (const pattern of patterns) {
108
- // Element can be bind, in that case, check note 13. If it is not, buildup set of bounded variables.
109
- if (pattern.type === 'bind') {
110
- if (boundedVars.has(pattern.variable.value)) {
111
- throw new Error(`Variable used to bind is already bound (?${pattern.variable.value})`);
112
- }
113
- }
114
- else if (pattern.type === 'group' || pattern.type === 'bgp') {
115
- findBoundVarsFromGroupGraphPattern(pattern, boundedVars);
116
- }
88
+ if (moreTriples) {
89
+ patterns.push(moreTriples);
117
90
  }
118
91
  });
92
+ ACTION(() => checkNote13(patterns));
119
93
  return patterns;
120
94
  },
121
95
  };
96
+ /**
97
+ * [[56]](https://www.w3.org/TR/sparql11-query/#rGraphPatternNotTriples)
98
+ */
122
99
  export const graphPatternNotTriples = {
123
100
  name: 'graphPatternNotTriples',
124
101
  impl: ({ SUBRULE, OR }) => () => OR([
@@ -132,24 +109,32 @@ export const graphPatternNotTriples = {
132
109
  { ALT: () => SUBRULE(inlineData, undefined) },
133
110
  ]),
134
111
  gImpl: ({ SUBRULE }) => (ast) => {
135
- switch (ast.type) {
112
+ switch (ast.subType) {
136
113
  case 'group':
137
114
  case 'union':
138
- return SUBRULE(groupOrUnionGraphPattern, ast, undefined);
115
+ SUBRULE(groupOrUnionGraphPattern, ast, undefined);
116
+ break;
139
117
  case 'optional':
140
- return SUBRULE(optionalGraphPattern, ast, undefined);
118
+ SUBRULE(optionalGraphPattern, ast, undefined);
119
+ break;
141
120
  case 'minus':
142
- return SUBRULE(minusGraphPattern, ast, undefined);
121
+ SUBRULE(minusGraphPattern, ast, undefined);
122
+ break;
143
123
  case 'graph':
144
- return SUBRULE(graphGraphPattern, ast, undefined);
124
+ SUBRULE(graphGraphPattern, ast, undefined);
125
+ break;
145
126
  case 'service':
146
- return SUBRULE(serviceGraphPattern, ast, undefined);
127
+ SUBRULE(serviceGraphPattern, ast, undefined);
128
+ break;
147
129
  case 'filter':
148
- return SUBRULE(filter, ast, undefined);
130
+ SUBRULE(filter, ast, undefined);
131
+ break;
149
132
  case 'bind':
150
- return SUBRULE(bind, ast, undefined);
133
+ SUBRULE(bind, ast, undefined);
134
+ break;
151
135
  case 'values':
152
- return SUBRULE(valuesClause, ast.values, undefined);
136
+ SUBRULE(inlineData, ast, undefined);
137
+ break;
153
138
  }
154
139
  },
155
140
  };
@@ -158,84 +143,113 @@ export const graphPatternNotTriples = {
158
143
  */
159
144
  export const optionalGraphPattern = {
160
145
  name: 'optionalGraphPattern',
161
- impl: ({ ACTION, SUBRULE, CONSUME }) => () => {
162
- CONSUME(l.optional);
146
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
147
+ const optional = CONSUME(l.optional);
163
148
  const group = SUBRULE(groupGraphPattern, undefined);
164
- return ACTION(() => ({
165
- type: 'optional',
166
- patterns: group.patterns,
167
- }));
149
+ return ACTION(() => C.factory.patternOptional(group.patterns, C.factory.sourceLocation(optional, group)));
150
+ },
151
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
152
+ F.printFilter(ast, () => PRINT_WORD('OPTIONAL'));
153
+ SUBRULE(groupGraphPattern, F.patternGroup(ast.patterns, ast.loc), undefined);
168
154
  },
169
- gImpl: ({ SUBRULE }) => ast => `OPTIONAL ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.patterns }, undefined)}`,
170
155
  };
171
156
  /**
172
157
  * [[58]](https://www.w3.org/TR/sparql11-query/#rGraphGraphPattern)
173
158
  */
174
159
  export const graphGraphPattern = {
175
160
  name: 'graphGraphPattern',
176
- impl: ({ ACTION, SUBRULE, CONSUME }) => () => {
177
- CONSUME(l.graph.graph);
161
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
162
+ const graph = CONSUME(l.graph.graph);
178
163
  const name = SUBRULE(varOrIri, undefined);
179
164
  const group = SUBRULE(groupGraphPattern, undefined);
180
- return ACTION(() => ({
181
- type: 'graph',
182
- name,
183
- patterns: group.patterns,
184
- }));
165
+ return ACTION(() => C.factory.patternGraph(name, group.patterns, C.factory.sourceLocation(graph, group)));
166
+ },
167
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
168
+ F.printFilter(ast, () => PRINT_WORD('GRAPH'));
169
+ SUBRULE(varOrTerm, ast.name, undefined);
170
+ SUBRULE(groupGraphPattern, F.patternGroup(ast.patterns, ast.loc), undefined);
185
171
  },
186
- gImpl: ({ SUBRULE }) => ast => `GRAPH ${SUBRULE(varOrTerm, ast.name, undefined)} ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.patterns }, undefined)}`,
187
172
  };
188
173
  /**
189
174
  * [[59]](https://www.w3.org/TR/sparql11-query/#rServiceGraphPattern)
190
175
  */
191
176
  export const serviceGraphPattern = {
192
177
  name: 'serviceGraphPattern',
193
- impl: ({ SUBRULE, CONSUME, OPTION }) => () => {
194
- CONSUME(l.service);
195
- const silent = Boolean(OPTION(() => CONSUME(l.silent)));
196
- const name = SUBRULE(varOrIri, undefined);
197
- const group = SUBRULE(groupGraphPattern, undefined);
198
- return {
199
- type: 'service',
200
- name,
201
- silent,
202
- patterns: group.patterns,
203
- };
178
+ impl: ({ ACTION, SUBRULE1, CONSUME, OPTION }) => (C) => {
179
+ const service = CONSUME(l.service);
180
+ const silent = OPTION(() => {
181
+ CONSUME(l.silent);
182
+ return true;
183
+ }) ?? false;
184
+ const name = SUBRULE1(varOrIri, undefined);
185
+ const group = SUBRULE1(groupGraphPattern, undefined);
186
+ return ACTION(() => C.factory.patternService(name, group.patterns, silent, C.factory.sourceLocation(service, group)));
187
+ },
188
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
189
+ F.printFilter(ast, () => {
190
+ PRINT_WORD('SERVICE');
191
+ if (ast.silent) {
192
+ PRINT_WORD('SILENT');
193
+ }
194
+ });
195
+ SUBRULE(varOrTerm, ast.name, undefined);
196
+ SUBRULE(groupGraphPattern, F.patternGroup(ast.patterns, ast.loc), undefined);
204
197
  },
205
- gImpl: ({ SUBRULE }) => ast => `SERVICE ${ast.silent ? 'SILENT ' : ''}${SUBRULE(varOrTerm, ast.name, undefined)} ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.patterns }, undefined)}`,
206
198
  };
207
199
  /**
208
200
  * [[60]](https://www.w3.org/TR/sparql11-query/#rBind)
209
201
  */
210
202
  export const bind = {
211
203
  name: 'bind',
212
- impl: ({ SUBRULE, CONSUME }) => () => {
213
- CONSUME(l.bind);
204
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
205
+ const bind = CONSUME(l.bind);
214
206
  CONSUME(l.symbols.LParen);
215
207
  const expressionVal = SUBRULE(expression, undefined);
216
208
  CONSUME(l.as);
217
209
  const variable = SUBRULE(var_, undefined);
218
- CONSUME(l.symbols.RParen);
219
- return {
220
- type: 'bind',
221
- variable,
222
- expression: expressionVal,
223
- };
210
+ const close = CONSUME(l.symbols.RParen);
211
+ return ACTION(() => C.factory.patternBind(expressionVal, variable, C.factory.sourceLocation(bind, close)));
212
+ },
213
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
214
+ F.printFilter(ast, () => PRINT_WORD('BIND', '('));
215
+ SUBRULE(expression, ast.expression, undefined);
216
+ F.printFilter(ast, () => PRINT_WORD('AS'));
217
+ SUBRULE(var_, ast.variable, undefined);
218
+ F.printFilter(ast, () => PRINT_WORD(')'));
224
219
  },
225
- gImpl: ({ SUBRULE }) => ast => `BIND ( ${SUBRULE(expression, ast.expression, undefined)} AS ${SUBRULE(var_, ast.variable, undefined)} )`,
226
220
  };
227
221
  /**
228
222
  * [[61]](https://www.w3.org/TR/sparql11-query/#rInlineData)
229
223
  */
230
224
  export const inlineData = {
231
225
  name: 'inlineData',
232
- impl: ({ SUBRULE, CONSUME }) => () => {
233
- CONSUME(l.values);
234
- const values = SUBRULE(dataBlock, undefined);
235
- return {
236
- type: 'values',
237
- values,
238
- };
226
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
227
+ const values = CONSUME(l.values);
228
+ const datablock = SUBRULE(dataBlock, undefined);
229
+ return ACTION(() => C.factory.patternValues(datablock.val, C.factory.sourceLocation(values, datablock)));
230
+ },
231
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
232
+ const variables = Object.keys(ast.values[0]);
233
+ F.printFilter(ast, () => {
234
+ PRINT_WORD('VALUES', '(');
235
+ for (const variable of variables) {
236
+ PRINT_WORD(`?${variable}`);
237
+ }
238
+ PRINT_WORD(')', '{');
239
+ });
240
+ for (const mapping of ast.values) {
241
+ F.printFilter(ast, () => PRINT_WORD('('));
242
+ for (const variable of variables) {
243
+ if (mapping[variable] === undefined) {
244
+ F.printFilter(ast, () => PRINT_WORD('UNDEF'));
245
+ }
246
+ else {
247
+ SUBRULE(graphNodePath, mapping[variable], undefined);
248
+ }
249
+ }
250
+ F.printFilter(ast, () => PRINT_WORD(')'));
251
+ }
252
+ F.printFilter(ast, () => PRINT_WORD('}'));
239
253
  },
240
254
  };
241
255
  /**
@@ -253,18 +267,18 @@ export const dataBlock = {
253
267
  */
254
268
  export const inlineDataOneVar = {
255
269
  name: 'inlineDataOneVar',
256
- impl: ({ ACTION, SUBRULE, CONSUME, MANY }) => () => {
270
+ impl: ({ ACTION, SUBRULE, CONSUME, MANY }) => (C) => {
257
271
  const res = [];
258
272
  const varVal = SUBRULE(var_, undefined);
259
273
  CONSUME(l.symbols.LCurly);
260
274
  MANY(() => {
261
275
  const value = SUBRULE(dataBlockValue, undefined);
262
- ACTION(() => res.push({
263
- [`?${varVal.value}`]: value,
264
- }));
276
+ ACTION(() => {
277
+ res.push({ [varVal.value]: value });
278
+ });
265
279
  });
266
- CONSUME(l.symbols.RCurly);
267
- return res;
280
+ const close = CONSUME(l.symbols.RCurly);
281
+ return ACTION(() => C.factory.wrap(res, C.factory.sourceLocation(varVal, close)));
268
282
  },
269
283
  };
270
284
  /**
@@ -272,72 +286,54 @@ export const inlineDataOneVar = {
272
286
  */
273
287
  export const inlineDataFull = {
274
288
  name: 'inlineDataFull',
275
- impl: ({ ACTION, OR, MANY1, MANY2, MANY3, MANY4, SUBRULE, CONSUME1, CONSUME2 }) => () => OR([
276
- // Grammar rule 64 together with note 11 learns us that a nil should be followed by a nil in DataBlock.
277
- {
278
- ALT: () => {
279
- const res = [];
280
- CONSUME1(l.terminals.nil);
281
- CONSUME1(l.symbols.LCurly);
282
- MANY1(() => {
283
- CONSUME2(l.terminals.nil);
284
- res.push({});
285
- });
286
- CONSUME1(l.symbols.RCurly);
287
- return res;
288
- },
289
- },
290
- {
291
- ALT: () => {
292
- const res = [];
293
- const vars = [];
294
- CONSUME1(l.symbols.LParen);
295
- MANY2(() => {
296
- vars.push(SUBRULE(var_, undefined));
297
- });
298
- CONSUME1(l.symbols.RParen);
299
- CONSUME2(l.symbols.LCurly);
300
- MANY3(() => {
301
- const varBinds = [];
302
- CONSUME2(l.symbols.LParen);
303
- MANY4({
304
- DEF: () => {
289
+ impl: ({ ACTION, OR, MANY1, MANY2, MANY3, MANY4, SUBRULE, CONSUME1, CONSUME2, }) => (C) => {
290
+ const res = [];
291
+ const vars = [];
292
+ return OR([
293
+ { ALT: () => {
294
+ // Grammar rule 64 together with note 11 learns us that a nil should be followed by a nil in DataBlock.
295
+ const nil = CONSUME1(l.terminals.nil);
296
+ CONSUME1(l.symbols.LCurly);
297
+ MANY1(() => {
298
+ CONSUME2(l.terminals.nil);
299
+ res.push({});
300
+ });
301
+ const close = CONSUME1(l.symbols.RCurly);
302
+ return ACTION(() => C.factory.wrap(res, C.factory.sourceLocation(nil, close)));
303
+ } },
304
+ { ALT: () => {
305
+ const open = CONSUME1(l.symbols.LParen);
306
+ MANY2(() => {
307
+ vars.push(SUBRULE(var_, undefined));
308
+ });
309
+ CONSUME1(l.symbols.RParen);
310
+ CONSUME2(l.symbols.LCurly);
311
+ MANY3(() => {
312
+ let parsedValues = 0;
313
+ const currentRow = {};
314
+ CONSUME2(l.symbols.LParen);
315
+ MANY4(() => {
316
+ if (parsedValues >= vars.length) {
317
+ throw new Error('Number of dataBlockValues does not match number of variables. Too much values.');
318
+ }
319
+ const value = SUBRULE(dataBlockValue, undefined);
305
320
  ACTION(() => {
306
- if (vars.length <= varBinds.length) {
307
- throw new Error('Number of dataBlockValues does not match number of variables. Too much values.');
308
- }
321
+ currentRow[vars[parsedValues].value] = value;
322
+ parsedValues++;
309
323
  });
310
- varBinds.push(SUBRULE(dataBlockValue, undefined));
311
- },
324
+ });
325
+ CONSUME2(l.symbols.RParen);
326
+ ACTION(() => {
327
+ res.push(currentRow);
328
+ if (vars.length !== parsedValues) {
329
+ throw new Error('Number of dataBlockValues does not match number of variables. Too few values.');
330
+ }
331
+ });
312
332
  });
313
- CONSUME2(l.symbols.RParen);
314
- ACTION(() => {
315
- if (varBinds.length !== vars.length) {
316
- throw new Error('Number of dataBlockValues does not match number of variables. Too few values.');
317
- }
318
- const row = {};
319
- for (const [index, varVal] of vars.entries()) {
320
- row[`?${varVal.value}`] = varBinds[index];
321
- }
322
- res.push(row);
323
- });
324
- });
325
- CONSUME2(l.symbols.RCurly);
326
- return res;
327
- },
328
- },
329
- ]),
330
- gImpl: ({ SUBRULE }) => (ast) => {
331
- const variables = Object.keys(ast[0]);
332
- const variableString = `( ${variables.join(' ')} )`;
333
- const values = ast.map((mapping) => {
334
- const valueString = variables.map((variable) => {
335
- const value = mapping[variable];
336
- return value ? SUBRULE(dataBlockValue, value, undefined) : 'UNDEF';
337
- }).join(' ');
338
- return `( ${valueString} )`;
339
- });
340
- return `VALUES ${variableString} { ${values.join(' ')} }`;
333
+ const close = CONSUME2(l.symbols.RCurly);
334
+ return ACTION(() => C.factory.wrap(res, C.factory.sourceLocation(open, close)));
335
+ } },
336
+ ]);
341
337
  },
342
338
  };
343
339
  /**
@@ -350,65 +346,58 @@ export const dataBlockValue = {
350
346
  { ALT: () => SUBRULE(rdfLiteral, undefined) },
351
347
  { ALT: () => SUBRULE(numericLiteral, undefined) },
352
348
  { ALT: () => SUBRULE(booleanLiteral, undefined) },
353
- {
354
- ALT: () => {
349
+ { ALT: () => {
355
350
  CONSUME(l.undef);
356
351
  // eslint-disable-next-line unicorn/no-useless-undefined
357
352
  return undefined;
358
- },
359
- },
353
+ } },
360
354
  ]),
361
- gImpl: ({ SUBRULE }) => (ast) => {
362
- if (ast) {
363
- return SUBRULE(graphTerm, ast, undefined);
364
- }
365
- return 'UNDEF';
366
- },
367
355
  };
368
356
  /**
369
357
  * [[66]](https://www.w3.org/TR/sparql11-query/#rMinusGraphPattern)
370
358
  */
371
359
  export const minusGraphPattern = {
372
360
  name: 'minusGraphPattern',
373
- impl: ({ ACTION, SUBRULE, CONSUME }) => () => {
374
- CONSUME(l.minus);
361
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
362
+ const minus = CONSUME(l.minus);
375
363
  const group = SUBRULE(groupGraphPattern, undefined);
376
- return ACTION(() => ({
377
- type: 'minus',
378
- patterns: group.patterns,
379
- }));
364
+ return ACTION(() => C.factory.patternMinus(group.patterns, C.factory.sourceLocation(minus, group)));
365
+ },
366
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
367
+ F.printFilter(ast, () => PRINT_WORD('MINUS'));
368
+ SUBRULE(groupGraphPattern, F.patternGroup(ast.patterns, ast.loc), undefined);
380
369
  },
381
- gImpl: ({ SUBRULE }) => ast => `MINUS ${SUBRULE(groupGraphPattern, { type: 'group', patterns: ast.patterns }, undefined)}`,
382
370
  };
383
371
  /**
384
372
  * [[67]](https://www.w3.org/TR/sparql11-query/#rGroupOrUnionGraphPattern)
385
373
  */
386
374
  export const groupOrUnionGraphPattern = {
387
375
  name: 'groupOrUnionGraphPattern',
388
- impl: ({ AT_LEAST_ONE_SEP, SUBRULE }) => () => {
376
+ impl: ({ ACTION, MANY, SUBRULE1, SUBRULE2, CONSUME }) => (C) => {
389
377
  const groups = [];
390
- AT_LEAST_ONE_SEP({
391
- DEF: () => {
392
- const group = SUBRULE(groupGraphPattern, undefined);
393
- groups.push(group);
394
- },
395
- SEP: l.union,
378
+ const group = SUBRULE1(groupGraphPattern, undefined);
379
+ groups.push(group);
380
+ MANY(() => {
381
+ CONSUME(l.union);
382
+ const group = SUBRULE2(groupGraphPattern, undefined);
383
+ groups.push(group);
396
384
  });
397
- return groups.length === 1 ?
385
+ return ACTION(() => groups.length === 1 ?
398
386
  groups[0] :
399
- {
400
- type: 'union',
401
- patterns: groups.map(group => deGroupSingle(group)),
402
- };
387
+ C.factory.patternUnion(groups, C.factory.sourceLocation(group, groups.at(-1))));
403
388
  },
404
- gImpl: ({ SUBRULE }) => (ast) => {
405
- if (ast.type === 'group') {
406
- return SUBRULE(groupGraphPattern, ast, undefined);
389
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
390
+ if (F.isPatternUnion(ast)) {
391
+ const [head, ...tail] = ast.patterns;
392
+ SUBRULE(groupGraphPattern, head, undefined);
393
+ for (const pattern of tail) {
394
+ F.printFilter(ast, () => PRINT_WORD('UNION'));
395
+ SUBRULE(groupGraphPattern, pattern, undefined);
396
+ }
397
+ }
398
+ else {
399
+ SUBRULE(groupGraphPattern, ast, undefined);
407
400
  }
408
- return ast.patterns.map(pattern => SUBRULE(groupGraphPattern, {
409
- type: 'group',
410
- patterns: [pattern],
411
- }, undefined)).join(' UNION ');
412
401
  },
413
402
  };
414
403
  /**
@@ -416,15 +405,16 @@ export const groupOrUnionGraphPattern = {
416
405
  */
417
406
  export const filter = {
418
407
  name: 'filter',
419
- impl: ({ SUBRULE, CONSUME }) => () => {
420
- CONSUME(l.filter);
408
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
409
+ const filterToken = CONSUME(l.filter);
421
410
  const expression = SUBRULE(constraint, undefined);
422
- return {
423
- type: 'filter',
424
- expression,
425
- };
411
+ return ACTION(() => C.factory.patternFilter(expression, C.factory.sourceLocation(filterToken, expression)));
412
+ },
413
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
414
+ F.printFilter(ast, () => PRINT_WORD('FILTER ('));
415
+ SUBRULE(expression, ast.expression, undefined);
416
+ F.printFilter(ast, () => PRINT_WORD(')'));
426
417
  },
427
- gImpl: ({ SUBRULE }) => ast => `FILTER ( ${SUBRULE(expression, ast.expression, undefined)} )`,
428
418
  };
429
419
  /**
430
420
  * [[69]](https://www.w3.org/TR/sparql11-query/#rConstraint)
@@ -442,13 +432,10 @@ export const constraint = {
442
432
  */
443
433
  export const functionCall = {
444
434
  name: 'functionCall',
445
- impl: ({ ACTION, SUBRULE }) => () => {
435
+ impl: ({ ACTION, SUBRULE }) => (C) => {
446
436
  const func = SUBRULE(iri, undefined);
447
437
  const args = SUBRULE(argList, undefined);
448
- return ACTION(() => ({
449
- ...args,
450
- function: func,
451
- }));
438
+ return ACTION(() => C.factory.expressionFunctionCall(func, args.val.args, args.val.distinct, C.factory.sourceLocation(func, args)));
452
439
  },
453
440
  };
454
441
  //# sourceMappingURL=whereClause.js.map