@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
@@ -5,117 +5,136 @@ import { booleanLiteral, iri, numericLiteral, numericLiteralNegative, numericLit
5
5
  import { groupGraphPattern } from './whereClause';
6
6
  export const argList = {
7
7
  name: 'argList',
8
- impl: ({ CONSUME, SUBRULE1, OPTION, OR, MANY_SEP }) => () => OR([
9
- {
10
- ALT: () => {
11
- CONSUME(l.terminals.nil);
12
- return {
13
- type: 'functionCall',
14
- args: [],
15
- distinct: false,
16
- };
17
- },
18
- },
19
- {
20
- ALT: () => {
8
+ impl: ({ ACTION, CONSUME, SUBRULE1, OPTION, OR, AT_LEAST_ONE_SEP }) => C => OR([
9
+ { ALT: () => {
10
+ const nil = CONSUME(l.terminals.nil);
11
+ return ACTION(() => C.factory.wrap({ args: [], distinct: false }, C.factory.sourceLocation(nil)));
12
+ } },
13
+ { ALT: () => {
21
14
  const args = [];
22
- CONSUME(l.symbols.LParen);
23
- const distinct = OPTION(() => CONSUME(l.distinct)) && true;
24
- MANY_SEP({
25
- DEF: () => args.push(SUBRULE1(expression, undefined)),
15
+ const open = CONSUME(l.symbols.LParen);
16
+ const distinct = OPTION(() => {
17
+ CONSUME(l.distinct);
18
+ return true;
19
+ }) ?? false;
20
+ AT_LEAST_ONE_SEP({
26
21
  SEP: l.symbols.comma,
22
+ DEF: () => {
23
+ const arg = SUBRULE1(expression, undefined);
24
+ args.push(arg);
25
+ },
27
26
  });
28
- CONSUME(l.symbols.RParen);
29
- return {
30
- type: 'functionCall',
31
- args,
32
- distinct: Boolean(distinct),
33
- };
34
- },
35
- },
27
+ const close = CONSUME(l.symbols.RParen);
28
+ return ACTION(() => C.factory.wrap({ args, distinct }, C.factory.sourceLocation(open, close)));
29
+ } },
36
30
  ]),
37
- gImpl: ({ SUBRULE }) => (ast) => {
38
- const builder = ['('];
39
- if (ast.distinct) {
40
- builder.push('DISTINCT');
31
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
32
+ F.printFilter(ast, () => {
33
+ PRINT_WORD('(');
34
+ if (ast.val.distinct) {
35
+ PRINT_WORD('distinct');
36
+ }
37
+ });
38
+ const [head, ...tail] = ast.val.args;
39
+ if (head) {
40
+ SUBRULE(expression, head, undefined);
41
41
  }
42
- // Slice brackets
43
- builder.push(SUBRULE(expression, ast.args, undefined).slice(1, -1));
44
- builder.push(')');
45
- return builder.join(' ');
42
+ for (const expr of tail) {
43
+ F.printFilter(ast, () => PRINT_WORD(','));
44
+ SUBRULE(expression, expr, undefined);
45
+ }
46
+ F.printFilter(ast, () => PRINT_WORD(')'));
46
47
  },
47
48
  };
49
+ /**
50
+ * [[72]](https://www.w3.org/TR/sparql11-query/#rConstructTemplate)
51
+ */
48
52
  export const expressionList = {
49
53
  name: 'expressionList',
50
- impl: ({ CONSUME, SUBRULE, MANY_SEP, OR }) => () => OR([
51
- {
52
- ALT: () => {
53
- CONSUME(l.terminals.nil);
54
- return [];
55
- },
56
- },
57
- {
58
- ALT: () => {
59
- const args = [];
60
- CONSUME(l.symbols.LParen);
61
- MANY_SEP({
62
- SEP: l.symbols.comma,
63
- DEF: () => {
64
- args.push(SUBRULE(expression, undefined));
65
- },
54
+ impl: ({ ACTION, CONSUME, MANY, OR, SUBRULE1, SUBRULE2 }) => C => OR([
55
+ { ALT: () => {
56
+ const nil = CONSUME(l.terminals.nil);
57
+ return ACTION(() => C.factory.wrap([], C.factory.sourceLocation(nil)));
58
+ } },
59
+ { ALT: () => {
60
+ const open = CONSUME(l.symbols.LParen);
61
+ const expr1 = SUBRULE1(expression, undefined);
62
+ const args = [expr1];
63
+ MANY(() => {
64
+ CONSUME(l.symbols.comma);
65
+ const expr = SUBRULE2(expression, undefined);
66
+ args.push(expr);
66
67
  });
67
- CONSUME(l.symbols.RParen);
68
- return args;
69
- },
70
- },
68
+ const close = CONSUME(l.symbols.RParen);
69
+ return ACTION(() => C.factory.wrap(args, C.factory.sourceLocation(open, close)));
70
+ } },
71
71
  ]),
72
72
  };
73
+ const infixOperators = new Set(['in', 'notin', '||', '&&', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', '/']);
74
+ const prefixOperator = new Set(['!', 'UPLUS', 'UMINUS']);
73
75
  /**
74
76
  * [[110]](https://www.w3.org/TR/sparql11-query/#rExpression)
75
77
  */
76
78
  export const expression = {
77
79
  name: 'expression',
78
80
  impl: ({ SUBRULE }) => () => SUBRULE(conditionalOrExpression, undefined),
79
- gImpl: ({ SUBRULE }) => (ast) => {
80
- if (Array.isArray(ast)) {
81
- return `( ${ast.map(arg => SUBRULE(expression, arg, undefined)).join(', ')} )`;
81
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
82
+ if (F.isTerm(ast)) {
83
+ SUBRULE(varOrTerm, ast, undefined);
82
84
  }
83
- if ('type' in ast) {
84
- if (ast.type === 'operation') {
85
- if (['||', '&&', '=', '!=', '<', '>', '<=', '>=', 'in', '+', '-', '*', '/'].includes(ast.operator)) {
86
- const [left, right] = ast.args;
87
- return `( ${SUBRULE(expression, left, undefined)} ${ast.operator} ${SUBRULE(expression, right, undefined)} )`;
88
- }
89
- if (ast.operator === 'notin') {
90
- const [left, right] = ast.args;
91
- return `( ${SUBRULE(expression, left, undefined)} NOT IN ${SUBRULE(expression, right, undefined)} )`;
92
- }
93
- if (['!', 'UPLUS', 'UMINUS'].includes(ast.operator)) {
94
- const [expr] = ast.args;
95
- return `${ast.operator}${SUBRULE(expression, expr, undefined)}`;
96
- }
97
- if (['in', 'notin'].includes(ast.operator)) {
98
- // ExpressionList
99
- const [first, rest] = ast.args;
100
- const operator = ast.operator === 'in' ? 'IN' : 'NOT IN';
101
- return `${SUBRULE(expression, first, undefined)} ${operator} ( ${SUBRULE(expression, rest, undefined)} )`;
85
+ else if (F.isExpressionOperator(ast)) {
86
+ if (infixOperators.has(ast.operator)) {
87
+ const [left, ...right] = ast.args;
88
+ F.printFilter(ast, () => PRINT_WORD('('));
89
+ SUBRULE(expression, left, undefined);
90
+ F.printFilter(ast, () => {
91
+ if (ast.operator === 'notin') {
92
+ PRINT_WORD('NOT IN');
93
+ }
94
+ else if (ast.operator === 'in') {
95
+ PRINT_WORD('IN');
96
+ }
97
+ else {
98
+ PRINT_WORD(ast.operator);
99
+ }
100
+ });
101
+ if (right.length === 1) {
102
+ SUBRULE(expression, right[0], undefined);
102
103
  }
103
- if (['exists', 'notexists'].includes(ast.operator)) {
104
- const patterns = ast.args;
105
- const operator = ast.operator === 'exists' ? 'EXISTS' : 'NOT EXISTS';
106
- return `${operator} ${SUBRULE(groupGraphPattern, { type: 'group', patterns }, undefined)}`;
104
+ else {
105
+ SUBRULE(argList, F.wrap({ args: right, distinct: false }, ast.loc), undefined);
107
106
  }
108
- // It's a builtin function
109
- return `${ast.operator}( ${ast.args.map(arg => SUBRULE(expression, arg, undefined)).join(', ')} )`;
107
+ F.printFilter(ast, () => PRINT_WORD(')'));
110
108
  }
111
- if (ast.type === 'functionCall') {
112
- return SUBRULE(iriOrFunction, ast, undefined);
109
+ else if (prefixOperator.has(ast.operator)) {
110
+ const [expr] = ast.args;
111
+ F.printFilter(ast, () => PRINT_WORD(ast.operator));
112
+ SUBRULE(expression, expr, undefined);
113
113
  }
114
- if (ast.type === 'aggregate') {
115
- return SUBRULE(aggregate, ast, undefined);
114
+ else {
115
+ F.printFilter(ast, () => PRINT_WORD(ast.operator, '('));
116
+ const [head, ...tail] = ast.args;
117
+ if (head) {
118
+ SUBRULE(expression, head, undefined);
119
+ }
120
+ for (const arg of tail) {
121
+ F.printFilter(ast, () => PRINT_WORD(','));
122
+ SUBRULE(expression, arg, undefined);
123
+ }
124
+ F.printFilter(ast, () => PRINT_WORD(')'));
116
125
  }
117
126
  }
118
- return SUBRULE(varOrTerm, ast, undefined);
127
+ else if (F.isExpressionPatternOperation(ast)) {
128
+ const patterns = ast.args;
129
+ F.printFilter(ast, () => PRINT_WORD(ast.operator === 'exists' ? 'EXISTS' : 'NOT EXISTS'));
130
+ SUBRULE(groupGraphPattern, patterns, undefined);
131
+ }
132
+ else if (F.isExpressionFunctionCall(ast)) {
133
+ SUBRULE(iriOrFunction, ast, undefined);
134
+ }
135
+ else if (F.isExpressionAggregate(ast)) {
136
+ SUBRULE(aggregate, ast, undefined);
137
+ }
119
138
  },
120
139
  };
121
140
  function constructLeftDeep(startGenerator, restGenerator, ACTION, MANY) {
@@ -124,11 +143,7 @@ function constructLeftDeep(startGenerator, restGenerator, ACTION, MANY) {
124
143
  MANY(() => {
125
144
  const res = restGenerator();
126
145
  ACTION(() => {
127
- iterExpr = {
128
- type: 'operation',
129
- operator: res.operator,
130
- args: [iterExpr, res.expr],
131
- };
146
+ iterExpr = res(iterExpr);
132
147
  });
133
148
  });
134
149
  return iterExpr;
@@ -138,12 +153,10 @@ function constructLeftDeep(startGenerator, restGenerator, ACTION, MANY) {
138
153
  */
139
154
  export const conditionalOrExpression = {
140
155
  name: 'conditionalOrExpression',
141
- impl: ({ ACTION, MANY, CONSUME, SUBRULE1, SUBRULE2 }) => () => constructLeftDeep(() => SUBRULE1(conditionalAndExpression, undefined), () => {
156
+ impl: ({ ACTION, MANY, CONSUME, SUBRULE1, SUBRULE2 }) => C => constructLeftDeep(() => SUBRULE1(conditionalAndExpression, undefined), () => {
142
157
  CONSUME(l.symbols.logicOr);
143
- return {
144
- expr: SUBRULE2(conditionalAndExpression, undefined),
145
- operator: '||',
146
- };
158
+ const args = SUBRULE2(conditionalAndExpression, undefined);
159
+ return left => ACTION(() => C.factory.expressionOperation('||', [left, args], C.factory.sourceLocation(left, args)));
147
160
  }, ACTION, MANY),
148
161
  };
149
162
  /**
@@ -151,12 +164,10 @@ export const conditionalOrExpression = {
151
164
  */
152
165
  export const conditionalAndExpression = {
153
166
  name: 'conditionalAndExpression',
154
- impl: ({ ACTION, MANY, SUBRULE1, SUBRULE2, CONSUME }) => () => constructLeftDeep(() => SUBRULE1(valueLogical, undefined), () => {
167
+ impl: ({ ACTION, MANY, SUBRULE1, SUBRULE2, CONSUME }) => C => constructLeftDeep(() => SUBRULE1(valueLogical, undefined), () => {
155
168
  CONSUME(l.symbols.logicAnd);
156
- return {
157
- expr: SUBRULE2(valueLogical, undefined),
158
- operator: '&&',
159
- };
169
+ const arg = SUBRULE2(valueLogical, undefined);
170
+ return left => ACTION(() => C.factory.expressionOperation('&&', [left, arg], C.factory.sourceLocation(left, arg)));
160
171
  }, ACTION, MANY),
161
172
  };
162
173
  /**
@@ -171,74 +182,32 @@ export const valueLogical = {
171
182
  */
172
183
  export const relationalExpression = {
173
184
  name: 'relationalExpression',
174
- impl: ({ ACTION, CONSUME, SUBRULE1, SUBRULE2, OPTION, OR, SUBRULE3, SUBRULE4, SUBRULE5, SUBRULE6, SUBRULE7 }) => () => {
185
+ impl: ({ ACTION, CONSUME, SUBRULE1, SUBRULE2, OPTION, OR1, OR2, OR3 }) => (C) => {
175
186
  const args1 = SUBRULE1(numericExpression, undefined);
176
- const arg2 = OPTION(() => OR([
177
- {
178
- ALT: () => {
179
- CONSUME(l.symbols.equal);
187
+ const expression = OPTION(() => OR1([
188
+ { ALT: () => {
189
+ // Stay in numeric;
190
+ const operator = OR2([
191
+ { ALT: () => CONSUME(l.symbols.equal) },
192
+ { ALT: () => CONSUME(l.symbols.notEqual) },
193
+ { ALT: () => CONSUME(l.symbols.lessThan) },
194
+ { ALT: () => CONSUME(l.symbols.greaterThan) },
195
+ { ALT: () => CONSUME(l.symbols.lessThanEqual) },
196
+ { ALT: () => CONSUME(l.symbols.greaterThanEqual) },
197
+ ]);
180
198
  const expr = SUBRULE2(numericExpression, undefined);
181
- return { operator: '=', args: expr };
182
- },
183
- },
184
- {
185
- ALT: () => {
186
- CONSUME(l.symbols.notEqual);
187
- const expr = SUBRULE3(numericExpression, undefined);
188
- return { operator: '!=', args: expr };
189
- },
190
- },
191
- {
192
- ALT: () => {
193
- CONSUME(l.symbols.lessThan);
194
- const expr = SUBRULE4(numericExpression, undefined);
195
- return { operator: '<', args: expr };
196
- },
197
- },
198
- {
199
- ALT: () => {
200
- CONSUME(l.symbols.greaterThan);
201
- const expr = SUBRULE5(numericExpression, undefined);
202
- return { operator: '>', args: expr };
203
- },
204
- },
205
- {
206
- ALT: () => {
207
- CONSUME(l.symbols.lessThanEqual);
208
- const expr = SUBRULE6(numericExpression, undefined);
209
- return { operator: '<=', args: expr };
210
- },
211
- },
212
- {
213
- ALT: () => {
214
- CONSUME(l.symbols.greaterThanEqual);
215
- const expr = SUBRULE7(numericExpression, undefined);
216
- return { operator: '>=', args: expr };
217
- },
218
- },
219
- {
220
- ALT: () => {
221
- CONSUME(l.in_);
199
+ return ACTION(() => C.factory.expressionOperation(operator.image, [args1, expr], C.factory.sourceLocation(args1, expr)));
200
+ } },
201
+ { ALT: () => {
202
+ const operator = OR3([
203
+ { ALT: () => CONSUME(l.in_) },
204
+ { ALT: () => CONSUME(l.notIn) },
205
+ ]);
222
206
  const args = SUBRULE1(expressionList, undefined);
223
- return { operator: 'in', args };
224
- },
225
- },
226
- {
227
- ALT: () => {
228
- CONSUME(l.notIn);
229
- const args = SUBRULE2(expressionList, undefined);
230
- return { operator: 'notin', args };
231
- },
232
- },
207
+ return ACTION(() => C.factory.expressionOperation(operator.image, [args1, ...args.val], C.factory.sourceLocation(args1, args)));
208
+ } },
233
209
  ]));
234
- if (!arg2) {
235
- return args1;
236
- }
237
- return ACTION(() => ({
238
- type: 'operation',
239
- operator: arg2.operator,
240
- args: [args1, arg2.args],
241
- }));
210
+ return expression ?? args1;
242
211
  },
243
212
  };
244
213
  /**
@@ -253,31 +222,20 @@ export const numericExpression = {
253
222
  */
254
223
  export const additiveExpression = {
255
224
  name: 'additiveExpression',
256
- impl: ({ ACTION, SUBRULE, CONSUME, SUBRULE1, SUBRULE2, SUBRULE3, MANY1, MANY2, OR1, OR2, OR3 }) => () => constructLeftDeep(() => SUBRULE1(multiplicativeExpression, undefined), () => OR1([
257
- {
258
- ALT: () => {
259
- CONSUME(l.symbols.opPlus);
260
- return {
261
- operator: '+',
262
- expr: SUBRULE2(multiplicativeExpression, undefined),
263
- };
264
- },
265
- },
266
- {
267
- ALT: () => {
268
- CONSUME(l.symbols.opMinus);
269
- return {
270
- operator: '-',
271
- expr: SUBRULE3(multiplicativeExpression, undefined),
272
- };
273
- },
274
- },
275
- {
276
- ALT: () => {
225
+ impl: ({ ACTION, SUBRULE, CONSUME, SUBRULE1, SUBRULE2, MANY1, MANY2, OR1, OR2, OR3, OR4 }) => C => constructLeftDeep(() => SUBRULE1(multiplicativeExpression, undefined), () => OR1([
226
+ { ALT: () => {
227
+ // Multiplicative expression as 2nd argument
228
+ const operator = OR2([
229
+ { ALT: () => CONSUME(l.symbols.opPlus) },
230
+ { ALT: () => CONSUME(l.symbols.opMinus) },
231
+ ]);
232
+ const arg = SUBRULE2(multiplicativeExpression, undefined);
233
+ return ACTION(() => left => C.factory.expressionOperation(operator.image, [left, arg], C.factory.sourceLocation(left, arg)));
234
+ } },
235
+ { ALT: () => {
277
236
  // The operator of this alternative is actually parsed as part of the signed numeric literal. (note #6)
278
- const { operator, args } = OR2([
279
- {
280
- ALT: () => {
237
+ const { operator, startInt } = OR3([
238
+ { ALT: () => {
281
239
  // Note #6. No spaces are allowed between the sign and a number.
282
240
  // In this rule however, we do not want to care about this.
283
241
  const integer = SUBRULE(numericLiteralPositive, undefined);
@@ -285,52 +243,31 @@ export const additiveExpression = {
285
243
  integer.value = integer.value.replace(/^\+/u, '');
286
244
  return {
287
245
  operator: '+',
288
- args: [integer],
246
+ startInt: integer,
289
247
  };
290
248
  });
291
- },
292
- },
293
- {
294
- ALT: () => {
249
+ } },
250
+ { ALT: () => {
295
251
  const integer = SUBRULE(numericLiteralNegative, undefined);
296
252
  return ACTION(() => {
297
253
  integer.value = integer.value.replace(/^-/u, '');
298
254
  return {
299
255
  operator: '-',
300
- args: [integer],
256
+ startInt: integer,
301
257
  };
302
258
  });
303
- },
304
- },
259
+ } },
305
260
  ]);
306
- const expr = constructLeftDeep(() => ACTION(() => args[0]), () => OR3([
307
- {
308
- ALT: () => {
309
- CONSUME(l.symbols.star);
310
- const expr = SUBRULE1(unaryExpression, undefined);
311
- return {
312
- operator: '*',
313
- expr,
314
- };
315
- },
316
- },
317
- {
318
- ALT: () => {
319
- CONSUME(l.symbols.slash);
320
- const expr = SUBRULE2(unaryExpression, undefined);
321
- return {
322
- operator: '/',
323
- expr,
324
- };
325
- },
326
- },
327
- ]), ACTION, MANY2);
328
- return {
329
- operator,
330
- expr,
331
- };
332
- },
333
- },
261
+ const multiplicativeExpr = constructLeftDeep(() => ACTION(() => startInt), () => {
262
+ const innerOperator = OR4([
263
+ { ALT: () => CONSUME(l.symbols.star) },
264
+ { ALT: () => CONSUME(l.symbols.slash) },
265
+ ]);
266
+ const innerExpr = SUBRULE1(unaryExpression, undefined);
267
+ return ACTION(() => leftInner => C.factory.expressionOperation(innerOperator.image, [leftInner, innerExpr], C.factory.sourceLocation(leftInner, innerExpr)));
268
+ }, ACTION, MANY2);
269
+ return left => C.factory.expressionOperation(operator, [left, multiplicativeExpr], C.factory.sourceLocation(left, multiplicativeExpr));
270
+ } },
334
271
  ]), ACTION, MANY1),
335
272
  };
336
273
  /**
@@ -338,69 +275,43 @@ export const additiveExpression = {
338
275
  */
339
276
  export const multiplicativeExpression = {
340
277
  name: 'multiplicativeExpression',
341
- impl: ({ ACTION, CONSUME, MANY, SUBRULE1, SUBRULE2, SUBRULE3, OR }) => () => constructLeftDeep(() => SUBRULE1(unaryExpression, undefined), () => OR([
342
- {
343
- ALT: () => {
344
- CONSUME(l.symbols.star);
345
- const expr = SUBRULE2(unaryExpression, undefined);
346
- return {
347
- operator: '*',
348
- expr,
349
- };
350
- },
351
- },
352
- {
353
- ALT: () => {
354
- CONSUME(l.symbols.slash);
355
- const expr = SUBRULE3(unaryExpression, undefined);
356
- return {
357
- operator: '/',
358
- expr,
359
- };
360
- },
361
- },
362
- ]), ACTION, MANY),
278
+ impl: ({ ACTION, CONSUME, MANY, SUBRULE1, SUBRULE2, OR }) => C => constructLeftDeep(() => SUBRULE1(unaryExpression, undefined), () => {
279
+ const operator = OR([
280
+ { ALT: () => CONSUME(l.symbols.star) },
281
+ { ALT: () => CONSUME(l.symbols.slash) },
282
+ ]);
283
+ const expr = SUBRULE2(unaryExpression, undefined);
284
+ return (left) => ({
285
+ type: 'expression',
286
+ subType: 'operation',
287
+ operator: operator.image,
288
+ args: [left, expr],
289
+ loc: C.factory.sourceLocation(left, expr),
290
+ });
291
+ }, ACTION, MANY),
363
292
  };
364
293
  /**
365
294
  * [[118]](https://www.w3.org/TR/sparql11-query/#rUnaryExpression)
366
295
  */
367
296
  export const unaryExpression = {
368
297
  name: 'unaryExpression',
369
- impl: ({ CONSUME, SUBRULE1, SUBRULE2, SUBRULE3, SUBRULE4, OR }) => () => OR([
370
- {
371
- ALT: () => {
372
- CONSUME(l.symbols.exclamation);
373
- const expr = SUBRULE1(primaryExpression, undefined);
374
- return {
375
- type: 'operation',
376
- operator: '!',
377
- args: [expr],
378
- };
379
- },
380
- },
381
- {
382
- ALT: () => {
383
- CONSUME(l.symbols.opPlus);
298
+ impl: ({ ACTION, CONSUME, SUBRULE1, SUBRULE2, OR1, OR2 }) => C => OR1([
299
+ { ALT: () => SUBRULE1(primaryExpression, undefined) },
300
+ { ALT: () => {
301
+ const operator = OR2([
302
+ { ALT: () => CONSUME(l.symbols.exclamation) },
303
+ { ALT: () => CONSUME(l.symbols.opPlus) },
304
+ { ALT: () => CONSUME(l.symbols.opMinus) },
305
+ ]);
384
306
  const expr = SUBRULE2(primaryExpression, undefined);
385
- return {
386
- type: 'operation',
387
- operator: 'UPLUS',
388
- args: [expr],
389
- };
390
- },
391
- },
392
- {
393
- ALT: () => {
394
- CONSUME(l.symbols.opMinus);
395
- const expr = SUBRULE3(primaryExpression, undefined);
396
- return {
397
- type: 'operation',
398
- operator: 'UMINUS',
307
+ return ACTION(() => ({
308
+ type: 'expression',
309
+ subType: 'operation',
310
+ operator: operator.image === '!' ? '!' : (operator.image === '+' ? 'UPLUS' : 'UMINUS'),
399
311
  args: [expr],
400
- };
401
- },
402
- },
403
- { ALT: () => SUBRULE4(primaryExpression, undefined) },
312
+ loc: C.factory.sourceLocation(operator, expr),
313
+ }));
314
+ } },
404
315
  ]),
405
316
  };
406
317
  /**
@@ -423,11 +334,14 @@ export const primaryExpression = {
423
334
  */
424
335
  export const brackettedExpression = {
425
336
  name: 'brackettedExpression',
426
- impl: ({ SUBRULE, CONSUME }) => () => {
427
- CONSUME(l.symbols.LParen);
337
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
338
+ const open = CONSUME(l.symbols.LParen);
428
339
  const expr = SUBRULE(expression, undefined);
429
- CONSUME(l.symbols.RParen);
430
- return expr;
340
+ const close = CONSUME(l.symbols.RParen);
341
+ return ACTION(() => {
342
+ expr.loc = C.factory.sourceLocation(open, close);
343
+ return expr;
344
+ });
431
345
  },
432
346
  };
433
347
  /**
@@ -435,21 +349,35 @@ export const brackettedExpression = {
435
349
  */
436
350
  export const iriOrFunction = {
437
351
  name: 'iriOrFunction',
438
- impl: ({ SUBRULE, OPTION }) => () => {
352
+ impl: ({ ACTION, SUBRULE, OPTION }) => (C) => {
439
353
  const iriVal = SUBRULE(iri, undefined);
440
- const args = OPTION(() => SUBRULE(argList, undefined));
441
- return args ?
442
- {
443
- ...args,
444
- function: iriVal,
445
- } :
446
- iriVal;
354
+ const functionCall = OPTION(() => {
355
+ const args = SUBRULE(argList, undefined);
356
+ return ACTION(() => {
357
+ const distinct = args.val.distinct;
358
+ if (!C.parseMode.has('canParseAggregate') && distinct) {
359
+ throw new Error(`DISTINCT implies that this function is an aggregated function, which is not allowed in this context.`);
360
+ }
361
+ return {
362
+ type: 'expression',
363
+ subType: 'functionCall',
364
+ function: iriVal,
365
+ args: args.val.args,
366
+ distinct,
367
+ loc: C.factory.sourceLocation(iriVal, args),
368
+ };
369
+ });
370
+ });
371
+ return functionCall ?? iriVal;
447
372
  },
448
- gImpl: ({ SUBRULE }) => (ast) => {
449
- if ('function' in ast) {
450
- return `${SUBRULE(iri, ast.function, undefined)}${SUBRULE(argList, ast, undefined)}`;
373
+ gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
374
+ if (F.isTermNamed(ast)) {
375
+ SUBRULE(iri, ast, undefined);
376
+ }
377
+ else {
378
+ SUBRULE(iri, ast.function, undefined);
379
+ SUBRULE(argList, F.wrap({ args: ast.args, distinct: ast.distinct }, ast.loc), undefined);
451
380
  }
452
- return SUBRULE(iri, ast, undefined);
453
381
  },
454
382
  };
455
383
  //# sourceMappingURL=expression.js.map