Class: LineInput

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

Overview

Utility class for line-wise file parsing

Instance Method Summary collapse

Constructor Details

#initialize(f, entry = nil) ⇒ LineInput

Returns a new instance of LineInput.



22
23
24
25
26
27
28
# File 'lib/bitclust/lineinput.rb', line 22

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

Instance Method Details

#eachObject



119
120
121
122
123
# File 'lib/bitclust/lineinput.rb', line 119

def each
  while line = gets()
    yield line
  end
end

#eof?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bitclust/lineinput.rb', line 34

def eof?
  @eof_p
end

#getblock(term_re) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/bitclust/lineinput.rb', line 175

def getblock(term_re)
  buf = [] #: Array[String]
  until_terminator(term_re) do |line|
    buf.push line
  end
  buf
end

#getlines_until(re) ⇒ Object Also known as: break



157
158
159
160
161
162
163
# File 'lib/bitclust/lineinput.rb', line 157

def getlines_until(re)
  buf = [] #: Array[String]
  until_match(re) do |line|
    buf.push line
  end
  buf
end

#getlines_while(re) ⇒ Object Also known as: span



136
137
138
139
140
141
142
# File 'lib/bitclust/lineinput.rb', line 136

def getlines_while(re)
  buf = [] #: Array[String]
  while_match(re) do |line|
    buf.push line
  end
  buf
end

#getsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bitclust/lineinput.rb', line 56

def gets
  unless @buf.empty?
    @lineno += 1
    line = @buf.pop
    line&.location ||= BitClust::Location.new(path, @lineno)
    return line
  end
  return nil if @eof_p   # to avoid ARGF blocking.
  line = @input.gets
  @eof_p = true unless line
  @lineno += 1
  line&.location ||= BitClust::Location.new(path, @lineno)
  line
end

#gets_if(re, index = nil) ⇒ Object



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

def gets_if(re, index = nil)
  line = gets()
  if not line or not (re =~ line)
    ungets line
    return nil
  end
  return $~&.[](index) if index
  line
end

#gets_unless(re) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/bitclust/lineinput.rb', line 110

def gets_unless(re)
  line = gets()
  if not line or re =~ line
    ungets line
    return nil
  end
  line
end

#inspectObject



30
31
32
# File 'lib/bitclust/lineinput.rb', line 30

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

#linenoObject



52
53
54
# File 'lib/bitclust/lineinput.rb', line 52

def lineno
  @lineno
end

#nameObject



44
45
46
47
48
49
50
# File 'lib/bitclust/lineinput.rb', line 44

def name
  if @entry
    @entry.inspect
  else
    "-"
  end
end

#next?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/bitclust/lineinput.rb', line 84

def next?
  peek() ? true : false
end

#pathObject



38
39
40
41
42
# File 'lib/bitclust/lineinput.rb', line 38

def path
  # @type var input: IO
  input = _ = @input
  input.path if input.respond_to?(:path)
end

#peekObject



78
79
80
81
82
# File 'lib/bitclust/lineinput.rb', line 78

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

#skip_blank_linesObject



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

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

#ungets(line) ⇒ Object



71
72
73
74
75
76
# File 'lib/bitclust/lineinput.rb', line 71

def ungets(line)
  return unless line
  @lineno -= 1
  @buf.push line
  line
end

#until_match(re) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/bitclust/lineinput.rb', line 146

def until_match(re)
  while line = gets()
    if re =~ line
      ungets line
      return
    end
    yield line
  end
  nil
end

#until_terminator(re) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/bitclust/lineinput.rb', line 167

def until_terminator(re)
  while line = gets()
    return if re =~ line   # discard terminal line
    yield line
  end
  nil
end

#while_match(re) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/bitclust/lineinput.rb', line 125

def while_match(re)
  while line = gets()
    unless re =~ line
      ungets line
      return
    end
    yield line
  end
  nil
end