Clojure Instaparse Parser Tutorial: Transform Parse Tree

By Xah Lee. Date: . Last updated: .

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/function map tree) → traverse the parse tree tree depth first, and for each node in tree, apply the corresponding function in key/function map. Return the result. The key/function map has the form {:key1 f1, :key2 f2, …}

(insta/transform {:switch (fn …)} tree) → … todo

(require 'instaparse.core)

(def myTree [:S
              [:A "a" "a"]
              [:B "b" "b"]]])

 {:A (fn [& args] (apply str args))}
;; returns:

;; [:S
;;  [:AB "aa"
;;   [:B "b" "b"]]]

back to Clojure Instaparse Parser Tutorial

If you have a question, put $5 at patreon and message me.

Clojure Instaparse