Skip to content

Instantly share code, notes, and snippets.

@Jared314
Last active December 26, 2015 04:39
Show Gist options
  • Save Jared314/7095415 to your computer and use it in GitHub Desktop.
Save Jared314/7095415 to your computer and use it in GitHub Desktop.
(ns stuff
(:require [instaparse.core :as insta]
[clojure.walk :as walk]))
(defn hiccup->sexp [sym-ns data]
(let [sym-ns-fn #(symbol sym-ns %)]
(walk/postwalk (fn [x] (if (and (vector? x) (keyword? (first x)))
(conj (rest x) (-> x first name sym-ns-fn))
x))
data)))
(defn run-lang [lang-n grammar s]
(let [parse-lang (if (ifn? grammar)
grammar
(insta/parser grammar))]
(eval (hiccup->sexp lang-n (parse-lang s)))))
(defn register-lang
([grammar] (register-lang *ns* grammar))
([lang-ns grammar]
(let [lang-n (str lang-ns)]
(set! *data-readers* (assoc *data-readers*
(symbol "custom" lang-n)
(partial run-lang lang-n (insta/parser grammar)))))))
(ns lang1
(:require [clojure.string :as string]))
(defn question [value] (str value " GET BACK TO WORK!"))
(defn statement [value] (str (string/upper-case value) "!?!"))
(stuff/register-lang "question = <'what'> <' '+> statement <' '*> <'?'>
statement = #'[a-z\\s]+[a-z]'")
#custom/lang1 "what about this?" ;; => "ABOUT THIS!?! GET BACK TO WORK!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment