-
-
Save rightfold/337d4de9fb4e760e0238 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
{id} = require("prelude-ls") | |
patterns = | |
* [/^\(/, \lparen] | |
* [/^\)/, \rparen] | |
* [/^\;/, \semicolon] | |
* [/^\=/, \eq] | |
* [/^".*?"/, \string, (s) -> s.substr(1, s.length - 2)] | |
* [/^proc\b/, \proc] | |
* [/^[a-zA-Z][a-zA-Z_]+/, \identifier, id] | |
* [/^$/, \eof] | |
whitespace = /^[ \n]+/ | |
lex_ = (code) -> | |
result = [] | |
while code != "" | |
code .= replace(whitespace, "") | |
ok = false | |
for [regex, type, value] in patterns | |
match_ = regex.exec(code) | |
continue unless match_ | |
code .= substr(match_[0].length) | |
result.push do | |
type: type | |
value: value && value(match_[0]) | |
ok = true | |
break | |
if !ok | |
console.log result | |
throw Error("unknown token") | |
result | |
insert-semicolons = (tokens) -> tokens | |
lex = lex_ >> insert-semicolons | |
module.exports = lex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment