Coverage report: /development/source/library/org/datagraph/spocq-shard/src/extensions/graphql/ssf-graphql.lisp
| Kind | Covered | All | % |
| expression | 0 | 427 | 0.0 |
| branch | 0 | 22 | 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.sparql-1-0-4; -*-
3
;;; (load "patches/ssf-graphql.lisp")
5
(in-package :org.datagraph.spocq.implementation)
7
(defpackage :org.datagraph.spocq.graphql
9
(:import-from :common-lisp nil)
10
(:export common-lisp:nil))
12
(defpackage :org.datagraph.spocq.graphql.s
14
(:nicknames :graphql.s))
16
(defpackage :org.datagraph.spocq.graphql-0-0-1
18
(:nicknames :graphql-0-0-1))
20
(eval-when (:compile-toplevel :load-toplevel :execute)
21
(loop for name in (append (mapcar #'string (coerce ",:={}()[]@$!" 'list))
33
do (intern name :org.datagraph.spocq.graphql)
34
do (export (intern name :org.datagraph.spocq.graphql.s)
35
:org.datagraph.spocq.graphql.s)))
37
(let ((graphql-id-pattern (cl-ppcre:parse-string "[a-zA-Z]\\w*")))
38
(setf (cl-ppcre:parse-tree-synonym 'graphql-id-pattern) graphql-id-pattern))
40
(defparameter *graphql-scanner*
41
(cl-ppcre:create-scanner '(:ALTERNATION
42
(:register (:ALTERNATION (:CHAR-CLASS #\, #\: #\= #\{ #\} #\( #\) #\[ #\] #\@ #\$ #\! )
43
(:sequence #\" (:GREEDY-REPETITION 0 NIL
44
(:alternation (:sequence #\\ #\")
45
(:INVERTED-CHAR-CLASS #\")))
61
(:GREEDY-REPETITION 1 NIL :WHITESPACE-CHAR-CLASS))))
63
(defun graphql-0-0-1::is-number (x)
66
(defun graphql-0-0-1::is-string (x)
69
(defun graphql-0-0-1::is-id (x)
70
(and (stringp x) (> (length x) 0)
71
(alpha-char-p (char x 0))
72
(loop for i from 1 below (length x) unless (graphic-char-p (char x i)) do (return nil)
75
(defun tokenize-graphql (graphql-string &key (start 0) (end (length graphql-string)))
76
"deconstructuct the string into a sequence of grapahq.s tokens and atomic term values."
78
(cl-ppcre:do-matches-as-strings (match *graphql-scanner* graphql-string (make-array (length tokens) :initial-contents (nreverse tokens))
79
:start start :end end)
80
(let* ((initial (char match 0))
81
(token (cond ((eql initial #\")
82
(subseq match 1 (1- (length match))))
83
((find initial "+-0123456789")
84
(read-from-string match))
85
((find-symbol match :org.datagraph.spocq.graphql))
86
((every #'cl-ppcre::whitespacep match)
91
(warn "tokenize-graphql: invalid token: ~s" match)
93
(when token (push token tokens))))))
96
(defun graphql-0-0-1::|boolean_value-Constructor| (value)
99
(defun graphql-0-0-1::|NUMBER-Constructor| (number)
100
(assert (numberp number) () "|NUMBER-Constructor|: invalid number: ~s." number)
103
(defun graphql-0-0-1::|ID-Constructor| (id)
107
(defun graphql-0-0-1::|STRING-Constructor| (string)
108
(assert (stringp string) () "|STRING-Constructor|: invalid string: ~s." string)
113
(defun graphql-0-0-1::|graphqlFile-Constructor| (definition*)
114
(cons 'graphql::|graphqlFile| definition*))
116
;;(defun graphql-0-0-1::|keywords-Constructor| ('type' | 'interface' | 'input' | 'query' | 'mutation')
118
(defun graphql-0-0-1::|definition-Constructor| (fragment_definition operation_definition type_definition)
119
(list 'graphql::|definition| type_definition operation_definition fragment_definition))
121
(defun graphql-0-0-1::|type_definition-Constructor| (field_definitions interfaces? named_type type_category)
122
(list 'graphql::|type_definition| type_category named_type interfaces? field_definitions))
124
(defun graphql-0-0-1::|type_category-Constructor| (category)
125
(list 'graphql::|type_category| category))
127
(defun graphql-0-0-1::|interfaces-Constructor| (named_type)
128
(list 'graphql::|interfaces| named_type))
130
(defun graphql-0-0-1::|field_definition_list-Constructor| (field_definition field_definition_list)
131
(cons field_definition field_definition_list))
133
(defun graphql-0-0-1::|field_definitions-Constructor| (field_definition_list)
134
(cons 'graphql::|field_definitions| field_definition_list))
136
(defun graphql-0-0-1::|field_definition-Constructor| (argument_definitions? field_name type_ref)
137
(list 'graphql::|field_definition| field_name argument_definitions? type_ref))
139
(defun graphql-0-0-1::|argument_definition_list-Constructor| (argument_definition argument_definition_list)
140
(cons argument_definition argument_definition_list))
142
(defun graphql-0-0-1::|argument_definitions-Constructor| (argument_definition_list)
143
(cons 'graphql::|argument_definitions| argument_definition_list))
145
(defun graphql-0-0-1::|argument_definition-Constructor| (argument_name type_ref)
146
(list 'graphql::|argument_definition| argument_name type_ref))
148
(defun graphql-0-0-1::|operation_definition-Constructor| (directive? operation_name? operation_type selection_set variable_definitions?)
149
(if (or operation_type operation_name? variable_definitions? directive? selection_set)
150
(list 'graphql::|operation_definition| operation_type operation_name? variable_definitions? directive? selection_set)
151
(list 'graphql::|operation_definition| selection_set)))
153
(defun graphql-0-0-1::|operation_name-Constructor| (ID)
154
(list 'graphql::|operation_name| ID))
156
(defun graphql-0-0-1::|operation_type-Constructor| (item)
157
(list 'graphql::|operation_type| item))
159
(defun graphql-0-0-1::|selection_set_list-Constructor| (selection selection_set_list)
160
(cons selection selection_set_list))
162
(defun graphql-0-0-1::|selection_set-Constructor| (selection_set_list)
163
(cons 'graphql::|selection_set| selection_set_list))
165
(defun graphql-0-0-1::|selection-Constructor| (field fragment_spread inline_fragment)
166
(list 'graphql::|selection| (or field fragment_spread inline_fragment)))
168
(defun graphql-0-0-1::|field-Constructor| (alias? arguments? directives? field_name selection_set?)
169
(list 'graphql::|field| alias? field_name arguments? directives? selection_set?))
171
(defun graphql-0-0-1::|field_name-Constructor| (name)
172
(list 'graphql::|field_name| name))
174
(defun graphql-0-0-1::|arguments-Constructor| (argument_list)
175
(cons 'graphql::|arguments-Constructor| argument_list))
177
(defun graphql-0-0-1::|argument_list-Constructor| (argument argument_list)
178
(cons argument argument_list))
180
(defun graphql-0-0-1::|argument-Constructor| (argument_name argument_value)
181
(list 'graphql::|argument| argument_name argument_value))
183
(defun graphql-0-0-1::|argument_name-Constructor| (item)
184
(list 'graphql::|argument_name| item))
186
(defun graphql-0-0-1::|argument_value-Constructor| (value)
187
(list 'graphql::|argument_value| value))
189
(defun graphql-0-0-1::|alias-Constructor| (alias_name)
190
(list 'graphql::|alias| alias_name))
192
(defun graphql-0-0-1::|alias_name-Constructor| (ID)
193
(list 'graphql::|alias_name| ID))
195
(defun graphql-0-0-1::|fragment_spread-Constructor| (directives? fragment_name)
196
(list 'graphql::|fragment_spread| fragment_name directives?))
198
(defun graphql-0-0-1::|fragment_definition-Constructor| (directives? fragment_name? type_condition selection_set)
199
(list 'graphql::|fragment_definition| fragment_name? type_condition directives? selection_set))
201
(defun graphql-0-0-1::|fragment_name-Constructor| (ID)
202
(list 'graphql::|fragment_name| ID))
204
(defun graphql-0-0-1::|inline_fragment-Constructor| (directives? selection_set type_condition?)
205
(list 'graphql::|inline_fragment| type_condition? directives? selection_set))
207
(defun graphql-0-0-1::|type_condition-Constructor| (named_type)
208
(list 'graphql::|type_condition| named_type))
210
(defun graphql-0-0-1::|value-Constructor| (variable NUMBER string_value boolean_value enum_value list_value object_value)
211
(or boolean_value enum_value list_value NUMBER object_value variable string_value))
213
(defun graphql-0-0-1::|string_value-Constructor| (STRING)
216
;; (defun graphql-0-0-1::|boolean_value-Constructor| ('true' | 'false')
218
(defun graphql-0-0-1::|enum_value-Constructor| (STRING)
219
(list 'graphql::|enum_value| STRING))
221
(defun graphql-0-0-1::|list_value-Constructor| (value_list)
222
(list 'graphql::|list_value| value_list))
224
(defun graphql-0-0-1::|list_value_list-Constructor| (list_value_list value)
225
(cons value list_value_list))
227
(defun graphql-0-0-1::|value*-Constructor| (value value*)
230
(defun graphql-0-0-1::|object_value-Constructor| (object_field*)
231
(list* 'graphql::|object_value| object_field*))
233
(defun graphql-0-0-1::|object_field-Constructor| (field_name value)
234
(list 'graphql::|object_field| field_name value))
236
(defun graphql-0-0-1::|variable_definition_list-Constructor| (variable_definition variable_definition_list)
237
(cons variable_definition variable_definition_list))
239
(defun graphql-0-0-1::|variable_definitions-Constructor| (variable_definition_list)
240
(cons 'graphql::|variable_definitions| variable_definition_list))
242
(defun graphql-0-0-1::|variable_definition-Constructor| (default_value? type_ref variable)
243
(list 'graphql::|variable_definition| variable type_ref default_value?))
245
(defun graphql-0-0-1::|variable-Constructor| (var_name)
246
(list 'graphql::|variable| var_name))
248
(defun graphql-0-0-1::|var_name-Constructor| (ID keywords)
249
(list 'graphql::|var_name| keywords ID))
251
(defun graphql-0-0-1::|default_value-Constructor| (value)
252
(list 'graphql::|default_value| value))
254
(defun graphql-0-0-1::|type_ref-Constructor| (list_type named_type)
255
(list 'graphql::|type_ref| (or list_type named_type)))
257
(defun graphql-0-0-1::|list_type-Constructor| (type_ref)
258
(list 'graphql::|list_type| type_ref))
260
(defun graphql-0-0-1::|named_type-Constructor| (ID)
261
(list 'graphql::|named_type| ID))
263
(defun graphql-0-0-1::|directives-Constructor| (directive*)
264
(list 'graphql::|directives| directive*))
266
(defun graphql-0-0-1::|directive-Constructor| (arguments? directive_name)
267
(list 'graphql::|directive| directive_name arguments?))
269
(defun graphql-0-0-1::|directive_name-Constructor| (ID)
270
(list 'graphql::|directive_name| ID))
275
(defparameter graphql::*max-input-index* 0)
277
(defun graphql-0-0-1::input-reference (index)
278
(when (< index (length atnp::*ATN-INPUT))
279
(setf graphql::*max-input-index* (max graphql::*max-input-index* index))
280
(aref atnp::*ATN-INPUT index)))
282
(defun graphql-0-0-1::input-eof? (index)
283
(>= index (length atnp::*ATN-INPUT)))
286
(defgeneric parse-graphql (source &key start end start-name accept)
287
(:method ((location spocq:iri) &rest args &key (accept "application/graphql") start end start-name)
288
(declare (ignore start end start-name))
289
(declare (dynamic-extent args))
290
(apply #'parse-graphql (with-http-request-stream (request-stream response-stream location :accept accept)
291
(declare (ignore request-stream))
292
(read-stream response-stream))
295
(:method ((string string) &key (start 0) (end (length string)) (start-name 'graphql-0-0-1::|graphqlFile|) accept)
296
(declare (ignore accept))
297
(let ((tokens (remove nil (tokenize-graphql string :start start :end end)))
298
(graphql::*max-input-index* 0))
300
(multiple-value-bind (result index success)
301
(funcall 'graphql-0-0-1::|graphqlFile-Parser| tokens :start-name start-name)
303
(values result tokens index)
304
(flet ((_aref (array index)
305
(when (and (integerp index) (< -1 index (length array))) (aref array index))))
306
(spocq.e::message-syntax-error :expression string
307
:token (loop for i from (max 0 (- graphql::*max-input-index* 5))
308
below (+ graphql::*max-input-index* 5)
309
collect (_aref tokens i))
310
:token-offset graphql::*max-input-index*)))))))
312
(:method ((location pathname) &rest args)
313
(declare (dynamic-extent args))
314
(apply #'parse-graphql (read-file location) args)))
316
(defmethod receive-message ((stream stream) (content-type mime:application/graphql) &rest args)
317
"transform the stream into a string and delegate.
318
if the stream is empty, cause an eof to be signaled"
319
(apply #'receive-message (read-stream stream :eof-p t) content-type args))
321
(defmethod receive-message ((message string) (content-type mime:application/graphql) &rest args)
322
"Parse a graphql message and return
324
- a list with the parsed sse and the graphql text string as query initialization arguments."
325
(declare (ignore args))
326
(let ((sse (parse-graphql message)))
328
(list :query-expression message
329
:sse-expression sse))))
334
;; there are three packages
335
;; graphql : the abstract algebra package, which contains the operator names for the expressions into which the surface syntax is trsnslated
336
;; graphql-001 : the source code package for generated constructors
337
;; graphql.s : the "surface syntax" token package, in which lexical tokens are present
339
;; now a packaged macro : (set-macro-character #\{ nil t)
340
(bnfp:compile-atn-system (read-file "/development/source/library/org/datagraph/spocq/src/extensions/graphql/GraphQL.bnf")
341
:execute t :compile nil
342
:token-package (find-package :org.datagraph.spocq.graphql)
343
:source-package (find-package :org.datagraph.spocq.graphql-0-0-1)
344
:source-pathname #p"/development/source/library/org/datagraph/spocq/src/extensions/graphql/graphql-grammar.lisp"
345
:input-function 'graphql-0-0-1::input-reference
346
:input-eof-function 'graphql-0-0-1::input-eof?
350
;;; to compile the parser state machine manually
351
(load (compile-file #p"LIBRARY:org;datagraph;spocq;src;core;encoding;graphql.lisp"))