Coverage report: /development/source/library/org/datagraph/spocq-shard/src/core/encoding/turtle.lisp

KindCoveredAll%
expression0175 0.0
branch014 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: org.datagraph.spocq.implementation; -*-
2
 
3
 (in-package :org.datagraph.spocq.implementation)
4
 
5
 ;;; build turtle parser from sparql pieces
6
 
7
 ;;; see 
8
 ;;;  http://www.w3.org/TR/2014/REC-turtle-20140225/#sec-grammar
9
 ;;;
10
 
11
 (defparameter *turtle-bnf* "
12
 [[1]] turtleDoc ::= statement*
13
 [[2]] statement ::= directive | ( triples '.' )
14
 [[3]] directive ::= prefixID | base | BaseDecl | PrefixDecl
15
 [[4]] prefixID ::= '@prefix' PNAME_NS NAMESPACE_REF '.'
16
 [[5]] base ::= '@base' NAMESPACE_REF '.'
17
 [[5s]] BaseDecl ::= 'BASE' NAMESPACE_REF
18
 [[6s]] PrefixDecl ::= 'PREFIX' PNAME_NS NAMESPACE_REF
19
 [[6]] triples ::= ( subject PropertyListNotEmpty ) | ( BlankNodePropertyList PropertyListNotEmpty? )
20
 //[[7]] PropertyListNotEmpty ::= verb objectList (';' (verb objectList)?)*
21
 [[8]] objectList ::= object (',' object)*
22
 [[9]] verb ::= predicate | 'a'
23
 [[10]] subject ::= iri | BlankNode | collection
24
 [[11]] predicate ::= iri
25
 [[12]] object ::= iri | BlankNode | collection | BlankNodePropertyList | literal
26
 [[13]] literal ::= RDFLiteral | NumericLiteral | BooleanLiteral
27
 [[14]] BlankNodePropertyList ::= '[' PropertyListNotEmpty ']'
28
 [[15]] collection ::= '(' object* ')'
29
 //[16]] NumericLiteral ::= INTEGER | DECIMAL | DOUBLE
30
 [[128s]] RDFLiteral ::= String (LANGTAG | ( '^^' iri ))?
31
 [[133s]] BooleanLiteral ::= 'true' | 'false'
32
 //[17]] String ::= STRING_LITERAL_QUOTE | STRING_LITERAL_SINGLE_QUOTE | STRING_LITERAL_LONG_SINGLE_QUOTE | STRING_LITERAL_LONG_QUOTE
33
 [[135s]] iri ::= IRI_REF | PrefixedName
34
 [[136s]] PrefixedName ::= PNAME_LN | PNAME_NS
35
 [[137s]] BlankNode ::= ( '_:' BLANK_NODE_LABEL) | ANON
36
 
37
 // from sparql
38
 [[72]] PropertyListNotEmpty ::= VerbObjectList ( ';' VerbObjectList? )* 
39
 [[72a]] VerbObjectList ::= verb objectList
40
 [[119]] NumericLiteral ::= NumericLiteralUnsigned | NumericLiteralPositive | NumericLiteralNegative
41
 [[120]] NumericLiteralUnsigned ::= INTEGER | DECIMAL | DOUBLE
42
 [[121]] NumericLiteralPositive ::= INTEGER_POSITIVE | DECIMAL_POSITIVE | DOUBLE_POSITIVE
43
 [[122]] NumericLiteralNegative ::= INTEGER_NEGATIVE | DECIMAL_NEGATIVE | DOUBLE_NEGATIVE
44
 [[124]] String ::= STRING_LITERAL1 | STRING_LITERAL2 | STRING_LITERAL_LONG1 | STRING_LITERAL_LONG2
45
 [[128]] IRI_REF ::= '<' IRI_NAMESTRING? '>'
46
 [[128a]] NAMESPACE_REF ::= '<' IRI_NAMESTRING? '>'
47
 [[152]] ANON ::= '['  ']'
48
 "
49
   "A turtle grammar which re-uses several productions from sparql in order to reuse the respectve constructor logic" )
50
 
51
 (defpackage :org.datagraph.turtle
52
   (:use :common-lisp
53
         :org.datagraph.spocq.implementation
54
         :de.setf.utility)
55
   (:nicknames :odt)
56
   (:import-from :sparql-1-0-4
57
                 "ANON-Constructor"
58
                 "BaseDecl-Constructor"
59
                 "BlankNode-Constructor"
60
                 "BlankNodePropertyList-Constructor"
61
                 "BLANK_NODE_LABEL-Constructor"
62
                 "BooleanLiteral-Constructor"
63
                 "LANGTAG-Constructor"
64
                 "NumericLiteral-Constructor"
65
                 "PrefixDecl-Constructor"
66
                 "IRI_NAMESTRING-Constructor"
67
                 "IRI_REF-Constructor"
68
                 "NAMESPACE_REF-Constructor"
69
                 "PNAME_NS-Constructor"
70
                 "PrefixedName-Constructor"
71
                 "PropertyListNotEmpty-Constructor"
72
                 "RDFLiteral-Constructor"
73
                 "String-Constructor"
74
                 "VerbObjectList-Constructor")
75
   (:import-from :spocq.i
76
                 :*turtle-bnf*
77
                 :IS-IRI_NAMESTRING))
78
 
79
 (defun odt::|turtleDoc-Constructor| (statement*)
80
   (reduce #'append statement* :from-end t))
81
 
82
 (defun odt::|base-Constructor| (IRI_REF)
83
   (sparql-1-0-4::|BaseDecl-Constructor| IRI_REF))
84
 
85
 (defun odt::|collection-Constructor| (object*)
86
   (if object* ; can be empty
87
       (let* ((tail '|rdf|:|nil|)
88
              (triples nil))
89
         (dolist (node object*)
90
           (let ((cell (ecase *nondistinguished-marker-type*
91
                         (:blank-node (cons-blank-node "b"))
92
                         (:variable (cons-variable "TTL")))))
93
             (when (consp node)
94
               (setf triples (append node triples))
95
               (setf node (second (first node))))
96
             (push (list 'spocq.a:|triple| cell '|rdf|:|rest| tail) triples)
97
             (push (list 'spocq.a:|triple| cell '|rdf|:|first| node) triples)
98
             (setf tail cell)))
99
         triples)
100
       '|rdf|:|nil|))
101
 ;;; (parse-turtle "[ <http://host> () ] .")
102
 ;;; (parse-turtle "[ a _:Class ; <http://consequent> [ a _:OtherClass  ; <http://steps> () ] ]."))
103
 
104
 (defun odt::|directive-Constructor| (prefixID base sparqlPrefix sparqlBase)
105
   (or prefixID base sparqlPrefix sparqlBase))
106
 
107
 (defun odt::|iri-Constructor| (IRI_REF PrefixedName)
108
   (or IRI_REF PrefixedName))
109
 
110
 (defun odt::|literal-Constructor| (BooleanLiteral NumericLiteral RDFLiteral)
111
   (or BooleanLiteral NumericLiteral RDFLiteral))
112
 
113
 (defun odt::|object-Constructor| (BlankNode BlankNodePropertyList collection iri literal)
114
   (or BlankNode BlankNodePropertyList collection iri literal))
115
 
116
 (defun odt::|objectList-Constructor| (object*)
117
   (reverse object*))
118
 
119
 (defun odt::|predicate-Constructor| (iri)
120
   iri)
121
 
122
 (defun odt::|prefixID-Constructor| (IRI_REF_NAMESTRING PNAME_NS)
123
   (sparql-1-0-4::|PrefixDecl-Constructor| IRI_REF_NAMESTRING PNAME_NS))
124
 
125
 (defun odt::|subject-Constructor| (BlankNode collection iri)
126
   (or BlankNode collection iri))
127
 
128
 (defun odt::|statement-Constructor| (directive triples)
129
   (declare (ignore directive))
130
   triples)
131
 
132
 (defun odt::|triples-Constructor| (BlankNodePropertyList PropertyListNotEmpty subject)
133
   (typecase subject
134
     (null
135
      (let ((subject-term (second (first BlankNodePropertyList))))
136
        (append BlankNodePropertyList
137
                (loop for plne-element in PropertyListNotEmpty
138
                  collect (list* 'spocq.a:|triple| subject-term plne-element)))))
139
     (cons
140
      ;; given a collection as subject, use the first statements susbject
141
      (let ((subject-term (second (first subject))))
142
        (append subject
143
                (loop for plne-element in PropertyListNotEmpty
144
                  collect (list* 'spocq.a:|triple| subject-term plne-element)))))
145
     (t
146
       (loop for plne-element in PropertyListNotEmpty
147
                  collect (list* 'spocq.a:|triple| subject plne-element)))))
148
 
149
 (defun odt::|verb-Constructor| (predicate)
150
   (or predicate |rdf|:|type|))
151
 
152
 
153
 (defun tokenize-turtle (string &rest args)
154
   (flet ((map-token (token)
155
            (cond ((equalp token "@base") 'SPOCQ.S::|@base|)
156
                   ((equalp token "@prefix") 'SPOCQ.S::|@prefix|)
157
                   ((equalp token "prefix") 'SPOCQ.S::PREFIX)
158
                   ((equalp token "base") 'SPOCQ.S::BASE)
159
                   (t token))))
160
     (let ((sparql-tokens (apply #'tokenize-sparql string args)))
161
       (map-into sparql-tokens #'map-token sparql-tokens))))
162
 
163
 (defun parse-turtle (string &key (start 0) (end (length string)) (start-name 'odt::|turtleDoc|))
164
   (let ((*max-input-index* 0)
165
         (atnp:*atn-term* nil)
166
         (*namespace-bindings* *namespace-bindings*))
167
     (multiple-value-bind (tokens byte-offsets line-offsets)
168
                          (tokenize-turtle string :start start :end end)
169
       (when tokens
170
         (multiple-value-bind (result index success)
171
                              (funcall 'odt::|turtleDoc-Parser| tokens :start-name start-name)
172
           (if success
173
               (values result tokens index)
174
               (flet ((_aref (array index)
175
                        (when (and (integerp index(< index (length array))) (aref array index))))
176
                 (spocq.e::message-syntax-error :expression string
177
                                                :token (_aref tokens *max-input-index*)
178
                                                :byte-offset (_aref byte-offsets *max-input-index*)
179
                                                :line-offset (_aref line-offsets *max-input-index*)))))))))
180
 
181
 
182
 #|
183
 (in-package :odt)
184
 (bnfp:compile-atn-system  spocq.i::*turtle-bnf*
185
                           :execute t :compile nil
186
                           :token-package (find-package :spocq.s)
187
                           :source-package (find-package :odt)
188
                           :source-pathname #p"/development/source/library/org/datagraph/spocq/src/core/encoding/turtle-grammar.lisp"
189
                           :input-function 'sparql-1-0-4::input-reference
190
                           :input-eof-function 'sparql-1-0-4::input-eof?
191
                           :ambiguous t
192
                           :trace nil)
193
 (load (compile-file #p"/development/source/library/org/datagraph/spocq/src/core/encoding/turtle-grammar.lisp"
194
                     :output-file "turtle-grammar.fasl"))
195
 (spocq.i::parse-turtle "
196
 prefix : <http://example.org#>
197
 [ a :Query ;
198
   :name 'Simple Query';
199
   :steps ( [ a :Decode; :location _:query] 
200
            [ a :Bind ]
201
            [ a :Project ] 
202
            [ a :Encode ] 
203
            ) ] .
204
 ")
205
 
206
 (spocq.i::parse-turtle "
207
 prefix : <http://example.org#>
208
 [ a :Decode; :location _:query] .
209
 [ a :Bind ] .
210
 [ a :Project ] .
211
 [ a :Encode ] .
212
 ")
213
 
214
 (spocq.i::parse-turtle "
215
 @base <http://example.org> .
216
 prefix : <http://example.org#>
217
 [ a :Decode; :location _:query] .
218
 [ a :Bind ] .
219
 [ a :Project ] .
220
 [ a :Encode ] .
221
 ")
222
 
223
 (tokenize-sparql "
224
 @base <http://example.org>  .
225
 PREFIX : <http://example.org#>
226
 [ a :Decode; :location _:query] .
227
 [ a :Bind ] .
228
 [ a :Project ] .
229
 [ a :Encode ] .
230
 ")
231
 |#