Class | RubyLex::BufferedReader |
In: |
parsers/parse_rb.rb
|
Parent: | Object |
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.
line_num | [R] |
# File parsers/parse_rb.rb, line 349 349: def initialize(content) 350: if /\t/ =~ content 351: tab_width = Options.instance.tab_width 352: content = content.split(/\n/).map do |line| 353: 1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)} && $~ #` 354: line 355: end .join("\n") 356: end 357: @content = content 358: @content << "\n" unless @content[-1,1] == "\n" 359: @size = @content.size 360: @offset = 0 361: @hwm = 0 362: @line_num = 1 363: @read_back_offset = 0 364: @last_newline = 0 365: @newline_pending = false 366: end
# File parsers/parse_rb.rb, line 422 422: def divert_read_from(reserve) 423: @content[@offset, 0] = reserve 424: @size = @content.size 425: end
# File parsers/parse_rb.rb, line 403 403: def get_read 404: res = @content[@read_back_offset...@offset] 405: @read_back_offset = @offset 406: res 407: end
# File parsers/parse_rb.rb, line 372 372: def getc 373: return nil if @offset >= @size 374: ch = @content[@offset, 1] 375: 376: @offset += 1 377: @hwm = @offset if @hwm < @offset 378: 379: if @newline_pending 380: @line_num += 1 381: @last_newline = @offset - 1 382: @newline_pending = false 383: end 384: 385: if ch == "\n" 386: @newline_pending = true 387: end 388: ch 389: end
# File parsers/parse_rb.rb, line 409 409: def peek(at) 410: pos = @offset + at 411: if pos >= @size 412: nil 413: else 414: @content[pos, 1] 415: end 416: end
# File parsers/parse_rb.rb, line 418 418: def peek_equal(str) 419: @content[@offset, str.length] == str 420: end