-
Notifications
You must be signed in to change notification settings - Fork 16
/
analyzer.clj
56 lines (48 loc) · 1.28 KB
/
analyzer.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
(ns hx.compiler.analyzer
(:require [clojure.walk :as walk]
[hx.compiler.parser :as parser]))
(defn child? [leaf]
(or (:hx/parsed leaf)
(string? leaf)
(seq? leaf)))
(defn make-node [leaf & {:keys [props children js-interop?]}]
(-> leaf
(assoc
:hx/analyzed true
:props props
:children children
:js-interop? js-interop?)
(dissoc :args)))
(defmulti analyze-element
(fn [leaf]
(:el leaf))
:default ::default)
(defmethod analyze-element
::default
[leaf]
(if (:hx/parsed leaf)
(let [{:keys [el args]} leaf]
(if (child? (first args))
;; first arg is an element
(make-node
leaf
:props nil
:children args
:js-interop? (string? el))
(make-node
leaf
:props (first args)
:children (rest args)
:js-interop? (string? el))))
leaf))
(defn analyze [tree]
(walk/prewalk analyze-element tree))
(def interceptor
{:name :hx.compiler/analyzer
:enter (fn [context]
(assoc context ::out (analyze (::parser/out context))))})
#_(analyze
(hx.compiler.parser/parse
[:div [:span 'x "wat"]
[:input {:type "button"} [:i {:id "jkl"} "sup"]]]))
#_(analyze (hx.compiler.parser/parse ['asdf [:span 'jkl "wat"]]))