about summary refs log tree commit diff stats
path: root/fff.scm
blob: 6e524ada9f00bd483c6da194251a85e1470df158 (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
(use-modules (ice-9 peg)
             (ice-9 match))

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

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

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

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

(define-peg-pattern item all
  object-item)

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

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

(define-peg-pattern name all
  (and key COLON COLON (* WHITESPACE) NEWLINE))

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

(define-peg-pattern val all
  (and (* nonl)
       NEWLINE))

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

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

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

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

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

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

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

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

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

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


(define (ensure-nested-list x)
  (if (list? (car x))
      x
      (list x)))

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

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

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

(define (fff->scm str)
  (let ((tree (peg:tree (match-pattern fff str))))
    (if (fff? tree)
        (fff-ref-resolve (fff-tree->ref-tree tree))
        #f)))

(define (fff-tree->ref-tree tree)
  (let loop ((xs (cdr tree))
             (it '()))
    (if (null? xs)
        (reverse it)
        (loop (cdr xs)
              (match (car xs)
                (`(object (name ,name) ,pairs)
                 (cons (cons name
                             (map fff-pair->scm
                                  (ensure-nested-list pairs)))
                       it))
                (`(array (name ,name) ,values)
                 (cons (cons name
                             (list->vector (map fff-value->scm
                                                (ensure-nested-list values))))
                       it))
                (`(item . ,pair)
                 (cons (fff-pair->scm pair)
                       it))
                (_ it))))))

(define* (fff-ref-resolve tree #:optional environment keep-dupes?)
  (define dupes '())
  (define env (append (or environment '())
                      '(("true" . #t)
                        ("false" . #f)
                        ("null" . null))))
  (filter (lambda (x)
            (if keep-dupes?
                #t
                (not (member (or (car-safe x) x)
                             dupes))))
          (let loop ((xs tree)
                     (v? #f)
                     (it '()))
            (if (null? xs)
                ((if v? list->vector identity)
                 (reverse it))
                (begin
                  (loop (cdr xs)
                        v?
                        (cons (let ((x (car xs)))
                                (cond
                                 ((atom? x)
                                  (set! env (cons x env))
                                  x)
                                 ((procedure? (cdr x))
                                  (set! dupes (cons (car x) dupes))
                                  (let ((res
                                         (cons (car x)
                                               ((cdr x) (append env tree)))))
                                    (set! env (cons res env))
                                    res))
                                 ((atom? (cdr x))
                                  (set! env (cons x env))
                                  x)
                                 ((vector? (cdr x))
                                  (let ((vl (vector->list (cdr x))))
                                    (set! env (cons (fff-ref-resolve vl env #t)
                                                    env))
                                    (cons (car x)
                                          (loop vl #t '()))))
                                 (else   ; object
                                  (set! env (cons (fff-ref-resolve x env #t)
                                                  env))
                                  (cons (car x) ; not tail-recursive!
                                        (loop (cdr x) #f '())))))
                              it)))))))

(define (fff-pair->scm pair)
  (cons (car pair) (fff-value->scm (cadr pair))))

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