This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

make sure we preserve metadata on the roundtrip

  • Loading branch information...
1 parent dd80f2c commit 1e35725d6a0128720a0a17aebb1e61e64d7d5588 @ztellman committed Sep 21, 2013
Showing with 59 additions and 63 deletions.
  1. +3 −2 project.clj
  2. +56 −61 src/sleight/reader.clj
View
@@ -1,6 +1,7 @@
-(defproject sleight "0.2.0"
+(defproject sleight "0.2.1-SNAPSHOT"
:description "whole-program transformations for clojure"
- :dependencies []
+ :dependencies [[org.clojars.trptcolin/sjacket "0.1.0.3"
+ :exclusions [org.clojure/clojure]]]
:profiles {:dev {:dependencies [[org.clojure/clojure "1.5.1"]
[riddley "0.1.5-SNAPSHOT"]]}}
:plugins [[lein-sleight "0.2.0"]]
View
@@ -32,76 +32,71 @@
;;;
(defn ->line-numbering-reader [r]
- (if (instance? LineNumberingPushbackReader r)
- r
- (LineNumberingPushbackReader. r)))
+ (if (instance? LineNumberingPushbackReader r)
+ r
+ (LineNumberingPushbackReader. r)))
- (defn empty-reader? [^PushbackReader r]
- (let [char (.read r)]
- (if (= -1 char)
- true
- (do
- (.unread r char)
- false))))
-
- (defn reader->forms [r]
- (let [r (->line-numbering-reader r)]
- (->> #(LispReader/read r false ::eof false)
- repeatedly
- (take-while #(not= ::eof %)))))
+(defn reader->forms [r]
+ (let [r (->line-numbering-reader r)]
+ (->> #(LispReader/read r false ::eof false)
+ repeatedly
+ (take-while #(not= ::eof %)))))
;;;
- (defn newline-generator []
- (let [counter (atom 1)]
- (fn [current-line]
- (if-not current-line
- ""
- (let [diff (max 0 (- current-line @counter))]
- (swap! counter + diff)
- (->> "\n" (repeat diff) (apply str)))))))
+(defn newline-generator []
+ (let [counter (atom 1)]
+ (fn [current-line]
+ (if-not current-line
+ ""
+ (let [diff (max 0 (- current-line @counter))]
+ (swap! counter + diff)
+ (->> "\n" (repeat diff) (apply str)))))))
- (defn line-preserving-pr-str [newlines & vals]
- (binding [*newlines* newlines
- *print-dup* true]
- (apply pr-str vals)))
+(defn line-and-meta-preserving-pr-str [newlines x]
+ (binding [*newlines* newlines
+ *print-dup* true]
+ (str
+ (when-let [m (meta x)]
+ (str "^" (pr-str m) " "))
+ (pr-str x))))
;;;
- (defn lazy-reader-seq [s]
- (let [s (atom s)]
- (proxy [Reader] []
- (close []
- )
- (read [cbuf offset len]
- (if-let [^Reader r (first @s)]
- (let [c (.read r)]
- (if (= -1 c)
- (do
- (swap! s rest)
- (.read this cbuf offset len))
- (do
- (aset cbuf offset (char c))
- 1)))
- -1)))))
+(defn lazy-reader-seq [s]
+ (let [s (atom s)]
+ (proxy [Reader] []
+ (close []
+ )
+ (read [cbuf offset len]
+ (if-let [^Reader r (first @s)]
+ (let [c (.read r)]
+ (if (= -1 c)
+ (do
+ (swap! s rest)
+ (.read this cbuf offset len))
+ (do
+ (aset cbuf offset (char c))
+ 1)))
+ -1)))))
- (defn dechunk [s]
- (when-not (empty? s)
- (cons
- (first s)
- (lazy-seq
- (dechunk (rest s))))))
+(defn dechunk [s]
+ (when-not (empty? s)
+ (cons
+ (first s)
+ (lazy-seq
+ (dechunk (rest s))))))
;;;
- (defn transform-reader [transform r]
- (let [_ @switched-printer ;; prime the switched printer
- newlines (newline-generator)]
- (->> r
- reader->forms
- dechunk
- (map transform)
- (map #(line-preserving-pr-str newlines %))
- (map #(StringReader. %))
- lazy-reader-seq
- LineNumberingPushbackReader.)))
+(defn transform-reader [transform r]
+ (let [_ @switched-printer ;; prime the switched printer
+ newlines (newline-generator)]
+ (->> r
+ reader->forms
+ dechunk
+ (map transform)
+ (map #(line-and-meta-preserving-pr-str newlines %))
+ (map #(StringReader. %))
+ lazy-reader-seq
+ LineNumberingPushbackReader.)))

0 comments on commit 1e35725

Please sign in to comment.