Coverage report: /development/source/library/org/datagraph/spocq-shard/src/core/encoding/turtle.lisp
| Kind | Covered | All | % |
| expression | 0 | 175 | 0.0 |
| branch | 0 | 14 | 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; -*-
3
(in-package :org.datagraph.spocq.implementation)
5
;;; build turtle parser from sparql pieces
8
;;; http://www.w3.org/TR/2014/REC-turtle-20140225/#sec-grammar
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
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 ::= '[' ']'
49
"A turtle grammar which re-uses several productions from sparql in order to reuse the respectve constructor logic" )
51
(defpackage :org.datagraph.turtle
53
:org.datagraph.spocq.implementation
56
(:import-from :sparql-1-0-4
58
"BaseDecl-Constructor"
59
"BlankNode-Constructor"
60
"BlankNodePropertyList-Constructor"
61
"BLANK_NODE_LABEL-Constructor"
62
"BooleanLiteral-Constructor"
64
"NumericLiteral-Constructor"
65
"PrefixDecl-Constructor"
66
"IRI_NAMESTRING-Constructor"
68
"NAMESPACE_REF-Constructor"
69
"PNAME_NS-Constructor"
70
"PrefixedName-Constructor"
71
"PropertyListNotEmpty-Constructor"
72
"RDFLiteral-Constructor"
74
"VerbObjectList-Constructor")
75
(:import-from :spocq.i
79
(defun odt::|turtleDoc-Constructor| (statement*)
80
(reduce #'append statement* :from-end t))
82
(defun odt::|base-Constructor| (IRI_REF)
83
(sparql-1-0-4::|BaseDecl-Constructor| IRI_REF))
85
(defun odt::|collection-Constructor| (object*)
86
(if object* ; can be empty
87
(let* ((tail '|rdf|:|nil|)
89
(dolist (node object*)
90
(let ((cell (ecase *nondistinguished-marker-type*
91
(:blank-node (cons-blank-node "b"))
92
(:variable (cons-variable "TTL")))))
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)
101
;;; (parse-turtle "[ <http://host> () ] .")
102
;;; (parse-turtle "[ a _:Class ; <http://consequent> [ a _:OtherClass ; <http://steps> () ] ]."))
104
(defun odt::|directive-Constructor| (prefixID base sparqlPrefix sparqlBase)
105
(or prefixID base sparqlPrefix sparqlBase))
107
(defun odt::|iri-Constructor| (IRI_REF PrefixedName)
108
(or IRI_REF PrefixedName))
110
(defun odt::|literal-Constructor| (BooleanLiteral NumericLiteral RDFLiteral)
111
(or BooleanLiteral NumericLiteral RDFLiteral))
113
(defun odt::|object-Constructor| (BlankNode BlankNodePropertyList collection iri literal)
114
(or BlankNode BlankNodePropertyList collection iri literal))
116
(defun odt::|objectList-Constructor| (object*)
119
(defun odt::|predicate-Constructor| (iri)
122
(defun odt::|prefixID-Constructor| (IRI_REF_NAMESTRING PNAME_NS)
123
(sparql-1-0-4::|PrefixDecl-Constructor| IRI_REF_NAMESTRING PNAME_NS))
125
(defun odt::|subject-Constructor| (BlankNode collection iri)
126
(or BlankNode collection iri))
128
(defun odt::|statement-Constructor| (directive triples)
129
(declare (ignore directive))
132
(defun odt::|triples-Constructor| (BlankNodePropertyList PropertyListNotEmpty subject)
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)))))
140
;; given a collection as subject, use the first statements susbject
141
(let ((subject-term (second (first subject))))
143
(loop for plne-element in PropertyListNotEmpty
144
collect (list* 'spocq.a:|triple| subject-term plne-element)))))
146
(loop for plne-element in PropertyListNotEmpty
147
collect (list* 'spocq.a:|triple| subject plne-element)))))
149
(defun odt::|verb-Constructor| (predicate)
150
(or predicate |rdf|:|type|))
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)
160
(let ((sparql-tokens (apply #'tokenize-sparql string args)))
161
(map-into sparql-tokens #'map-token sparql-tokens))))
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)
170
(multiple-value-bind (result index success)
171
(funcall 'odt::|turtleDoc-Parser| tokens :start-name start-name)
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*)))))))))
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?
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#>
198
:name 'Simple Query';
199
:steps ( [ a :Decode; :location _:query]
206
(spocq.i::parse-turtle "
207
prefix : <http://example.org#>
208
[ a :Decode; :location _:query] .
214
(spocq.i::parse-turtle "
215
@base <http://example.org> .
216
prefix : <http://example.org#>
217
[ a :Decode; :location _:query] .
224
@base <http://example.org> .
225
PREFIX : <http://example.org#>
226
[ a :Decode; :location _:query] .