Last active
August 29, 2015 14:06
-
-
Save cgrand/23a908c021bcd5d08008 to your computer and use it in GitHub Desktop.
readers from grammars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn lazy-reader [start prods] | |
(let [step (vm/stepper (asm/link (grammar start prods)))] | |
(fn self | |
([input] (self input (vector-tree-builder input))) | |
([input builder] | |
((fn f [state i] | |
(lazy-seq | |
(when (< i (count input)) | |
(let [state (step state i (nth input i))] | |
(if (when-let [stacks (get state -2)] | |
(zero? (-> stacks :carry (:error 0)))) | |
(cons (builder (reduce builder (builder) (:events (:carry (get state -2))))) | |
(f (step) (inc i))) | |
(f state (inc i))))))) | |
(step) 0))))) | |
=> (let [r (lazy-reader :root lang)] | |
(r "(todo)[todo]")) | |
([[:list "(" [:symbol "todo"] ")"]] [[:vector "(todo)[" [:symbol "todo"] "]"]]) | |
=> (count *1) | |
2 | |
=> (let [r (lazy-reader :root lang)] | |
(r "(todo) [todo]")) | |
([[:list "(" [:symbol "todo"] ")"]] [[:whitespace "(todo) "] [:vector "[" [:symbol "todo"] "]"]]) | |
=> (count *1) | |
2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment