|
| 1 | +import { unCapitalize } from '@traqula/core'; |
| 2 | +import type { TokenType } from 'chevrotain'; |
| 3 | +import { var_ } from './grammar'; |
| 4 | +import { expression, expressionList } from './grammar/expression'; |
| 5 | +import { groupGraphPattern } from './grammar/whereClause'; |
| 6 | +import * as l from './lexer'; |
| 7 | +import type { |
| 8 | + Expression, |
| 9 | + ExpressionAggregateDefault, |
| 10 | + ExpressionOperation, |
| 11 | + ExpressionPatternOperation, |
| 12 | + TermVariable, |
| 13 | +} from './RoundTripTypes'; |
| 14 | +import type { SparqlGrammarRule } from './Sparql11types'; |
| 15 | + |
| 16 | +export type ExpressionFunctionX<U extends Expression[]> = ExpressionOperation & { |
| 17 | + args: U; |
| 18 | +}; |
| 19 | + |
| 20 | +export type RuleDefExpressionFunctionX<T extends string, U extends Expression[]> |
| 21 | + = SparqlGrammarRule<T, ExpressionFunctionX<U>>; |
| 22 | + |
| 23 | +export function funcExpr1<T extends string>(func: TokenType & { name: T }): |
| 24 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [Expression]> { |
| 25 | + return { |
| 26 | + name: unCapitalize(func.name), |
| 27 | + impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => { |
| 28 | + const operator = CONSUME(func); |
| 29 | + CONSUME(l.symbols.LParen); |
| 30 | + const arg = SUBRULE(expression, undefined); |
| 31 | + const close = CONSUME(l.symbols.RParen); |
| 32 | + return ACTION(() => |
| 33 | + C.factory.expressionOperation(operator.image, [ arg ], C.factory.sourceLocation(operator, close))); |
| 34 | + }, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +export function funcExpr2<T extends string>(func: TokenType & { name: T }): |
| 39 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [Expression, Expression]> { |
| 40 | + return { |
| 41 | + name: unCapitalize(func.name), |
| 42 | + impl: ({ ACTION, CONSUME, SUBRULE1, SUBRULE2 }) => (C) => { |
| 43 | + const operator = CONSUME(func); |
| 44 | + CONSUME(l.symbols.LParen); |
| 45 | + const arg1 = SUBRULE1(expression, undefined); |
| 46 | + CONSUME(l.symbols.comma); |
| 47 | + const arg2 = SUBRULE2(expression, undefined); |
| 48 | + const close = CONSUME(l.symbols.RParen); |
| 49 | + return ACTION(() => |
| 50 | + C.factory.expressionOperation(operator.image, [ arg1, arg2 ], C.factory.sourceLocation(operator, close))); |
| 51 | + }, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +export function funcExpr3<T extends string>(func: TokenType & { name: T }): |
| 56 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [Expression, Expression, Expression]> { |
| 57 | + return { |
| 58 | + name: unCapitalize(func.name), |
| 59 | + impl: ({ ACTION, CONSUME, CONSUME1, CONSUME2, SUBRULE1, SUBRULE2, SUBRULE3 }) => (C) => { |
| 60 | + const operator = CONSUME(func); |
| 61 | + CONSUME(l.symbols.LParen); |
| 62 | + const arg1 = SUBRULE1(expression, undefined); |
| 63 | + CONSUME1(l.symbols.comma); |
| 64 | + const arg2 = SUBRULE2(expression, undefined); |
| 65 | + CONSUME2(l.symbols.comma); |
| 66 | + const arg3 = SUBRULE3(expression, undefined); |
| 67 | + const close = CONSUME(l.symbols.RParen); |
| 68 | + |
| 69 | + return ACTION(() => |
| 70 | + C.factory.expressionOperation(operator.image, [ arg1, arg2, arg3 ], C.factory.sourceLocation(operator, close))); |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +export function funcVar1<T extends string>(func: TokenType & { name: T }): |
| 76 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [TermVariable]> { |
| 77 | + return { |
| 78 | + name: unCapitalize(func.name), |
| 79 | + impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => { |
| 80 | + const operator = CONSUME(func); |
| 81 | + CONSUME(l.symbols.LParen); |
| 82 | + const arg = SUBRULE(var_, undefined); |
| 83 | + const close = CONSUME(l.symbols.RParen); |
| 84 | + return ACTION(() => |
| 85 | + C.factory.expressionOperation(operator.image, [ arg ], C.factory.sourceLocation(operator, close))); |
| 86 | + }, |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +export function funcExprOrNil1<T extends string>(func: TokenType & { name: T }): |
| 91 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [] | [Expression]> { |
| 92 | + return { |
| 93 | + name: unCapitalize(func.name), |
| 94 | + impl: ({ ACTION, CONSUME, OR, SUBRULE }) => (C) => { |
| 95 | + const operator = CONSUME(func); |
| 96 | + return OR<ExpressionFunctionX<[] | [Expression]>>([ |
| 97 | + { ALT: () => { |
| 98 | + CONSUME(l.symbols.LParen); |
| 99 | + const arg = SUBRULE(expression, undefined); |
| 100 | + const close = CONSUME(l.symbols.RParen); |
| 101 | + return ACTION(() => |
| 102 | + C.factory.expressionOperation(operator.image, [ arg ], C.factory.sourceLocation(operator, close))); |
| 103 | + } }, |
| 104 | + { ALT: () => { |
| 105 | + const nil = CONSUME(l.terminals.nil); |
| 106 | + return ACTION(() => |
| 107 | + C.factory.expressionOperation(operator.image, [], C.factory.sourceLocation(operator, nil))); |
| 108 | + } }, |
| 109 | + ]); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +export function funcNil1<T extends string>(func: TokenType & { name: T }): |
| 115 | +RuleDefExpressionFunctionX<Uncapitalize<T>, []> { |
| 116 | + return { |
| 117 | + name: unCapitalize(func.name), |
| 118 | + impl: ({ ACTION, CONSUME }) => (C) => { |
| 119 | + const operator = CONSUME(func); |
| 120 | + const nil = CONSUME(l.terminals.nil); |
| 121 | + return ACTION(() => |
| 122 | + C.factory.expressionOperation(operator.image, [], C.factory.sourceLocation(operator, nil))); |
| 123 | + }, |
| 124 | + }; |
| 125 | +} |
| 126 | + |
| 127 | +export function funcExprList1<T extends string>(func: TokenType & { name: T }): |
| 128 | +RuleDefExpressionFunctionX<Uncapitalize<T>, Expression[]> { |
| 129 | + return { |
| 130 | + name: unCapitalize(func.name), |
| 131 | + impl: ({ ACTION, CONSUME, SUBRULE }) => (C) => { |
| 132 | + const operator = CONSUME(func); |
| 133 | + const args = SUBRULE(expressionList, undefined); |
| 134 | + return ACTION(() => |
| 135 | + C.factory.expressionOperation(operator.image, args.val, C.factory.sourceLocation(operator, args))); |
| 136 | + }, |
| 137 | + }; |
| 138 | +} |
| 139 | + |
| 140 | +export function funcExpr2or3<T extends string>(func: TokenType & { name: T }): |
| 141 | +RuleDefExpressionFunctionX<Uncapitalize<T>, [Expression, Expression] | [Expression, Expression, Expression]> { |
| 142 | + return { |
| 143 | + name: unCapitalize(func.name), |
| 144 | + impl: ({ ACTION, CONSUME, SUBRULE1, SUBRULE2, SUBRULE3, CONSUME1, OPTION, CONSUME2 }) => |
| 145 | + (C) => { |
| 146 | + const operator = CONSUME(func); |
| 147 | + CONSUME(l.symbols.LParen); |
| 148 | + const arg1 = SUBRULE1(expression, undefined); |
| 149 | + CONSUME1(l.symbols.comma); |
| 150 | + const arg2 = SUBRULE2(expression, undefined); |
| 151 | + const arg3 = OPTION(() => { |
| 152 | + CONSUME2(l.symbols.comma); |
| 153 | + return SUBRULE3(expression, undefined); |
| 154 | + }); |
| 155 | + const close = CONSUME(l.symbols.RParen); |
| 156 | + return ACTION(() => C.factory.expressionOperation( |
| 157 | + operator.image, |
| 158 | + [ arg1, arg2, ...(arg3 ? [ arg3 ] : []) ], |
| 159 | + C.factory.sourceLocation(operator, close), |
| 160 | + )); |
| 161 | + }, |
| 162 | + }; |
| 163 | +} |
| 164 | + |
| 165 | +export function funcExpr3or4<T extends string>(func: TokenType & { name: T }): |
| 166 | +RuleDefExpressionFunctionX< |
| 167 | + Uncapitalize<T>, |
| 168 | + [Expression, Expression, Expression] | [Expression, Expression, Expression, Expression] |
| 169 | +> { |
| 170 | + return { |
| 171 | + name: unCapitalize(func.name), |
| 172 | + impl: ({ |
| 173 | + ACTION, |
| 174 | + CONSUME, |
| 175 | + SUBRULE1, |
| 176 | + SUBRULE2, |
| 177 | + SUBRULE3, |
| 178 | + SUBRULE4, |
| 179 | + CONSUME1, |
| 180 | + OPTION, |
| 181 | + CONSUME2, |
| 182 | + CONSUME3, |
| 183 | + }) => |
| 184 | + (C) => { |
| 185 | + const operator = CONSUME(func); |
| 186 | + CONSUME(l.symbols.LParen); |
| 187 | + const arg1 = SUBRULE1(expression, undefined); |
| 188 | + CONSUME1(l.symbols.comma); |
| 189 | + const arg2 = SUBRULE2(expression, undefined); |
| 190 | + CONSUME2(l.symbols.comma); |
| 191 | + const arg3 = SUBRULE3(expression, undefined); |
| 192 | + const arg4 = OPTION(() => { |
| 193 | + CONSUME3(l.symbols.comma); |
| 194 | + return SUBRULE4(expression, undefined); |
| 195 | + }); |
| 196 | + const close = CONSUME(l.symbols.RParen); |
| 197 | + return ACTION(() => C.factory.expressionOperation( |
| 198 | + operator.image, |
| 199 | + [ arg1, arg2, arg3, ...(arg4 ? [ arg4 ] : []) ], |
| 200 | + C.factory.sourceLocation(operator, close), |
| 201 | + )); |
| 202 | + }, |
| 203 | + }; |
| 204 | +} |
| 205 | + |
| 206 | +export function funcGroupGraphPattern<T extends string>(func: TokenType & { name: T }): |
| 207 | +SparqlGrammarRule<Uncapitalize<T>, ExpressionPatternOperation> { |
| 208 | + return { |
| 209 | + name: unCapitalize(func.name), |
| 210 | + impl: ({ ACTION, SUBRULE, CONSUME }) => (C) => { |
| 211 | + const operator = CONSUME(func); |
| 212 | + const group = SUBRULE(groupGraphPattern, undefined); |
| 213 | + return ACTION(() => C.factory.expressionPatternOperation( |
| 214 | + operator.image, |
| 215 | + [ C.factory.deGroupSingle(group)(undefined) ], |
| 216 | + C.factory.sourceLocation(operator, group.loc), |
| 217 | + )); |
| 218 | + }, |
| 219 | + }; |
| 220 | +} |
| 221 | + |
| 222 | +export type RuleDefExpressionAggregatorX<T extends string> = SparqlGrammarRule<T, ExpressionAggregateDefault>; |
| 223 | + |
| 224 | +export function baseAggregateFunc<T extends string>(func: TokenType & { name: T }): |
| 225 | +RuleDefExpressionAggregatorX<Uncapitalize<T>> { |
| 226 | + return { |
| 227 | + name: unCapitalize(func.name), |
| 228 | + impl: ({ ACTION, CONSUME, SUBRULE, OPTION }) => (C) => { |
| 229 | + const operator = CONSUME(func); |
| 230 | + CONSUME(l.symbols.LParen); |
| 231 | + const distinct = OPTION(() => CONSUME(l.distinct)); |
| 232 | + const expr1 = SUBRULE(expression, undefined); |
| 233 | + const close = CONSUME(l.symbols.RParen); |
| 234 | + |
| 235 | + return ACTION(() => C.factory.aggregate( |
| 236 | + operator.image, |
| 237 | + distinct !== undefined, |
| 238 | + expr1, |
| 239 | + undefined, |
| 240 | + C.factory.sourceLocation(operator, close), |
| 241 | + )); |
| 242 | + }, |
| 243 | + }; |
| 244 | +} |
0 commit comments