Keep working on translating parser
This commit is contained in:
parent
70e8763dc5
commit
a65b4ba873
|
@ -1,3 +1,6 @@
|
||||||
|
### in repl, make sure to (os/cwd) into the janet dir
|
||||||
|
(import ./clj-loop :prefix "")
|
||||||
|
|
||||||
(defn ? [val default] (if (nil? val) default val))
|
(defn ? [val default] (if (nil? val) default val))
|
||||||
|
|
||||||
(defn ok? [{:status status}]
|
(defn ok? [{:status status}]
|
||||||
|
@ -180,7 +183,7 @@
|
||||||
{:name name
|
{:name name
|
||||||
:rule (fn order-fn [tokens]
|
:rule (fn order-fn [tokens]
|
||||||
(let [origin (first tokens)]
|
(let [origin (first tokens)]
|
||||||
(loop [ps parsers
|
(clj-loop [ps parsers # now we can use my handy macro
|
||||||
results []
|
results []
|
||||||
ts tokens]
|
ts tokens]
|
||||||
(let [result (apply-parser (first ps) ts)
|
(let [result (apply-parser (first ps) ts)
|
||||||
|
@ -220,21 +223,23 @@
|
||||||
(:err :none)
|
(:err :none)
|
||||||
(update result :trace |(array/push $ name))))))))})
|
(update result :trace |(array/push $ name))))))))})
|
||||||
|
|
||||||
|
|
||||||
(defn quiet [parser]
|
(defn quiet [parser]
|
||||||
{:name (kw+str (? (get parser :name) parser) "-quiet")
|
{:name (kw+str (? (get parser :name) parser) "-quiet")
|
||||||
:rule (fn quiet-fn [tokens]
|
:rule (fn quiet-fn [tokens]
|
||||||
(let [result (apply-parser parser tokens)]
|
(let [result (apply-parser parser tokens)]
|
||||||
(if (pass? result)
|
(if (pass? result)
|
||||||
(assoc result :status :quiet)
|
(put result :status :quiet)
|
||||||
result)))})
|
result)))})
|
||||||
|
|
||||||
|
### Start here
|
||||||
|
### How to write variadic functions here that preserve this logic
|
||||||
|
### Janet doesn't allow for Clojure's multiple clauses, :(
|
||||||
(defn zero+
|
(defn zero+
|
||||||
([parser] (zero+ (pname parser) parser))
|
([parser] (zero+ (pname parser) parser))
|
||||||
([name parser]
|
([name parser]
|
||||||
{:name (kw+str name "-zero+")
|
{:name (kw+str name "-zero+")
|
||||||
:rule (fn zero+fn [tokens]
|
:rule (fn zero+fn [tokens]
|
||||||
(loop [results []
|
(clj-loop [results []
|
||||||
ts tokens]
|
ts tokens]
|
||||||
(let [result (apply-parser parser ts)]
|
(let [result (apply-parser parser ts)]
|
||||||
(case (get result :status)
|
(case (get result :status)
|
||||||
|
@ -310,14 +315,16 @@
|
||||||
:rule (fn group-fn [tokens]
|
:rule (fn group-fn [tokens]
|
||||||
(let [result (apply-parser parser tokens)]
|
(let [result (apply-parser parser tokens)]
|
||||||
(if (= :group (get result :status))
|
(if (= :group (get result :status))
|
||||||
(assoc result :status :ok)
|
(put result :status :ok)
|
||||||
result)))}))
|
result)))}))
|
||||||
|
|
||||||
(defn err-msg [{token :token trace :trace}]
|
(defn err-msg [{:token token :trace trace}]
|
||||||
(println "Unexpected token " (get token :type) " on line " (get token :line))
|
(print "Unexpected token " (get token :type) " on line " (get token :line))
|
||||||
(println "Expected token " (first trace)))
|
(print "Expected token " (first trace)))
|
||||||
|
|
||||||
|
(defn butlast [xs] (if (empty? xs) [] (slice xs 0 -2)))
|
||||||
|
|
||||||
(defmacro defp [name & items]
|
(defmacro defp [name & items]
|
||||||
(let [arg (last items)
|
(let [arg (last items)
|
||||||
fns (into [] (butlast items))]
|
fns (butlast items)]
|
||||||
`(defn ~name [] ((apply comp ~fns) (keyword '~name) ~arg))))
|
~(defn ,name [] ((apply comp ,fns) (keyword ',name) ,arg))))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user