Class: SDL4R::Parser::Reader
- Inherits:
-
Object
- Object
- SDL4R::Parser::Reader
- 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
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#line_no ⇒ Object
readonly
The current line no (zero-based).
-
#pos ⇒ Object
readonly
The position of the char currently pointed by this reader in the current line.
Instance Method Summary collapse
-
#close ⇒ Object
Closes this Reader and its underlying
IO
. -
#current_char ⇒ Object
Returns the character at the current position or nil after end-of-line or end-of -file.
-
#eof? ⇒ Boolean
Indicates whether the end of file has been reached.
-
#eol? ⇒ Boolean
Returns whether the end of the current
line
as been reached. -
#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.
-
#get_line_char(pos) ⇒ Object
Returns the character at position
pos
in the current line. -
#initialize(io) ⇒ Reader
constructor
io
an open IO from which the characters are read. - #line_length ⇒ Object
-
#more_chars_in_line? ⇒ Boolean
Indicates whether there are more characters in the current line after the current char.
-
#previous_char ⇒ Object
Returns to the previous char if possible.
-
#read_char ⇒ Object
Returns the current char and go to the next.
-
#read_line ⇒ Object
Reads next line in stream skipping comment lines and blank lines.
-
#read_raw_line ⇒ Object
Reads and returns a “raw” line including lines with comments and blank lines.
-
#rest_of_line ⇒ Object
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.
-
#skip_char ⇒ Object
Go to the next character in the line.
-
#skip_to(new_pos) ⇒ Object
Skips the specified position in the current line.
-
#skip_to_eol ⇒ Object
Skips the current line by going just after its end.
-
#skip_whitespaces ⇒ Object
Skips the whitespaces that follow the current position.
-
#substring(from, to = -1)) ⇒ Object
Returns a subpart of the current line starting from
from
and stopping atto
(excluded).
Constructor Details
#initialize(io) ⇒ Reader
io
an open IO from which the characters are read.
31 32 33 34 35 36 37 38 39 |
# File 'lib/sdl4r/parser/reader.rb', line 31 def initialize(io) raise ArgumentError, "io == nil" unless io @io = io @line = nil @line_chars = nil @line_no = -1 @pos = 0 end |
Instance Attribute Details
#line ⇒ Object (readonly)
Returns the value of attribute line.
49 50 51 |
# File 'lib/sdl4r/parser/reader.rb', line 49 def line @line end |
#line_no ⇒ Object (readonly)
The current line no (zero-based)
42 43 44 |
# File 'lib/sdl4r/parser/reader.rb', line 42 def line_no @line_no end |
#pos ⇒ Object (readonly)
The position of the char currently pointed by this reader in the current line. This is not necessarily the position of the char you just got from a method, it might be the position of the next char, for instance.
47 48 49 |
# File 'lib/sdl4r/parser/reader.rb', line 47 def pos @pos end |
Instance Method Details
#close ⇒ Object
Closes this Reader and its underlying IO
.
176 177 178 |
# File 'lib/sdl4r/parser/reader.rb', line 176 def close @io.close end |
#current_char ⇒ Object
Returns the character at the current position or nil after end-of-line or end-of -file.
114 115 116 |
# File 'lib/sdl4r/parser/reader.rb', line 114 def current_char return get_line_char(@pos) end |
#eof? ⇒ Boolean
Indicates whether the end of file has been reached.
76 77 78 |
# File 'lib/sdl4r/parser/reader.rb', line 76 def eof? return @line.nil? end |
#eol? ⇒ Boolean
Returns whether the end of the current line
as been reached.
86 87 88 |
# File 'lib/sdl4r/parser/reader.rb', line 86 def eol? 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.
139 140 141 142 |
# File 'lib/sdl4r/parser/reader.rb', line 139 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.
105 106 107 108 109 110 111 |
# File 'lib/sdl4r/parser/reader.rb', line 105 def get_line_char(pos) if @line_chars and pos < line_length return @line_chars[pos] else return nil end end |
#line_length ⇒ Object
51 52 53 |
# File 'lib/sdl4r/parser/reader.rb', line 51 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 after the current char
81 82 83 |
# File 'lib/sdl4r/parser/reader.rb', line 81 def more_chars_in_line? return @pos < line_length - 1 end |
#previous_char ⇒ Object
Returns to the previous char if possible.
133 134 135 |
# File 'lib/sdl4r/parser/reader.rb', line 133 def previous_char @pos -= 1 if @pos >= 1 end |
#read_char ⇒ Object
Returns the current char and go to the next. Returns nil if end-of-line or -file has been reached.
126 127 128 129 130 |
# File 'lib/sdl4r/parser/reader.rb', line 126 def read_char c = current_char skip_char() c end |
#read_line ⇒ Object
Reads next line in stream skipping comment lines and blank lines.
Returns the next line or nil at the end of the file.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sdl4r/parser/reader.rb', line 58 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_line ⇒ Object
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.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/sdl4r/parser/reader.rb', line 160 def read_raw_line @line = @io.gets() # Remove a possible \r at the end of line @line.gsub!(/\r+$/, "") if @line @pos = 0; @line_chars = nil if @line @line_no += 1 @line_chars = @line.scan(/./m) end return @line end |
#rest_of_line ⇒ Object
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.
71 72 73 |
# File 'lib/sdl4r/parser/reader.rb', line 71 def rest_of_line return @line[@pos..-1] end |
#skip_char ⇒ Object
Go to the next character in the line. This method doesn’t skip to the next line once the reached eol.
120 121 122 |
# File 'lib/sdl4r/parser/reader.rb', line 120 def skip_char @pos += 1 if @pos < line_length end |
#skip_to(new_pos) ⇒ Object
Skips the specified position in the current line.
145 146 147 |
# File 'lib/sdl4r/parser/reader.rb', line 145 def skip_to(new_pos) @pos = new_pos end |
#skip_to_eol ⇒ Object
Skips the current line by going just after its end.
91 92 93 |
# File 'lib/sdl4r/parser/reader.rb', line 91 def skip_to_eol @pos = line_length end |
#skip_whitespaces ⇒ Object
Skips the whitespaces that follow the current position.
96 97 98 99 100 101 |
# File 'lib/sdl4r/parser/reader.rb', line 96 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).
151 152 153 |
# File 'lib/sdl4r/parser/reader.rb', line 151 def substring(from, to = -1) return @line[from..to] end |