Created
May 19, 2013 06:53
-
-
Save Jared314/5606912 to your computer and use it in GitHub Desktop.
Using Instaparse and Enlive for simple parsing and tree transformation / rewriting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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