-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshiftless-load.el
More file actions
288 lines (267 loc) · 8.86 KB
/
shiftless-load.el
File metadata and controls
288 lines (267 loc) · 8.86 KB
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
(require 'cl-lib)
(require 'dash)
(require 'rx)
(require 'shiftless-types)
(require 'shiftless-access)
(defvar shiftless::read-table
'(;; [
(91 . shiftless::read-sequence)
;; '
(39 . shiftless::read-string)))
(defun shiftless::split-references (string)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(let ((start (point)))
(append
(cl-loop
while (char-after)
for spot = (point)
for ref = (shiftless::read-reference)
if ref collect (buffer-substring-no-properties
start spot)
and collect ref
and do (setf start (point))
else do (forward-char))
(list (string-trim (buffer-substring-no-properties
start (point-max))
""))))))
(defun shiftless::delay-reference (string)
(if (string-match (rx ".[" (one-or-more anything) "]")
string)
(let ((parts (shiftless::split-references string)))
(vector
`(lambda (data)
(shiftless::read-from-string
(apply 'concat
(mapcar
(lambda (part)
(if (consp part)
(apply 'shiftless:access-as 'string data part)
part))
',parts))))))
string))
(defun shiftless::make-atom (string)
(cond
((not (stringp string)) string)
((equal "" string) nil)
;; string
;; '
((shiftless::stringp string)
(let ((unquoted (substring string 1 (- (length string) 1))))
(->>
unquoted
(replace-regexp-in-string
(rx "\\'") "'")
(replace-regexp-in-string
(rx "\\\\") "\\"))))
;; number
((string-match (rx string-start
(zero-or-one "-")
(one-or-more digit)
(zero-or-one "."
(one-or-more digit))
string-end)
string)
(string-to-number string))
;; symbol
(:else
(intern (downcase string)))))
(defun shiftless::association-p (obj)
(and obj (listp obj)
(= 0 (mod (length obj) 3))
(->>
obj
(-map-indexed
(lambda (index item)
(cons (mod index 3) item)))
(-every-p
(lambda (pair)
(let ((place (car pair))
(item (cdr pair)))
(cond
;; key
((= 0 place)
(or (consp item)
(symbolp (shiftless::make-atom item))))
;; equal sign
((= 1 place)
(equal item "="))
(:else t))))))))
(defun shiftless::add-to-association (key value alist)
(cond
((null key) value)
((consp key)
(let ((key (shiftless::atom-symbol (car key)))
(keys (cdr key)))
(let ((existing-value (cdr (assoc key alist))))
(unless (listp existing-value)
(error "Sequenced key cannt mix with non-assocation value (%S)" key))
(append (cl-remove key alist :key 'car)
(list (cons key
(shiftless::add-to-association
keys value existing-value)))))))
(:else
(let ((key (shiftless::atom-symbol key)))
(if (cl-member key alist :key 'car)
(error "duplicate keys not allowed (%S)" key)
;; not member
(append alist (list (cons key value))))))))
(defun shiftless::association-from-sequence (obj)
(cl-loop
with data = (list)
for (key = value) on obj by 'cdddr
do (setf data (shiftless::add-to-association
key value data))
finally return data))
(defun shiftless::reference-p ()
(save-excursion
(and (= 46 (or (char-after) 0)) ;; .
(progn (forward-char) t)
(= 91 (or (char-after) 0))))) ;; [
(defun shiftless::read-reference ()
(when (shiftless::reference-p)
(forward-char 2)
(prog1 (mapcar 'shiftless::make-atom ;automatically imply types for reference parts
(cl-loop
do (shiftless::skip-whitespace-and-comments)
until (eq 93 (or (char-after) 0)) ;]
for sym = (shiftless::read-symbol-or-number)
collect sym))
(forward-char))))
(defun shiftless::read-string ()
(let ((start (point)))
(forward-char)
(while (not (and (= 39 (char-after)) ;'
;; \
(not (= 92 (char-before)))))
(forward-char))
(forward-char)
(shiftless::delay-reference
(buffer-substring-no-properties start (point)))))
(defun shiftless::skip-whitespace-and-comments ()
(cl-loop for start = (point)
do (when (= 59 (or (char-after) 0)) ; ;
(while (not (= ?\n (or (char-after) ?\n)))
(forward-char)))
do (skip-chars-forward " \t\r\n")
while (/= start (point))))
(defun shiftless::read-symbol-or-number ()
(let ((start (point)))
(while (and (char-after)
(not (member (char-after) '(32 ;spacep
91 ;[
93 ;]
39 ;'
?\n
?\t
?\r))))
(shiftless::read-reference)
(forward-char))
(shiftless::delay-reference
(buffer-substring-no-properties start (point)))))
(defun shiftless::read-sequence ()
(forward-char)
(let ((sexp (cl-loop
do (shiftless::skip-whitespace-and-comments)
;; ]
while (/= 93 (or (char-after) 0))
collect (shiftless::read))))
(forward-char)
(if (shiftless::association-p sexp)
(shiftless::association-from-sequence sexp)
(when (cl-member "=" sexp :test 'equal)
(warn "The list %S contains the = symbol but is not an association"
sexp))
sexp)))
(defun shiftless::read ()
(shiftless::skip-whitespace-and-comments)
(funcall (or (cdr (assoc (char-after) shiftless::read-table
'equal))
'shiftless::read-symbol-or-number)))
(defun shiftless::read-from-string (string)
(with-temp-buffer
(insert string)
(goto-char (point-min))
(shiftless::read)))
(defun shiftless::resolve-references (data structure)
(cond
;; reference
((vectorp structure)
(funcall (elt structure 0) data))
;; list and association
((proper-list-p structure)
(mapcar
(lambda (struct)
(shiftless::resolve-references data struct))
structure))
;; association cell
((consp structure)
(cons (car structure)
(shiftless::resolve-references data (cdr structure))))
(:else
structure)))
(defun shiftless::apply-implicit-schema (data)
(cond
((proper-list-p data)
(mapcar 'shiftless::apply-implicit-schema data))
((consp data)
(cons (car data)
(shiftless::apply-implicit-schema (cdr data))))
((stringp data)
(shiftless::make-atom data))
(:else
data)))
(defun shiftless::apply-schema (data schema
&optional accessors)
(cond
((shiftless::alistp data)
(mapcar
(lambda (pair)
(let ((key (car pair))
(value (cdr pair)))
(cons key
(shiftless::apply-schema value schema
(append accessors
(list key))))))
data))
((proper-list-p data)
(mapcar
(lambda (item)
(shiftless::apply-schema item schema
(append accessors
(list 0))))
data))
(:else
(let ((type (apply 'shiftless:access schema accessors)))
(unless (consp type)
(funcall (shiftless::function-from-type type)
data))))))
(defun shiftless:load (string &optional schema)
"Return a lisp data structure encoded in the file STRING or directly in STRING."
(with-temp-buffer
(if (not (file-exists-p string))
;; load string
(insert string)
;; load file
(insert "[]")
(forward-char -1)
(insert-file-contents string))
(goto-char (point-min))
(let* ((struct (shiftless::read))
(struct (shiftless::resolve-references
struct struct)))
(unless (looking-at (rx string-end))
(warn "You have some extra characters at the end: %S"
(buffer-substring-no-properties
(point) (point-max))))
(cond
((eq :implicit schema)
(shiftless::apply-implicit-schema struct))
(schema
(shiftless::apply-schema struct
(shiftless:load schema :implicit)))
(:else
struct)))))
(provide 'shiftless-load)
;;; shiftless-load.el ends here