Created
July 29, 2009 18:43
-
-
Save weavejester/158326 to your computer and use it in GitHub Desktop.
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- 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