Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-lyman committed Dec 30, 2011
1 parent 3807f4b commit 294ca64
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/com/lithinos/amotoen/core.clj
Expand Up @@ -17,6 +17,7 @@
(sp [t j] "Set pos") ; E.V.I.L. ... maybe (sp [t j] "Set pos") ; E.V.I.L. ... maybe
(in [t] "Indent - for debugging") (in [t] "Indent - for debugging")
(de [t] "Dedent - for debugging") (de [t] "Dedent - for debugging")
(end [t] "End of input")
(m [t] "Returns the 'c' then (inc pos)") (m [t] "Returns the 'c' then (inc pos)")
(c [t] "The character at pos")) (c [t] "The character at pos"))
(defn gen-ps (defn gen-ps
Expand All @@ -25,19 +26,21 @@
(let [j (ref j) (let [j (ref j)
indent (ref 0)] indent (ref 0)]
(reify IPosition (reify IPosition
(psdebug [t] (str (if (< @j 0) (psdebug [t]
(str "<-" (subs s 0 20)) (let [indent-string (apply str (take @indent (repeat " ")))
(str "'" (subs s (max 0 (- @j 30)) (max 0 @j)) "'" padding (apply str (take 60 (repeat " ")))
" " (c t) " " x (str "'" (pr-str (subs s (max 0 (- @j 30)) (max 0 @j))) "'")
"'" (subs s (inc @j) (min (+ @j 30) (count s))) "'")) y (str " " (pr-str (c t)) " ")
(apply str (take @indent (repeat " "))))) z (str "'" (pr-str (subs s (inc @j) (min (+ @j 30) (count s)))) "'")]
(str (subs (str (if (< @j 0) (str "<-" (subs s 0 20)) (str x y z)) padding) 0 60) indent-string)))
(in [t] (dosync (alter indent inc))) (in [t] (dosync (alter indent inc)))
(de [t] (dosync (alter indent dec))) (de [t] (dosync (alter indent dec)))
(gp [t] @j) (gp [t] @j)
(sp [t k] (dosync (ref-set j k))) (sp [t k] (dosync (ref-set j k)))
(clone [t] (gen-ps s @j)) (clone [t] (gen-ps s @j))
(end [t] (= @j (count s)))
(m [t] (let [r (c t)] (dosync (alter j inc)) r)) (m [t] (let [r (c t)] (dosync (alter j inc)) r))
(c [t] (.charAt s @j)))))) (c [t] (try (.charAt s @j) (catch Exception e nil)))))))
(defn lpegs [t s] (reverse (into '() (cons t (seq s))))) ; This doesn't need to be fast, but shouldn't the following work? (list (cons t (seq s))) (defn lpegs [t s] (reverse (into '() (cons t (seq s))))) ; This doesn't need to be fast, but shouldn't the following work? (list (cons t (seq s)))
(defn pegs [s] (vec (seq s))) (defn pegs [s] (vec (seq s)))


Expand Down Expand Up @@ -92,19 +95,29 @@
(defn- type-list [n g w] (defn- type-list [n g w]
(let [t (first n) (let [t (first n)
b (second n)] b (second n)]
(cond (= t '|) (let [temp (either n g w)] (debug w "Either returning:" temp) temp) (cond (= t '|) (let [temp (either n g w)] (debug w "Either returning:" (pr-str temp)) temp)
(= t '*) (list (doall (take-while #(if (keyword? b) (b %) %) (= t '*) (list (doall (take-while #(if (keyword? b) (b %) %)
(repeatedly #(pegasus b g w))))) (repeatedly #(pegasus b g w)))))
(= t '?) (pegasus b g w) (= t '?) (pegasus b g w)
; If we succeed, then we fail - that's the point of AnyNot... If we fail, then we accept the current char (= t '%) (let [c (c w)
(= t '%) (let [c (c w)] (if (pegasus b g w) nil (do (debug w "AnyNot MATCH:" (pr-str b)) (m w) c) ))))) p (gp w)]
(if (pegasus b g w)
(do ; If we succeed, then we fail - that's the point of AnyNot...
(sp w p) ; Don't forget 'rollback'
nil)
(do ; If we fail, then we accept the current char
(debug w "AnyNot MATCH:" (pr-str b))
(m w)
c))))))


(defn- try-char [n w] (defn- try-char [n w]
(if (= n (c w)) (if (= n (c w))
(do (do
(debug w (str "MATCH: '" (pr-str n) "' with '" (c w) "'")) (debug w (str "MATCH: '" (pr-str n) "' with '" (pr-str (c w)) "'"))
(m w)) (m w))
nil)) (do
(debug w (str "FAIL: '" (pr-str n) "' with '" (pr-str (c w)) "'"))
nil)))


(defn- peg-vec [n g w] (defn- peg-vec [n g w]
(loop [remaining n (loop [remaining n
Expand Down Expand Up @@ -134,9 +147,11 @@
([g] (validate g false)) ([g] (validate g false))
([g d] ([g d]
(dosync (ref-set *debug* d)) (dosync (ref-set *debug* d))
(if (nil? (pegasus :Grammar grammar-grammar (gen-ps (pr-str g)))) (let [w (gen-ps (pr-str g))]
(println "Fail") (if (or (nil? (pegasus :Grammar grammar-grammar w))
(println "Pass")) (not (end w)))
(println "Fail")
(println "Pass")))
(dosync (ref-set *debug* false)))) (dosync (ref-set *debug* false))))


(defn self-check [] (validate grammar-grammar)) (defn self-check [] (validate grammar-grammar))
Expand All @@ -145,5 +160,6 @@
; TODO ; TODO
; ;
; If pegasus is given a keyword, but it doesn't exist in the given grammar, a useful error should be thrown ; If pegasus is given a keyword, but it doesn't exist in the given grammar, a useful error should be thrown
; Allow debug to accept a 'limit' - if 0 then always dump everything. If more than 0, only keep limit number of lines of debug and print out at end
; ;


0 comments on commit 294ca64

Please sign in to comment.