-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
61 lines (54 loc) · 2.08 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns css-parser.core
(:require [instaparse.core :as p]))
(defn css-parser-full-file []
(p/parser (str "file = (rule | sh+ | at-rules)+ \n"
(slurp (clojure.java.io/resource "rules.bnf"))
"\n"
(slurp (clojure.java.io/resource "types.bnf")))))
(defn css-parser-single-rule []
(p/parser (str "single-rule = sh? (rule | at-rules) \n"
(slurp "resources/rules.bnf")
"\n"
(slurp "resources/types.bnf"))))
(defn eliminate-comment [string]
(let [re (re-pattern #"\*\/(?s)(.*)\*/")]
(clojure.string/replace string re "")))
(defn eliminate-blank [string]
(clojure.string/replace string #"(\{|;)(\s)" "$1"))
(defn- merge-lines [seq-of-lines]
(concat (first seq-of-lines)
(lazy-seq
(merge-lines (rest seq-of-lines)))))
(defn file-reader->char-seq [f]
(let [lines (line-seq f)]
(merge-lines lines)))
(defn split-char-seq-in-rules [sequence]
(loop [s sequence
counter 0
buffer (transient [])]
(if (= -1 counter) ;;
(cons (apply str (persistent! buffer))
(lazy-seq (split-char-seq-in-rules s)))
(if (not (seq s)) ;; if the sequence is terminate we return the buffer and nil to indicate the end
(cons (apply str (persistent! buffer)) nil)
(let [actual (first s)]
(case counter
1 (recur (rest s) ;; possible next cut
(case actual
\{ (inc counter)
\} -1
counter)
(conj! buffer actual))
0 (recur (rest s) ;; "normal" state
(if (= \{ actual)
1 0)
(conj! buffer actual))
-1 (recur (rest s) ;; sequence to cut here
0
(transient []))
(recur (rest s) ;; most generic state
(case actual
\{ (inc counter)
\} (dec counter)
counter)
(conj! buffer actual))))))))