lib/sdl4r/parser/reader.rb in sdl4r-0.9.6 vs lib/sdl4r/parser/reader.rb in sdl4r-0.9.7

- old
+ new

@@ -27,21 +27,29 @@ # Parser class from these problems. class Reader # :nodoc: all # +io+ an open IO from which the characters are read. def initialize(io) - raise ArgumentError, "io == nil" if io.nil? + raise ArgumentError, "io == nil" unless io @io = io @line = nil @line_chars = nil - @line_no = 0 + @line_no = -1 @pos = 0 end - attr_reader :line_no, :pos, :line; + # The current line no (zero-based) + attr_reader :line_no + # 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. + attr_reader :pos + + attr_reader :line + def line_length return @line_chars.nil? ? 0 : @line_chars.length end # Reads next line in stream skipping comment lines and blank lines. @@ -63,26 +71,26 @@ def rest_of_line return @line[@pos..-1] end # Indicates whether the end of file has been reached. - def end_of_file? + def eof? return @line.nil? end - # Indicates whether there are more characters in the current line + # Indicates whether there are more characters in the current line after the current char def more_chars_in_line? return @pos < line_length - 1 end # Returns whether the end of the current +line+ as been reached. - def end_of_line? + def eol? return @pos >= line_length end # Skips the current line by going just after its end. - def skip_line + def skip_to_eol @pos = line_length end # Skips the whitespaces that follow the current position. def skip_whitespaces @@ -105,28 +113,31 @@ # Returns the character at the current position or nil after end-of-line or end-of -file. def current_char return get_line_char(@pos) end - # Go to the next character in the stream. + # Go to the next character in the line. This method doesn't skip to the next line once the + # reached eol. def skip_char @pos += 1 if @pos < line_length end - # Go to the next character and returns it (or nil if end-of-line or -file has been reached). + # Returns the current char and go to the next. + # Returns nil if end-of-line or -file has been reached. def read_char + c = current_char skip_char() - return current_char + c end # Returns to the previous char if possible. def previous_char - @pos -= 1 if @pos >= -1 + @pos -= 1 if @pos >= 1 end - # 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. + # 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. def find_next_in_line(searched, start_pos = nil) start_pos = @pos + 1 unless start_pos return @line.index(searched, start_pos) end @@ -147,15 +158,11 @@ # # This method changes the value of @line, @lineNo and @pos. 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 + # Remove a possible \r at the end of line + @line.gsub!(/\r+$/, "") if @line @pos = 0; @line_chars = nil if @line @line_no += 1