Class: LineInput

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

Overview

Utility class for line-wise file parsing

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f, entry = nil) ⇒ LineInput

Returns a new instance of LineInput.



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

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

Class Method Details

.for_string(s) ⇒ Object



17
18
19
# File 'lib/bitclust/lineinput.rb', line 17

def LineInput.for_string(s)
  new(StringIO.new(s))
end

Instance Method Details

#eachObject



116
117
118
119
120
# File 'lib/bitclust/lineinput.rb', line 116

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

#eof?Boolean

Returns:

  • (Boolean)


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

def eof?
  @eof_p
end

#getblock(term_re) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/bitclust/lineinput.rb', line 172

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

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



154
155
156
157
158
159
160
# File 'lib/bitclust/lineinput.rb', line 154

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

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



133
134
135
136
137
138
139
# File 'lib/bitclust/lineinput.rb', line 133

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

#getsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bitclust/lineinput.rb', line 53

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



97
98
99
100
101
102
103
104
105
# File 'lib/bitclust/lineinput.rb', line 97

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



107
108
109
110
111
112
113
114
# File 'lib/bitclust/lineinput.rb', line 107

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

#inspectObject



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

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

#linenoObject



49
50
51
# File 'lib/bitclust/lineinput.rb', line 49

def lineno
  @lineno
end

#nameObject



41
42
43
44
45
46
47
# File 'lib/bitclust/lineinput.rb', line 41

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

#next?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/bitclust/lineinput.rb', line 81

def next?
  peek() ? true : false
end

#pathObject



37
38
39
# File 'lib/bitclust/lineinput.rb', line 37

def path
  @input.path if @input.respond_to?(:path)
end

#peekObject



75
76
77
78
79
# File 'lib/bitclust/lineinput.rb', line 75

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

#skip_blank_linesObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bitclust/lineinput.rb', line 85

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



68
69
70
71
72
73
# File 'lib/bitclust/lineinput.rb', line 68

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

#until_match(re) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/bitclust/lineinput.rb', line 143

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

#until_terminator(re) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/bitclust/lineinput.rb', line 164

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

#while_match(re) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/bitclust/lineinput.rb', line 122

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