Permalink
Browse files

adding a new example that tries to parse a clojure code.

* known bug: set syntax #{} is ignored
  • Loading branch information...
ujihisa committed Mar 20, 2012
1 parent 920b836 commit aa0db3ed4e61ab0688a8eedf0f37910546f30cfe
Showing with 41 additions and 0 deletions.
  1. +41 −0 src/zetta/examples/clojure.clj
@@ -0,0 +1,41 @@
+(ns zetta.examples.clojure
+ ^{:doc "A parser for a subset of Clojure"}
+ (:refer-clojure :exclude [char])
+ (:require [clojure.core :as core]
+ [clojure.string :as str]
+ [clojure.java.io :as io]
+ [zetta.core :as z])
+
+ (:use [zetta.core :only [<$> <* *>]])
+ (:use
+ [zetta.combinators
+ :only
+ [sep-by around many many1 choice]]
+ [zetta.parser.seq
+ :only
+ [string char not-char number whitespace]]))
+
+(def whitespaces
+ (many whitespace))
+
+(def clojure
+ (around
+ whitespaces
+ (choice
+ [(<$> (comp #(Integer/parseInt %) str/join)
+ (many1 number))
+ (<$> str/join ;(comp symbol str/join)
+ (many1 (not-char #{\ \newline \( \) \[ \] \#})))
+ (<$> str/join
+ (*> (string "#{")
+ (<* (sep-by clojure whitespaces) (char \}))))
+ (<$> (partial apply vector)
+ (*> (char \[)
+ (<* (sep-by clojure whitespaces) (char \]))))
+ (*> (char \()
+ (<* (sep-by clojure whitespaces) (char \))))])))
+
+(defn -main []
+ (let [x (z/parse-once clojure "(defn f [x]\n [x 'a #{1 1 2} 123])")]
+ (prn (:result x))
+ (prn (:remainder x))))

0 comments on commit aa0db3e

Please sign in to comment.