Class: Apricot::Reader

Inherits:
Object show all
Defined in:
lib/apricot/reader.rb

Defined Under Namespace

Classes: FnState

Constant Summary collapse

IDENTIFIER =
/[^'`~()\[\]{}";,\s]/
OCTAL =
/[0-7]/
HEX =
/[0-9a-fA-F]/
DIGITS =
('0'..'9').to_a + ('a'..'z').to_a
CHAR_ESCAPES =
{
  "a" => "\a", "b" => "\b", "t" => "\t", "n" => "\n",
  "v" => "\v", "f" => "\f", "r" => "\r", "e" => "\e"
}
REGEXP_OPTIONS =
{
  'i' => Regexp::IGNORECASE,
  'x' => Regexp::EXTENDED,
  'm' => Regexp::MULTILINE
}
QUOTE =
Identifier.intern(:quote)
UNQUOTE =
Identifier.intern(:unquote)
UNQUOTE_SPLICING =
Identifier.intern(:'unquote-splicing')
CONCAT =
Identifier.intern(:concat)
APPLY =
Identifier.intern(:apply)
LIST =
Identifier.intern(:list)
FN =
Identifier.intern(:fn)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, filename = '(none)', line = 1) ⇒ Reader

Returns a new instance of Reader.

Parameters:

  • io (IO)

    an input stream object to read forms from



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apricot/reader.rb', line 32

def initialize(io, filename = '(none)', line = 1)
  @filename = filename
  @io = io
  @location = 0
  @line = line

  @fn_state = []

  # Read the first character
  next_char
end

Class Method Details

.read_file(filename) ⇒ Object



44
45
46
# File 'lib/apricot/reader.rb', line 44

def self.read_file(filename)
  File.open(filename) {|f| new(f, filename).read }
end

.read_string(source, filename = '(none)', line = 1) ⇒ Object



48
49
50
# File 'lib/apricot/reader.rb', line 48

def self.read_string(source, filename = '(none)', line = 1)
  new(StringIO.new(source), filename, line).read
end

Instance Method Details

#readObject

Return a list of the forms that were read.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/apricot/reader.rb', line 53

def read
  forms = []

  skip_whitespace
  while @char
    forms << read_form
    skip_whitespace
  end

  forms
end

#read_oneObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/apricot/reader.rb', line 65

def read_one
  skip_whitespace
  form = read_form

  # Unget the last character because the reader always reads one character
  # ahead.
  @io.ungetc(@char) if @char

  form
end