Skip to content

Commit 62b55cb

Browse files
committed
tests responese look fine
1 parent 47b27a8 commit 62b55cb

6 files changed

Lines changed: 45 additions & 35 deletions

File tree

engines/parser-sparql-1-1/test/triplesTemplateParser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { SparqlContext, TripleNesting, Wrap } from '@traqula/rules-sparql-1-1';
1+
import type { BasicGraphPattern, SparqlContext, Wrap } from '@traqula/rules-sparql-1-1';
22
import { TraqulaFactory, completeParseContext, lex as l } from '@traqula/rules-sparql-1-1';
33
import { describe, it } from 'vitest';
44
import { triplesTemplateParserBuilder } from '../lib';
55

66
describe('a SPARQL 1.1 objectlist parser', () => {
77
const F = new TraqulaFactory();
8-
function parse(query: string, context: Partial<SparqlContext>): Wrap<TripleNesting[]> {
8+
function parse(query: string, context: Partial<SparqlContext>): Wrap<BasicGraphPattern> {
99
const parser = triplesTemplateParserBuilder.consumeToParser({
1010
tokenVocabulary: l.sparql11Tokens.build(),
1111
});

packages/core/lib/CoreFactory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export class CoreFactory {
3030
return { sourceLocationType: 'noMaterialize' };
3131
}
3232

33+
/**
34+
* Returns a copy of the argument that is not materialized
35+
*/
36+
public dematerialized<T extends Node>(arg: T): T & { loc: SourceLocationNoMaterialize } {
37+
return { ...arg, loc: this.sourceLocationNoMaterialize() };
38+
}
39+
3340
public isSourceLocation(loc: object): loc is SourceLocation {
3441
return 'sourceLocationType' in loc;
3542
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type Quads = PatternBgp | GraphQuads;
3030
export type GraphQuads = Node & {
3131
type: 'graph';
3232
graph: TermIri | TermVariable;
33-
triples: TripleNesting[];
33+
triples: BasicGraphPattern;
3434
};
3535

3636
// https://www.w3.org/TR/sparql11-query/#rUpdate1
@@ -117,7 +117,7 @@ export type QuerySelect = QueryBase & {
117117
};
118118
export type QueryConstruct = QueryBase & {
119119
queryType: 'construct';
120-
template: TripleNesting[];
120+
template: BasicGraphPattern;
121121
};
122122
export type QueryDescribe = QueryBase & {
123123
queryType: 'describe';
@@ -204,12 +204,13 @@ export type PatternUnion = PatternBase & {
204204
patternType: 'union';
205205
patterns: Pattern[];
206206
};
207+
export type BasicGraphPattern = (TripleNesting | TripleCollection)[];
207208
export type PatternBgp = PatternBase & {
208209
patternType: 'bgp';
209210
/**
210-
* Only the first appearance of a subject and predicate should have a string manifestation
211+
* Only the first appearance of a subject and predicate have a string manifestation
211212
*/
212-
triples: TripleNesting[];
213+
triples: BasicGraphPattern;
213214
};
214215
export type PatternBind = PatternBase & {
215216
patternType: 'bind';

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SourceLocation } from '@traqula/core';
22
import { CoreFactory } from '@traqula/core';
33

44
import type {
5+
BasicGraphPattern,
56
ContextDefinition,
67
ContextDefinitionBaseDecl,
78
ContextDefinitionPrefixDecl,
@@ -249,7 +250,7 @@ export class TraqulaFactory extends CoreFactory {
249250
return x.type === 'query';
250251
}
251252

252-
public patternBgp(triples: TripleNesting[], loc: SourceLocation): PatternBgp {
253+
public patternBgp(triples: BasicGraphPattern, loc: SourceLocation): PatternBgp {
253254
return { type: 'pattern', patternType: 'bgp', triples, loc };
254255
}
255256

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RuleDefReturn, SourceLocation } from '@traqula/core';
22
import * as l from '../../lexer';
33
import type {
4+
BasicGraphPattern,
45
PatternBind,
56
PatternValues,
67
Query,
@@ -11,7 +12,6 @@ import type {
1112
SubSelect,
1213
TermIri,
1314
TermVariable,
14-
TripleNesting,
1515
Wrap,
1616
} from '../../RoundTripTypes';
1717
import type {
@@ -51,11 +51,15 @@ export const query: SparqlRule<'query', Query> = <const> {
5151
const values = SUBRULE(valuesClause, undefined);
5252

5353
return ACTION(() => (<Query>{
54-
...prologueValues,
54+
context: prologueValues,
5555
...queryType,
5656
type: 'query',
5757
...(values && { values }),
58-
loc: C.factory.sourceLocation(prologueValues[0].loc, queryType.loc, ...(values ? [ values.loc ] : [])),
58+
loc: C.factory.sourceLocation(
59+
...(prologueValues.at(0) ? [ prologueValues.at(0)!.loc ] : []),
60+
queryType.loc,
61+
...(values ? [ values.loc ] : []),
62+
),
5963
}));
6064
},
6165
gImpl: () => () => '',
@@ -343,7 +347,7 @@ export const valuesClause: SparqlRule<'valuesClause', PatternValues | undefined>
343347
/**
344348
* [[73]](https://www.w3.org/TR/sparql11-query/#ConstructTemplate)
345349
*/
346-
export const constructTemplate: SparqlRule<'constructTemplate', Wrap<TripleNesting[]>> = <const> {
350+
export const constructTemplate: SparqlRule<'constructTemplate', Wrap<BasicGraphPattern>> = <const> {
347351
name: 'constructTemplate',
348352
impl: ({ ACTION, SUBRULE1, CONSUME, OPTION }) => (C) => {
349353
const open = CONSUME(l.symbols.LCurly);

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IToken } from 'chevrotain';
22
import { CommonIRIs } from '../grammar-helpers/utils';
33
import * as l from '../lexer';
44
import type {
5+
BasicGraphPattern,
56
GraphNode,
67
Path,
78
PatternBgp,
@@ -20,10 +21,10 @@ import type {
2021
import { var_, varOrTerm, verb } from './general';
2122
import { path } from './propertyPaths';
2223

23-
function triplesDotSeperated(triplesSameSubjectSubrule: SparqlGrammarRule<string, TripleNesting[]>):
24-
SparqlGrammarRule<string, Wrap<TripleNesting[]>>['impl'] {
24+
function triplesDotSeperated(triplesSameSubjectSubrule: SparqlGrammarRule<string, BasicGraphPattern>):
25+
SparqlGrammarRule<string, Wrap<BasicGraphPattern>>['impl'] {
2526
return ({ ACTION, AT_LEAST_ONE, SUBRULE, CONSUME, OPTION }) => (C) => {
26-
const triples: TripleNesting[] = [];
27+
const triples: BasicGraphPattern = [];
2728

2829
let parsedDot = true;
2930
let dotToken: undefined | IToken;
@@ -57,36 +58,32 @@ export const triplesBlock: SparqlRule<'triplesBlock', PatternBgp> = <const>{
5758
const triples = triplesDotSeperated(triplesSameSubjectPath)(implArgs)(C, undefined);
5859
return implArgs.ACTION(() => C.factory.patternBgp(triples.val, C.factory.sourceLocation(triples)));
5960
},
60-
gImpl: ({ SUBRULE, PRINT_WORD }) => (ast, { factory: F }) => {
61-
for (const triple of ast.triples) {
62-
SUBRULE(graphNode, triple.subject, undefined);
63-
if (F.isTerm(triple.predicate) && F.isTermVariable(triple.predicate)) {
64-
SUBRULE(var_, triple.predicate, undefined);
65-
} else {
66-
SUBRULE(path, triple.predicate, undefined);
67-
}
68-
SUBRULE(graphNode, triple.object, undefined);
69-
70-
if (!ast.loc) {
71-
PRINT_WORD('.');
72-
}
73-
}
74-
},
61+
gImpl: () => () => {},
7562
};
7663

7764
/**
7865
* [[75]](https://www.w3.org/TR/sparql11-query/#rTriplesSameSubject)
7966
* [[81]](https://www.w3.org/TR/sparql11-query/#rTriplesSameSubjectPath)
8067
* CONTRACT: triples generated from the subject come first, then comes the main triple,
81-
* and then come the triples from the object
68+
* and then come the triples from the object. Only the first occurrence of a term has `SourceLocationType = source`
8269
*/
83-
function triplesSameSubjectImpl<T extends string>(name: T, allowPaths: boolean): SparqlGrammarRule<T, TripleNesting[]> {
70+
function triplesSameSubjectImpl<T extends string>(name: T, allowPaths: boolean):
71+
SparqlGrammarRule<T, BasicGraphPattern> {
8472
return <const> {
8573
name,
86-
impl: ({ ACTION, SUBRULE, OR }) => C => OR<TripleNesting[]>([
74+
impl: ({ ACTION, SUBRULE, OR }) => C => OR<BasicGraphPattern>([
8775
{ ALT: () => {
8876
const subject = SUBRULE(varOrTerm, undefined);
89-
return SUBRULE(allowPaths ? propertyListPathNotEmpty : propertyListNotEmpty, { subject });
77+
const res = SUBRULE(
78+
allowPaths ? propertyListPathNotEmpty : propertyListNotEmpty,
79+
{ subject: ACTION(() => C.factory.dematerialized(subject)) },
80+
);
81+
return ACTION(() => {
82+
if (res.length > 0) {
83+
res[0].subject = subject;
84+
}
85+
return res;
86+
});
9087
} },
9188
{ ALT: () => {
9289
const subjectNode = SUBRULE(allowPaths ? triplesNodePath : triplesNode, undefined);
@@ -95,7 +92,7 @@ function triplesSameSubjectImpl<T extends string>(name: T, allowPaths: boolean):
9592
{ subject: ACTION(() => C.factory.graphNodeIdentifier(subjectNode)) },
9693
);
9794
return ACTION(() => [
98-
...subjectNode.triples,
95+
subjectNode,
9996
...restNode,
10097
]);
10198
} },
@@ -108,7 +105,7 @@ export const triplesSameSubjectPath = triplesSameSubjectImpl('triplesSameSubject
108105
/**
109106
* [[52]](https://www.w3.org/TR/sparql11-query/#rTriplesTemplate)
110107
*/
111-
export const triplesTemplate: SparqlGrammarRule<'triplesTemplate', Wrap<TripleNesting[]>> = <const> {
108+
export const triplesTemplate: SparqlGrammarRule<'triplesTemplate', Wrap<BasicGraphPattern>> = <const> {
112109
name: 'triplesTemplate',
113110
impl: triplesDotSeperated(triplesSameSubject),
114111
};

0 commit comments

Comments
 (0)