@traqula/rules-sparql-1-2 0.0.1-alpha.148 → 0.0.1-alpha.9

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