Class: SDL4R::Parser::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl4r/parser/reader.rb

Overview

Gives access to the characters read to the Parser. This class was designed to gather the handling of the UTF-8 issues in one place and shield the Parser class from these problems.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Reader

io an open IO from which the characters are read.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
# File 'lib/sdl4r/parser/reader.rb', line 31

def initialize(io)
  raise ArgumentError, "io == nil" if io.nil?

  @io = io
  @line = nil
  @line_chars = nil
  @line_no = 0
  @pos = 0
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line.



41
42
43
# File 'lib/sdl4r/parser/reader.rb', line 41

def line
  @line
end

#line_noObject (readonly)

Returns the value of attribute line_no.



41
42
43
# File 'lib/sdl4r/parser/reader.rb', line 41

def line_no
  @line_no
end

#posObject (readonly)

Returns the value of attribute pos.



41
42
43
# File 'lib/sdl4r/parser/reader.rb', line 41

def pos
  @pos
end

Instance Method Details

#closeObject

Closes this Reader and its underlying IO.



169
170
171
# File 'lib/sdl4r/parser/reader.rb', line 169

def close
  @io.close
end

#current_charObject

Returns the character at the current position or nil after end-of-line or end-of -file.



106
107
108
# File 'lib/sdl4r/parser/reader.rb', line 106

def current_char
  return get_line_char(@pos)
end

#end_of_file?Boolean

Indicates whether the end of file has been reached.

Returns:

  • (Boolean)


68
69
70
# File 'lib/sdl4r/parser/reader.rb', line 68

def end_of_file?
  return @line.nil?
end

#end_of_line?Boolean

Returns whether the end of the current line as been reached.

Returns:

  • (Boolean)


78
79
80
# File 'lib/sdl4r/parser/reader.rb', line 78

def end_of_line?
  return @pos >= line_length
end

#find_next_in_line(searched, start_pos = nil) ⇒ Object

Returns the next index of the expression (string, regexp, fixnum) in the current line, starting from after the current position if no position is specified.



128
129
130
131
# File 'lib/sdl4r/parser/reader.rb', line 128

def find_next_in_line(searched, start_pos = nil)
  start_pos = @pos + 1 unless start_pos
  return @line.index(searched, start_pos)
end

#get_line_char(pos) ⇒ Object

Returns the character at position pos in the current line. Returns nil if there is no current line or if pos is after the end of the line.



97
98
99
100
101
102
103
# File 'lib/sdl4r/parser/reader.rb', line 97

def get_line_char(pos)
  if @line_chars and pos < line_length
    return @line_chars[pos]
  else
    return nil
  end
end

#line_lengthObject



43
44
45
# File 'lib/sdl4r/parser/reader.rb', line 43

def line_length
  return @line_chars.nil? ? 0 : @line_chars.length
end

#more_chars_in_line?Boolean

Indicates whether there are more characters in the current line

Returns:

  • (Boolean)


73
74
75
# File 'lib/sdl4r/parser/reader.rb', line 73

def more_chars_in_line?
  return @pos < line_length - 1
end

#previous_charObject

Returns to the previous char if possible.



122
123
124
# File 'lib/sdl4r/parser/reader.rb', line 122

def previous_char
  @pos -= 1 if @pos >= -1
end

#read_charObject

Go to the next character and returns it (or nil if end-of-line or -file has been reached).



116
117
118
119
# File 'lib/sdl4r/parser/reader.rb', line 116

def read_char
  skip_char()
  return current_char
end

#read_lineObject

Reads next line in stream skipping comment lines and blank lines.

Returns the next line or nil at the end of the file.



50
51
52
53
54
55
56
57
58
59
# File 'lib/sdl4r/parser/reader.rb', line 50

def read_line
  @line_chars = nil

  while @line = read_raw_line()
    # Skip empty and commented lines
    break unless @line.empty? or @line =~ /^#/
  end

  return @line
end

#read_raw_lineObject

Reads and returns a “raw” line including lines with comments and blank lines.

Returns the next line or nil if at the end of the file.

This method changes the value of @line, @lineNo and @pos.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sdl4r/parser/reader.rb', line 149

def read_raw_line
  @line = @io.gets()

  # We ensure that only \n is used as an end-of-line by replacing \r and \r\n.
  if @line
    if not @line.gsub!(/\r\n/m, "\n")
      @line.gsub!(/\r/m, "\n")
    end
  end

  @pos = 0;
  @line_chars = nil
  if @line
    @line_no += 1
    @line_chars = @line.scan(/./m)
  end
  return @line
end

#rest_of_lineObject

Returns the string that goes from the current position of this Reader to the end of the line or nil if the current position doesn’t allow that.



63
64
65
# File 'lib/sdl4r/parser/reader.rb', line 63

def rest_of_line
  return @line[@pos..-1]
end

#skip_charObject

Go to the next character in the stream.



111
112
113
# File 'lib/sdl4r/parser/reader.rb', line 111

def skip_char
  @pos += 1 if @pos < line_length
end

#skip_lineObject

Skips the current line by going just after its end.



83
84
85
# File 'lib/sdl4r/parser/reader.rb', line 83

def skip_line
  @pos = line_length
end

#skip_to(new_pos) ⇒ Object

Skips the specified position in the current line.



134
135
136
# File 'lib/sdl4r/parser/reader.rb', line 134

def skip_to(new_pos)
  @pos = new_pos
end

#skip_whitespacesObject

Skips the whitespaces that follow the current position.



88
89
90
91
92
93
# File 'lib/sdl4r/parser/reader.rb', line 88

def skip_whitespaces
  while (@pos + 1) < line_length and
      (@line[@pos + 1] == ?\s or @line[@pos + 1] == ?\t)
    @pos += 1
  end
end

#substring(from, to = -1)) ⇒ Object

Returns a subpart of the current line starting from from and stopping at to (excluded).



140
141
142
# File 'lib/sdl4r/parser/reader.rb', line 140

def substring(from, to = -1)
  return @line[from..to]
end