summaryrefslogtreecommitdiff
path: root/cook/parse.scm
blob: 54ea23c55531870a2083624e81c4e20f4e94f6a7 (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
#|
https://github.com/cooklang/spec/blob/main/EBNF.md
|#
(define-record-type <amount>
  (make-amount quantity unit)
  amount?
  (quantity amount-quantity)
  (unit amount-unit))

(define-record-type <component>
  (make-component name amount)
  component?
  (name component-name)
  (amount component-amount))

(define-record-type <ingredient>
  (make-ingredient name amount)
  ingredient?
  (name ingredient-name)
  (amount ingredient-amount))

(define (component->ingredient comp)
  (make-ingredient (component-name comp) (component-amount comp)))

(define-record-type <cookware>
  (make-cookware name amount)
  cookware?
  (name cookware-name)
  (amount cookware-amount))

(define (component->cookware comp)
  (make-cookware (component-name comp) (component-amount comp)))

(define-record-type <timer>
  (make-timer name amount)
  timer?
  (name timer-name)
  (amount timer-amount))

(define (component->timer comp)
  (make-timer (component-name comp) (component-amount comp)))

(define-record-type <metadata-line>
  (make-metadata-line key value)
  metadata-line?
  (key metadata-line-key)
  (value metadata-line-value))

(define-record-type <step>
  (make-step elements)
  step?
  (elements step-elements))

(define-record-type <comment>
  (make-comment text)
  comment?
  (text comment-text))

(define-record-type <note>
  (make-note text)
  note?
  (text note-text))

(define-record-type <section>
  (make-section name)
  section?
  (name section-name))

(define-record-type <recipe>
  (make-recipe metadata body)
  recipe?
  (metadata recipe-metadata)
  (body recipe-body))

(define (metadata-line-list->hash-table meta-lines)
  (let ((metadata (make-hash-table)))
    (for-each (lambda (line)
                (hash-table-set! metadata
                                 (metadata-line-key line)
                                 (metadata-line-value line)))
              meta-lines)
    metadata))

(define (merge-step-strings lis)
  (reduce-right
    (lambda (el acc)
      (cond
        ((char? el)
         (if (and (< 0 (length acc)) (string? (car acc)))
           (cons (string-append (string el) (car acc)) (cdr acc))
           (append (list (string el)) acc)))
        (else
           (append (list el) acc))))
    '()
    lis))


(define word-chars      (char-set-difference char-set:full
                                             punctuation-chars
                                             newline-chars
                                             whitespace-chars))
(define any-text-chars  (char-set-difference char-set:full
                                             newline-chars))
(define text-chars      (char-set-difference any-text-chars
                                             (char-set #\@ #\# #\~)))
(define unit-chars      (char-set-difference text-chars (char-set #\})))
(define component-chars (char-set-difference char-set:full
                                             (char-set #\@ #\# #\~)))
(define component-word-chars (char-set-difference component-chars
                                                  punctuation-chars
                                                  newline-chars
                                                  whitespace-chars))
(define quantity-chars  (char-set-difference text-chars (char-set #\} #\%)))
(define metadata-chars  (char-set-difference text-chars (char-set #\:)))

(define (string-trimmer-from-sets . sets)
  (lambda (str)
    (string-trim str
                 (lambda (x)
                   (char-set-contains? (apply char-set-union sets) x)))))

(define string-trim-whitespace (string-trimmer-from-sets whitespace-chars))

(define (string-split-newline str)
  (string-split str (lambda (x) (char-set-contains? newline-chars x))))

(define-grammar cook
    (one-nl         (,(parse-map
                         (parse-sre `(: eol (+ ,newline-chars) bol))
                         (lambda _ '(#\space)))))
    (nl             ((: ,newline-chars)))
    (whitespace     ((=> x (+ ,whitespace-chars))
                     (list->string x)))

    (any-text-item  ((: (=> c (+ ,any-text-chars)))
                     (list->string c)))

    (comment        ((: "--" (=> c (+ ,any-text-chars)) (or eos ,nl))
                     (make-comment (list->string c)))
                    ((: "[-" (=> c (* any)) "-]")
                     (make-comment (list->string c))))

    (note-line      ((: ">" (=> c (+ ,any-text-chars)) (or eos ,nl))
                     (string-trim-whitespace (list->string c))))
    (note           ((=> n (+ ,note-line))
                     (make-note (string-join n " "))))

    (word           ((=> w (+ ,word-chars))
                     w))
    (unit           ((=> u ,(parse-map-substring
                              (parse-repeat+ (parse-char unit-chars))
                              string-trim-whitespace))
                     u))
    (quantity       ((=> q ,(parse-map-substring
                              (parse-repeat+ (parse-char quantity-chars))
                              string-trim-whitespace))
                     (or (string->number q) q)))
    (meta-key       ((=> k (+ ,metadata-chars))
                     (list->string k)))

    (amount         ((=> q ,whitespace)
                     #f)
                    ((: (=> q  ,quantity) "%" (=> u ,unit))
                     (make-amount q u))
                    ((=> q ,quantity)
                     (make-amount q #f)))

    (amount-block      ((: "{" (? (=> a ,amount)) "}")
                        a))
    (no-word-component ((: (=> a ,amount-block))
                        (make-component #f a)))

    (component-word ((=> w (+ ,component-word-chars))
                     (list->string w)))
    (component      ((: (=> cw ,component-word)
                        (? (: (=> cc (* ,component-chars))
                              (=> a ,amount-block))))
                     (make-component
                       (string-join
                         (string-split-newline
                           (string-append cw (if cc (list->string cc) "")))
                         " ")
                       a)))

    (timer          ((: "~" (=> c (or ,component ,no-word-component)))
                     (component->timer c)))
    (cookware       ((: "#" (=> c ,component))
                     (component->cookware c)))
    (ingredient     ((: "@" (=> c ,component))
                     (component->ingredient c)))

    (text-item      ((=> t (+ (or ,comment
                                  ,ingredient
                                  ,cookware
                                  ,timer
                                  ,any-text-chars)))
                     t))

    (step-line      ((: (? ,whitespace)
                        (=> s (+ ,text-item))
                        (? ,nl))
                       (append! (concatenate! s))))

    (step           ((=> s (: ,step-line
                              ,(parse-map
                                 (parse-repeat
                                   (parse-map ;; Add space where newline was
                                     step-line (lambda x
                                                (append '(#\space)
                                                        (concatenate x)))))
                                 (lambda x (concatenate!
                                             (concatenate! x))))))
                     (make-step (merge-step-strings (concatenate! s)))))

    (section        ((: "=" (=> c (+ ,any-text-chars)) (or eos ,nl))
                     (make-section
                       ((string-trimmer-from-sets whitespace-chars
                                                  (string->char-set "="))
                        (list->string c)))))

    (metadata       ((: bol ">>"
                        (=> k ,(parse-map meta-key string-trim-whitespace))
                        (* ,whitespace) ":" (* ,whitespace)
                        (=> v ,(parse-map any-text-item string-trim-whitespace))
                        (* ,whitespace) eol)
                     (make-metadata-line k v)))

    (element        ((or ,metadata ,comment ,note ,section ,step)))

    (recipe         ((=> els
                         (* (: ,element ,(parse-ignore
                                           (parse-seq
                                             (parse-repeat nl)
                                             (parse-optional parse-end))))))
                     (concatenate! els))))

(define (parse-cook str)
  (let ((lis (parse-fully recipe str)))
    (let-values (((meta-lines body) (partition metadata-line? lis)))
      (make-recipe (metadata-line-list->hash-table meta-lines) body))))