about summary refs log tree commit diff stats
path: root/fff.scm
blob: c34976512937c9478f04d08fced4f868132a4182 (plain)
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
#!/bin/sh
#| -*- scheme -*-
exec guile -e main -s "$0" "$@"

Flat Fuck Format
--- a new configuration format, because who doesn't need that?

Copyright (C) 2023 Case Duckworth <acdw@acdw.net>

Everyone is permitted to do whatever with this software, without
limitation.  This software comes without any warranty whatsoever,
but with two pieces of advice:

- Don't hurt yourself.
- Make good choices.

Commentary:

This script will convert files defined in the Flat Fuck Format (fff) into json.
It will not convert anything back to fff.  fff is explicitly made to be as
simple as possible, and exclusively human-written.  If a machine writes your
configuration, ... use a better configuration format.  Or make your program
scriptable!

FLAT FUCK FORMAT : Specification
|#
!#


;;; Format

(use-modules (ice-9 peg))

;;; Structure

(define-peg-pattern fff all
  (and (* (or WHITESPACE NEWLINE))
       (* (or comment
              object
              array
              object-item))
       blanks
       (not-followed-by peg-any)))

(define-peg-pattern object all
  (and name
       (+ object-item)
       blanks))

(define-peg-pattern array all
  (and name
       (+ array-item)
       blanks))

(define-peg-pattern anon all
  (and (+ object-item)
       blanks))

;; (define-peg-pattern anon-array all
;;   (and (+ array-item)
;;        (* NEWLINE)))

(define-peg-pattern name body
  (and key COLON COLON NEWLINE))

(define-peg-pattern object-item body
  (and key (* WHITESPACE)
       COLON (* WHITESPACE)
       (or ref val)
       blanks))

(define-peg-pattern array-item body
  (and (* WHITESPACE)
       COLON (* WHITESPACE)
       (or ref val)
       blanks))

(define-peg-pattern key body
  (+ (and (not-followed-by COLON)
          NONEWLINE)))

;; (define-peg-pattern key body
;;   key-raw)

(define-peg-pattern val all
  (* NONEWLINE))

(define-peg-pattern ref all
  (and AT key))

;;; Comments

(define-peg-pattern comment none
  (and HASH (* WHITESPACE)
       (* (and (not-followed-by NEWLINE)
               peg-any))
       blanks))

(define-peg-pattern blanks none
  (* NEWLINE))

;;; Escaped characters

(define-peg-pattern escaped body
  (and BACKSLASH (or (and NEWLINE (* WHITESPACE))
                     peg-any)))

;;; Terminals

(define-peg-pattern BACKSLASH none
  "\\")

(define-peg-pattern COLON none
  ":")

(define-peg-pattern NEWLINE none
  "\n")

(define-peg-pattern WHITESPACE none
  (or " " "\t"))

(define-peg-pattern NONEWLINE body
  (or escaped
      (and (not-followed-by NEWLINE)
           peg-any)))

(define-peg-pattern HASH none
  "#")

(define-peg-pattern AT none
  "@")


;;; Serialization
;; I want fff to serialize to a structure that's compatible with the structure
;; guile-json uses ( https://github.com/aconchillo/guile-json )

(use-modules (ice-9 exceptions)
             (ice-9 match))

(define (fff? x)
  (and (pair? x)
       (eq? (car x) 'fff)))

;; (define (car-safe x)
;;   (if (pair? x)
;;       (car x)
;;       #f))

;; (define (fff-tree->scm tree)
;;   (let loop ((rest (cdr tree))
;;              (accum '()))
;;     (if (null? rest)
;;         (reverse accum)
;;         (let ((this (match (car rest)
;;                       (`(anon . ,obj)
;;                        (cons 'anon (fff-object->scm obj)))
;;                       (`(object  ,name ,obj)
;;                        (cons (cons 'obj name) (fff-object->scm obj)))
;;                       (`(array ,name ,arr)
;;                        (cons (cons 'arr name) (fff-array->scm arr))))))
;;           (loop (cdr rest) (cons this accum))))))

(define (fff->scm str)
  (let ((fff (peg:tree
              (match-pattern fff str))))
    (if fff
        (fff/refs->scm (fff->scm/refs fff))
        #f)))

(define (fff->scm/refs tree)
  (let loop ((xs (cdr tree))
             (it '()))
    (if (null? xs)
        (reverse it)
        (loop (cdr xs)
              (match (car xs)
                ;; (`(anon . ,obj)
                ;;  ;; Because of how peg's parsing works it collapses one-element
                ;;  ;; lists, necessitating this check.
                ;;  (if (pair? (car obj))
                ;;      ;; We could use `map-in-order', but according to JSON the
                ;;      ;; order of an object's keys doesn't matter ....
                ;;      (append (map fff-pair->scm obj) it)
                ;;      (cons (fff-pair->scm obj) it)))
                (`(array ,name ,vals)
                 (cons (cons name
                             (list->vector (map fff-val->scm vals)))
                       it))
                (`(object ,name ,pairs)
                 (cons (cons name
                             (map fff-pair->scm pairs))
                       it))
                (`(item . ,pair)
                 (cons (fff-pair->scm pair)
                       it))
                (_ it))))))

(define (fff-val->scm val)
  (match val
    (`(val ,v) v)
    (`(ref ,r) (lambda (alist)
                 (assoc-ref alist r)))))

(define (fff-pair->scm pair)
  (cons (cdr pair) (fff-val->scm (cadr pair))))

(define (atom? x)
  (and (not (null? x))
       (not (pair? x))))

(define (fff/refs->scm tree)
  (define dupes '())
  (filter (lambda (x)
            (not (member (car x) dupes)))
          (let loop ((xs tree)
                     (it '())
                     (v? #f))
            (if (null? xs)
                ((if v? list->vector identity) (reverse it))
                (loop (cdr xs)
                      (cons (let ((x (car xs)))
                              (cond
                               ((atom? x)
                                x)
                               ((procedure? (cdr x))
                                (set! dupes (cons (car x) dupes))
                                (cons (car x)
                                      ((cdr x) tree)))
                               ((atom? (cdr x))
                                x)
                               ((vector? (cdr x))
                                (cons (car x)
                                      (loop (vector->list (cdr x)) '() #t)))
                               (else
                                (cons (car x)  ; still not tail-recursive!
                                      (loop (cdr x) '() #f)))))
                            it)
                      v?)))))

;; (define (ref-resolve tree)
;;   (map (lambda (x)
;;          (display x)
;;          (cond
;;           ((atom? x)
;;            (display ": atom\n")
;;            x)
;;           ((procedure? (cdr x))
;;            (display " (cdr): procedure\n")
;;            (cons (car x) ((cdr x) )))
;;           ((atom? (cdr x))
;;            (display " (cdr): atom\n")
;;            x)
;;           (else ; not tail-recursive!
;;            (display " ...\n")
;;            (cons (car x) (ref-resolve (cdr x))))))
;;        tree))

;; (define (fff-object->scm obj)
;;   (let loop ((rest obj)
;;              (accum '()))
;;     (if (null? rest)
;;         (reverse accum)
;;         (let* ((this (car rest))
;;                (k (car (assq-ref this 'key)))
;;                (v (car-safe (assq-ref this 'val)))
;;                (r (assq-ref this 'ref)))
;;           (loop (cdr rest)
;;                 (cons (cond
;;                        (v (cons k v))
;;                        (r (cons 'ref (cons k r)))
;;                        (else 'fuck))
;;                       accum))))))

;; (define (fff-array->scm arr)
;;   (let loop ((rest arr)
;;              (accum '()))
;;     (if (null? rest)
;;         (list->vector (reverse accum))
;;         (let* ((this (car rest)))
;;           (loop (cdr rest)
;;                 (cons (case (car this)
;;                         ((val) (cadr this))
;;                         ((ref) this))
;;                       accum))))))


;;; Testing

(use-modules (ice-9 textual-ports))

(define test-input
  (call-with-input-file "/home/case/var/fff.fff" get-string-all))

(define test-input2
  "
a: 1
b:2

c:3

d::
da: 10
db: 20

e::
: 30
:40
:     50

f::
z: @d
y: @a

g::
f: @f
another one: @f
")

(use-modules (ice-9 format))

(define (test-parse input)
  (let loop ((str "")
             (num 0)
             (lst (string-split input #\newline)))
    (if (or (null? lst)
            (not (match-pattern fff str)))
        (format #t "~s~%" lst)
        (begin
          ;; (display (peg:tree (match-pattern fff str)))
          (format #t "~s~%" (match-pattern fff str))
          (when (match-pattern fff str)
            (format #t "~s~%~%" (car lst)))
          (loop (string-append str "\n" (car lst))
                (+ num 1)
                (cdr lst))))))


;;; Notes

#|
allow only `key:value` or named objects (list::..., dict::...)
put everything in a big object/dict
resolve references: fff string => list with refs (lambdas ?) => resolved list
|#