Class: ReVIEW::LineInput

Inherits:
Object show all
Defined in:
lib/review/lineinput.rb

Constant Summary collapse

INVALID_CHARACTER_PATTERN =

accept 0x09: TAB, 0x0a: LF, 0x0d: CR

/[\x00-\x08\x0b-\x0c\x0e-\x1f]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ LineInput

Returns a new instance of LineInput.



16
17
18
19
20
21
# File 'lib/review/lineinput.rb', line 16

def initialize(f)
  @input = f
  @buf = []
  @lineno = 0
  @eof_p = false
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



14
15
16
# File 'lib/review/lineinput.rb', line 14

def lineno
  @lineno
end

Instance Method Details

#eachObject



83
84
85
86
87
# File 'lib/review/lineinput.rb', line 83

def each
  while line = gets
    yield line
  end
end

#eof?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/review/lineinput.rb', line 27

def eof?
  @eof_p
end

#getsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/review/lineinput.rb', line 43

def gets
  unless @buf.empty?
    @lineno += 1
    return @buf.pop
  end
  return nil if @eof_p # to avoid ARGF blocking.

  line = @input.gets
  @eof_p = true unless line
  @lineno += 1
  invalid_char = lookup_invalid_char(line)
  if invalid_char
    raise SyntaxError, "found invalid control-sequence character (#{sprintf('%#x', invalid_char.codepoints[0])})."
  end

  line
end

#inspectObject



23
24
25
# File 'lib/review/lineinput.rb', line 23

def inspect
  "#<#{self.class} file=#{@input.inspect} line=#{lineno}>"
end

#next?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/review/lineinput.rb', line 67

def next?
  peek ? true : false
end

#peekObject



61
62
63
64
65
# File 'lib/review/lineinput.rb', line 61

def peek
  line = gets
  ungets(line) if line
  line
end

#skip_blank_linesObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/review/lineinput.rb', line 71

def skip_blank_lines
  n = 0
  while line = gets
    unless line.strip.empty?
      ungets(line)
      return n
    end
    n += 1
  end
  n
end

#skip_comment_linesObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/review/lineinput.rb', line 31

def skip_comment_lines
  n = 0
  while line = gets
    unless /\A\#@/.match?(line.strip)
      ungets(line)
      return n
    end
    n += 1
  end
  n
end

#until_match(re) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/review/lineinput.rb', line 100

def until_match(re)
  while line = gets
    if re&.match?(line)
      ungets(line)
      return
    end
    yield line
  end
  nil
end

#while_match(re) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/review/lineinput.rb', line 89

def while_match(re)
  while line = gets
    unless re&.match?(line)
      ungets(line)
      return
    end
    yield line
  end
  nil
end