forked from jasom/nyaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringify.lisp
212 lines (181 loc) · 5.82 KB
/
stringify.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
(in-package "NYAML")
;;; customization
(defparameter *indent* " "
"The string to use as a single indent.")
(defparameter *indent-level* 0)
(defparameter *document-separator*
(format nil "~%~%---~%~%")
"The string to separate yaml documents.")
(defun output-current-indent (stream &optional (offset 0))
(loop :repeat (- *indent-level* offset) :do (write-sequence *indent* stream)))
(defun stringify (yaml)
(with-output-to-string (s)
(output yaml s)))
(defgeneric output (yaml stream)
(:documentation "Output yaml to stream"))
;;; special
;; not a number
(defmethod output ((yaml (eql :nan)) stream)
(write-sequence ".nan" stream))
#+sbcl
(defmethod output ((yaml (eql *sbcl-nan-value*)) stream)
(write-sequence ".nan" stream))
#+allegro
(defmethod output ((yaml (eql #.excl:*nan-double*)) stream)
(write-sequence ".nan" stream))
;; positive infinity
(defmethod output ((yaml (eql :+inf)) stream)
(write-sequence "+.inf" stream))
#+sbcl
(defmethod output ((yaml (eql sb-ext:double-float-positive-infinity)) stream)
(write-sequence "+.inf" stream))
#+allegro
(defmethod output ((yaml (eql #.excl:*infinity-double*)) stream)
(write-sequence "+.inf"))
;; negative infinity
(defmethod output ((yaml (eql :-inf)) stream)
(write-sequence "-.inf" stream))
#+sbcl
(defmethod output ((yaml (eql sb-ext:double-float-negative-infinity)) stream)
(write-sequence "-.inf" stream))
#+allegro
(defmethod output ((yaml (eql #.excl:*negative-infinity-double*)) stream)
(write-sequence "-.inf" stream))
;; custom
(defmethod output (yaml stream)
(cond
((equal *false* yaml) (write-sequence "false" stream))
((equal *null* yaml) (write-sequence "null" stream))
(t (error "Can't stringify ~a to yaml" yaml))))
;;; boolean
(defmethod output ((yaml (eql t)) stream)
(write-sequence "true" stream))
;;; number
(defmethod output ((yaml integer) stream)
(format stream "~D" yaml))
(defmethod output ((yaml float) stream)
(let ((*read-default-float-format* (type-of yaml)))
(princ yaml stream)))
;;; string
(defrule c-unquoted
(and
(! (character-ranges #\Newline #\" #\\))
c-printable)
(:lambda (x) (second x)))
(defrule unquoted-chars
(* c-unquoted)
(:text t))
(defun quote-char (c s)
(cond
((eql c #\Newline)
(write-sequence "\\n" s))
((eql c #\\)
(write-sequence "\\\\" s))
((< (char-code c) 255)
(format s "\\x~2,'0x" (char-code c)))
((< (char-code c) 65536)
(format s "\\u~4,'0x" (char-code c)))
(t
(format s "\\U~4,'0x" (char-code c)))))
(defmethod output ((yaml string) stream)
(write-char #\" stream)
(loop for (some next _) = (multiple-value-list (esrap:parse 'unquoted-chars yaml :junk-allowed t))
then (multiple-value-list (esrap:parse 'unquoted-chars yaml :start next :junk-allowed t))
do (write-sequence some stream)
while next
do (quote-char (char yaml next) stream)
(incf next))
(write-char #\" stream))
;;; sequence
(defmethod output ((yaml array) stream)
(if (emptyp yaml)
(write-sequence "[]" stream)
(let* ((*indent-level* (1+ *indent-level*)))
(loop :for yaml :across yaml
:for first = t :then nil
:unless first
:do (write-char #\Newline stream)
(output-current-indent stream 1)
:do (write-sequence "- " stream)
(output yaml stream)))))
(defun output-documents (yaml stream)
(loop for (item . rest) on (rest yaml)
do (write-sequence item stream)
when rest
do (write-sequence *document-separator* stream)))
(defun output-list (yaml stream)
(let* ((*indent-level* (+ *indent-level* 1)))
(loop :for (yaml . rest) :on yaml
:do (write-sequence "- " stream)
(output yaml stream)
:when rest
:do
(write-char #\Newline stream)
(output-current-indent stream 1))))
(defmethod output ((yaml cons) stream)
(cond
;; documents
((eq :documents (first yaml))
(output-documents yaml stream))
;; alist
((alistp yaml)
(output-alist yaml stream))
;; regular list
(t (output-list yaml stream))))
;;; mapping
;; hash-table
(defmethod output ((yaml hash-table) stream)
(if (= 0 (hash-table-count yaml))
(write-sequence "{}" stream)
(let* ((*indent-level* (+ *indent-level* 1)))
(loop :for key :being :the :hash-keys :of yaml
:using (:hash-value value)
:for first = t then nil
:unless first :do
(write-char #\Newline stream)
(output-current-indent stream)
:if (typep value '(or (and array (not string))
cons hash-table))
:do (output key stream)
(format stream ":~%")
(output-current-indent stream)
(output value stream)
:else
:do
(output key stream)
(write-sequence ": " stream)
(output value stream)))))
;; alist
(defun alistp (list)
(and (listp list)
(every 'consp list)
(every (lambda (pair) (typep (first pair) 'alexandria:string-designator))
list)))
(defun output-alist (yaml stream)
(if (null yaml)
(write-sequence "{}" stream)
(let* ((*indent-level* (+ *indent-level* 1)))
(loop :for (key . value) :in (remove-duplicates yaml :test 'equal :key #'car)
:for first = t :then nil
:unless first :do
(output-current-indent stream)
(write-char #\Newline stream)
:if (typep value '(or (and array (not string))
cons hash-table))
:do
(output key stream)
(format stream ":~%")
(output-current-indent stream)
(output value stream)
:else
:do (output key stream)
(write-sequence ": " stream)
(output value stream)))))
;;; dump to file
(defun dump (yaml path &key if-exists (if-does-not-exist :create))
"Stringify a lisp data structure and the resulting yaml string to a file."
(with-open-file (out path
:direction :output
:if-exists if-exists
:if-does-not-exist if-does-not-exist)
(output yaml out)))