What we have? ``` .container h1 { color: rgba(255, 0, 0, 0.9); font-size: 24px; font-family: Monaco; } ``` What we want to get? ```clojure {:selector ".container h1", :rules ({:key "color", :value "rgba(255, 0, 0, 0.9)"} {:key "font-size", :value "24px"} {:key "font-family", :value "Monaco"})} ``` Let's do this with [Monadic parsing](http://eprints.nottingham.ac.uk/223/1/pearl.pdf) approach. Step-by-step explanation of how it works one can find in [Monadic Parsing in Python](http://goo.gl/X7klJ8). What about Clojure? ## 1. Parser abstraction From theory: ```haskell type Parser a = String -> [(a, String)] ``` So, we're going to represent ```parser``` as simple function that takes input and return seq of possible results. Simplest parser: ```clojure ;; takes any input and "consume" first char from it (defn any [input] (if (empty? input) '() (list [(first input) (apply str (rest input))]))) (any "clojure-1.7") ;; => ([c lojure-1.7]) ``` Will use few helpers: ```clojure (defn parse [parser input] (parser input)) (defn parse-all [parser input] (ffirst (filter #(= "" (second %)) (parse parser input)))) ``` ## 2. Monads ## 3. Basic parsers ## 4. Combinators ## 5. CSS