Skip to content

Instantly share code, notes, and snippets.

@anmonteiro
Forked from timsgardner/parse.clj
Last active September 6, 2015 18:05
Show Gist options
  • Save anmonteiro/95eac0d7e9e71ac45a44 to your computer and use it in GitHub Desktop.
Save anmonteiro/95eac0d7e9e71ac45a44 to your computer and use it in GitHub Desktop.
parse
(defn parse-form [down-tok?, up-tok?, [tok & toks]]
(loop [frm [tok], toks toks]
(if-let [[tok2 & toks2] (seq toks)]
(cond
(down-tok? tok2) (let [[res toks3] (parse-form down-tok?, up-tok?, toks)]
(recur (conj frm res) toks3))
(up-tok? tok2) [(conj frm tok2) toks2]
:else (recur (conj frm tok2) toks2))
[frm nil])))
(defn parse [down-tok?, up-tok?, toks]
(loop [top [], toks toks]
(if-let [[tok & rtoks] (seq toks)]
(if (down-tok? tok)
(let [[res toks2] (parse-form down-tok? up-tok? toks)]
(recur (conj top res) toks2))
(recur (conj top tok) rtoks))
top)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment