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

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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024–now Jitse De Smet
4
+ Comunica Association and Ghent University – imec, Belgium
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # TRAQULA Rules SPARQL 1.2 package
2
+
3
+ TRAQULA rules SPARQL 1.2 contains additional rules required for creating a parser for SPARQL 1.2 from the SPARQL 1.1 rules.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @traqula/rules-sparql-1-2
9
+ ```
10
+
11
+ or
12
+
13
+ ```bash
14
+ yarn add @traqula/rules-sparql-1-2
15
+ ```
@@ -0,0 +1,157 @@
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 type { RuleDefReturn, RuleDef } from '@traqula/core';
8
+ import { gram as S11 } from '@traqula/rules-sparql-1-1';
9
+ import type * as T11 from '@traqula/rules-sparql-1-1';
10
+ import type { BaseQuadTerm, Expression, IGraphNode, Term, Triple, TripleCreatorS, TripleCreatorSP } from './sparql12Types';
11
+ /**
12
+ * [[56]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlock) Used by triplesSameSubject
13
+ */
14
+ export declare const reifiedTripleBlock: RuleDef<"reifiedTripleBlock", Triple[]>;
15
+ /**
16
+ * [[57]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlockPath) Used by TriplesSameSubjectPath
17
+ */
18
+ export declare const reifiedTripleBlockPath: RuleDef<"reifiedTripleBlockPath", Triple[]>;
19
+ /**
20
+ * OVERRIDING RULE: {@link S11.dataBlockValue}.
21
+ * [[67]](https://www.w3.org/TR/sparql12-query/#rDataBlockValue)
22
+ */
23
+ export declare const dataBlockValue: RuleDef<'dataBlockValue', RuleDefReturn<typeof S11.dataBlockValue> | BaseQuadTerm>;
24
+ /**
25
+ * [[68]](https://www.w3.org/TR/sparql12-query/#rReifier)
26
+ */
27
+ export declare const reifier: RuleDef<'reifier', T11.VariableTerm | T11.IriTerm | T11.BlankTerm>;
28
+ /**
29
+ * [[68]](https://www.w3.org/TR/sparql12-query/#rVarOrReifierId)
30
+ */
31
+ export declare const varOrReifierId: RuleDef<'varOrReifierId', T11.VariableTerm | T11.IriTerm | T11.BlankTerm>;
32
+ /**
33
+ * OVERRIDING RULE {@link S11.triplesSameSubject}
34
+ * [[79]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubject)
35
+ */
36
+ export declare const triplesSameSubject: RuleDef<"triplesSameSubject", Triple[]>;
37
+ /**
38
+ * OVERRIDING RULE {@link S11.triplesSameSubjectPath}
39
+ * [[85]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath)
40
+ */
41
+ export declare const triplesSameSubjectPath: RuleDef<"triplesSameSubjectPath", Triple[]>;
42
+ /**
43
+ * OVERRIDING RULE: {@link S11.object}.
44
+ * [[84]](https://www.w3.org/TR/sparql12-query/#rObject) Used by ObjectList
45
+ */
46
+ export declare const object: RuleDef<"object", TripleCreatorSP[]>;
47
+ /**
48
+ * OVERRIDING RULE: {@link S11.objectPath}.
49
+ * [[91]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath) Used by ObjectListPath
50
+ */
51
+ export declare const objectPath: RuleDef<"objectPath", TripleCreatorSP[]>;
52
+ /**
53
+ * [[107]](https://www.w3.org/TR/sparql12-query/#rAnnotationPath)
54
+ */
55
+ export declare const annotationPath: RuleDef<"annotationPath", IGraphNode[]>;
56
+ /**
57
+ * [[109]](https://www.w3.org/TR/sparql12-query/#rAnnotation)
58
+ */
59
+ export declare const annotation: RuleDef<"annotation", IGraphNode[]>;
60
+ /**
61
+ * [[108]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlockPath)
62
+ */
63
+ export declare const annotationBlockPath: RuleDef<"annotationBlockPath", TripleCreatorS[]>;
64
+ /**
65
+ * [[110]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlock)
66
+ */
67
+ export declare const annotationBlock: RuleDef<"annotationBlock", TripleCreatorS[]>;
68
+ /**
69
+ * OVERRIDING RULE: {@link S11.graphNode}.
70
+ * [[111]](https://www.w3.org/TR/sparql12-query/#rGraphNode)
71
+ */
72
+ export declare const graphNode: RuleDef<'graphNode', IGraphNode>;
73
+ /**
74
+ * OVERRIDING RULE: {@link S11.graphNodePath}.
75
+ * [[112]](https://www.w3.org/TR/sparql12-query/#rGraphNodePath)
76
+ */
77
+ export declare const graphNodePath: RuleDef<'graphNodePath', IGraphNode>;
78
+ /**
79
+ * OVERRIDING RULE: {@link S11.varOrTerm}.
80
+ * [[113]](https://www.w3.org/TR/sparql12-query/#rVarOrTerm)
81
+ */
82
+ export declare const varOrTerm: RuleDef<'varOrTerm', Term>;
83
+ /**
84
+ * [[114]](https://www.w3.org/TR/sparql12-query/#rReifiedTriple)
85
+ */
86
+ export declare const reifiedTriple: RuleDef<'reifiedTriple', IGraphNode & {
87
+ node: T11.BlankTerm | T11.VariableTerm | T11.IriTerm;
88
+ }>;
89
+ /**
90
+ * [[115]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleSubject)
91
+ */
92
+ export declare const reifiedTripleSubject: RuleDef<'reifiedTripleSubject', IGraphNode>;
93
+ /**
94
+ * [[116]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleObject)
95
+ */
96
+ export declare const reifiedTripleObject: RuleDef<'reifiedTripleObject', RuleDefReturn<typeof reifiedTripleSubject>>;
97
+ /**
98
+ * [[117]](https://www.w3.org/TR/sparql12-query/#rTripleTerm)
99
+ */
100
+ export declare const tripleTerm: RuleDef<'tripleTerm', BaseQuadTerm>;
101
+ /**
102
+ * [[118]](https://www.w3.org/TR/sparql12-query/#rTripleTermSubject)
103
+ */
104
+ export declare const tripleTermSubject: RuleDef<'tripleTermSubject', T11.VariableTerm | T11.IriTerm | T11.LiteralTerm | T11.BlankTerm>;
105
+ /**
106
+ * [[119]](https://www.w3.org/TR/sparql12-query/#rTripleTermObject)
107
+ */
108
+ export declare const tripleTermObject: RuleDef<'tripleTermObject', RuleDefReturn<typeof tripleTermSubject> | BaseQuadTerm>;
109
+ /**
110
+ * [[120]](https://www.w3.org/TR/sparql12-query/#rTripleTermData)
111
+ */
112
+ export declare const tripleTermData: RuleDef<'tripleTermData', BaseQuadTerm>;
113
+ /**
114
+ * [[121]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataSubject)
115
+ */
116
+ export declare const tripleTermDataSubject: RuleDef<'tripleTermDataSubject', T11.IriTerm | T11.LiteralTerm>;
117
+ /**
118
+ * [[122]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataObject)
119
+ */
120
+ export declare const tripleTermDataObject: RuleDef<'tripleTermDataObject', RuleDefReturn<typeof tripleTermDataSubject> | BaseQuadTerm>;
121
+ /**
122
+ * OVERRIDING RULE: {@link S11.primaryExpression}.
123
+ * [[134]](https://www.w3.org/TR/sparql12-query/#rPrimaryExpression)
124
+ */
125
+ export declare const primaryExpression: RuleDef<'primaryExpression', Expression>;
126
+ /**
127
+ * [[135]](https://www.w3.org/TR/sparql12-query/#rExprTripleTerm)
128
+ */
129
+ export declare const exprTripleTerm: RuleDef<'exprTripleTerm', BaseQuadTerm>;
130
+ /**
131
+ * [[136]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermSubject)
132
+ */
133
+ export declare const exprTripleTermSubject: RuleDef<'exprTripleTermSubject', T11.IriTerm | T11.VariableTerm | T11.LiteralTerm>;
134
+ /**
135
+ * [[137]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermObject)
136
+ */
137
+ export declare const exprTripleTermObject: RuleDef<'exprTripleTermObject', RuleDefReturn<typeof exprTripleTermSubject> | BaseQuadTerm>;
138
+ export declare const builtinLangDir: S11.RuleDefExpressionFunctionX<"builtInLangdir", [T11.Expression]>;
139
+ export declare const builtinLangStrDir: S11.RuleDefExpressionFunctionX<"builtInStrLangdir", [T11.Expression, T11.Expression, T11.Expression]>;
140
+ export declare const builtinHasLang: S11.RuleDefExpressionFunctionX<"builtInHasLang", [T11.Expression]>;
141
+ export declare const builtinHasLangDir: S11.RuleDefExpressionFunctionX<"builtInHasLangdir", [T11.Expression]>;
142
+ export declare const builtinIsTriple: S11.RuleDefExpressionFunctionX<"builtInIsTriple", [T11.Expression]>;
143
+ export declare const builtinTriple: S11.RuleDefExpressionFunctionX<"builtInTriple", [T11.Expression, T11.Expression, T11.Expression]>;
144
+ export declare const builtinSubject: S11.RuleDefExpressionFunctionX<"builtInSubject", [T11.Expression]>;
145
+ export declare const builtinPredicate: S11.RuleDefExpressionFunctionX<"builtInPredicate", [T11.Expression]>;
146
+ export declare const builtinObject: S11.RuleDefExpressionFunctionX<"builtInObject", [T11.Expression]>;
147
+ /**
148
+ * OVERRIDING RULE: {@link S11.builtInCall}.
149
+ * [[139]](https://www.w3.org/TR/sparql12-query/#rBuiltInCall)
150
+ */
151
+ export declare const builtInCall: typeof S11.builtInCall;
152
+ /**
153
+ * OVERRIDING RULE: {@link S11.rdfLiteral}.
154
+ * No retyping is needed since the return type is the same
155
+ * [[147]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
156
+ */
157
+ export declare const rdfLiteral: typeof S11.rdfLiteral;
package/lib/grammar.js ADDED
@@ -0,0 +1,485 @@
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 * 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';
11
+ function reifiedTripleBlockImpl(name, allowPath) {
12
+ return {
13
+ name,
14
+ impl: ({ ACTION, SUBRULE }) => () => {
15
+ const triple = SUBRULE(reifiedTriple);
16
+ const properties = SUBRULE(allowPath ? S11.propertyListPath : S11.propertyList);
17
+ return ACTION(() => [
18
+ ...triple.triples,
19
+ ...properties.map(partial => partial({ subject: triple.node })),
20
+ ]);
21
+ },
22
+ };
23
+ }
24
+ /**
25
+ * [[56]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlock) Used by triplesSameSubject
26
+ */
27
+ export const reifiedTripleBlock = reifiedTripleBlockImpl('reifiedTripleBlock', false);
28
+ /**
29
+ * [[57]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleBlockPath) Used by TriplesSameSubjectPath
30
+ */
31
+ export const reifiedTripleBlockPath = reifiedTripleBlockImpl('reifiedTripleBlockPath', true);
32
+ /**
33
+ * OVERRIDING RULE: {@link S11.dataBlockValue}.
34
+ * [[67]](https://www.w3.org/TR/sparql12-query/#rDataBlockValue)
35
+ */
36
+ export const dataBlockValue = {
37
+ name: 'dataBlockValue',
38
+ impl: $ => () => $.OR2([
39
+ { ALT: () => S11.dataBlockValue.impl($)() },
40
+ { ALT: () => $.SUBRULE(tripleTermData) },
41
+ ]),
42
+ };
43
+ /**
44
+ * [[68]](https://www.w3.org/TR/sparql12-query/#rReifier)
45
+ */
46
+ export const reifier = {
47
+ name: 'reifier',
48
+ impl: ({ ACTION, CONSUME, SUBRULE, OPTION, context }) => () => {
49
+ CONSUME(l12.tilde);
50
+ const reifier = OPTION(() => SUBRULE(varOrReifierId));
51
+ return ACTION(() => {
52
+ if (reifier === undefined && !context.parseMode.has(S11.canCreateBlankNodes)) {
53
+ throw new Error('Cannot create blanknodes in current parse mode');
54
+ }
55
+ return reifier ?? context.dataFactory.blankNode();
56
+ });
57
+ },
58
+ };
59
+ /**
60
+ * [[68]](https://www.w3.org/TR/sparql12-query/#rVarOrReifierId)
61
+ */
62
+ export const varOrReifierId = {
63
+ name: 'varOrReifierId',
64
+ impl: ({ SUBRULE, OR }) => () => OR([
65
+ { ALT: () => SUBRULE(S11.var_) },
66
+ { ALT: () => SUBRULE(S11.iri) },
67
+ { ALT: () => SUBRULE(S11.blankNode) },
68
+ ]),
69
+ };
70
+ function triplesSameSubjectImpl(name, allowPaths) {
71
+ return {
72
+ name,
73
+ impl: $ => () => $.OR2([
74
+ { ALT: () => allowPaths ? S11.triplesSameSubjectPath.impl($)() : S11.triplesSameSubject.impl($)() },
75
+ { ALT: () => $.SUBRULE(allowPaths ? reifiedTripleBlockPath : reifiedTripleBlock) },
76
+ ]),
77
+ };
78
+ }
79
+ /**
80
+ * OVERRIDING RULE {@link S11.triplesSameSubject}
81
+ * [[79]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubject)
82
+ */
83
+ export const triplesSameSubject = triplesSameSubjectImpl('triplesSameSubject', false);
84
+ /**
85
+ * OVERRIDING RULE {@link S11.triplesSameSubjectPath}
86
+ * [[85]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath)
87
+ */
88
+ export const triplesSameSubjectPath = triplesSameSubjectImpl('triplesSameSubjectPath', true);
89
+ function objectImpl(name, allowPaths) {
90
+ return {
91
+ name,
92
+ impl: ({ ACTION, SUBRULE, context }) => () => {
93
+ const objectVal = SUBRULE(allowPaths ? graphNodePath : graphNode);
94
+ const annotationVal = SUBRULE(allowPaths ? annotationPath : annotation);
95
+ return ACTION(() => {
96
+ const result = [
97
+ // You parse the object
98
+ ({ subject, predicate }) => ({ subject, predicate, object: objectVal.node }),
99
+ // You might get some additional triples from parsing the object (like when it's a collection)
100
+ ...objectVal.triples.map(triple => () => triple),
101
+ ];
102
+ for (const annotation of annotationVal) {
103
+ result.push(({ subject, predicate }) => {
104
+ if ('type' in predicate && predicate.type === 'path') {
105
+ throw new Error('Note 17 violation');
106
+ }
107
+ return context.dataFactory.quad(annotation.node, context.dataFactory.namedNode(CommonIRIs.REIFIES), context.dataFactory.quad(subject, predicate, objectVal.node));
108
+ });
109
+ result.push(...annotation.triples.map(triple => () => triple));
110
+ }
111
+ return result;
112
+ });
113
+ },
114
+ };
115
+ }
116
+ /**
117
+ * OVERRIDING RULE: {@link S11.object}.
118
+ * [[84]](https://www.w3.org/TR/sparql12-query/#rObject) Used by ObjectList
119
+ */
120
+ export const object = objectImpl('object', false);
121
+ /**
122
+ * OVERRIDING RULE: {@link S11.objectPath}.
123
+ * [[91]](https://www.w3.org/TR/sparql12-query/#rTriplesSameSubjectPath) Used by ObjectListPath
124
+ */
125
+ export const objectPath = objectImpl('objectPath', true);
126
+ function annotationImpl(name, allowPaths) {
127
+ return {
128
+ name,
129
+ impl: ({ ACTION, SUBRULE, OR, MANY, context }) => () => {
130
+ const annotations = [];
131
+ let currentReifier;
132
+ function flush() {
133
+ if (currentReifier) {
134
+ annotations.push({ node: currentReifier, triples: [] });
135
+ currentReifier = undefined;
136
+ }
137
+ }
138
+ MANY(() => {
139
+ OR([
140
+ { ALT: () => {
141
+ const node = SUBRULE(reifier);
142
+ ACTION(() => flush());
143
+ currentReifier = node;
144
+ } },
145
+ { ALT: () => {
146
+ const block = SUBRULE(allowPaths ? annotationBlockPath : annotationBlock);
147
+ ACTION(() => {
148
+ if (currentReifier === undefined && !context.parseMode.has(S11.canCreateBlankNodes)) {
149
+ throw new Error('Cannot create blanknodes in current parse mode');
150
+ }
151
+ const node = currentReifier ?? context.dataFactory.blankNode();
152
+ annotations.push({
153
+ node,
154
+ triples: block.map(partial => partial({ subject: node })),
155
+ });
156
+ currentReifier = undefined;
157
+ });
158
+ } },
159
+ ]);
160
+ });
161
+ return ACTION(() => {
162
+ flush();
163
+ return annotations;
164
+ });
165
+ },
166
+ };
167
+ }
168
+ /**
169
+ * [[107]](https://www.w3.org/TR/sparql12-query/#rAnnotationPath)
170
+ */
171
+ export const annotationPath = annotationImpl('annotationPath', true);
172
+ /**
173
+ * [[109]](https://www.w3.org/TR/sparql12-query/#rAnnotation)
174
+ */
175
+ export const annotation = annotationImpl('annotation', false);
176
+ function annotationBlockImpl(name, allowPaths) {
177
+ return {
178
+ name,
179
+ impl: ({ SUBRULE, CONSUME }) => () => {
180
+ CONSUME(l12.annotationOpen);
181
+ const res = SUBRULE(allowPaths ? S11.propertyListPathNotEmpty : S11.propertyListNotEmpty);
182
+ CONSUME(l12.annotationClose);
183
+ return res;
184
+ },
185
+ };
186
+ }
187
+ /**
188
+ * [[108]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlockPath)
189
+ */
190
+ export const annotationBlockPath = annotationBlockImpl('annotationBlockPath', true);
191
+ /**
192
+ * [[110]](https://www.w3.org/TR/sparql12-query/#rAnnotationBlock)
193
+ */
194
+ export const annotationBlock = annotationBlockImpl('annotationBlock', false);
195
+ /**
196
+ * OVERRIDING RULE: {@link S11.graphNode}.
197
+ * [[111]](https://www.w3.org/TR/sparql12-query/#rGraphNode)
198
+ */
199
+ export const graphNode = {
200
+ name: 'graphNode',
201
+ impl: $ => () => $.OR2([
202
+ { ALT: () => S11.graphNode.impl($)() },
203
+ { ALT: () => $.SUBRULE(reifiedTriple) },
204
+ ]),
205
+ };
206
+ /**
207
+ * OVERRIDING RULE: {@link S11.graphNodePath}.
208
+ * [[112]](https://www.w3.org/TR/sparql12-query/#rGraphNodePath)
209
+ */
210
+ export const graphNodePath = {
211
+ name: 'graphNodePath',
212
+ impl: $ => () => $.OR2([
213
+ { ALT: () => S11.graphNodePath.impl($)() },
214
+ { ALT: () => $.SUBRULE(reifiedTriple) },
215
+ ]),
216
+ };
217
+ /**
218
+ * OVERRIDING RULE: {@link S11.varOrTerm}.
219
+ * [[113]](https://www.w3.org/TR/sparql12-query/#rVarOrTerm)
220
+ */
221
+ export const varOrTerm = {
222
+ name: 'varOrTerm',
223
+ impl: ({ SUBRULE, OR, CONSUME, context }) => () => OR([
224
+ { ALT: () => SUBRULE(S11.var_) },
225
+ { ALT: () => SUBRULE(S11.iri) },
226
+ { ALT: () => SUBRULE(rdfLiteral) },
227
+ { ALT: () => SUBRULE(S11.numericLiteral) },
228
+ { ALT: () => SUBRULE(S11.booleanLiteral) },
229
+ { ALT: () => SUBRULE(S11.blankNode) },
230
+ { ALT: () => {
231
+ CONSUME(l11.terminals.nil);
232
+ return context.dataFactory.namedNode(CommonIRIs.NIL);
233
+ } },
234
+ { ALT: () => SUBRULE(tripleTerm) },
235
+ ]),
236
+ };
237
+ /**
238
+ * [[114]](https://www.w3.org/TR/sparql12-query/#rReifiedTriple)
239
+ */
240
+ export const reifiedTriple = {
241
+ name: 'reifiedTriple',
242
+ impl: ({ ACTION, CONSUME, SUBRULE, OPTION, context }) => () => {
243
+ CONSUME(l12.reificationOpen);
244
+ const subject = SUBRULE(reifiedTripleSubject);
245
+ const predicate = SUBRULE(S11.verb);
246
+ const object = SUBRULE(reifiedTripleObject);
247
+ const reifierVal = OPTION(() => SUBRULE(reifier));
248
+ CONSUME(l12.reificationClose);
249
+ return ACTION(() => {
250
+ if (reifierVal === undefined && !context.parseMode.has(S11.canCreateBlankNodes)) {
251
+ throw new Error('Cannot create blanknodes in current parse mode');
252
+ }
253
+ const reifier = reifierVal ?? context.dataFactory.blankNode();
254
+ const tripleTerm = context.dataFactory.quad(subject.node, predicate, object.node);
255
+ return {
256
+ node: reifier,
257
+ triples: [
258
+ ...subject.triples,
259
+ context.dataFactory.quad(reifier, context.dataFactory.namedNode(CommonIRIs.REIFIES), tripleTerm),
260
+ ...object.triples,
261
+ ],
262
+ };
263
+ });
264
+ },
265
+ };
266
+ /**
267
+ * [[115]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleSubject)
268
+ */
269
+ export const reifiedTripleSubject = {
270
+ name: 'reifiedTripleSubject',
271
+ impl: ({ OR, SUBRULE }) => () => OR([
272
+ { ALT: () => ({ node: SUBRULE(S11.var_), triples: [] }) },
273
+ { ALT: () => ({ node: SUBRULE(S11.iri), triples: [] }) },
274
+ { ALT: () => ({ node: SUBRULE(rdfLiteral), triples: [] }) },
275
+ { ALT: () => ({ node: SUBRULE(S11.numericLiteral), triples: [] }) },
276
+ { ALT: () => ({ node: SUBRULE(S11.booleanLiteral), triples: [] }) },
277
+ { ALT: () => ({ node: SUBRULE(S11.blankNode), triples: [] }) },
278
+ { ALT: () => SUBRULE(reifiedTriple) },
279
+ ]),
280
+ };
281
+ /**
282
+ * [[116]](https://www.w3.org/TR/sparql12-query/#rReifiedTripleObject)
283
+ */
284
+ export const reifiedTripleObject = {
285
+ name: 'reifiedTripleObject',
286
+ impl: $ => () => $.OR2([
287
+ { ALT: () => reifiedTripleSubject.impl($)() },
288
+ { ALT: () => ({ node: $.SUBRULE(tripleTerm), triples: [] }) },
289
+ ]),
290
+ };
291
+ /**
292
+ * [[117]](https://www.w3.org/TR/sparql12-query/#rTripleTerm)
293
+ */
294
+ export const tripleTerm = {
295
+ name: 'tripleTerm',
296
+ impl: ({ CONSUME, SUBRULE, context }) => () => {
297
+ CONSUME(l12.tripleTermOpen);
298
+ const subject = SUBRULE(tripleTermSubject);
299
+ const predicate = SUBRULE(S11.verb);
300
+ const object = SUBRULE(tripleTermObject);
301
+ CONSUME(l12.tripleTermClose);
302
+ return context.dataFactory.quad(subject, predicate, object);
303
+ },
304
+ };
305
+ /**
306
+ * [[118]](https://www.w3.org/TR/sparql12-query/#rTripleTermSubject)
307
+ */
308
+ export const tripleTermSubject = {
309
+ name: 'tripleTermSubject',
310
+ impl: ({ SUBRULE, OR }) => () => OR([
311
+ { ALT: () => SUBRULE(S11.var_) },
312
+ { ALT: () => SUBRULE(S11.iri) },
313
+ { ALT: () => SUBRULE(rdfLiteral) },
314
+ { ALT: () => SUBRULE(S11.numericLiteral) },
315
+ { ALT: () => SUBRULE(S11.booleanLiteral) },
316
+ { ALT: () => SUBRULE(S11.blankNode) },
317
+ ]),
318
+ };
319
+ /**
320
+ * [[119]](https://www.w3.org/TR/sparql12-query/#rTripleTermObject)
321
+ */
322
+ export const tripleTermObject = {
323
+ name: 'tripleTermObject',
324
+ impl: $ => () => $.OR2([
325
+ { ALT: () => tripleTermSubject.impl($)() },
326
+ { ALT: () => $.SUBRULE(tripleTerm) },
327
+ ]),
328
+ };
329
+ /**
330
+ * [[120]](https://www.w3.org/TR/sparql12-query/#rTripleTermData)
331
+ */
332
+ export const tripleTermData = {
333
+ name: 'tripleTermData',
334
+ impl: ({ ACTION, CONSUME, OR, SUBRULE, context }) => () => {
335
+ CONSUME(l12.tripleTermOpen);
336
+ const subject = SUBRULE(tripleTermDataSubject);
337
+ const predicate = OR([
338
+ { ALT: () => SUBRULE(S11.iri) },
339
+ { ALT: () => {
340
+ CONSUME(l11.a);
341
+ return ACTION(() => context.dataFactory.namedNode(CommonIRIs.TYPE));
342
+ } },
343
+ ]);
344
+ const object = SUBRULE(tripleTermDataObject);
345
+ CONSUME(l12.tripleTermClose);
346
+ return ACTION(() => context.dataFactory.quad(subject, predicate, object));
347
+ },
348
+ };
349
+ /**
350
+ * [[121]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataSubject)
351
+ */
352
+ export const tripleTermDataSubject = {
353
+ name: 'tripleTermDataSubject',
354
+ impl: ({ OR, SUBRULE }) => () => OR([
355
+ { ALT: () => SUBRULE(S11.iri) },
356
+ { ALT: () => SUBRULE(rdfLiteral) },
357
+ { ALT: () => SUBRULE(S11.numericLiteral) },
358
+ { ALT: () => SUBRULE(S11.booleanLiteral) },
359
+ ]),
360
+ };
361
+ /**
362
+ * [[122]](https://www.w3.org/TR/sparql12-query/#rTripleTermDataObject)
363
+ */
364
+ export const tripleTermDataObject = {
365
+ name: 'tripleTermDataObject',
366
+ impl: $ => () => $.OR2([
367
+ { ALT: () => tripleTermDataSubject.impl($)() },
368
+ { ALT: () => $.SUBRULE(tripleTermData) },
369
+ ]),
370
+ };
371
+ /**
372
+ * OVERRIDING RULE: {@link S11.primaryExpression}.
373
+ * [[134]](https://www.w3.org/TR/sparql12-query/#rPrimaryExpression)
374
+ */
375
+ export const primaryExpression = {
376
+ name: 'primaryExpression',
377
+ impl: $ => () => $.OR2([
378
+ { ALT: () => S11.primaryExpression.impl($)() },
379
+ { ALT: () => $.SUBRULE(exprTripleTerm) },
380
+ ]),
381
+ };
382
+ /**
383
+ * [[135]](https://www.w3.org/TR/sparql12-query/#rExprTripleTerm)
384
+ */
385
+ export const exprTripleTerm = {
386
+ name: 'exprTripleTerm',
387
+ impl: ({ ACTION, CONSUME, SUBRULE, context }) => () => {
388
+ CONSUME(l12.tripleTermOpen);
389
+ const subject = SUBRULE(exprTripleTermSubject);
390
+ const predicate = SUBRULE(S11.verb);
391
+ const object = SUBRULE(exprTripleTermObject);
392
+ CONSUME(l12.tripleTermClose);
393
+ return ACTION(() => context.dataFactory.quad(subject, predicate, object));
394
+ },
395
+ };
396
+ /**
397
+ * [[136]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermSubject)
398
+ */
399
+ export const exprTripleTermSubject = {
400
+ name: 'exprTripleTermSubject',
401
+ impl: ({ OR, SUBRULE }) => () => OR([
402
+ { ALT: () => SUBRULE(S11.iri) },
403
+ { ALT: () => SUBRULE(rdfLiteral) },
404
+ { ALT: () => SUBRULE(S11.numericLiteral) },
405
+ { ALT: () => SUBRULE(S11.booleanLiteral) },
406
+ { ALT: () => SUBRULE(S11.var_) },
407
+ ]),
408
+ };
409
+ /**
410
+ * [[137]](https://www.w3.org/TR/sparql12-query/#rExprTripleTermObject)
411
+ */
412
+ export const exprTripleTermObject = {
413
+ name: 'exprTripleTermObject',
414
+ impl: $ => () => $.OR2([
415
+ { ALT: () => exprTripleTermSubject.impl($)() },
416
+ { ALT: () => $.SUBRULE(exprTripleTerm) },
417
+ ]),
418
+ };
419
+ export const builtinLangDir = funcExpr1(l12.builtinLangDir);
420
+ export const builtinLangStrDir = funcExpr3(l12.builtinStrLangDir);
421
+ export const builtinHasLang = funcExpr1(l12.builtinHasLang);
422
+ export const builtinHasLangDir = funcExpr1(l12.builtinHasLangDir);
423
+ export const builtinIsTriple = funcExpr1(l12.builtinIsTRIPLE);
424
+ export const builtinTriple = funcExpr3(l12.builtinTRIPLE);
425
+ export const builtinSubject = funcExpr1(l12.builtinSUBJECT);
426
+ export const builtinPredicate = funcExpr1(l12.builtinPREDICATE);
427
+ export const builtinObject = funcExpr1(l12.builtinOBJECT);
428
+ /**
429
+ * OVERRIDING RULE: {@link S11.builtInCall}.
430
+ * [[139]](https://www.w3.org/TR/sparql12-query/#rBuiltInCall)
431
+ */
432
+ export const builtInCall = {
433
+ name: 'builtInCall',
434
+ impl: $ => () => $.OR2([
435
+ { ALT: () => S11.builtInCall.impl($)() },
436
+ { ALT: () => $.SUBRULE(builtinLangDir) },
437
+ { ALT: () => $.SUBRULE(builtinLangStrDir) },
438
+ { ALT: () => $.SUBRULE(builtinHasLang) },
439
+ { ALT: () => $.SUBRULE(builtinHasLangDir) },
440
+ { ALT: () => $.SUBRULE(builtinIsTriple) },
441
+ { ALT: () => $.SUBRULE(builtinTriple) },
442
+ { ALT: () => $.SUBRULE(builtinSubject) },
443
+ { ALT: () => $.SUBRULE(builtinPredicate) },
444
+ { ALT: () => $.SUBRULE(builtinObject) },
445
+ ]),
446
+ };
447
+ function isLangDir(dir) {
448
+ return dir === 'ltr' || dir === 'rtl';
449
+ }
450
+ /**
451
+ * OVERRIDING RULE: {@link S11.rdfLiteral}.
452
+ * No retyping is needed since the return type is the same
453
+ * [[147]](https://www.w3.org/TR/sparql12-query/#rRDFLiteral)
454
+ */
455
+ export const rdfLiteral = {
456
+ name: 'rdfLiteral',
457
+ impl: ({ ACTION, SUBRULE, OPTION, CONSUME, OR, context }) => () => {
458
+ const value = SUBRULE(S11.string);
459
+ const langOrDataType = OPTION(() => OR([
460
+ { ALT: () => {
461
+ const langTag = CONSUME(l12.LANG_DIR).image.slice(1);
462
+ return ACTION(() => {
463
+ const dirSplit = langTag.split('--');
464
+ if (dirSplit.length > 1) {
465
+ const [language, direction] = dirSplit;
466
+ if (!isLangDir(direction)) {
467
+ throw new Error(`language direction "${direction}" of literal "${value}@${langTag}" is not is required range 'ltr' | 'rtl'.`);
468
+ }
469
+ return {
470
+ language,
471
+ direction,
472
+ };
473
+ }
474
+ return langTag;
475
+ });
476
+ } },
477
+ { ALT: () => {
478
+ CONSUME(l11.symbols.hathat);
479
+ return SUBRULE(S11.iri);
480
+ } },
481
+ ]));
482
+ return context.dataFactory.literal(value, langOrDataType);
483
+ },
484
+ };
485
+ //# sourceMappingURL=grammar.js.map