@traqula/rules-sparql-1-2 0.0.1-alpha.10

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.
package/lib/grammar.js ADDED
@@ -0,0 +1,611 @@
1
+ /**
2
+ * This module will define patch rules that should be used in combination with the sparql11 grammar to create
3
+ * a sparql12 grammar.
4
+ * Rules in this module redefine the return type of core grammar rules.
5
+ * It is therefore essential that the parser retypes the rules from the core grammar.
6
+ */
7
+ import { funcExpr1, funcExpr3, gram as S11, lex as l11, CommonIRIs, } from '@traqula/rules-sparql-1-1';
8
+ import * as l12 from './lexer';
9
+ import { langTagHasCorrectDomain } from './validator';
10
+ /**
11
+ *[[7]](https://www.w3.org/TR/sparql12-query/#rVersionDecl)
12
+ */
13
+ export const versionDecl = {
14
+ name: 'versionDecl',
15
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => {
16
+ const versionToken = CONSUME(l12.version);
17
+ const identifier = SUBRULE(versionSpecifier, undefined);
18
+ return ACTION(() => C.factory.contextDefinitionVersion(identifier.val, C.factory.sourceLocation(versionToken, identifier)));
19
+ },
20
+ gImpl: ({ PRINT_WORDS }) => (ast, { factory: F }) => {
21
+ F.printFilter(ast, () => {
22
+ PRINT_WORDS('VERSION', `${S11.stringEscapedLexical(ast.version)}`);
23
+ });
24
+ },
25
+ };
26
+ /**
27
+ * [[8]](https://www.w3.org/TR/sparql12-query/#rVersionSpecifier)
28
+ */
29
+ export const versionSpecifier = {
30
+ name: 'versionSpecifier',
31
+ impl: ({ ACTION, CONSUME, OR }) => (C) => {
32
+ const token = OR([
33
+ { ALT: () => CONSUME(l11.terminals.stringLiteral1) },
34
+ { ALT: () => CONSUME(l11.terminals.stringLiteral2) },
35
+ ]);
36
+ return ACTION(() => C.factory.wrap(token.image.slice(1, -1), C.factory.sourceLocation(token)));
37
+ },
38
+ };
39
+ /**
40
+ * OVERRIDING RULE {@link S11.prologue}
41
+ * [[8]](https://www.w3.org/TR/sparql12-query/#rVersionSpecifier)
42
+ */
43
+ export const prologue = {
44
+ name: 'prologue',
45
+ impl: ({ SUBRULE, MANY, OR }) => () => {
46
+ const result = [];
47
+ MANY(() => OR([
48
+ { ALT: () => result.push(SUBRULE(S11.baseDecl, undefined)) },
49
+ // TODO: the [spec](https://www.w3.org/TR/sparql11-query/#iriRefs) says you cannot redefine prefixes.
50
+ // We might need to check this.
51
+ { ALT: () => result.push(SUBRULE(S11.prefixDecl, undefined)) },
52
+ { ALT: () => result.push(SUBRULE(versionDecl, undefined)) },
53
+ ]));
54
+ return result;
55
+ },
56
+ gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
57
+ for (const context of ast) {
58
+ if (F.isContextDefinitionBase(context)) {
59
+ SUBRULE(S11.baseDecl, context, undefined);
60
+ }
61
+ else if (F.isContextDefinitionPrefix(context)) {
62
+ SUBRULE(S11.prefixDecl, context, undefined);
63
+ }
64
+ else if (F.isContextDefinitionVersion(context)) {
65
+ SUBRULE(versionDecl, context, undefined);
66
+ }
67
+ }
68
+ },
69
+ };
70
+ function reifiedTripleBlockImpl(name, allowPath) {
71
+ return {
72
+ name,
73
+ impl: ({ ACTION, SUBRULE }) => (C) => {
74
+ const triple = SUBRULE(reifiedTriple, undefined);
75
+ const properties = SUBRULE(allowPath ? S11.propertyListPath : S11.propertyList, { subject: ACTION(() => C.factory.dematerialized(triple.identifier)) });
76
+ return ACTION(() => [triple, ...properties]);
77
+ },
78
+ };
79
+ }
80
+ /**
81
+ * [[58]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlock) Used by triplesSameSubject
82
+ */
83
+ export const reifiedTripleBlock = reifiedTripleBlockImpl('reifiedTripleBlock', false);
84
+ /**
85
+ * [[59]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlockPath) Used by TriplesSameSubjectPath
86
+ */
87
+ export const reifiedTripleBlockPath = reifiedTripleBlockImpl('reifiedTripleBlockPath', true);
88
+ /**
89
+ * OVERRIDING RULE: {@link S11.dataBlockValue}.
90
+ * [[69]](https://www.w3.org/TR/sparql12-query/#rDataBlockValue)
91
+ */
92
+ export const dataBlockValue = {
93
+ name: 'dataBlockValue',
94
+ impl: $ => C => $.OR2([
95
+ { ALT: () => S11.dataBlockValue.impl($)(C, undefined) },
96
+ { ALT: () => $.SUBRULE(tripleTermData, undefined) },
97
+ ]),
98
+ };
99
+ /**
100
+ * [[70]](https://www.w3.org/TR/sparql12-query/#rReifier)
101
+ */
102
+ export const reifier = {
103
+ name: 'reifier',
104
+ impl: ({ ACTION, CONSUME, SUBRULE, OPTION }) => (C) => {
105
+ CONSUME(l12.tilde);
106
+ const reifier = OPTION(() => SUBRULE(varOrReifierId, undefined));
107
+ return ACTION(() => {
108
+ if (reifier === undefined && !C.parseMode.has('canCreateBlankNodes')) {
109
+ throw new Error('Cannot create blanknodes in current parse mode');
110
+ }
111
+ return reifier ?? C.factory.blankNode(undefined, C.factory.sourceLocation());
112
+ });
113
+ },
114
+ };
115
+ /**
116
+ * [[71]](https://www.w3.org/TR/sparql12-query/#rVarOrReifierId)
117
+ */
118
+ export const varOrReifierId = {
119
+ name: 'varOrReifierId',
120
+ impl: ({ SUBRULE, OR }) => C => OR([
121
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
122
+ { ALT: () => SUBRULE(S11.iri, undefined) },
123
+ { ALT: () => SUBRULE(S11.blankNode, undefined) },
124
+ ]),
125
+ };
126
+ function triplesSameSubjectImpl(name, allowPaths) {
127
+ return {
128
+ name,
129
+ impl: $ => C => $.OR2([
130
+ { ALT: () => allowPaths ?
131
+ S11.triplesSameSubjectPath.impl($)(C, undefined) :
132
+ S11.triplesSameSubject.impl($)(C, undefined) },
133
+ { ALT: () => $.SUBRULE(allowPaths ? reifiedTripleBlockPath : reifiedTripleBlock, undefined) },
134
+ ]),
135
+ };
136
+ }
137
+ /**
138
+ * OVERRIDING RULE {@link S11.triplesSameSubject}
139
+ * [[81]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubject)
140
+ */
141
+ export const triplesSameSubject = triplesSameSubjectImpl('triplesSameSubject', false);
142
+ /**
143
+ * OVERRIDING RULE {@link S11.triplesSameSubjectPath}
144
+ * [[87]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath)
145
+ */
146
+ export const triplesSameSubjectPath = triplesSameSubjectImpl('triplesSameSubjectPath', true);
147
+ function objectImpl(name, allowPaths) {
148
+ return {
149
+ name,
150
+ impl: ({ ACTION, SUBRULE }) => (C, arg) => {
151
+ const objectVal = SUBRULE(allowPaths ? graphNodePath : graphNode, undefined);
152
+ const annotationVal = SUBRULE(allowPaths ? annotationPath : annotation, undefined);
153
+ return ACTION(() => {
154
+ const { subject, predicate } = arg;
155
+ const F = C.factory;
156
+ if (F.isPathPure(predicate) && annotationVal.length > 0) {
157
+ throw new Error('Note 17 violation');
158
+ }
159
+ return F.annotatedTriple(subject, predicate, objectVal, annotationVal);
160
+ });
161
+ },
162
+ };
163
+ }
164
+ /**
165
+ * OVERRIDING RULE: {@link S11.object}.
166
+ * [[84]](https://www.w3.org/TR/sparql12-query/#rObject) Used by ObjectList
167
+ */
168
+ export const object = objectImpl('object', false);
169
+ /**
170
+ * OVERRIDING RULE: {@link S11.objectPath}.
171
+ * [[91]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath) Used by ObjectListPath
172
+ */
173
+ export const objectPath = objectImpl('objectPath', true);
174
+ function annotationImpl(name, allowPaths) {
175
+ return {
176
+ name,
177
+ impl: ({ ACTION, SUBRULE, OR, MANY }) => (C) => {
178
+ const annotations = [];
179
+ let currentReifier;
180
+ MANY(() => {
181
+ OR([
182
+ { ALT: () => {
183
+ const node = SUBRULE(reifier, undefined);
184
+ annotations.push(node);
185
+ currentReifier = node;
186
+ } },
187
+ { ALT: () => {
188
+ ACTION(() => {
189
+ if (!currentReifier && !C.parseMode.has('canCreateBlankNodes')) {
190
+ throw new Error('Cannot create blanknodes in current parse mode');
191
+ }
192
+ currentReifier = currentReifier ?? C.factory.blankNode(undefined, C.factory.sourceLocation());
193
+ });
194
+ const block = SUBRULE(allowPaths ? annotationBlockPath : annotationBlock, currentReifier);
195
+ ACTION(() => {
196
+ annotations.push(block);
197
+ currentReifier = undefined;
198
+ });
199
+ } },
200
+ ]);
201
+ });
202
+ return annotations;
203
+ },
204
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
205
+ for (const annotation of ast) {
206
+ if (F.isTerm(annotation)) {
207
+ F.printFilter(annotation, () => PRINT_WORD('~'));
208
+ SUBRULE(graphNodePath, annotation, undefined);
209
+ }
210
+ else {
211
+ SUBRULE(annotationBlockPath, annotation, undefined);
212
+ }
213
+ }
214
+ },
215
+ };
216
+ }
217
+ /**
218
+ * [[107]](https://www.w3.org/TR/sparql12-query/#rAnnotationPath)
219
+ */
220
+ export const annotationPath = annotationImpl('annotationPath', true);
221
+ /**
222
+ * [[111]](https://www.w3.org/TR/sparql12-query/#rAnnotation)
223
+ */
224
+ export const annotation = annotationImpl('annotation', false);
225
+ function annotationBlockImpl(name, allowPaths) {
226
+ return {
227
+ name,
228
+ impl: ({ ACTION, SUBRULE, CONSUME }) => (C, arg) => {
229
+ const open = CONSUME(l12.annotationOpen);
230
+ const res = SUBRULE(allowPaths ? S11.propertyListPathNotEmpty : S11.propertyListNotEmpty, { subject: arg });
231
+ const close = CONSUME(l12.annotationClose);
232
+ return ACTION(() => C.factory.tripleCollectionBlankNodeProperties(arg, res, C.factory.sourceLocation(open, close)));
233
+ },
234
+ gImpl: ({ SUBRULE, PRINT_WORD, HANDLE_LOC }) => (ast, { factory: F }) => {
235
+ F.printFilter(ast, () => PRINT_WORD('{|'));
236
+ for (const triple of ast.triples) {
237
+ HANDLE_LOC(triple, () => {
238
+ if (F.isTerm(triple.predicate)) {
239
+ SUBRULE(graphNodePath, triple.predicate, undefined);
240
+ }
241
+ else {
242
+ SUBRULE(S11.pathGenerator, triple.predicate, undefined);
243
+ }
244
+ SUBRULE(graphNodePath, triple.object, undefined);
245
+ F.printFilter(ast, () => PRINT_WORD(';'));
246
+ });
247
+ }
248
+ F.printFilter(ast, () => PRINT_WORD('|}'));
249
+ },
250
+ };
251
+ }
252
+ /**
253
+ * [[110]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlockPath)
254
+ */
255
+ export const annotationBlockPath = annotationBlockImpl('annotationBlockPath', true);
256
+ /**
257
+ * [[112]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlock)
258
+ */
259
+ export const annotationBlock = annotationBlockImpl('annotationBlock', false);
260
+ /**
261
+ * OVERRIDING RULE: {@link S11.graphNode}.
262
+ * [[111]](https://www.w3.org/TR/sparql12-query/#rGraphNode)
263
+ */
264
+ export const graphNode = {
265
+ name: 'graphNode',
266
+ impl: $ => C => $.OR2([
267
+ { ALT: () => S11.graphNode.impl($)(C, undefined) },
268
+ { ALT: () => $.SUBRULE(reifiedTriple, undefined) },
269
+ ]),
270
+ };
271
+ /**
272
+ * OVERRIDING RULE: {@link S11.graphNodePath}.
273
+ * [[114]](https://www.w3.org/TR/sparql12-query/#rGraphNodePath)
274
+ */
275
+ export const graphNodePath = {
276
+ name: 'graphNodePath',
277
+ impl: $ => C => $.OR2([
278
+ { ALT: () => S11.graphNodePath.impl($)(C, undefined) },
279
+ { ALT: () => $.SUBRULE(reifiedTriple, undefined) },
280
+ ]),
281
+ gImpl: $ => (ast, C, params) => {
282
+ if (C.factory.isTripleCollectionReifiedTriple(ast)) {
283
+ $.SUBRULE(reifiedTriple, ast, undefined);
284
+ }
285
+ else {
286
+ S11.graphNodePath.gImpl($)(ast, C, params);
287
+ }
288
+ },
289
+ };
290
+ /**
291
+ * OVERRIDING RULE: {@link S11.varOrTerm}.
292
+ * [[115]](https://www.w3.org/TR/sparql12-query/#rVarOrTerm)
293
+ */
294
+ export const varOrTerm = {
295
+ name: 'varOrTerm',
296
+ impl: ({ ACTION, SUBRULE, OR, CONSUME }) => C => OR([
297
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
298
+ { ALT: () => SUBRULE(S11.iri, undefined) },
299
+ { ALT: () => SUBRULE(rdfLiteral, undefined) },
300
+ { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
301
+ { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
302
+ { ALT: () => SUBRULE(S11.blankNode, undefined) },
303
+ { ALT: () => {
304
+ const token = CONSUME(l11.terminals.nil);
305
+ return ACTION(() => C.factory.namedNode(C.factory.sourceLocation(token), CommonIRIs.NIL));
306
+ } },
307
+ { ALT: () => SUBRULE(tripleTerm, undefined) },
308
+ ]),
309
+ // Generation remains untouched - go through graphTerm
310
+ };
311
+ /**
312
+ * [[114]](https://www.w3.org/TR/sparql12-query/#rReifiedTriple)
313
+ */
314
+ export const reifiedTriple = {
315
+ name: 'reifiedTriple',
316
+ impl: ({ ACTION, CONSUME, SUBRULE, OPTION }) => (C) => {
317
+ const open = CONSUME(l12.reificationOpen);
318
+ const subject = SUBRULE(reifiedTripleSubject, undefined);
319
+ const predicate = SUBRULE(S11.verb, undefined);
320
+ const object = SUBRULE(reifiedTripleObject, undefined);
321
+ const reifierVal = OPTION(() => SUBRULE(reifier, undefined));
322
+ const close = CONSUME(l12.reificationClose);
323
+ return ACTION(() => {
324
+ // A reifier would be auto generated in this case, but we are not allowed to use them.
325
+ if (reifierVal === undefined && !C.parseMode.has('canCreateBlankNodes')) {
326
+ throw new Error('Cannot create blanknodes in current parse mode');
327
+ }
328
+ return C.factory.tripleCollectionReifiedTriple(C.factory.sourceLocation(open, close), subject, predicate, object, reifierVal);
329
+ });
330
+ },
331
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
332
+ F.printFilter(ast, () => PRINT_WORD('<<'));
333
+ const triple = ast.triples[0];
334
+ SUBRULE(graphNodePath, triple.subject, undefined);
335
+ if (F.isPathPure(triple.predicate)) {
336
+ SUBRULE(S11.pathGenerator, triple.predicate, undefined);
337
+ }
338
+ else {
339
+ SUBRULE(graphNodePath, triple.predicate, undefined);
340
+ }
341
+ SUBRULE(graphNodePath, triple.object, undefined);
342
+ SUBRULE(annotationPath, [ast.identifier], undefined);
343
+ F.printFilter(ast, () => PRINT_WORD('>>'));
344
+ },
345
+ };
346
+ /**
347
+ * [[115]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleSubject)
348
+ */
349
+ export const reifiedTripleSubject = {
350
+ name: 'reifiedTripleSubject',
351
+ impl: ({ OR, SUBRULE }) => C => OR([
352
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
353
+ { ALT: () => SUBRULE(S11.iri, undefined) },
354
+ { ALT: () => SUBRULE(rdfLiteral, undefined) },
355
+ { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
356
+ { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
357
+ { ALT: () => SUBRULE(S11.blankNode, undefined) },
358
+ { ALT: () => SUBRULE(reifiedTriple, undefined) },
359
+ { ALT: () => SUBRULE(tripleTerm, undefined) },
360
+ ]),
361
+ };
362
+ /**
363
+ * [[116]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleObject)
364
+ */
365
+ export const reifiedTripleObject = {
366
+ name: 'reifiedTripleObject',
367
+ impl: reifiedTripleSubject.impl,
368
+ };
369
+ /**
370
+ * [[117]](https://www.w3.org/TR/sparql12-query/#rTripleTerm)
371
+ */
372
+ export const tripleTerm = {
373
+ name: 'tripleTerm',
374
+ impl: ({ ACTION, CONSUME, SUBRULE }) => (C) => {
375
+ const open = CONSUME(l12.tripleTermOpen);
376
+ const subject = SUBRULE(tripleTermSubject, undefined);
377
+ const predicate = SUBRULE(S11.verb, undefined);
378
+ const object = SUBRULE(tripleTermObject, undefined);
379
+ const close = CONSUME(l12.tripleTermClose);
380
+ return ACTION(() => C.factory.termTriple(subject, predicate, object, C.factory.sourceLocation(open, close)));
381
+ },
382
+ gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
383
+ F.printFilter(ast, () => PRINT_WORD('<<('));
384
+ SUBRULE(graphNodePath, ast.subject, undefined);
385
+ SUBRULE(graphNodePath, ast.predicate, undefined);
386
+ SUBRULE(graphNodePath, ast.object, undefined);
387
+ F.printFilter(ast, () => PRINT_WORD(')>>'));
388
+ },
389
+ };
390
+ /**
391
+ * [[120]](https://www.w3.org/TR/sparql12-query/#rTripleTermSubject)
392
+ */
393
+ export const tripleTermSubject = {
394
+ name: 'tripleTermSubject',
395
+ impl: ({ SUBRULE, OR }) => C => OR([
396
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
397
+ { ALT: () => SUBRULE(S11.iri, undefined) },
398
+ { ALT: () => SUBRULE(rdfLiteral, undefined) },
399
+ { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
400
+ { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
401
+ { ALT: () => SUBRULE(S11.blankNode, undefined) },
402
+ { ALT: () => SUBRULE(tripleTerm, undefined) },
403
+ ]),
404
+ };
405
+ /**
406
+ * [[121]](https://www.w3.org/TR/sparql12-query/#rTripleTermObject)
407
+ */
408
+ export const tripleTermObject = {
409
+ name: 'tripleTermObject',
410
+ impl: tripleTermSubject.impl,
411
+ };
412
+ /**
413
+ * [[122]](https://www.w3.org/TR/sparql12-query/#rTripleTermData)
414
+ */
415
+ export const tripleTermData = {
416
+ name: 'tripleTermData',
417
+ impl: ({ ACTION, CONSUME, OR, SUBRULE }) => (C) => {
418
+ const open = CONSUME(l12.tripleTermOpen);
419
+ const subject = SUBRULE(tripleTermDataSubject, undefined);
420
+ const predicate = OR([
421
+ { ALT: () => SUBRULE(S11.iri, undefined) },
422
+ { ALT: () => {
423
+ const token = CONSUME(l11.a);
424
+ return ACTION(() => C.factory.namedNode(C.factory.sourceLocation(token), CommonIRIs.TYPE));
425
+ } },
426
+ ]);
427
+ const object = SUBRULE(tripleTermDataObject, undefined);
428
+ const close = CONSUME(l12.tripleTermClose);
429
+ return ACTION(() => C.factory.termTriple(subject, predicate, object, C.factory.sourceLocation(open, close)));
430
+ },
431
+ };
432
+ /**
433
+ * [[123]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataSubject)
434
+ */
435
+ export const tripleTermDataSubject = {
436
+ name: 'tripleTermDataSubject',
437
+ impl: ({ OR, SUBRULE }) => () => OR([
438
+ { ALT: () => SUBRULE(S11.iri, undefined) },
439
+ { ALT: () => SUBRULE(rdfLiteral, undefined) },
440
+ { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
441
+ { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
442
+ { ALT: () => SUBRULE(tripleTermData, undefined) },
443
+ ]),
444
+ };
445
+ /**
446
+ * [[124]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataObject)
447
+ */
448
+ export const tripleTermDataObject = {
449
+ name: 'tripleTermDataObject',
450
+ impl: tripleTermDataSubject.impl,
451
+ };
452
+ /**
453
+ * OVERRIDING RULE: {@link S11.primaryExpression}.
454
+ * [[136]](https://www.w3.org/TR/sparql12-query/#rPrimaryExpression)
455
+ */
456
+ export const primaryExpression = {
457
+ name: 'primaryExpression',
458
+ impl: $ => C => $.OR2([
459
+ { ALT: () => S11.primaryExpression.impl($)(C, undefined) },
460
+ { ALT: () => $.SUBRULE(exprTripleTerm, undefined) },
461
+ ]),
462
+ };
463
+ /**
464
+ * [[135]](https://www.w3.org/TR/sparql12-query/#rExprTripleTerm)
465
+ */
466
+ export const exprTripleTerm = {
467
+ name: 'exprTripleTerm',
468
+ impl: ({ ACTION, CONSUME, SUBRULE }) => (C) => {
469
+ const open = CONSUME(l12.tripleTermOpen);
470
+ const subject = SUBRULE(exprTripleTermSubject, undefined);
471
+ const predicate = SUBRULE(S11.verb, undefined);
472
+ const object = SUBRULE(exprTripleTermObject, undefined);
473
+ const close = CONSUME(l12.tripleTermClose);
474
+ return ACTION(() => C.factory.termTriple(subject, predicate, object, C.factory.sourceLocation(open, close)));
475
+ },
476
+ };
477
+ /**
478
+ * [[138]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermSubject)
479
+ */
480
+ export const exprTripleTermSubject = {
481
+ name: 'exprTripleTermSubject',
482
+ impl: ({ OR, SUBRULE }) => C => OR([
483
+ { ALT: () => SUBRULE(S11.iri, undefined) },
484
+ { ALT: () => SUBRULE(rdfLiteral, undefined) },
485
+ { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
486
+ { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
487
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
488
+ { ALT: () => SUBRULE(exprTripleTerm, undefined) },
489
+ ]),
490
+ };
491
+ /**
492
+ * [[139]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermObject)
493
+ */
494
+ export const exprTripleTermObject = {
495
+ name: 'exprTripleTermObject',
496
+ impl: exprTripleTermSubject.impl,
497
+ };
498
+ export const builtinLangDir = funcExpr1(l12.builtinLangDir);
499
+ export const builtinLangStrDir = funcExpr3(l12.builtinStrLangDir);
500
+ export const builtinHasLang = funcExpr1(l12.builtinHasLang);
501
+ export const builtinHasLangDir = funcExpr1(l12.builtinHasLangDir);
502
+ export const builtinIsTriple = funcExpr1(l12.builtinIsTRIPLE);
503
+ export const builtinTriple = funcExpr3(l12.builtinTRIPLE);
504
+ export const builtinSubject = funcExpr1(l12.builtinSUBJECT);
505
+ export const builtinPredicate = funcExpr1(l12.builtinPREDICATE);
506
+ export const builtinObject = funcExpr1(l12.builtinOBJECT);
507
+ /**
508
+ * OVERRIDING RULE: {@link S11.builtInCall}.
509
+ * [[141]](https://www.w3.org/TR/sparql12-query/#rBuiltInCall)
510
+ */
511
+ export const builtInCall = {
512
+ name: 'builtInCall',
513
+ impl: $ => C => $.OR2([
514
+ { ALT: () => S11.builtInCall.impl($)(C, undefined) },
515
+ { ALT: () => $.SUBRULE(builtinLangDir, undefined) },
516
+ { ALT: () => $.SUBRULE(builtinLangStrDir, undefined) },
517
+ { ALT: () => $.SUBRULE(builtinHasLang, undefined) },
518
+ { ALT: () => $.SUBRULE(builtinHasLangDir, undefined) },
519
+ { ALT: () => $.SUBRULE(builtinIsTriple, undefined) },
520
+ { ALT: () => $.SUBRULE(builtinTriple, undefined) },
521
+ { ALT: () => $.SUBRULE(builtinSubject, undefined) },
522
+ { ALT: () => $.SUBRULE(builtinPredicate, undefined) },
523
+ { ALT: () => $.SUBRULE(builtinObject, undefined) },
524
+ ]),
525
+ };
526
+ /**
527
+ * OVERRIDING RULE: {@link S11.rdfLiteral}.
528
+ * No retyping is needed since the return type is the same
529
+ * [[149]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
530
+ */
531
+ export const rdfLiteral = {
532
+ name: 'rdfLiteral',
533
+ impl: ({ ACTION, SUBRULE, OPTION, CONSUME, OR }) => (C) => {
534
+ const value = SUBRULE(S11.string, undefined);
535
+ return OPTION(() => OR([
536
+ { ALT: () => {
537
+ const langTag = CONSUME(l12.LANG_DIR);
538
+ return ACTION(() => {
539
+ const literal = C.factory.literalTerm(C.factory.sourceLocation(value, langTag), value.value, langTag.image.slice(1).toLowerCase());
540
+ langTagHasCorrectDomain(literal);
541
+ return literal;
542
+ });
543
+ } },
544
+ { ALT: () => {
545
+ CONSUME(l11.symbols.hathat);
546
+ const iriVal = SUBRULE(S11.iri, undefined);
547
+ return ACTION(() => C.factory.literalTerm(C.factory.sourceLocation(value, iriVal), value.value, iriVal));
548
+ } },
549
+ ])) ?? value;
550
+ },
551
+ };
552
+ /**
553
+ * OVERRIDING RULE: {@link S11.triplesBlock}.
554
+ */
555
+ export const generateTriplesBlock = {
556
+ name: 'triplesBlock',
557
+ gImpl: ({ SUBRULE, PRINT_WORD, HANDLE_LOC }) => (ast, { factory: F }) => {
558
+ for (const [index, triple] of ast.triples.entries()) {
559
+ HANDLE_LOC(triple, () => {
560
+ const nextTriple = ast.triples.at(index);
561
+ if (F.isTripleCollection(triple)) {
562
+ SUBRULE(graphNodePath, triple, undefined);
563
+ // A top level tripleCollection block means that it is not used in a triple. So you end with DOT.
564
+ F.printFilter(triple, () => PRINT_WORD('.'));
565
+ }
566
+ else {
567
+ // Subject
568
+ SUBRULE(graphNodePath, triple.subject, undefined);
569
+ // Predicate
570
+ if (F.isPathPure(triple.predicate)) {
571
+ SUBRULE(S11.pathGenerator, triple.predicate, undefined);
572
+ }
573
+ else {
574
+ SUBRULE(graphNodePath, triple.predicate, undefined);
575
+ }
576
+ // Object
577
+ SUBRULE(graphNodePath, triple.object, undefined);
578
+ SUBRULE(annotationPath, triple.annotations ?? [], undefined);
579
+ // If no more things, or a top level collection (only possible if new block was part), or new subject: add DOT
580
+ if (nextTriple === undefined || F.isTripleCollection(nextTriple) ||
581
+ !F.isSourceLocationNoMaterialize(nextTriple.subject.loc)) {
582
+ F.printFilter(ast, () => PRINT_WORD('.'));
583
+ }
584
+ else if (F.isSourceLocationNoMaterialize(nextTriple.predicate.loc)) {
585
+ F.printFilter(ast, () => PRINT_WORD(','));
586
+ }
587
+ else {
588
+ F.printFilter(ast, () => PRINT_WORD(';'));
589
+ }
590
+ }
591
+ });
592
+ }
593
+ },
594
+ };
595
+ /**
596
+ * OVERRIDING RULE: {@link S11.graphTerm}.
597
+ * No retyping is needed since the return type is the same
598
+ * [[149]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
599
+ */
600
+ export const generateGraphTerm = {
601
+ name: 'graphTerm',
602
+ gImpl: $ => (ast, C, par) => {
603
+ if (C.factory.isTermTriple(ast)) {
604
+ $.SUBRULE(tripleTerm, ast, undefined);
605
+ }
606
+ else {
607
+ S11.graphTerm.gImpl($)(ast, C, par);
608
+ }
609
+ },
610
+ };
611
+ //# sourceMappingURL=grammar.js.map