@traqula/rules-sparql-1-2 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.
package/lib/grammar.js CHANGED
@@ -4,32 +4,90 @@
4
4
  * Rules in this module redefine the return type of core grammar rules.
5
5
  * It is therefore essential that the parser retypes the rules from the core grammar.
6
6
  */
7
- import { funcExpr1, funcExpr3, gram as S11, lex as l11, CommonIRIs } from '@traqula/rules-sparql-1-1';
7
+ import { funcExpr1, funcExpr3, gram as S11, lex as l11, CommonIRIs, } from '@traqula/rules-sparql-1-1';
8
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
+ };
9
70
  function reifiedTripleBlockImpl(name, allowPath) {
10
71
  return {
11
72
  name,
12
- impl: ({ ACTION, SUBRULE }) => () => {
73
+ impl: ({ ACTION, SUBRULE }) => (C) => {
13
74
  const triple = SUBRULE(reifiedTriple, undefined);
14
- const properties = SUBRULE(allowPath ? S11.propertyListPath : S11.propertyList, { subject: triple.node });
15
- return ACTION(() => [
16
- ...triple.triples,
17
- ...properties,
18
- ]);
75
+ const properties = SUBRULE(allowPath ? S11.propertyListPath : S11.propertyList, { subject: ACTION(() => C.factory.dematerialized(triple.identifier)) });
76
+ return ACTION(() => [triple, ...properties]);
19
77
  },
20
78
  };
21
79
  }
22
80
  /**
23
- * [[56]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlock) Used by triplesSameSubject
81
+ * [[58]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlock) Used by triplesSameSubject
24
82
  */
25
83
  export const reifiedTripleBlock = reifiedTripleBlockImpl('reifiedTripleBlock', false);
26
84
  /**
27
- * [[57]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlockPath) Used by TriplesSameSubjectPath
85
+ * [[59]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlockPath) Used by TriplesSameSubjectPath
28
86
  */
29
87
  export const reifiedTripleBlockPath = reifiedTripleBlockImpl('reifiedTripleBlockPath', true);
30
88
  /**
31
89
  * OVERRIDING RULE: {@link S11.dataBlockValue}.
32
- * [[67]](https://www.w3.org/TR/sparql12-query/#rDataBlockValue)
90
+ * [[69]](https://www.w3.org/TR/sparql12-query/#rDataBlockValue)
33
91
  */
34
92
  export const dataBlockValue = {
35
93
  name: 'dataBlockValue',
@@ -37,15 +95,9 @@ export const dataBlockValue = {
37
95
  { ALT: () => S11.dataBlockValue.impl($)(C, undefined) },
38
96
  { ALT: () => $.SUBRULE(tripleTermData, undefined) },
39
97
  ]),
40
- gImpl: ({ SUBRULE }) => (ast) => {
41
- if (ast) {
42
- return SUBRULE(varOrTerm, ast, undefined);
43
- }
44
- return 'UNDEF';
45
- },
46
98
  };
47
99
  /**
48
- * [[68]](https://www.w3.org/TR/sparql12-query/#rReifier)
100
+ * [[70]](https://www.w3.org/TR/sparql12-query/#rReifier)
49
101
  */
50
102
  export const reifier = {
51
103
  name: 'reifier',
@@ -56,17 +108,17 @@ export const reifier = {
56
108
  if (reifier === undefined && !C.parseMode.has('canCreateBlankNodes')) {
57
109
  throw new Error('Cannot create blanknodes in current parse mode');
58
110
  }
59
- return reifier ?? C.dataFactory.blankNode();
111
+ return reifier ?? C.factory.blankNode(undefined, C.factory.sourceLocation());
60
112
  });
61
113
  },
62
114
  };
63
115
  /**
64
- * [[68]](https://www.w3.org/TR/sparql12-query/#rVarOrReifierId)
116
+ * [[71]](https://www.w3.org/TR/sparql12-query/#rVarOrReifierId)
65
117
  */
66
118
  export const varOrReifierId = {
67
119
  name: 'varOrReifierId',
68
- impl: ({ SUBRULE, OR }) => () => OR([
69
- { ALT: () => SUBRULE(S11.var_, undefined) },
120
+ impl: ({ SUBRULE, OR }) => C => OR([
121
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
70
122
  { ALT: () => SUBRULE(S11.iri, undefined) },
71
123
  { ALT: () => SUBRULE(S11.blankNode, undefined) },
72
124
  ]),
@@ -84,12 +136,12 @@ function triplesSameSubjectImpl(name, allowPaths) {
84
136
  }
85
137
  /**
86
138
  * OVERRIDING RULE {@link S11.triplesSameSubject}
87
- * [[79]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubject)
139
+ * [[81]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubject)
88
140
  */
89
141
  export const triplesSameSubject = triplesSameSubjectImpl('triplesSameSubject', false);
90
142
  /**
91
143
  * OVERRIDING RULE {@link S11.triplesSameSubjectPath}
92
- * [[85]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath)
144
+ * [[87]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath)
93
145
  */
94
146
  export const triplesSameSubjectPath = triplesSameSubjectImpl('triplesSameSubjectPath', true);
95
147
  function objectImpl(name, allowPaths) {
@@ -98,24 +150,13 @@ function objectImpl(name, allowPaths) {
98
150
  impl: ({ ACTION, SUBRULE }) => (C, arg) => {
99
151
  const objectVal = SUBRULE(allowPaths ? graphNodePath : graphNode, undefined);
100
152
  const annotationVal = SUBRULE(allowPaths ? annotationPath : annotation, undefined);
101
- // This rule knows the annotation. And for each annotation node, we need to make a triple:
102
- // <annotationNode, reifies, parsedSubjectAndObject>
103
153
  return ACTION(() => {
104
154
  const { subject, predicate } = arg;
105
- if ('type' in predicate && predicate.type === 'path' && annotationVal.length > 0) {
155
+ const F = C.factory;
156
+ if (F.isPathPure(predicate) && annotationVal.length > 0) {
106
157
  throw new Error('Note 17 violation');
107
158
  }
108
- const result = [
109
- // You parse the object
110
- { subject, predicate, object: objectVal.node },
111
- // You might get some additional triples from parsing the object (like when it's a collection)
112
- ...objectVal.triples,
113
- ];
114
- for (const annotation of annotationVal) {
115
- result.push(C.dataFactory.quad(annotation.node, C.dataFactory.namedNode(CommonIRIs.REIFIES), C.dataFactory.quad(subject, predicate, objectVal.node)));
116
- result.push(...annotation.triples);
117
- }
118
- return result;
159
+ return F.annotatedTriple(subject, predicate, objectVal, annotationVal);
119
160
  });
120
161
  },
121
162
  };
@@ -136,42 +177,40 @@ function annotationImpl(name, allowPaths) {
136
177
  impl: ({ ACTION, SUBRULE, OR, MANY }) => (C) => {
137
178
  const annotations = [];
138
179
  let currentReifier;
139
- function flush() {
140
- if (currentReifier) {
141
- annotations.push({ node: currentReifier, triples: [] });
142
- currentReifier = undefined;
143
- }
144
- }
145
180
  MANY(() => {
146
181
  OR([
147
182
  { ALT: () => {
148
183
  const node = SUBRULE(reifier, undefined);
149
- ACTION(() => flush());
184
+ annotations.push(node);
150
185
  currentReifier = node;
151
186
  } },
152
187
  { ALT: () => {
153
- let node;
154
- const block = SUBRULE(allowPaths ? annotationBlockPath : annotationBlock, { subject: ACTION(() => {
155
- if (currentReifier === undefined && !C.parseMode.has('canCreateBlankNodes')) {
156
- throw new Error('Cannot create blanknodes in current parse mode');
157
- }
158
- node = currentReifier ?? C.dataFactory.blankNode();
159
- return node;
160
- }) });
161
188
  ACTION(() => {
162
- annotations.push({
163
- node,
164
- triples: block,
165
- });
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);
166
197
  currentReifier = undefined;
167
198
  });
168
199
  } },
169
200
  ]);
170
201
  });
171
- return ACTION(() => {
172
- flush();
173
- return annotations;
174
- });
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
+ }
175
214
  },
176
215
  };
177
216
  }
@@ -180,26 +219,42 @@ function annotationImpl(name, allowPaths) {
180
219
  */
181
220
  export const annotationPath = annotationImpl('annotationPath', true);
182
221
  /**
183
- * [[109]](https://www.w3.org/TR/sparql12-query/#rAnnotation)
222
+ * [[111]](https://www.w3.org/TR/sparql12-query/#rAnnotation)
184
223
  */
185
224
  export const annotation = annotationImpl('annotation', false);
186
225
  function annotationBlockImpl(name, allowPaths) {
187
226
  return {
188
227
  name,
189
- impl: ({ ACTION, SUBRULE, CONSUME }) => (_, arg) => {
190
- CONSUME(l12.annotationOpen);
191
- const res = SUBRULE(allowPaths ? S11.propertyListPathNotEmpty : S11.propertyListNotEmpty, { subject: ACTION(() => arg.subject) });
192
- CONSUME(l12.annotationClose);
193
- return res;
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('|}'));
194
249
  },
195
250
  };
196
251
  }
197
252
  /**
198
- * [[108]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlockPath)
253
+ * [[110]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlockPath)
199
254
  */
200
255
  export const annotationBlockPath = annotationBlockImpl('annotationBlockPath', true);
201
256
  /**
202
- * [[110]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlock)
257
+ * [[112]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlock)
203
258
  */
204
259
  export const annotationBlock = annotationBlockImpl('annotationBlock', false);
205
260
  /**
@@ -215,7 +270,7 @@ export const graphNode = {
215
270
  };
216
271
  /**
217
272
  * OVERRIDING RULE: {@link S11.graphNodePath}.
218
- * [[112]](https://www.w3.org/TR/sparql12-query/#rGraphNodePath)
273
+ * [[114]](https://www.w3.org/TR/sparql12-query/#rGraphNodePath)
219
274
  */
220
275
  export const graphNodePath = {
221
276
  name: 'graphNodePath',
@@ -223,41 +278,35 @@ export const graphNodePath = {
223
278
  { ALT: () => S11.graphNodePath.impl($)(C, undefined) },
224
279
  { ALT: () => $.SUBRULE(reifiedTriple, undefined) },
225
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
+ },
226
289
  };
227
290
  /**
228
291
  * OVERRIDING RULE: {@link S11.varOrTerm}.
229
- * [[113]](https://www.w3.org/TR/sparql12-query/#rVarOrTerm)
292
+ * [[115]](https://www.w3.org/TR/sparql12-query/#rVarOrTerm)
230
293
  */
231
294
  export const varOrTerm = {
232
295
  name: 'varOrTerm',
233
296
  impl: ({ ACTION, SUBRULE, OR, CONSUME }) => C => OR([
234
- { ALT: () => SUBRULE(S11.var_, undefined) },
297
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
235
298
  { ALT: () => SUBRULE(S11.iri, undefined) },
236
299
  { ALT: () => SUBRULE(rdfLiteral, undefined) },
237
300
  { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
238
301
  { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
239
302
  { ALT: () => SUBRULE(S11.blankNode, undefined) },
240
303
  { ALT: () => {
241
- CONSUME(l11.terminals.nil);
242
- return ACTION(() => C.dataFactory.namedNode(CommonIRIs.NIL));
304
+ const token = CONSUME(l11.terminals.nil);
305
+ return ACTION(() => C.factory.namedNode(C.factory.sourceLocation(token), CommonIRIs.NIL));
243
306
  } },
244
307
  { ALT: () => SUBRULE(tripleTerm, undefined) },
245
308
  ]),
246
- gImpl: ({ SUBRULE }) => (ast) => {
247
- if (ast.termType === 'Variable') {
248
- return SUBRULE(S11.var_, ast, undefined);
249
- }
250
- if (ast.termType === 'NamedNode') {
251
- return SUBRULE(S11.iri, ast, undefined);
252
- }
253
- if (ast.termType === 'BlankNode') {
254
- return SUBRULE(S11.blankNode, ast, undefined);
255
- }
256
- if (ast.termType === 'Quad') {
257
- return SUBRULE(tripleTerm, ast, undefined);
258
- }
259
- return SUBRULE(S11.rdfLiteral, ast, undefined);
260
- },
309
+ // Generation remains untouched - go through graphTerm
261
310
  };
262
311
  /**
263
312
  * [[114]](https://www.w3.org/TR/sparql12-query/#rReifiedTriple)
@@ -265,42 +314,49 @@ export const varOrTerm = {
265
314
  export const reifiedTriple = {
266
315
  name: 'reifiedTriple',
267
316
  impl: ({ ACTION, CONSUME, SUBRULE, OPTION }) => (C) => {
268
- CONSUME(l12.reificationOpen);
317
+ const open = CONSUME(l12.reificationOpen);
269
318
  const subject = SUBRULE(reifiedTripleSubject, undefined);
270
319
  const predicate = SUBRULE(S11.verb, undefined);
271
320
  const object = SUBRULE(reifiedTripleObject, undefined);
272
321
  const reifierVal = OPTION(() => SUBRULE(reifier, undefined));
273
- CONSUME(l12.reificationClose);
322
+ const close = CONSUME(l12.reificationClose);
274
323
  return ACTION(() => {
324
+ // A reifier would be auto generated in this case, but we are not allowed to use them.
275
325
  if (reifierVal === undefined && !C.parseMode.has('canCreateBlankNodes')) {
276
326
  throw new Error('Cannot create blanknodes in current parse mode');
277
327
  }
278
- const reifier = reifierVal ?? C.dataFactory.blankNode();
279
- const tripleTerm = C.dataFactory.quad(subject.node, predicate, object.node);
280
- return {
281
- node: reifier,
282
- triples: [
283
- ...subject.triples,
284
- C.dataFactory.quad(reifier, C.dataFactory.namedNode(CommonIRIs.REIFIES), tripleTerm),
285
- ...object.triples,
286
- ],
287
- };
328
+ return C.factory.tripleCollectionReifiedTriple(C.factory.sourceLocation(open, close), subject, predicate, object, reifierVal);
288
329
  });
289
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
+ },
290
345
  };
291
346
  /**
292
347
  * [[115]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleSubject)
293
348
  */
294
349
  export const reifiedTripleSubject = {
295
350
  name: 'reifiedTripleSubject',
296
- impl: ({ OR, SUBRULE }) => () => OR([
297
- { ALT: () => ({ node: SUBRULE(S11.var_, undefined), triples: [] }) },
298
- { ALT: () => ({ node: SUBRULE(S11.iri, undefined), triples: [] }) },
299
- { ALT: () => ({ node: SUBRULE(rdfLiteral, undefined), triples: [] }) },
300
- { ALT: () => ({ node: SUBRULE(S11.numericLiteral, undefined), triples: [] }) },
301
- { ALT: () => ({ node: SUBRULE(S11.booleanLiteral, undefined), triples: [] }) },
302
- { ALT: () => ({ node: SUBRULE(S11.blankNode, undefined), triples: [] }) },
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) },
303
358
  { ALT: () => SUBRULE(reifiedTriple, undefined) },
359
+ { ALT: () => SUBRULE(tripleTerm, undefined) },
304
360
  ]),
305
361
  };
306
362
  /**
@@ -308,10 +364,7 @@ export const reifiedTripleSubject = {
308
364
  */
309
365
  export const reifiedTripleObject = {
310
366
  name: 'reifiedTripleObject',
311
- impl: $ => C => $.OR2([
312
- { ALT: () => reifiedTripleSubject.impl($)(C, undefined) },
313
- { ALT: () => ({ node: $.SUBRULE(tripleTerm, undefined), triples: [] }) },
314
- ]),
367
+ impl: reifiedTripleSubject.impl,
315
368
  };
316
369
  /**
317
370
  * [[117]](https://www.w3.org/TR/sparql12-query/#rTripleTerm)
@@ -319,61 +372,65 @@ export const reifiedTripleObject = {
319
372
  export const tripleTerm = {
320
373
  name: 'tripleTerm',
321
374
  impl: ({ ACTION, CONSUME, SUBRULE }) => (C) => {
322
- CONSUME(l12.tripleTermOpen);
375
+ const open = CONSUME(l12.tripleTermOpen);
323
376
  const subject = SUBRULE(tripleTermSubject, undefined);
324
377
  const predicate = SUBRULE(S11.verb, undefined);
325
378
  const object = SUBRULE(tripleTermObject, undefined);
326
- CONSUME(l12.tripleTermClose);
327
- return ACTION(() => C.dataFactory.quad(subject, predicate, object));
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(')>>'));
328
388
  },
329
- gImpl: ({ SUBRULE }) => ast => `<<( ${SUBRULE(varOrTerm, ast.subject, undefined)} ${SUBRULE(varOrTerm, ast.predicate, undefined)} ${SUBRULE(varOrTerm, ast.object, undefined)} )>>`,
330
389
  };
331
390
  /**
332
- * [[118]](https://www.w3.org/TR/sparql12-query/#rTripleTermSubject)
391
+ * [[120]](https://www.w3.org/TR/sparql12-query/#rTripleTermSubject)
333
392
  */
334
393
  export const tripleTermSubject = {
335
394
  name: 'tripleTermSubject',
336
- impl: ({ SUBRULE, OR }) => () => OR([
337
- { ALT: () => SUBRULE(S11.var_, undefined) },
395
+ impl: ({ SUBRULE, OR }) => C => OR([
396
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
338
397
  { ALT: () => SUBRULE(S11.iri, undefined) },
339
398
  { ALT: () => SUBRULE(rdfLiteral, undefined) },
340
399
  { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
341
400
  { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
342
401
  { ALT: () => SUBRULE(S11.blankNode, undefined) },
402
+ { ALT: () => SUBRULE(tripleTerm, undefined) },
343
403
  ]),
344
404
  };
345
405
  /**
346
- * [[119]](https://www.w3.org/TR/sparql12-query/#rTripleTermObject)
406
+ * [[121]](https://www.w3.org/TR/sparql12-query/#rTripleTermObject)
347
407
  */
348
408
  export const tripleTermObject = {
349
409
  name: 'tripleTermObject',
350
- impl: $ => C => $.OR2([
351
- { ALT: () => tripleTermSubject.impl($)(C, undefined) },
352
- { ALT: () => $.SUBRULE(tripleTerm, undefined) },
353
- ]),
410
+ impl: tripleTermSubject.impl,
354
411
  };
355
412
  /**
356
- * [[120]](https://www.w3.org/TR/sparql12-query/#rTripleTermData)
413
+ * [[122]](https://www.w3.org/TR/sparql12-query/#rTripleTermData)
357
414
  */
358
415
  export const tripleTermData = {
359
416
  name: 'tripleTermData',
360
417
  impl: ({ ACTION, CONSUME, OR, SUBRULE }) => (C) => {
361
- CONSUME(l12.tripleTermOpen);
418
+ const open = CONSUME(l12.tripleTermOpen);
362
419
  const subject = SUBRULE(tripleTermDataSubject, undefined);
363
420
  const predicate = OR([
364
421
  { ALT: () => SUBRULE(S11.iri, undefined) },
365
422
  { ALT: () => {
366
- CONSUME(l11.a);
367
- return ACTION(() => C.dataFactory.namedNode(CommonIRIs.TYPE));
423
+ const token = CONSUME(l11.a);
424
+ return ACTION(() => C.factory.namedNode(C.factory.sourceLocation(token), CommonIRIs.TYPE));
368
425
  } },
369
426
  ]);
370
427
  const object = SUBRULE(tripleTermDataObject, undefined);
371
- CONSUME(l12.tripleTermClose);
372
- return ACTION(() => C.dataFactory.quad(subject, predicate, object));
428
+ const close = CONSUME(l12.tripleTermClose);
429
+ return ACTION(() => C.factory.termTriple(subject, predicate, object, C.factory.sourceLocation(open, close)));
373
430
  },
374
431
  };
375
432
  /**
376
- * [[121]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataSubject)
433
+ * [[123]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataSubject)
377
434
  */
378
435
  export const tripleTermDataSubject = {
379
436
  name: 'tripleTermDataSubject',
@@ -382,21 +439,19 @@ export const tripleTermDataSubject = {
382
439
  { ALT: () => SUBRULE(rdfLiteral, undefined) },
383
440
  { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
384
441
  { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
442
+ { ALT: () => SUBRULE(tripleTermData, undefined) },
385
443
  ]),
386
444
  };
387
445
  /**
388
- * [[122]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataObject)
446
+ * [[124]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataObject)
389
447
  */
390
448
  export const tripleTermDataObject = {
391
449
  name: 'tripleTermDataObject',
392
- impl: $ => C => $.OR2([
393
- { ALT: () => tripleTermDataSubject.impl($)(C, undefined) },
394
- { ALT: () => $.SUBRULE(tripleTermData, undefined) },
395
- ]),
450
+ impl: tripleTermDataSubject.impl,
396
451
  };
397
452
  /**
398
453
  * OVERRIDING RULE: {@link S11.primaryExpression}.
399
- * [[134]](https://www.w3.org/TR/sparql12-query/#rPrimaryExpression)
454
+ * [[136]](https://www.w3.org/TR/sparql12-query/#rPrimaryExpression)
400
455
  */
401
456
  export const primaryExpression = {
402
457
  name: 'primaryExpression',
@@ -411,36 +466,34 @@ export const primaryExpression = {
411
466
  export const exprTripleTerm = {
412
467
  name: 'exprTripleTerm',
413
468
  impl: ({ ACTION, CONSUME, SUBRULE }) => (C) => {
414
- CONSUME(l12.tripleTermOpen);
469
+ const open = CONSUME(l12.tripleTermOpen);
415
470
  const subject = SUBRULE(exprTripleTermSubject, undefined);
416
471
  const predicate = SUBRULE(S11.verb, undefined);
417
472
  const object = SUBRULE(exprTripleTermObject, undefined);
418
- CONSUME(l12.tripleTermClose);
419
- return ACTION(() => C.dataFactory.quad(subject, predicate, object));
473
+ const close = CONSUME(l12.tripleTermClose);
474
+ return ACTION(() => C.factory.termTriple(subject, predicate, object, C.factory.sourceLocation(open, close)));
420
475
  },
421
476
  };
422
477
  /**
423
- * [[136]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermSubject)
478
+ * [[138]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermSubject)
424
479
  */
425
480
  export const exprTripleTermSubject = {
426
481
  name: 'exprTripleTermSubject',
427
- impl: ({ OR, SUBRULE }) => () => OR([
482
+ impl: ({ OR, SUBRULE }) => C => OR([
428
483
  { ALT: () => SUBRULE(S11.iri, undefined) },
429
484
  { ALT: () => SUBRULE(rdfLiteral, undefined) },
430
485
  { ALT: () => SUBRULE(S11.numericLiteral, undefined) },
431
486
  { ALT: () => SUBRULE(S11.booleanLiteral, undefined) },
432
- { ALT: () => SUBRULE(S11.var_, undefined) },
487
+ { GATE: () => C.parseMode.has('canParseVars'), ALT: () => SUBRULE(S11.var_, undefined) },
488
+ { ALT: () => SUBRULE(exprTripleTerm, undefined) },
433
489
  ]),
434
490
  };
435
491
  /**
436
- * [[137]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermObject)
492
+ * [[139]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermObject)
437
493
  */
438
494
  export const exprTripleTermObject = {
439
495
  name: 'exprTripleTermObject',
440
- impl: $ => C => $.OR2([
441
- { ALT: () => exprTripleTermSubject.impl($)(C, undefined) },
442
- { ALT: () => $.SUBRULE(exprTripleTerm, undefined) },
443
- ]),
496
+ impl: exprTripleTermSubject.impl,
444
497
  };
445
498
  export const builtinLangDir = funcExpr1(l12.builtinLangDir);
446
499
  export const builtinLangStrDir = funcExpr3(l12.builtinStrLangDir);
@@ -453,7 +506,7 @@ export const builtinPredicate = funcExpr1(l12.builtinPREDICATE);
453
506
  export const builtinObject = funcExpr1(l12.builtinOBJECT);
454
507
  /**
455
508
  * OVERRIDING RULE: {@link S11.builtInCall}.
456
- * [[139]](https://www.w3.org/TR/sparql12-query/#rBuiltInCall)
509
+ * [[141]](https://www.w3.org/TR/sparql12-query/#rBuiltInCall)
457
510
  */
458
511
  export const builtInCall = {
459
512
  name: 'builtInCall',
@@ -470,42 +523,89 @@ export const builtInCall = {
470
523
  { ALT: () => $.SUBRULE(builtinObject, undefined) },
471
524
  ]),
472
525
  };
473
- function isLangDir(dir) {
474
- return dir === 'ltr' || dir === 'rtl';
475
- }
476
526
  /**
477
527
  * OVERRIDING RULE: {@link S11.rdfLiteral}.
478
528
  * No retyping is needed since the return type is the same
479
- * [[147]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
529
+ * [[149]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
480
530
  */
481
531
  export const rdfLiteral = {
482
532
  name: 'rdfLiteral',
483
533
  impl: ({ ACTION, SUBRULE, OPTION, CONSUME, OR }) => (C) => {
484
534
  const value = SUBRULE(S11.string, undefined);
485
- const langOrDataType = OPTION(() => OR([
535
+ return OPTION(() => OR([
486
536
  { ALT: () => {
487
- const langTag = CONSUME(l12.LANG_DIR).image.slice(1);
537
+ const langTag = CONSUME(l12.LANG_DIR);
488
538
  return ACTION(() => {
489
- const dirSplit = langTag.split('--');
490
- if (dirSplit.length > 1) {
491
- const [language, direction] = dirSplit;
492
- if (!isLangDir(direction)) {
493
- throw new Error(`language direction "${direction}" of literal "${value}@${langTag}" is not is required range 'ltr' | 'rtl'.`);
494
- }
495
- return {
496
- language,
497
- direction,
498
- };
499
- }
500
- return langTag;
539
+ const literal = C.factory.literalTerm(C.factory.sourceLocation(value, langTag), value.value, langTag.image.slice(1).toLowerCase());
540
+ langTagHasCorrectDomain(literal);
541
+ return literal;
501
542
  });
502
543
  } },
503
544
  { ALT: () => {
504
545
  CONSUME(l11.symbols.hathat);
505
- return SUBRULE(S11.iri, undefined);
546
+ const iriVal = SUBRULE(S11.iri, undefined);
547
+ return ACTION(() => C.factory.literalTerm(C.factory.sourceLocation(value, iriVal), value.value, iriVal));
506
548
  } },
507
- ]));
508
- return ACTION(() => C.dataFactory.literal(value, langOrDataType));
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
+ }
509
609
  },
510
610
  };
511
611
  //# sourceMappingURL=grammar.js.map