Class: Deb822::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/deb822/parser.rb

Overview

High-level parser for deb822 documents

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



7
8
9
# File 'lib/deb822/parser.rb', line 7

def initialize(input)
  @scanner = Scanner.new(input)
end

Instance Method Details

#each_paragraph {|last_par| ... } ⇒ Object Also known as: paragraphs

Yields:

  • (last_par)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/deb822/parser.rb', line 11

def each_paragraph
  return to_enum(:each_paragraph) unless block_given?

  last_par = last_val = nil

  @scanner.each_line do |l|
    case l[0]
    when :paragraph_separator
      yield last_par if last_par
      last_par = last_val = nil
    when :comment
      next
    when :field
      last_par ||= Paragraph.new
      last_par[l[1]] = last_val = l[2]
    when :continuation
      last_val << "\n" unless last_val.end_with?("\n")
      last_val << l[1]
    else
      fail "BUG: unreachable code: #{l.inspect}"
    end
  end

  yield last_par if last_par
end