Class: Diff2Xml::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Parser

Returns a new instance of Parser.



35
36
37
38
39
40
# File 'lib/diff2xml/parser.rb', line 35

def initialize(handler)
  @source = ""
  @started = false
  @block_started = false
  @handler = handler
end

Instance Method Details

#parse(istream) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/diff2xml/parser.rb', line 42

def parse(istream)
  @handler.start_parse
  istream.each do |line|
    if /^\-\-\-[\t\s]*([\w+\.\/\\\:]+)/.match(line) then
      @source = $1
    elsif /^\+\+\+[\t\s]*([\w+\.\/\\\:]+)/.match(line) then
      target = $1
      if @started then
        @handler.end_document
      end
      @handler.start_document(@source, target)
      @started = true
    elsif /^\-(.*)/.match(line) then
      @handler.source_line($1)
    elsif /^\+(.*)/.match(line) then
      @handler.target_line($1)
    elsif /^\s(.*)/.match(line) then
      @handler.common_line($1)
    elsif /^@@\s\-(\d+),(\d+)\s\+(\d+),(\d+)\s@@/.match(line) then
      if @block_started then
        @handler.end_block
      end
      @handler.start_block($1, $2, $3, $4)
      @block_started = true
    end
  end
  if @block_started then
    @handler.end_block
  end
  if @started then
    @handler.end_document
  end
  @handler.end_parse
end