Class: XfOOrth::AbstractSource

Inherits:
Object
  • Object
show all
Includes:
ReadPoint
Defined in:
lib/fOOrth/compiler/source.rb

Overview

The Source class used to contain code common to most sources.

Direct Known Subclasses

FileSource, StringSource

Instance Attribute Summary

Attributes included from ReadPoint

#read_buffer

Instance Method Summary collapse

Methods included from ReadPoint

#eoln?, #read, #reset_read_point

Constructor Details

#initializeAbstractSource

Initialize the abstract base class.



13
14
15
16
17
# File 'lib/fOOrth/compiler/source.rb', line 13

def initialize
  reset_read_point
  @eof = false
  @peek_buffer = nil
end

Instance Method Details

#closeObject

Close the source.



20
21
22
23
24
# File 'lib/fOOrth/compiler/source.rb', line 20

def close
  @eoln = true
  @eof = true
  @peek_buffer = nil
end

#eof?Boolean

Has the source reached the end of the available data?
Returns:

  • True if the end is reached else false.

Returns:

  • (Boolean)


54
55
56
# File 'lib/fOOrth/compiler/source.rb', line 54

def eof?
  @eof
end

#getObject

Get the next character of input data
Returns:

  • The next character or nil if none are available.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fOOrth/compiler/source.rb', line 29

def get
  return nil if (@eof && !@peek_buffer)

  @peek_buffer || read do
    begin
      @read_step.next.rstrip
    rescue StopIteration
      @eof = true
      nil
    end
  end
ensure
  @peek_buffer = nil
end

#peekObject

Peek ahead by one character.
Returns:

  • A peek at next character or nil if none are available.



47
48
49
# File 'lib/fOOrth/compiler/source.rb', line 47

def peek
  @peek_buffer ||= get unless eoln?
end