-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp-toml.lisp
440 lines (377 loc) · 12 KB
/
pp-toml.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; paul's parser for tom's own minimal language
;;;;
;;;; (PARSE-TOML "string") is the entry point.
;;;;
;;;; Other function definitions are exported for the purposes of
;;;; Better Modular Testing.
;;;;
;;;; Uses esrap for the parsing heavy lifting. Some errors may look esrapy.
;;;;
;;;; (C) 2013-2021 Paul Nathan & contributors
;;;; License: LLGPL (http://opensource.franz.com/preamble.html)
(defpackage :pp-toml
(:use
:common-lisp
:esrap
:generic-comparability)
(:export
#:parse-toml
#:parse-string
#:strip-comments
:value
:datetime
:preamble
:keyvalue
:keygroup))
(in-package :pp-toml)
(defparameter *comment-scanner*
(cl-ppcre:create-scanner
;; initial regex kindly contributed by [email protected]
"^(([^#\"]|\"(([^\\\"]*\\.)*[^\\\"]*\"))+)?#(.*)$"
:single-line-mode nil
:multi-line-mode t)
"Scanner for # comments. Handles \"#\" strings")
(defun strip-comments (string)
"Strips the comments from the string"
(cl-ppcre:regex-replace-all
*comment-scanner*
string
"\\1"))
(defun not-doublequote (char)
(not (eql #\" char)))
(defun not-bracket (char)
(not (eql #\] char)))
(defun not-integer (string)
(when (find-if-not #'digit-char-p string)
t))
(defun not-special-case (char)
(not (member char
'(#\[
#\]
#\"
#\Space
#\Newline
#\tab
#\=
#\.))))
(defun transliterate-unicode (string)
(cl-ppcre:regex-replace-all
"\\\\u(\\d{4})" string
#'(lambda
;; Interface expected by regexp-replace-all
(target-string start end match-start match-end reg-starts reg-ends)
(declare (ignore start end match-start match-end))
(format t "~a; ~a~&" reg-starts reg-ends)
(let ((matched-code
(subseq target-string
(aref reg-starts 0)
(aref reg-ends 0))))
;; convert the char into a string
(string
;;convert the integer to the code character
(code-char
;; convert the string to an integer
(parse-integer matched-code)))))))
(defun transliterate-to-specials (string)
"Transliterate escape characters into Common Lisp characters"
(flet ((tr (target repl)
(setf string (cl-ppcre:regex-replace-all
(cl-ppcre:quote-meta-chars
target)
string
(string repl)))))
;; alpha sorted
(tr "\\b" #\Backspace)
;; Not all Lisps have the #\Form character
#+#.(cl:if (cl:ignore-errors (cl:read-from-string "#\\Form"))
'(and)
'(or))
(tr "\\f" #\Form)
(tr "\\n" #\Linefeed)
(tr "\\r" #\Return)
(tr "\\t" #\Tab)
;; todo: determine why this is commented out
;; (tr "\\\"" #\")
(tr "\/" #\/)
(tr "\\\\" #\\)))
(defrule integer
(or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(defrule 4-integers
(and integer integer integer integer))
(defrule 2-integers
(and integer integer ) )
(defrule time-secfrac
(and "." (+ integer)))
(defrule time-numoffset
(and (or "+" "-") 2-integers ":" 2-integers))
;;1979-05-27T07:32:00Z
(defrule datetime
(and 4-integers #\- 2-integers #\- 2-integers #\T
2-integers #\: 2-integers #\: 2-integers (? time-secfrac) (or #\Z time-numoffset))
(:lambda (list)
(list
:datetime
(local-time:parse-timestring
(format nil "~{~a~}" (alexandria:flatten list))))))
(defrule whitespace
(+ (or #\space #\tab #\newline))
(:constant :ws))
(defrule alphanumeric
(alphanumericp character))
(defrule string-char
(or (not-doublequote character) (and #\\ #\')))
;; Keygroups are known as tables in v0.2.0
(defrule keygroup-char
(or (not-bracket character) #\. ))
(defrule normal-key
(+ (not-special-case character))
(:text
list))
(defrule string-contents
(* (or (and "\\" "\"")
string-char
))
(:lambda (s)
(format nil "~{~c~}"
(loop for var in s collect
(if (listp var)
#\"
var)))))
(defrule string
(and #\" string-contents #\")
(:destructure (q1 string q2)
(declare (ignore q1 q2))
(list
:string
(transliterate-to-specials
(transliterate-unicode
(text string))))))
(defrule literal-string
(and #\' (* (not (or #\newline #\'))) #\')
(:destructure (q1 characters q2)
(declare (ignore q1 q2))
(list :string (coerce characters 'string))))
(defrule number
(and (? "-" ) (and
(+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))
(?
(and
#\.
(+ (or "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"))))))
(:destructure (sign list)
(list
:number
(if sign
(parse-number:parse-number (text (push sign list)) :radix 10)
(parse-number:parse-number (text list) :radix 10)))))
(defrule bool
(or "true" "false")
(:lambda (thing)
(list
:bool
(if (string= thing "true")
;; Returns keywords to allow a semantic walker ignore them
;; when stripping out foos
:true
:false))))
(defrule array-contents
(and value
(* (and (? whitespace )
#\,
(? whitespace) value))
(? whitespace)
(? #\,)
(? whitespace))
(:lambda (thing)
;; Drop the whitespace captures
(let ((array-list
(butlast (butlast (butlast thing)))))
(append
(list (car array-list))
;; strip the whitespace and comma info
(loop for group in (cadr array-list)
collect
(fourth group))))))
(defrule array
(and #\[
(? whitespace)
array-contents
(? whitespace) #\])
(:lambda (thing)
(list
:array
(third thing))))
(defrule value
(or
array
datetime
bool
number
string
literal-string))
(defrule end-of-information
(and (* (or #\Space #\tab))
#\Newline)
(:constant :ws))
(defrule keyvalue
(and (? whitespace)
normal-key
(? whitespace)
#\=
(? whitespace)
value
end-of-information)
(:destructure (w1 key w2 e1 w3 value w4)
(declare (ignore w1 w2 e1 w3 w4))
(list
:keyvalue
key value)))
;; aka tables in 0.2.0
(defrule keygroup
(and (? whitespace) #\[ (+ keygroup-char) #\] (? whitespace))
(:destructure (_1 _2 name _3 _4)
(declare (ignore _1 _2 _3 _4))
(list :header
(text name))))
(defrule preamble
(* keyvalue))
;; Toplevel grammar specification.
(defrule file-grammar
(and
preamble
;; interleaving
(* (and
keygroup
(* keyvalue)))
(? whitespace)))
(defun parse-string (string)
"Returns the toml parsed structure from `string` or :parse-error"
(parse 'file-grammar string))
;;; Semantic analysis tools
;;; Lexing and parsing are over, time for the hard stuff. :)
(defun extract-lisp-structure (parsed-structure)
;; Expecting parsed-structure to be two lists of lists. List 1
;; will be the keys not belonging to a top-level value. List 2 is
;; teh list of values belonging to top-level keys.
(let ((table (make-hash-table
;; Will be comparing strings for the keys
:test #'equal)))
;;pass 1: get all unheadered keys into a hash table
(loop for keyvalue in (first parsed-structure)
do (setf (gethash (second keyvalue) table)
(process-value-data (third keyvalue))))
;; pass 2: normalize headers into keyvalues.
;;
;; as partof this pass, duplicate keys are detected and an error
;; is thrown
;;
;; [h1.h2] key1 = t => h1.h2.key1
(loop for header in (second parsed-structure)
do
(assert (eq (caar header) :header))
(let ((keygroup-header (cadar header))
(section (cadr header)))
(unless section
(setf (gethash keygroup-header table)
(make-hash-table :test #'equal)))
(loop for keyvalue in section do
(assert (eq (first keyvalue) :keyvalue))
(let ((key
;; Format the key
(format nil "~a.~a"
keygroup-header
(second keyvalue))))
(multiple-value-bind (value gotten)
(gethash key table)
(declare (ignore value))
;; Duplicate value detection!
(assert (not gotten) ))
(setf (gethash key table)
;; collapse values from the (:type <stuff>)
;; information.
(process-value-data (third keyvalue)))))))
;; break! at this point: we have a flat common lisp hash table,
;; no duplicate keys. each value is a lisp value correctly
;; transcribed from the toml.
;; pass 3. place the h1.h2.key2 into nested hash tables h1 => h2>
;; key2 => values; since we return the new table, we don't need to
;; worry about nuking the old values.
(explode-hash-table table)))
(defun process-value-data (value)
"`value` may be any of the standard toml value types: array,
datetime, bool, number, or string. Of those, arrays are a special case:
Supposing an array is encountered, process-value-data recurses upon the array.
Toml does not support references as of v0.1, and there for we can traverse arrays without cyles of references."
(alexandria:switch ((first value) :test #'eq )
(:array
(loop for ele in (second value)
collect (process-value-data ele)))
(:bool
(alexandria:switch ((second value))
(:true t)
(:false nil)
(t
(error
"Unable to understand: ~a was thought to be a :BOOL must be :true or :false"
(second value)))))
(:datetime
;; The datetime is already parsed
(second value))
(:string
;; Text is already parsed as well
(second value))
(:number
(second value))
(t
(error
"Unable to understand: ~a; must be a legit pp-toml type"
(first value)))))
;; TODO: shift down to being a flet
(defun key-p (key table)
"Does `key` exist in `table?"
(multiple-value-bind
(value found-p)
(gethash key table)
(declare (ignore value))
found-p))
(defun make-nested-hash (table key-list value)
;; Recursively work through the hash table.
;; Should be TCO'able.
(let ((key (car key-list)))
(cond
;; are we at the end of the key list?
((not (cdr key-list))
(setf (gethash key table) value))
(t
(if (not (key-p key table))
(progn
(setf (gethash key table)
(make-hash-table :test #'equal))))
(make-nested-hash (gethash key table) (cdr key-list) value)))))
;; TODO: turn into a flet for explode-hash-table
(defun splode-y (table splitter key value)
(let ((key-list
(split-sequence:split-sequence splitter key)))
(make-nested-hash table key-list value)))
(defun explode-hash-table (table)
(let ((new-table (make-hash-table :test #'equal)))
(maphash
#'(lambda (k v)
(splode-y new-table #\. k v))
table)
new-table))
(defun parse-toml (string &key (strict nil))
"Parse a TOML `string`, returning a hash table comparable by EQUAL.
`strict` is not supported at present.
"
;; not supported
(declare (ignore strict))
(let ((postfix-newlined-string
(cl-ppcre:regex-replace-all "\\b\\s*$" string
(concatenate 'string '(#\Newline)))))
(let ((cleaned-string (strip-comments postfix-newlined-string)))
(if (cl-ppcre:scan "^\\s*$" cleaned-string)
(make-hash-table :test #'equal)
(extract-lisp-structure
(parse-string cleaned-string))))))