Skip to content

Instantly share code, notes, and snippets.

@gigasquid
Created June 5, 2013 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigasquid/5713819 to your computer and use it in GitHub Desktop.
Save gigasquid/5713819 to your computer and use it in GitHub Desktop.
Clojure and Coffee with Instaparse
(ns brackets.core-test
(:use clojure.test
brackets.core
instaparse.core))
(def my-parser
(parser
"S = func-apply | vector | integer
integer = #'[0-9]+'
<space> = <' '>
vector = <'['> space* (integer space*)* <']'>
function = #'[a-z-]+'
func-apply = function space* vector
"
))
(my-parser "[1]")
(def transform-options
{:S identity
:integer read-string
:vector (comp vec list)
:function (comp resolve read-string)
:func-apply (comp eval list)
})
(defn pparse [input]
(->> (my-parser input) (transform transform-options)))
(pparse "count [1 2 3]")
(pparse "second [1 2 3]")
(deftest integer-test
(testing "ints"
(is (= (pparse "1") 1))))
(deftest vector-test
(testing "vector"
(is (= (pparse "[1]") [1]))
(is (= (pparse "[1 2 3]") [1 2 3]))))
(deftest func-apply-test
(testing "func-apply"
(is (= (pparse "count [1]") 1))
(is (= (pparse "count [1 2 3]") 3))
(is (= (pparse "second [1 2 3]") 2))
))
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment