Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Instaparse and Enlive for simple parsing and tree transformation / rewriting
(ns test1
(:require [instaparse.core :as insta]
[net.cgrand.enlive-html :as enlive]
[clojure.walk :as walk]))
(defn render [nodes]
(walk/postwalk #(if (and (map? %) (:tag %))
(with-meta (hash-map (:tag %) (:content %)) (:attrs %))
%) nodes))
(def parse (insta/parser
"S = AB*
AB = A B
A = 'a'+
B = 'b'+"
:output-format :enlive))
(def data (parse "aaaaabbbaaaabb"))
(def transformed-data
(enlive/at data
[:S] enlive/unwrap
[:A] nil))
(render transformed-data) ;=> ({:AB ({:B ("b" "b" "b")})} {:AB ({:B ("b" "b")})})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment