@@ -2,6 +2,7 @@ import type { IToken } from 'chevrotain';
22import { CommonIRIs } from '../grammar-helpers/utils' ;
33import * as l from '../lexer' ;
44import type {
5+ BasicGraphPattern ,
56 GraphNode ,
67 Path ,
78 PatternBgp ,
@@ -20,10 +21,10 @@ import type {
2021import { var_ , varOrTerm , verb } from './general' ;
2122import { 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