Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created May 2, 2015 20:15
Show Gist options
  • Save rightfold/337d4de9fb4e760e0238 to your computer and use it in GitHub Desktop.
Save rightfold/337d4de9fb4e760e0238 to your computer and use it in GitHub Desktop.
{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