Class: SXP::Reader::Scheme

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

Overview

A Scheme R4RS S-expressions parser.

See Also:

  • https:/peoplehttps:/people.csailhttps:/people.csail.mithttps:/people.csail.mit.edu/jaffer/r4rs_9https:/people.csail.mit.edu/jaffer/r4rs_9.htmlhttps:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65

Constant Summary collapse

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+)$/
CHARACTERS =

Escape characters, used in the form ‘#newline`. Case is treated insensitively

See Also:

  • SXP::Reader::Scheme.https:/peoplehttps:/people.csailhttps:/people.csail.mithttps:/people.csail.mit.edu/jaffer/r4rs_9https:/people.csail.mit.edu/jaffer/r4rs_9.htmlhttps:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65
{
  'newline'   => "\n",
  'space'     => " ",
}

Constants inherited from Extended

Extended::ATOM, Extended::LPARENS, Extended::RPARENS

Constants inherited from Basic

Basic::ATOM, Basic::INTEGER, Basic::LPARENS, Basic::RPARENS

Instance Attribute Summary

Attributes inherited from SXP::Reader

#input, #options

Instance Method Summary collapse

Methods inherited from Extended

#skip_comments

Methods inherited from Basic

#read_literal, #read_string

Methods inherited from SXP::Reader

#each, read, #read, read_all, #read_all, read_file, #read_files, #read_integer, #read_list, #read_literal, #read_string, read_url

Constructor Details

#initialize(input, version: :r4rs, **options, &block) ⇒ Scheme

Initializes the reader.

Parameters:

Options Hash (**options):

  • :version (Symbol) — default: :r4rs


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

def initialize(input, version: :r4rs, **options, &block)
  super(input, version: version, **options, &block)
end

Instance Method Details

#read_atomObject

Returns:



44
45
46
47
48
49
50
51
52
# File 'lib/sxp/reader/scheme.rb', line 44

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

#read_characterString

Read characters sequences like ‘#space`. Otherwise, reads a single character. Requires the ability to put eroneously read characters back in the input stream

Returns:

See Also:

  • SXP::Reader::Scheme.https:/peoplehttps:/people.csailhttps:/people.csail.mithttps:/people.csail.mit.edu/jaffer/r4rs_9https:/people.csail.mit.edu/jaffer/r4rs_9.htmlhttps:/people.csail.mit.edu/jaffer/r4rs_9.html#SEC65


80
81
82
83
84
85
86
87
88
89
# File 'lib/sxp/reader/scheme.rb', line 80

def read_character
  lit = read_literal

  return " " if lit.empty? && peek_char == " "
  CHARACTERS.fetch(lit.downcase) do
    # Return just the first character
    unread(lit[1..-1])
    lit[0,1]
  end
end

#read_sharpObject

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sxp/reader/scheme.rb', line 56

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 # comment character
    when ?!      then skip_line; skip # shebang
    else raise Error, "invalid sharp-sign read syntax: ##{char.chr}"
  end
end

#read_tokenObject

Returns:



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

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