Skip to content

Instantly share code, notes, and snippets.

@weavejester
Created July 29, 2009 18:43
Show Gist options
  • Save weavejester/158326 to your computer and use it in GitHub Desktop.
Save weavejester/158326 to your computer and use it in GitHub Desktop.
(defn- lex-1
"Lex one symbol from a string, and return the symbol and trailing source."
[src clauses]
(some
(fn [[re action]]
(let [matcher (re-matcher re src)]
(if (.lookingAt matcher)
[(if (fn? action) (action matcher) action)
(.substring src (.end matcher))])))
(partition 2 clauses)))
(defn- lex
"Lex a string into tokens by matching against regexs and evaluating
the matching associated function."
[src & clauses]
(loop [results []
src src
clauses clauses]
(if-let [[result src] (lex-1 src clauses)]
(let [results (conj results result)]
(if (= src "")
results
(recur results src clauses))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment