Skip to content

Commit

Permalink
Support for parsing literal symbols, chars, booleans, regexes and nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Tero Parviainen committed Apr 12, 2012
1 parent a83216f commit 1b94418
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/marginalia/parser.clj
Expand Up @@ -109,7 +109,7 @@

(defn parse* [reader]
(take-while
:form
#(not= :_eof (:form %))
(flatten
(repeatedly
(fn []
Expand All @@ -118,7 +118,7 @@
(let [start (.getLineNumber reader)
form (binding [*comments* sub-level-comments]
(try (. clojure.lang.LispReader
(read reader false nil false))
(read reader false :_eof false))
(catch Exception ex
(let [msg (str "Problem parsing near line " start
" <" (.readLine reader) ">"
Expand Down Expand Up @@ -247,12 +247,14 @@
[form raw nspace-sym]
[nil raw])

(defn- literal-form? [form]
(or (string? form) (number? form) (keyword? form) (symbol? form)
(char? form) (true? form) (false? form) (instance? java.util.regex.Pattern form)))

(defmethod dispatch-form :default
[form raw nspace-sym]
;; Strings which are inlined into clojure files outside of forms are parsed
;; as `String` instances, while numbers - as `Number` subclasses.
(cond (or (string? form) (number? form) (keyword? form))
(dispatch-literal form raw nspace-sym)
(cond (literal-form? form)
(dispatch-literal form raw nspace-sym)
(and (first form)
(.isInstance clojure.lang.Named (first form))
(re-find #"^def" (-> form first name)))
Expand Down

0 comments on commit 1b94418

Please sign in to comment.