module Malady::Reader
Public Instance Methods
parse_string(string)
click to toggle source
# File lib/malady/reader.rb, line 4 def parse_string(string) read_str(string) end
read_atom(token)
click to toggle source
# File lib/malady/reader.rb, line 35 def read_atom(token) case token when /^-?\d+$/ [:integer, token.to_i] when 'true' [:boolean, :true] when 'false' [:boolean, :false] when /^\D+$/ [:symbol, token] else raise 'Reader error: Unknown token' end end
read_form(tokens)
click to toggle source
# File lib/malady/reader.rb, line 13 def read_form(tokens) if tokens.first =~ /(\(|\[)/ read_list(tokens) else read_atom(tokens.shift) end end
read_list(tokens)
click to toggle source
# File lib/malady/reader.rb, line 21 def read_list(tokens) raise 'Reader error: read_list called on non-list' if tokens.shift !~ /(\(|\[)/ list = [:list] while tokens.first !~ /(\)|\])/ raise 'Reader error: Unmatched parens' if tokens.empty? list << read_form(tokens) end tokens.shift # pop our closing paren list end
read_str(string)
click to toggle source
# File lib/malady/reader.rb, line 8 def read_str(string) tokens = tokenizer(string) read_form(tokens) end
tokenizer(string)
click to toggle source
# File lib/malady/reader.rb, line 50 def tokenizer(string) pos = 0 re = /[\s,]*(~@|[\[\]{}()'`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}('"`,;)]*)/ result = [] while (m = re.match(string, pos)) && pos < string.size result << m.to_s.strip pos = m.end(0) end result end