Class: YARD::Parser::Ruby::Legacy::RubyLex::BufferedReader

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/parser/ruby/legacy/ruby_lex.rb

Overview

####################################################################

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    stuff
    for
  A
, <<-B    stuff
    for
  B
)

When the lexer encounters the <

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (BufferedReader) initialize(content)

A new instance of BufferedReader



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 338

def initialize(content)
  if /\t/ =~ content
    tab_width = 2
    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

Instance Attribute Details

- (Object) line_num (readonly)

Returns the value of attribute line_num



336
337
338
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 336

def line_num
  @line_num
end

Instance Method Details

- (Object) column



357
358
359
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 357

def column
  @offset - @last_newline
end

- (Object) divert_read_from(reserve)



411
412
413
414
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 411

def divert_read_from(reserve)
  @content[@offset, 0] = reserve
  @size      = @content.size
end

- (Object) get_read



392
393
394
395
396
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 392

def get_read
  res = @content[@read_back_offset...@offset]
  @read_back_offset = @offset
  res
end

- (Object) getc



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 361

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

- (Object) getc_already_read



380
381
382
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 380

def getc_already_read
  getc
end

- (Object) peek(at)



398
399
400
401
402
403
404
405
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 398

def peek(at)
  pos = @offset + at
  if pos >= @size
    nil
  else
    @content[pos, 1]
  end
end

- (Object) peek_equal(str)



407
408
409
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 407

def peek_equal(str)
  @content[@offset, str.length] == str
end

- (Object) ungetc(ch)



384
385
386
387
388
389
390
# File 'lib/yard/parser/ruby/legacy/ruby_lex.rb', line 384

def ungetc(ch)
  raise "unget past beginning of file" if @offset <= 0
  @offset -= 1
  if @content[@offset] == ?\n
    @newline_pending = false
  end
end