Class: Perforce2Svn::Mapping::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/perforce2svn/mapping/lexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Lexer

Returns a new instance of Lexer.



23
24
25
# File 'lib/perforce2svn/mapping/lexer.rb', line 23

def initialize(content)
  @content = content
end

Instance Method Details

#each(&block) ⇒ Object

Yields each parsed line of the configuration



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/perforce2svn/mapping/lexer.rb', line 28

def each(&block)
  if not block_given?
    raise ArgumentError, "Requires a block"
  end

  lines = @content.readlines
  i = 1
  continues_from_previous = false
  previous = []
  
  lines.each do |line|
    parts= tokenize(line)

    will_continue_on_next_line = false
    if line =~ /\\\s*$/
      will_continue_on_next_line = true
    end

    if parts.length > 0
      if continues_from_previous
        previous << parts
      else
        previous = []
        name = parts.shift
        yield Token.new(name, parts, i)
      end
    end

    continues_from_previous = will_continue_on_next_line
    i += 1
  end
end

#tokenize(line) ⇒ Object

Reads a line for all of the possible tokens



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/perforce2svn/mapping/lexer.rb', line 62

def tokenize(line)
  chars = line.scan(/./)
  parts = []
  
  part = ""
  i = 0
  while i < chars.length
    current = chars[i]
    if current == '\\'
      nxt = chars[i + 1]
      if nxt == ' '
        i += 1
        part << ' '
      elsif nxt == "\n"
        break
      end
    elsif current == ' '
      if part.length > 0
        parts << part
        part = ""
      end
    elsif current == '#'
      break
    else
      part << current
    end

    i += 1
  end

  if not parts[-1].eql?(part)
    parts << part
  end

  parts.delete_if {|p| p =~ /^\s*$/}
  parts
end