Created
February 6, 2019 03:00
-
-
Save swarley/8e77aee0400489807311b137118db7f1 to your computer and use it in GitHub Desktop.
worst_s_exp_parser_literally_ever_conceived.rb
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
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