Class: SXP::Reader::Scheme

Inherits:
Extended show all
Defined in:
lib/sxp/reader/scheme.rb

Overview

A Scheme R4RS S-expressions parser.

See Also:

Constant Summary

DECIMAL =
/^[+-]?(\d*)?\.\d*$/
INTEGER_BASE_2 =
/^[+-]?[01]+$/
INTEGER_BASE_8 =
/^[+-]?[0-7]+$/
INTEGER_BASE_10 =
/^[+-]?\d+$/
INTEGER_BASE_16 =
/^[+-]?[\da-z]+$/i
RATIONAL =
/^([+-]?\d+)\/(\d+)$/

Constants inherited from Extended

ATOM, LPARENS, RPARENS

Constants inherited from Basic

ATOM, DECIMAL, INTEGER, LPARENS, RATIONAL, RPARENS

Instance Method Summary (collapse)

Methods inherited from Extended

#skip_comments

Methods inherited from Basic

#read_character, #read_literal, #read_string

Methods inherited from SXP::Reader

#each, #eof?, #peek_char, #read, read, read_all, #read_all, #read_char, #read_character, #read_chars, read_file, read_files, #read_integer, #read_list, #read_literal, #read_string, read_url, #skip_comments, #skip_line

Constructor Details

- (Scheme) initialize(input, options = {}, &block)

Initializes the reader.

Parameters:

Options Hash (options):

  • (Symbol) :version — default: :r4rs


20
21
22
# File 'lib/sxp/reader/scheme.rb', line 20

def initialize(input, options = {}, &block)
  super(input, {:version => :r4rs}.merge(options), &block)
end

Instance Method Details

- (Object) read_atom

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/sxp/reader/scheme.rb', line 35

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

- (Object) read_sharp

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sxp/reader/scheme.rb', line 47

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

- (Object) read_token

Returns:



26
27
28
29
30
31
# File 'lib/sxp/reader/scheme.rb', line 26

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