Skip to content

Commit c5a2e6f

Browse files
committed
part of quety generation
1 parent 77c9160 commit c5a2e6f

4 files changed

Lines changed: 121 additions & 20 deletions

File tree

packages/rules-sparql-1-1/lib/grammar/builtIn.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
} from '../RoundTripTypes';
2828
import type { SparqlGrammarRule, SparqlRule } from '../Sparql11types';
2929
import { expression } from './expression';
30-
import { string } from './literals';
30+
import { string, stringEscapedLexical } from './literals';
3131

3232
export const builtInStr = funcExpr1(l.builtIn.str);
3333
export const builtInLang = funcExpr1(l.builtIn.lang);
@@ -294,5 +294,22 @@ export const aggregate: SparqlRule<'aggregate', ExpressionAggregate> = <const>{
294294

295295
return result;
296296
},
297-
gImpl: () => () => {},
297+
gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
298+
F.printFilter(ast, () => {
299+
PRINT_WORD(ast.aggregation, '(');
300+
if (ast.distinct) {
301+
PRINT_WORD('DISTINCT');
302+
}
303+
});
304+
const arg = ast.expression[0];
305+
if (F.isWildcard(arg)) {
306+
F.printFilter(ast, () => PRINT_WORD('*'));
307+
} else {
308+
SUBRULE(expression, arg, undefined);
309+
}
310+
if (F.isExpressionAggregateSeparator(ast)) {
311+
F.printFilter(ast, () => PRINT_WORD(';', 'SEPARATOR', '=', stringEscapedLexical(ast.separator)));
312+
}
313+
F.printFilter(ast, () => PRINT_WORD(')'));
314+
},
298315
};

packages/rules-sparql-1-1/lib/grammar/expression.ts

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import type {
1212
SparqlGrammarRule,
1313
SparqlRule,
1414
} from '../Sparql11types';
15-
import { builtInCall } from './builtIn';
15+
import { aggregate, builtInCall } from './builtIn';
1616
import {
1717
var_,
18+
varOrTerm,
1819
} from './general';
1920
import {
2021
booleanLiteral,
@@ -24,9 +25,7 @@ import {
2425
numericLiteralPositive,
2526
rdfLiteral,
2627
} from './literals';
27-
28-
const infixOperators = new Set([ '||', '&&', '=', '!=', '<', '>', '<=', '>=', 'in', 'notin', '+', '-', '*', '/' ]);
29-
const prefixOperator = new Set([ '!', 'UPLUS', 'UMINUS' ]);
28+
import { groupGraphPattern } from './whereClause';
3029

3130
/**
3231
* [[71]](https://www.w3.org/TR/sparql11-query/#rArgList)
@@ -64,13 +63,27 @@ export const argList: SparqlRule<'argList', Wrap<IArgList>> = <const> {
6463
C.factory.wrap({ args, distinct }, C.factory.sourceLocation(open, close)));
6564
} },
6665
]),
67-
gImpl: () => () => {},
66+
gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
67+
F.printFilter(ast, () => {
68+
PRINT_WORD('(');
69+
if (ast.val.distinct) {
70+
PRINT_WORD('distinct');
71+
}
72+
});
73+
const [ head, ...tail ] = ast.val.args;
74+
SUBRULE(expression, head, undefined);
75+
for (const expr of tail) {
76+
F.printFilter(ast, () => PRINT_WORD(','));
77+
SUBRULE(expression, expr, undefined);
78+
}
79+
F.printFilter(ast, () => PRINT_WORD(')'));
80+
},
6881
};
6982

7083
/**
7184
* [[72]](https://www.w3.org/TR/sparql11-query/#rConstructTemplate)
7285
*/
73-
export const expressionList: SparqlRule<'expressionList', Wrap<Expression[]>> = <const> {
86+
export const expressionList: SparqlGrammarRule<'expressionList', Wrap<Expression[]>> = <const> {
7487
name: 'expressionList',
7588
impl: ({ ACTION, CONSUME, MANY, OR, SUBRULE1, SUBRULE2 }) => C => OR([
7689
{ ALT: () => {
@@ -90,16 +103,66 @@ export const expressionList: SparqlRule<'expressionList', Wrap<Expression[]>> =
90103
return ACTION(() => C.factory.wrap(args, C.factory.sourceLocation(open, close)));
91104
} },
92105
]),
93-
gImpl: () => () => '',
94106
};
95107

108+
const infixOperators = new Set([ '||', '&&', '=', '!=', '<', '>', '<=', '>=', '+', '-', '*', '/' ]);
109+
const prefixOperator = new Set([ '!', 'UPLUS', 'UMINUS' ]);
110+
96111
/**
97112
* [[110]](https://www.w3.org/TR/sparql11-query/#rExpression)
98113
*/
99114
export const expression: SparqlRule<'expression', Expression> = <const> {
100115
name: 'expression',
101116
impl: ({ SUBRULE }) => () => SUBRULE(conditionalOrExpression, undefined),
102-
gImpl: () => () => {},
117+
gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
118+
if (F.isTerm(ast)) {
119+
SUBRULE(varOrTerm, ast, undefined);
120+
} else {
121+
if (ast.expressionType === 'operation') {
122+
if (infixOperators.has(ast.operator)) {
123+
const [ left, right ] = <[Expression, Expression]>ast.args;
124+
F.printFilter(ast, () => PRINT_WORD('('));
125+
SUBRULE(expression, left, undefined);
126+
F.printFilter(ast, () => {
127+
if (ast.operator === 'notin') {
128+
PRINT_WORD('NOT IN');
129+
} else if (ast.operator === 'in') {
130+
PRINT_WORD('IN');
131+
} else {
132+
PRINT_WORD(ast.operator);
133+
}
134+
});
135+
SUBRULE(expression, right, undefined);
136+
F.printFilter(ast, () => PRINT_WORD(')'));
137+
} else if (prefixOperator.has(ast.operator)) {
138+
const [ expr ] = <[Expression]>ast.args;
139+
F.printFilter(ast, () => PRINT_WORD(ast.operator));
140+
SUBRULE(expression, expr, undefined);
141+
} else {
142+
F.printFilter(ast, () => PRINT_WORD(ast.operator, '('));
143+
const [ head, ...tail ] = ast.args;
144+
if (head) {
145+
SUBRULE(expression, head, undefined);
146+
}
147+
for (const arg of tail) {
148+
F.printFilter(ast, () => PRINT_WORD(ast.operator, ','));
149+
SUBRULE(expression, arg, undefined);
150+
}
151+
}
152+
}
153+
if (ast.expressionType === 'patternOperation') {
154+
const patterns = ast.args;
155+
F.printFilter(ast, () => PRINT_WORD(ast.operator === 'exists' ? 'EXISTS' : 'NOT EXISTS'));
156+
SUBRULE(groupGraphPattern, F.patternGroup(patterns, ast.loc), undefined);
157+
}
158+
if (ast.expressionType === 'functionCall') {
159+
return SUBRULE(iriOrFunction, ast, undefined);
160+
}
161+
if (ast.expressionType === 'aggregate') {
162+
return SUBRULE(aggregate, ast, undefined);
163+
}
164+
}
165+
},
103166
};
104167

105168
type LeftDeepBuildArgs = (left: Expression) => ExpressionOperation;
@@ -403,5 +466,12 @@ export const iriOrFunction: SparqlRule<'iriOrFunction', TermIri | ExpressionFunc
403466
});
404467
return functionCall ?? iriVal;
405468
},
406-
gImpl: () => () => {},
469+
gImpl: ({ SUBRULE }) => (ast, { factory: F }) => {
470+
if (F.isTermIri(ast)) {
471+
SUBRULE(iri, ast, undefined);
472+
} else {
473+
SUBRULE(iri, ast.function, undefined);
474+
SUBRULE(argList, F.wrap({ args: ast.args, distinct: ast.distinct }, ast.loc), undefined);
475+
}
476+
},
407477
};

packages/rules-sparql-1-1/lib/grammar/general.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export const baseDecl: SparqlRule<'baseDecl', ContextDefinitionBaseDecl> = <cons
4040
const val = SUBRULE(iriFull, undefined);
4141
return ACTION(() => C.factory.baseDecl(C.factory.sourceLocation(base, val), val));
4242
},
43-
gImpl: () => () => {},
43+
gImpl: ({ SUBRULE, PRINT_WORD, PRINT }) => (ast, { factory: F }) => {
44+
F.printFilter(ast, () => PRINT_WORD('BASE'));
45+
SUBRULE(iri, ast.value, undefined);
46+
F.printFilter(ast, () => PRINT('\n'));
47+
},
4448
};
4549

4650
/**
@@ -56,7 +60,13 @@ export const prefixDecl: SparqlRule<'prefixDecl', ContextDefinitionPrefixDecl> =
5660

5761
return ACTION(() => C.factory.prefixDecl(C.factory.sourceLocation(prefix, value), name, value));
5862
},
59-
gImpl: () => () => {},
63+
gImpl: ({ SUBRULE, PRINT_WORD, PRINT }) => (ast, { factory: F }) => {
64+
F.printFilter(ast, () => {
65+
PRINT_WORD('PREFIX', `${ast.key}:`);
66+
});
67+
SUBRULE(iri, ast.value, undefined);
68+
F.printFilter(ast, () => PRINT('\n'));
69+
},
6070
};
6171

6272
/**
@@ -110,10 +120,8 @@ export const var_: SparqlRule<'var', TermVariable> = <const> {
110120
]);
111121
return ACTION(() => C.factory.variable(varToken.image.slice(1), C.factory.sourceLocation(varToken)));
112122
},
113-
gImpl: ({ PRINT_WORD }) => (ast) => {
114-
if (!ast.loc) {
115-
PRINT_WORD('?', ast.value);
116-
}
123+
gImpl: ({ PRINT_WORD }) => (ast, { factory: F }) => {
124+
F.printFilter(ast, () => PRINT_WORD(`?${ast.value}`));
117125
},
118126
};
119127

packages/rules-sparql-1-1/lib/grammar/updateUnit/updateUnit.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import type {
2727
SparqlRule,
2828
} from '../../Sparql11types';
2929
import { usingClauseStar } from '../dataSetClause';
30-
import { prologue, varOrIri } from '../general';
30+
import { prologue, varOrIri, varOrTerm } from '../general';
3131
import { iri } from '../literals';
32-
import { triplesTemplate } from '../tripleBlock';
32+
import { triplesBlock, triplesTemplate } from '../tripleBlock';
3333
import { groupGraphPattern } from '../whereClause';
3434

3535
/**
@@ -455,5 +455,11 @@ export const quadsNotTriples: SparqlRule<'quadsNotTriples', GraphQuads> = <const
455455
loc: C.factory.sourceLocation(graph, close),
456456
}));
457457
},
458-
gImpl: () => () => '',
458+
gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
459+
F.printFilter(ast, () => PRINT_WORD('GRAPH'));
460+
SUBRULE(varOrTerm, ast.graph, undefined);
461+
F.printFilter(ast, () => PRINT_WORD('{'));
462+
SUBRULE(triplesBlock, F.patternBgp(ast.triples, ast.loc), undefined);
463+
F.printFilter(ast, () => PRINT_WORD('}'));
464+
},
459465
};

0 commit comments

Comments
 (0)