Skip to content

Instantly share code, notes, and snippets.

@Jared314
Created May 19, 2013 06:53
Show Gist options
  • Save Jared314/5606912 to your computer and use it in GitHub Desktop.
Save Jared314/5606912 to your computer and use it in GitHub Desktop.
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