Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save swarley/8e77aee0400489807311b137118db7f1 to your computer and use it in GitHub Desktop.
Save swarley/8e77aee0400489807311b137118db7f1 to your computer and use it in GitHub Desktop.
worst_s_exp_parser_literally_ever_conceived.rb
def lex(str, tokens=[])
str.is_a?(String) ?
lex(StringScanner.new(str))
:
(str.scan(/\(/) ?
lex(str, tokens << :l_paren)
:
(str.scan(/\)/) ?
lex(str, tokens << :r_paren)
:
((token = str.scan(/\s+/)) ?
lex(str, tokens)
:
((token = str.scan(/"(?:[^"\\]|\\.)*"/) ?
lex(str, tokens << [:string, token])
:
((token = str.scan(/\d+/)) ?
lex(str, tokens << [:digit, token.to_i])
:
((token = str.scan(/;.*\n/)) ?
lex(str, tokens << [:comment])
:
((token = str.scan(/[A-z][\-A-z0-9]*/)) ?
lex(str, tokens << [:word, token])
:
((token = str.scan(/[+\-\&\*\%\$\#\!\^\:\>\<\.\,\?\=]+/)) ?
lex(str, tokens << [:special_token, token])
:
(str.eos? ?
tokens
:
(raise "Unexpected token")))))))))))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment