PageRenderTime 24ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/sumarian/src/com/ociweb/compressor.clj

http://hdbc.googlecode.com/
Clojure | 49 lines | 39 code | 10 blank | 0 comment | 0 complexity | 7e550ae306a0a81fbee4784d55309366 MD5 | raw file
 1
 2(ns com.ociweb.compressor
 3  (:gen-class)
 4  (:use clojure.contrib.duck-streams)
 5  (:import (java.io File)))
 6
 7(defn procedure-name [ln]
 8  (second(re-find #"(\w{1,})\s*\(" ln)))
 9
10(defn is-public-procedure? [ln]
11  (.find
12   (re-matcher #"\s*public(\s+static)?(\s+final)?\s+[\w, <>]{1,}\s+\w{1,}\s*\(" ln)))
13
14(defn compress-line [ln]
15  (let [matches (re-seq #"[;{}]" ln)]
16    (reduce str matches)))
17
18(defn- char-count [s char]
19  (reduce (fn [v a] (if (= char a) (inc v) v)) 0 s))
20
21(defn compressed-proc-terminated? [cln]
22  (let [left-bracket-count (char-count cln \{)
23	right-bracket-count (char-count cln \})]
24    (if (< left-bracket-count right-bracket-count)
25      (throw (IllegalStateException. "we have read past the end of a procedure")))
26    (if (not (.contains cln "{"))
27      false
28      (if (> left-bracket-count right-bracket-count)
29	false
30	(if (= left-bracket-count right-bracket-count)
31	  true)))))
32
33(defn compress-lines [ls acc]
34  (if (or (empty? ls) (compressed-proc-terminated? acc))
35    acc
36    (compress-lines (rest ls) (str acc (compress-line (first ls))))))
37
38
39(defn lines2summarys [lines]
40  (loop [result '() ls lines]
41    (if (empty? ls)
42      result
43      (if (is-public-procedure? (first ls))
44	(recur (cons (procedure-name (first ls)) (cons (compress-lines ls "") result)) (rest ls))
45	(recur result (rest ls))))))
46
47(defn content-summary [#^File f]
48  (let [lines (read-lines f)]
49    (lines2summarys lines)))