In Files

  • rdoc/parser/ruby.rb

Files

Class/Module Index [+]

Quicksearch

RDoc::RubyLex::BufferedReader

Read an input stream character by character. We allow for unlimited ungetting of characters just read.

We simplify the implementation greatly by reading the entire input into a buffer initially, and then simply traversing it using pointers.

We also have to allow for the here document diversion. This little gem comes about when the lexer encounters a here document. At this point we effectively need to split the input stream into two parts: one to read the body of the here document, the other to read the rest of the input line where the here document was initially encountered. For example, we might have

do_something(<<-A, <<-B)
  stuff
  for
A
  stuff
  for
B

When the lexer encounters the <<A, it reads until the end of the line, and keeps it around for later. It then reads the body of the here document. Once complete, it needs to read the rest of the original line, but then skip the here document body.

Attributes

line_num[R]

Public Class Methods

new(content, options) click to toggle source
 
               # File rdoc/parser/ruby.rb, line 349
def initialize(content, options)
  @options = options

  if /\t/ =~ content
    tab_width = @options.tab_width
    content = content.split(/\n/).map do |line|
      1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)}  && $~ #`
      line
    end .join("\n")
  end
  @content   = content
  @content << "\n" unless @content[-1,1] == "\n"
  @size      = @content.size
  @offset    = 0
  @hwm       = 0
  @line_num  = 1
  @read_back_offset = 0
  @last_newline = 0
  @newline_pending = false
end
            

Public Instance Methods

column() click to toggle source
 
               # File rdoc/parser/ruby.rb, line 370
def column
  @offset - @last_newline
end
            
divert_read_from(reserve) click to toggle source
 
               # File rdoc/parser/ruby.rb, line 424
def divert_read_from(reserve)
  @content[@offset, 0] = reserve
  @size      = @content.size
end
            
get_read() click to toggle source
 
               # File rdoc/parser/ruby.rb, line 405
def get_read
  res = @content[@read_back_offset...@offset]
  @read_back_offset = @offset
  res
end
            
getc() click to toggle source
 
               # File rdoc/parser/ruby.rb, line 374
def getc
  return nil if @offset >= @size
  ch = @content[@offset, 1]

  @offset += 1
  @hwm = @offset if @hwm < @offset

  if @newline_pending
    @line_num += 1
    @last_newline = @offset - 1
    @newline_pending = false
  end

  if ch == "\n"
    @newline_pending = true
  end
  ch
end
            
getc_already_read() click to toggle source
 
               # File rdoc/parser/ruby.rb, line 393
def getc_already_read
  getc
end
            
peek(at) click to toggle source
 
               # File rdoc/parser/ruby.rb, line 411
def peek(at)
  pos = @offset + at
  if pos >= @size
    nil
  else
    @content[pos, 1]
  end
end
            
peek_equal(str) click to toggle source
 
               # File rdoc/parser/ruby.rb, line 420
def peek_equal(str)
  @content[@offset, str.length] == str
end
            
ungetc(ch) click to toggle source
 
               # File rdoc/parser/ruby.rb, line 397
def ungetc(ch)
  raise "unget past beginning of file" if @offset <= 0
  @offset -= 1
  if @content[@offset] == ?\n
    @newline_pending = false
  end
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus