Sha256: 39314d262716c4306bb916aef49d5bbd230ca218934966fee5e997999017e37a

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

module SXP; class Reader
  ##
  # A Scheme R4RS S-expressions parser.
  #
  # @see http://people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
  class Scheme < Extended
    DECIMAL         = /^[+-]?(\d*)?\.\d*$/.freeze
    INTEGER_BASE_2  = /^[+-]?[01]+$/.freeze
    INTEGER_BASE_8  = /^[+-]?[0-7]+$/.freeze
    INTEGER_BASE_10 = /^[+-]?\d+$/.freeze
    INTEGER_BASE_16 = /^[+-]?[\da-z]+$/i.freeze
    RATIONAL        = /^([+-]?\d+)\/(\d+)$/.freeze

    ##
    # Initializes the reader.
    #
    # @param  [IO, StringIO, String]   input
    # @param  [Hash{Symbol => Object}] options
    # @option options [Symbol]         :version (:r4rs)
    def initialize(input, options = {}, &block)
      super(input, {:version => :r4rs}.merge(options), &block)
    end

    ##
    # @return [Object]
    def read_token
      case peek_char
        when ?# then [:atom, read_sharp]
        else super
      end
    end

    ##
    # @return [Object]
    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)
        when INTEGER_BASE_10 then Integer(buffer)
        else buffer.to_sym
      end
    end

    ##
    # @return [Object]
    def read_sharp
      skip_char # '#'
      case char = read_char
        when ?n, ?N  then nil    # not in Scheme per se
        when ?f, ?F  then false
        when ?t, ?T  then true
        when ?b, ?B  then read_integer(2)
        when ?o, ?O  then read_integer(8)
        when ?d, ?D  then read_integer(10)
        when ?x, ?X  then read_integer(16)
        when ?\\     then read_character
        when ?;      then skip; read
        when ?!      then skip_line; read # shebang
        else raise Error, "invalid sharp-sign read syntax: ##{char.chr}"
      end
    end
  end # class Scheme
end; end # class SXP::Reader

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sxp-0.0.7 lib/sxp/reader/scheme.rb
sxp-0.0.6 lib/sxp/reader/scheme.rb