class Satre::Lexer

Public Class Methods

alpanumeric?(str) click to toggle source
# File lib/satre/parser/lexer.rb, line 28
def alpanumeric?(str)
  matches("abcdefghijklmnopqrstuvwxyz_'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", str)
end
lex(inp) click to toggle source
# File lib/satre/parser/lexer.rb, line 42
def lex(inp)
  inp, rest = self.lexwhile(:space?, inp)
  return [] if rest == "" || rest.nil?
  c = rest[0]
  cs = rest[1..-1]
  prop =  if self.alpanumeric?(c) then :alpanumeric?
          elsif self.symbolic?(c) then :symbolic?
          else :refute end
  toktl, rest = self.lexwhile(prop, cs)
  [c+toktl] + lex(rest)
end
lexwhile(prop, inp) click to toggle source
# File lib/satre/parser/lexer.rb, line 36
def lexwhile(prop, inp)
  tokl = inp.split("").take_while { |c| self.send(prop, c) }.inject(:+)
  tokl = "" if tokl.nil? 
  return tokl, inp[tokl.length..-1]
end
matches() click to toggle source
# File lib/satre/parser/lexer.rb, line 4
def matches
  lambda { |pattern, str| str.split("").all? { |c| pattern.include? c } }
end
numeric?(str) click to toggle source
# File lib/satre/parser/lexer.rb, line 24
def numeric?(str)
  matches("0123456789", str)
end
punctuation?(str) click to toggle source
# File lib/satre/parser/lexer.rb, line 16
def punctuation?(str)
  matches("()[]{}", str)
end
refute(*) click to toggle source
# File lib/satre/parser/lexer.rb, line 32
def refute(*)
  false
end
space?(str) click to toggle source
# File lib/satre/parser/lexer.rb, line 12
def space?(str)
  matches(" \t\n\r", str)
end
symbolic?(str) click to toggle source
# File lib/satre/parser/lexer.rb, line 20
def symbolic?(str)
  matches("~`!@#%$^&*-+<=>\\/|", str)
end