Skip to content

Commit 9c71565

Browse files
committed
semi compiling parser
1 parent ed3c383 commit 9c71565

11 files changed

Lines changed: 811 additions & 1102 deletions

File tree

packages/core/lib/CoreFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from './nodeTypings';
1212

1313
export class CoreFactory {
14-
public sourceLocation(...elements: (IToken | SourceLocation)[]): SourceLocation {
14+
public sourceLocation(...elements: (Pick<IToken, 'startOffset' | 'endOffset'> | SourceLocation)[]): SourceLocation {
1515
const filtered =
1616
elements.filter(element => !this.isSourceLocation(element) || this.isSourceLocationSource(element));
1717
if (filtered.length === 0) {

packages/rules-sparql-1-1/lib/RoundTripTypes.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export type UpdateOperation =
9393
export type Update = Node & {
9494
type: 'update';
9595
updates: {
96-
operation: UpdateOperation;
96+
operation?: UpdateOperation;
9797
context: ContextDefinition[];
9898
}[];
9999
};
@@ -220,6 +220,9 @@ export type PatternService = PatternBase & {
220220
silent: boolean;
221221
patterns: Pattern[];
222222
};
223+
/**
224+
* A single list of assignments maps the variable identifier to the value
225+
*/
223226
export type ValuePatternRow = Record<string, TermIri | TermBlank | TermLiteral | undefined>;
224227
export type PatternValues = PatternBase & {
225228
patternType: 'values';
@@ -247,7 +250,7 @@ export type SolutionModifiers = {
247250
limitOffset?: SolutionModifierLimitOffset;
248251
};
249252
export type SolutionModifierBase = Node & { type: 'solutionModifier'; modifierType: string };
250-
export type SolutionModifierGroupBind = {
253+
export type SolutionModifierGroupBind = Pick<Node, 'loc'> & {
251254
variable: TermVariable;
252255
value: Expression;
253256
};
@@ -261,7 +264,7 @@ export type SolutionModifierHaving = SolutionModifierBase & {
261264
};
262265
export type Ordering =
263266
| Expression
264-
| ({ descending: boolean; expression: Expression });
267+
| ({ descending: boolean; expression: Expression } & Pick<Node, 'loc'>);
265268
export type SolutionModifierOrder = SolutionModifierBase & {
266269
modifierType: 'order';
267270
orderDefs: Ordering[];
@@ -299,6 +302,11 @@ export type ExpressionOperation = ExpressionBase & {
299302
args: Expression[];
300303
};
301304

305+
export type ExpressionBracketted = ExpressionBase & {
306+
expressionType: 'bracketted';
307+
expression: Expression;
308+
};
309+
302310
export type ExpressionPatternOperation = ExpressionBase & {
303311
expressionType: 'patternOperation';
304312
operator: string;
@@ -315,6 +323,7 @@ export type ExpressionFunctionCall = ExpressionBase & {
315323

316324
export type Expression =
317325
| ExpressionOperation
326+
| ExpressionBracketted
318327
| ExpressionPatternOperation
319328
| ExpressionFunctionCall
320329
| ExpressionAggregate

packages/rules-sparql-1-1/lib/expressionHelpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ RuleDefExpressionFunctionX<Uncapitalize<T>, [Expression, Expression] | [Expressi
155155
const close = CONSUME(l.symbols.RParen);
156156
return ACTION(() => C.factory.expressionOperation(
157157
operator.image,
158-
[ arg1, arg2, ...(arg3 ? [ arg3 ] : []) ],
158+
arg3 ? <const> [ arg1, arg2, arg3 ] : <const> [ arg1, arg2 ],
159159
C.factory.sourceLocation(operator, close),
160160
));
161161
},
@@ -196,7 +196,7 @@ RuleDefExpressionFunctionX<
196196
const close = CONSUME(l.symbols.RParen);
197197
return ACTION(() => C.factory.expressionOperation(
198198
operator.image,
199-
[ arg1, arg2, arg3, ...(arg4 ? [ arg4 ] : []) ],
199+
arg4 ? <const> [ arg1, arg2, arg3, arg4 ] : <const> [ arg1, arg2, arg3 ],
200200
C.factory.sourceLocation(operator, close),
201201
));
202202
},
@@ -212,7 +212,7 @@ SparqlGrammarRule<Uncapitalize<T>, ExpressionPatternOperation> {
212212
const group = SUBRULE(groupGraphPattern, undefined);
213213
return ACTION(() => C.factory.expressionPatternOperation(
214214
operator.image,
215-
[ C.factory.deGroupSingle(group)(undefined) ],
215+
[ C.factory.deGroupSingle(group) ],
216216
C.factory.sourceLocation(operator, group.loc),
217217
));
218218
},

packages/rules-sparql-1-1/lib/factory.ts

Lines changed: 151 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import type {
1616
ExpressionPatternOperation,
1717
GraphNode,
1818
GraphRef,
19+
GraphRefAll,
1920
GraphRefDefault,
21+
GraphRefNamed,
2022
GraphRefSpecific,
2123
Ordering,
2224
Path,
@@ -28,10 +30,13 @@ import type {
2830
PatternBgp,
2931
PatternBind,
3032
PatternFilter,
33+
PatternGraph,
3134
PatternGroup,
3235
PatternMinus,
36+
PatternOptional,
3337
PatternService,
3438
PatternUnion,
39+
PatternValues,
3540
PropertyPathChain,
3641
Quads,
3742
Query,
@@ -62,6 +67,7 @@ import type {
6267
UpdateOperationLoad,
6368
UpdateOperationModify,
6469
UpdateOperationMove,
70+
ValuePatternRow,
6571
Wildcard,
6672
} from './RoundTripTypes';
6773

@@ -78,7 +84,7 @@ export class TraqulaFactory extends CoreFactory {
7884
};
7985
}
8086

81-
public baseDecl(loc: SourceLocation, img1: string, value: TermIriFull): ContextDefinitionBaseDecl {
87+
public baseDecl(loc: SourceLocation, value: TermIriFull): ContextDefinitionBaseDecl {
8288
return {
8389
type: 'contextDef',
8490
contextType: 'base',
@@ -186,6 +192,22 @@ export class TraqulaFactory extends CoreFactory {
186192
};
187193
}
188194

195+
public expressionFunctionCall<Args extends Expression[]>(
196+
functionOp: TermIri,
197+
args: Args,
198+
distinct: boolean,
199+
loc: SourceLocation,
200+
): ExpressionFunctionCall & { args: Args } {
201+
return {
202+
type: 'expression',
203+
expressionType: 'functionCall',
204+
function: functionOp,
205+
args,
206+
distinct,
207+
loc,
208+
};
209+
}
210+
189211
public expressionPatternOperation<Args extends Pattern[]>(
190212
operator: string,
191213
args: Args,
@@ -231,6 +253,22 @@ export class TraqulaFactory extends CoreFactory {
231253
return { type: 'pattern', patternType: 'bgp', triples, loc };
232254
}
233255

256+
public patternGroup(patterns: Pattern[], loc: SourceLocation): PatternGroup {
257+
return { type: 'pattern', patternType: 'group', patterns, loc };
258+
}
259+
260+
public patternGraph(name: TermIri | TermVariable, patterns: Pattern[], loc: SourceLocation): PatternGraph {
261+
return { type: 'pattern', patternType: 'graph', name, patterns, loc };
262+
}
263+
264+
public patternOptional(patterns: Pattern[], loc: SourceLocation): PatternOptional {
265+
return { type: 'pattern', patternType: 'optional', patterns, loc };
266+
}
267+
268+
public patternValues(values: ValuePatternRow[], loc: SourceLocation): PatternValues {
269+
return { type: 'pattern', patternType: 'values', values, loc };
270+
}
271+
234272
public patternFilter(expression: Expression, loc: SourceLocation): PatternFilter {
235273
return {
236274
type: 'pattern',
@@ -288,6 +326,39 @@ export class TraqulaFactory extends CoreFactory {
288326
};
289327
}
290328

329+
public graphRefSpecific(graph: TermIri, loc: SourceLocation): GraphRefSpecific {
330+
return {
331+
type: 'graphRef',
332+
graphRefType: 'specific',
333+
graph,
334+
loc,
335+
};
336+
}
337+
338+
public graphRefDefault(loc: SourceLocation): GraphRefDefault {
339+
return {
340+
type: 'graphRef',
341+
graphRefType: 'default',
342+
loc,
343+
};
344+
}
345+
346+
public graphRefNamed(loc: SourceLocation): GraphRefNamed {
347+
return {
348+
type: 'graphRef',
349+
graphRefType: 'named',
350+
loc,
351+
};
352+
}
353+
354+
public graphRefAll(loc: SourceLocation): GraphRefAll {
355+
return {
356+
type: 'graphRef',
357+
graphRefType: 'all',
358+
loc,
359+
};
360+
}
361+
291362
public deGroupSingle(group: PatternGroup & Pattern): Pattern {
292363
if (group.patterns.length === 1) {
293364
return group.patterns[0];
@@ -504,12 +575,22 @@ export class TraqulaFactory extends CoreFactory {
504575
};
505576
}
506577

507-
private updateOperationClearDrop<T extends 'clear' | 'drop' | 'create', Dest extends GraphRef>(
508-
operationType: T,
578+
public updateOperationClearDrop(operationType: 'clear', silent: boolean, destination: GraphRef, loc: SourceLocation):
579+
UpdateOperationClear;
580+
public updateOperationClearDrop(operationType: 'drop', silent: boolean, destination: GraphRef, loc: SourceLocation):
581+
UpdateOperationDrop;
582+
public updateOperationClearDrop(
583+
operationType: 'clear' | 'drop',
509584
silent: boolean,
510-
destination: Dest,
585+
destination: GraphRef,
586+
loc: SourceLocation
587+
): UpdateOperationClear | UpdateOperationDrop;
588+
public updateOperationClearDrop(
589+
operationType: 'clear' | 'drop',
590+
silent: boolean,
591+
destination: GraphRef,
511592
loc: SourceLocation,
512-
): Omit<UpdateOperationDrop, 'destination' | 'operationType'> & { operationType: T; destination: Dest } {
593+
): UpdateOperationClear | UpdateOperationDrop {
513594
return {
514595
type: 'updateOperation',
515596
operationType,
@@ -540,16 +621,50 @@ export class TraqulaFactory extends CoreFactory {
540621
silent: boolean,
541622
loc: SourceLocation,
542623
): UpdateOperationCreate {
543-
return this.updateOperationClearDrop('create', silent, destination, loc);
624+
return {
625+
type: 'updateOperation',
626+
operationType: 'create',
627+
silent,
628+
destination,
629+
loc,
630+
};
544631
}
545632

546-
private updateOperationAddMoveCopy<T extends 'add' | 'move' | 'copy'>(
547-
operationType: T,
633+
public updateOperationAddMoveCopy(
634+
operationType: 'add',
635+
source: GraphRefDefault | GraphRefSpecific,
636+
destination: GraphRefDefault | GraphRefSpecific,
637+
silent: boolean,
638+
loc: SourceLocation,
639+
): UpdateOperationAdd;
640+
public updateOperationAddMoveCopy(
641+
operationType: 'move',
642+
source: GraphRefDefault | GraphRefSpecific,
643+
destination: GraphRefDefault | GraphRefSpecific,
644+
silent: boolean,
645+
loc: SourceLocation,
646+
): UpdateOperationMove ;
647+
public updateOperationAddMoveCopy(
648+
operationType: 'copy',
649+
source: GraphRefDefault | GraphRefSpecific,
650+
destination: GraphRefDefault | GraphRefSpecific,
651+
silent: boolean,
652+
loc: SourceLocation,
653+
): UpdateOperationCopy;
654+
public updateOperationAddMoveCopy(
655+
operationType: 'add' | 'move' | 'copy',
656+
source: GraphRefDefault | GraphRefSpecific,
657+
destination: GraphRefDefault | GraphRefSpecific,
658+
silent: boolean,
659+
loc: SourceLocation,
660+
): UpdateOperationAdd | UpdateOperationMove | UpdateOperationCopy;
661+
public updateOperationAddMoveCopy(
662+
operationType: 'add' | 'move' | 'copy',
548663
source: GraphRefDefault | GraphRefSpecific,
549664
destination: GraphRefDefault | GraphRefSpecific,
550665
silent: boolean,
551666
loc: SourceLocation,
552-
): Omit<UpdateOperationAdd, 'operationType'> & { operationType: T } {
667+
): UpdateOperationAdd | UpdateOperationMove | UpdateOperationCopy {
553668
return {
554669
type: 'updateOperation',
555670
operationType,
@@ -587,39 +702,55 @@ export class TraqulaFactory extends CoreFactory {
587702
return this.updateOperationAddMoveCopy('copy', source, destination, silent, loc);
588703
}
589704

590-
private updateOperationInsertDeleteDelWhere<T extends 'insertdata' | 'deletedata' | 'deletewhere'>(
591-
operationType: T,
705+
public updateOperationInsDelDataWhere(operationType: 'insertdata', data: Quads[], loc: SourceLocation):
706+
UpdateOperationInsertData;
707+
public updateOperationInsDelDataWhere(operationType: 'deletedata', data: Quads[], loc: SourceLocation):
708+
UpdateOperationDeleteData;
709+
public updateOperationInsDelDataWhere(operationType: 'deletewhere', data: Quads[], loc: SourceLocation):
710+
UpdateOperationDeleteWhere;
711+
public updateOperationInsDelDataWhere(
712+
operationType: 'insertdata' | 'deletedata' | 'deletewhere',
592713
data: Quads[],
593714
loc: SourceLocation,
594-
): Omit<UpdateOperationInsertData, 'operationType'> & { operationType: T } {
595-
return { type: 'updateOperation', operationType, data, loc };
715+
): UpdateOperationInsertData | UpdateOperationDeleteData | UpdateOperationDeleteWhere;
716+
public updateOperationInsDelDataWhere(
717+
operationType: 'insertdata' | 'deletedata' | 'deletewhere',
718+
data: Quads[],
719+
loc: SourceLocation,
720+
): UpdateOperationInsertData | UpdateOperationDeleteData | UpdateOperationDeleteWhere {
721+
return {
722+
type: 'updateOperation',
723+
operationType,
724+
data,
725+
loc,
726+
};
596727
}
597728

598729
public updateOperationInsertData(data: Quads[], loc: SourceLocation): UpdateOperationInsertData {
599-
return this.updateOperationInsertDeleteDelWhere('insertdata', data, loc);
730+
return this.updateOperationInsDelDataWhere('insertdata', data, loc);
600731
}
601732

602733
public updateOperationDeleteData(data: Quads[], loc: SourceLocation): UpdateOperationDeleteData {
603-
return this.updateOperationInsertDeleteDelWhere('deletedata', data, loc);
734+
return this.updateOperationInsDelDataWhere('deletedata', data, loc);
604735
}
605736

606737
public updateOperationDeleteWhere(data: Quads[], loc: SourceLocation): UpdateOperationDeleteWhere {
607-
return this.updateOperationInsertDeleteDelWhere('deletewhere', data, loc);
738+
return this.updateOperationInsDelDataWhere('deletewhere', data, loc);
608739
}
609740

610741
public updateOperationModify(
611742
loc: SourceLocation,
612-
insert: Quads[],
613-
del: Quads[],
743+
insert: Quads[] | undefined,
744+
del: Quads[] | undefined,
614745
where: Pattern[],
615746
from: DatasetClauses,
616747
graph?: TermIri | undefined,
617748
): UpdateOperationModify {
618749
return {
619750
type: 'updateOperation',
620751
operationType: 'modify',
621-
insert,
622-
delete: del,
752+
insert: insert ?? [],
753+
delete: del ?? [],
623754
graph,
624755
where,
625756
from,

0 commit comments

Comments
 (0)