Clojure Instaparse Parser Tutorial: Transform Parse Tree
The “transform” function takes a map and parse tree, and map the corresponding function into the parse tree. Basically, it lets you transform or eval the parse tree.
(instaparse.core/transform key_f_map tree)
-
traverse the parse tree tree depth first, and for each node in tree, apply the corresponding function in key_f_map. Return the result. The key_f_map has the form
{:key1 f1, :key2 f2, etc}
(insta/transform {:switch (fn …)} tree)
- … todo
(require 'instaparse.core) (def myTree [:S [:AB [:A "a" "a"] [:B "b" "b"]]]) (instaparse.core/transform {:A (fn [& args] (apply str args))} myTree) ;; returns: ;; [:S ;; [:AB "aa" ;; [:B "b" "b"]]]