What we have?
.container h1 {
color: rgba(255, 0, 0, 0.9);
font-size: 24px;
font-family: Monaco;
}
What we want to get?
{: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 approach. Step-by-step explanation of how it works one can find in Monadic Parsing in Python.
What about Clojure?
From theory:
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:
;; 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:
(defn parse [parser input]
(parser input))
(defn parse-all [parser input]
(ffirst (filter #(= "" (second %)) (parse parser input))))