class SXP::Reader::Basic

A basic S-expression parser.

Constants

ATOM
DECIMAL
INTEGER
LPARENS
RATIONAL
RPARENS

Public Instance Methods

read_atom() click to toggle source

@return [Object]

# File lib/sxp/reader/basic.rb, line 24
def read_atom
  case buffer = read_literal
    when '.'      then buffer.to_sym
    when RATIONAL then Rational($1.to_i, $2.to_i)
    when DECIMAL  then Float(buffer) # FIXME?
    when INTEGER  then Integer(buffer)
    else buffer.to_sym
  end
end
read_character() click to toggle source

@return [String]

# File lib/sxp/reader/basic.rb, line 52
def read_character
  case char = read_char
    when ?b  then ?\b
    when ?f  then ?\f
    when ?n  then ?\n
    when ?r  then ?\r
    when ?t  then ?\t
    when ?u  then read_chars(4).to_i(16).chr
    when ?U  then read_chars(8).to_i(16).chr
    when ?"  then char #"
    when ?\\ then char
    else char
  end
end
read_literal() click to toggle source

@return [String]

# File lib/sxp/reader/basic.rb, line 69
def read_literal
  grammar = self.class.const_get(:ATOM)
  buffer = String.new
  buffer << read_char while !eof? && peek_char.chr =~ grammar
  buffer
end
read_string() click to toggle source

@return [String]

# File lib/sxp/reader/basic.rb, line 36
def read_string
  buffer = String.new
  skip_char # '"'
  until peek_char == ?" #"
    buffer <<
      case char = read_char
        when ?\\ then read_character
        else char
      end
  end
  skip_char # '"'
  buffer
end
read_token() click to toggle source

@return [Object]

Calls superclass method SXP::Reader#read_token
# File lib/sxp/reader/basic.rb, line 14
def read_token
  case peek_char
    when ?(, ?) then [:list, read_char]
    when ?"     then [:atom, read_string] #"
    else super
  end
end