-
Notifications
You must be signed in to change notification settings - Fork 1
/
css3.clj
476 lines (411 loc) · 17.5 KB
/
css3.clj
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
(ns html5-css3-ebnf.css3
(:require [clojure.data.json :as json]
[clojure.string :as string]
[clojure.tools.cli :refer [parse-opts]]
[instacheck.core :as instacheck]
[instaparse.core :as instaparse]))
;; TODO: global properties (inherit, initial, unset, revert)
;; https://developer.mozilla.org/en-US/docs/Web/CSS/Value_definition_syntax
;; https://www.smashingmagazine.com/2016/05/understanding-the-css-property-value-syntax/
;; https://www.w3.org/TR/CSS21/grammar.html
;; https://github.com/mdn/data/tree/master/css
;; https://github.com/csstree/csstree/
;; https://csstree.github.io/docs/syntax.html
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn filter-css-definitions
"Filter properties we want (also removes '--*')"
[defs]
(into {} (filter (fn [[name attrs]]
(and
(re-find #"^[@a-z-]+$" name)
(= (get attrs "status") "standard")))
defs)))
(defn mangle-css-syntaxes
"Fixup syntax definitions."
[syntaxes]
(into {} (for [[k {:strs [syntax] :as v}] syntaxes]
(cond
;; TODO: file bug against github.com/mdn/data
(= syntax "<custom-ident>: <integer>+;")
[k {"syntax" "<custom-ident> : <integer>+ ;"}]
;; TODO: file bug against github.com/mdn/data
(= syntax "rect(<top>, <right>, <bottom>, <left>)")
[k {"syntax" "rect( <top>, <right>, <bottom>, <left> ) | rect( <top> <right> <bottom> <left> )"}]
;; <media-condition>, <media-condition-without-or>, and
;; <supports-condition> are currently mutually
;; recursive definitions (unsupported). Rewrite them
;; to pull in child syntax in order to make them
;; directly recursive (supported).
;; Original <media-condition> syntax:
;; "<media-not> | <media-and> | <media-or> | <media-in-parens>"
(= k "media-condition")
[k {"syntax" "[ not? [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] ] | [ [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] [ [ and | or ] [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] ]+ ]"}]
;; Original <media-condition-without-or> syntax:
;; "<media-not> | <media-and> | <media-in-parens>"
(= k "media-condition")
[k {"syntax" "[ not? [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] ] | [ [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] [ and [ ( <media-condition> ) | <media-feature> | <general-enclosed> ] ]+ ]"}]
;; Original <supports-condition> syntax:
;; "not <supports-in-parens> | <supports-in-parens> [ and <supports-in-parens> ]* | <supports-in-parens> [ or <supports-in-parens> ]*"
(= k "supports-condition")
[k {"syntax" "not [ ( <supports-condition> ) | <supports-feature> | <general-enclosed> ] | [ ( <supports-condition> ) | <supports-feature> | <general-enclosed> ] [ and [ ( <supports-condition> ) | <supports-feature> | <general-enclosed> ] ]* | [ ( <supports-condition> ) | <supports-feature> | <general-enclosed> ] [ or [ ( <supports-condition> ) | <supports-feature> | <general-enclosed> ] ]*"}]
;; Drop unused syntaxes that also have recursion
(= k "page-body")
nil
:else
[k v]))))
(defn mangle-css-at-rules
"Fixup the at-rules definitions."
[at-rules]
(into {} (for [[k {:strs [syntax] :as v}] at-rules]
(cond
;; TODO: MDN data doesn't have a way of representing
;; non-standard descriptors in the parent syntax. Add
;; non-standard font-display to parent.
(= k "@font-face")
[k (assoc v "syntax" (string/replace syntax #"\n*}$" " ||\n [ font-display: <font-display>; ]\n}"))]
:else
[k v]))))
;; TODO: these from descriptors syntaxes are the only unique ones
;; actually referenced by at-rules: 'src', 'unicode-range',
;; 'font-variation-settings', 'suffix', 'speak-as', 'range', 'prefix',
;; 'additive-symbols'. 'font-display' isn't referenced in the parent
;; but we add it above in the mangler.
(def include-descriptors #{"src" "font-display"})
(defn css-vds-combined [properties syntaxes at-rules]
(let [syns (mangle-css-syntaxes syntaxes)
arules (mangle-css-at-rules at-rules)
ps (for [[prop {:strs [syntax]}] (sort properties)]
(if (= prop "all")
(str "<'" prop "'> = " syntax "\n")
(str "<'" prop "'> = <'all'> | " syntax "\n")))
ss (for [[syn {:strs [syntax]}] (sort syns)]
(str "<" syn "> = " syntax "\n"))
as (for [[rule-name {:strs [syntax]}] (sort arules)]
(str "<'" rule-name "'> = " syntax "\n"))
ads (for [[_ {:strs [descriptors]}] (sort arules)
[rule-name {:strs [syntax]}] (sort descriptors)
:when (get include-descriptors rule-name)]
(str "<" rule-name "> = " syntax "\n"))]
(apply str (concat ps
["\n\n"]
ss
["\n\n"]
as
["\n\n"]
ads))))
(comment
(spit "data/css3.vds" (css-vds-combined
(filter-css-definitions
(json/read-str (slurp "./mdn_data/css/properties.json")))
(json/read-str (slurp "./mdn_data/css/syntaxes.json"))))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn parsed-tree->items [tree]
(assert (= :assignments (first tree))
(str "Parse tree started with "
(first tree) " rather than :assignments"))
(assert (every? #(and (vector? %) (= :assignment (first %)))
(drop 1 tree))
"Parse tree contained invalid property")
(for [prop (drop 1 tree)]
(let [ptype (first (second prop))
name (second (second prop))
data (nth prop 2)]
(condp = ptype
:property [(str "'" name "'") data]
:non-property [name data]))))
(defn parsed-tree->map [tree]
(let [items (parsed-tree->items tree)
repeats (filter #(> (val %) 1) (frequencies (map first items)))]
(assert ;;(= (count items) (count (set (map first items))))
(empty? repeats)
(str "Repeated properties:" (vector repeats)))
(into {} items)))
(comment
(def css3-syntax-parser (instaparse/parser "resources/css-vds.ebnf"))
;; Takes 4 seconds
(def parse-tree (css3-syntax-parser (slurp "data/css3.vds")))
(def parse-map (parsed-tree->map parse-tree))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(declare juxtapose-ebnf double-amp-ebnf double-bar-ebnf component-ebnf
component-single-ebnf component-multiplied-ebnf brackets-ebnf
block-ebnf func-ebnf braces-ebnf)
;; Operator precendence summary:
;; mult, juxt, &&, ||, |
;;
;; Notes:
;; - juxtaposition has precedence over the double ampersand, meaning
;; that
;; bold thin && <length> is equivalent to
;; [ bold thin ] && <length>
;; - the double ampersand has precedence over the double bar, meaning
;; that
;; bold || thin && <length> is equivalent to
;; bold || [ thin && <length> ]
;; - the double bar has precedence over the single bar, meaning that
;; bold | thin || <length> is equivalent to
;; bold | [ thin || <length> ]
;; - multipliers cannot be added and have all precedence over
;; combinators.
;;
(defn name-ebnf [k]
(-> (cond
(= \' (first k))
(str "prop-" (string/replace k #"'" ""))
(re-find #"\(\)$" k)
(str "func-" (string/replace k #"\(\)$" ""))
:else
(str "nonprop-" k))
(string/replace #"@" "AT-")))
(defn single-bar-ebnf
"One of the values must occur."
[tree indent]
;;(prn :** :single-bar-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))]
(if (= 1 (count tree))
(double-bar-ebnf (drop 1 (first tree)) indent)
(str pre "(\n"
(string/join
(str " |\n")
(for [t tree]
(double-bar-ebnf (drop 1 t) (+ 1 indent))))
"\n"
pre ")"))))
;; TODO: for EBNF we just treat this like a single-bar with '+'
;; appended. This means that we may get more than one of each
;; element.
(defn double-bar-ebnf
"One or more of the values must occur in any order."
[tree indent]
;;(prn :** :double-bar-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))]
(if (= 1 (count tree))
(double-amp-ebnf (drop 1 (first tree)) indent)
(str pre "( (\n"
(string/join
(str " |\n")
(for [t tree]
(double-amp-ebnf (drop 1 t) (+ 1 indent))))
"\n"
pre ")+ )"))))
;; TODO: for EBNF we just treat this as juxtapose we means we use the
;; original order it is defined with. Fix this to allow any order.
(defn double-amp-ebnf
"All values must occur in any order."
[tree indent]
;;(prn :** :double-amp-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))]
(if (= 1 (count tree))
(juxtapose-ebnf (drop 1 (first tree)) indent)
(str pre "(\n"
(string/join
"\n"
(for [t tree]
(if (= :comma (first t))
(str pre " ',' S")
(juxtapose-ebnf (drop 1 t) (+ 1 indent)))))
"\n"
pre ")"))))
(defn juxtapose-ebnf
"Each value must occur."
[tree indent]
;;(prn :** :juxtapose-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))]
(if (= 1 (count tree))
(component-ebnf (second (first tree)) indent)
(str pre "(\n"
(string/join
"\n"
(for [t tree]
(if (= :comma (first t))
(str pre " ',' S")
(component-ebnf (second t) (+ 1 indent)))))
"\n"
pre ")"))))
(defn component-ebnf [tree indent]
;;(prn :** :component-ebnf (count tree) tree :indent indent)
(str
(condp = (first tree)
:component-single (component-single-ebnf (second tree) indent)
:component-multiplied (component-multiplied-ebnf (drop 1 tree) indent))))
(defn component-single-ebnf [tree indent]
;;(prn :** :component-single-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))]
(condp = (first tree)
:literal (str pre "('" (second tree) "' S)")
:keyword-value (str pre "('" (second tree) "' S)")
:non-property (str pre (name-ebnf (second tree)))
:property (str pre (name-ebnf (second tree)))
:brackets (brackets-ebnf (drop 1 tree) indent)
:block (block-ebnf (drop 1 tree) indent)
:func (func-ebnf (drop 1 tree) indent)
)))
(defn component-multiplied-ebnf [[tree multiplier] indent]
;;(prn :** :component-multiplied-ebnf (count tree) tree :indent indent)
(let [pre (apply str (repeat indent " "))
single (partial component-single-ebnf (second tree))]
(condp = (first (second multiplier))
:question (str (single indent) "?")
:asterisk (str (single indent) "*")
:plus (str (single indent) "+")
:hash (str (single indent) " (\n"
pre " ',' S\n"
(single (+ 1 indent)) "\n"
pre ")*")
:braces (braces-ebnf (second (second multiplier))
false (single (+ 2 indent)) indent)
:hash-braces (braces-ebnf (second (second (second multiplier)))
true (single (+ 2 indent)) indent)
)))
(defn brackets-ebnf [tree indent]
;;(prn :** :brackets-ebnf tree indent)
;; TODO: deal with bang?
(single-bar-ebnf (drop 1 (first tree)) indent))
(defn func-ebnf [tree indent]
;;(prn :** :func-ebnf tree indent)
(let [pre (apply str (repeat indent " "))]
(str pre "'" (first tree) "(' S\n"
(single-bar-ebnf (drop 1 (second tree)) (+ 1 indent)) "\n"
pre "')' S")))
(defn block-ebnf [tree indent]
;;(prn :** :block-ebnf tree indent)
(let [pre (apply str (repeat indent " "))]
(condp = (first (first tree))
\{ (str pre "'{' S\n"
(single-bar-ebnf (drop 1 (second tree)) (+ 1 indent)) "\n"
pre "'}' S")
:single-bar (single-bar-ebnf (drop 1 (first tree)) indent))))
(defn braces-ebnf [kind hash? single indent]
;;(prn :** :braces-ebnf kind hash? single indent)
(let [pre (apply str (repeat indent " "))
bmin (Integer. (second (second kind)))
bmax (Integer. (second (nth kind 2 [:digit bmin])))]
;;(assert (= :bracesA-B (first kind))
;; (str "Unsupported curly braces repeat form:" kind))
(assert (and (>= bmin 0)
(>= bmax bmin)
(<= bmax 20))
(str "Unsupported curly braces range: " bmin "-" bmax))
(str pre "(\n"
pre " (\n"
(string/join
" |\n"
(for [i (range bmin (+ 1 bmax))]
(string/join
(if hash? " ',' S\n" " rS\n")
(if (= i 0)
(str pre "''")
(repeat i single)))))
"\n"
pre " )\n"
pre ")")))
;;
(defn value-ebnf [k v]
;;(prn :value-ebnf :k k :v v)
(str (name-ebnf k) " =\n"
(single-bar-ebnf (drop 1 v) 1)))
(defn map->ebnf [m]
(string/join
" ;\n\n"
(for [[k v] m] (value-ebnf k v))))
(comment
;; Takes 4 seconds
(def css-tree (css3-syntax-parser (slurp "data/css3.vds")))
(def css-map (parsed-tree->map css-tree))
;; The following takes 25 seconds
(def css-ebnf (map->ebnf css-map))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn pr-err
[& args]
(binding [*out* *err*]
(apply println args)))
(defn opt-errors [opts]
(when (:errors opts)
(doall (map pr-err (:errors opts)))
(System/exit 2))
opts)
(def cli-options
[[nil "--vds-grammar EBNF-OUTPUT"
"Path to generic VDS grammar definition."
:default "./resources/css-vds.ebnf"]
[nil "--css-vds-output CSS-VDS-OUTPUT"
"Write full CSS VDS syntax to file"
:default "./data/css3.vds"]
[nil "--ebnf-output EBNF-OUTPUT"
"Write intermediate EBNF to file"
:default "./data/css3.ebnf"]
[nil "--css-properties CSS-PROPERTIES"
"Path to CSS properties JSON file."
:default "./mdn_data/css/properties.json"]
[nil "--css-syntaxes CSS-SYNTAXES"
"Path to CSS syntaxes JSON file."
:default "mdn_data/css/syntaxes.json"]
[nil "--css-at-rules CSS-AT-RULES"
"Path to CSS at-rules JSON file."
:default "mdn_data/css/at-rules.json"]
[nil "--ebnf-prefix EBNF-PREFIX"
"Path to prefix file to include in EBNF output"
:default "./resources/css3-prefix.ebnf"]
[nil "--ebnf-base EBNF-BASE"
"Path to base grammar file to include in EBNF output"
:default "./resources/css3-base.ebnf"]
[nil "--ebnf-common EBNF-COMMON"
"Path to common rules to include in EBNF output"
:default "./resources/common.ebnf"]])
(defn css-known-ebnf [suffix props]
(str "css-known-" suffix " =\n"
" (\n"
(string/join
" |\n"
(for [p props]
(str " \"" p "\" S \":\" S prop-" p "")))
"\n"
" ) ;"))
(defn ebnf-combined-str [css-map properties opts]
(let [cfilt (fn [pred xs] (filter #(pred (get (val %) "status")) xs))
standard (map first (cfilt #(= "standard" %) properties))
;;nonstandard (map first (cfilt #(not= "standard" %) properties))
]
(string/join
"\n\n"
["(* Generated by mend.w3c.css3 *)"
(slurp (:ebnf-prefix opts))
(css-known-ebnf "standard" (sort standard))
;;(css-known-ebnf "nonstandard" (sort nonstandard))
(map->ebnf (sort-by key css-map))
(slurp (:ebnf-base opts))
(slurp (:ebnf-common opts))])))
(defn -main [& args]
"Generate a CSS 3 EBNF grammar based on specification data from
Mozilla Developer Network (MDN).
This takes about 11 seconds to run."
(let [opts (:options (opt-errors (parse-opts args cli-options)))
_ (pr-err "Creating VDS grammar parser from:" (:vds-grammar opts))
css3-syntax-parser (instaparse/parser (:vds-grammar opts))
properties-file (:css-properties opts)
syntaxes-file (:css-syntaxes opts)
at-rules-file (:css-at-rules opts)
_ (pr-err "Generating full CSS VDS grammar based on:"
properties-file syntaxes-file at-rules-file)
properties (filter-css-definitions
(json/read-str (slurp properties-file)))
syntaxes (json/read-str (slurp syntaxes-file))
at-rules (filter-css-definitions
(json/read-str (slurp at-rules-file)))
vds-text (css-vds-combined properties syntaxes at-rules)
_ (when-let [pfile (:css-vds-output opts)]
(pr-err "Saving full CSS VDS grammar file to:" pfile)
(spit pfile vds-text))
_ (pr-err "Parsing CSS VDS grammar")
css-tree (instacheck/parse css3-syntax-parser vds-text)
css-map (parsed-tree->map css-tree)
_ (pr-err "Converting CSS VDS grammar to EBNF")
css3-ebnf-str (ebnf-combined-str css-map properties opts)]
(println "Saving EBNF to" (:ebnf-output opts))
(spit (:ebnf-output opts) css3-ebnf-str)
(println "Checking CSS3 EBNF grammar")
;; The following takes 5 seconds
(instaparse/parser css3-ebnf-str)))