Class: Epos::TextParser

Inherits:
Object
  • Object
show all
Defined in:
lib/epos/text-parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(text) ⇒ Object



20
21
22
23
24
25
26
27
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
60
61
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
99
100
101
102
103
# File 'lib/epos/text-parser.rb', line 20

def parse(text)
  @result   = []
  @format   = [{}]
  @fragment = ""
  @cmd      = ""

  s    = :reading_fragment
  code = ""

  text.each_char do |c|

    case s

    when :reading_fragment
      case c 
      when "\\"
        s = :escape_started
      when "{"
        self.flush
        @format << @format.last.clone
      when "}"
        self.flush
        @format.pop if @format.length > 1 # Entry "bum-bum" is broken.
      else
        @fragment << c
      end

    when :escape_started
      case c
      when "\\"
        @fragment << c
        s = :reading_fragment
      when "{"
        @fragment << c
        s = :reading_fragment
      when "'"
        code = ""
        s = :reading_code
      else
        @cmd = c
        s = :reading_command
      end

    when :reading_command
      case c
      when " "
        self.command
        s = :reading_fragment
      when "\\"
        self.command
        s = :escape_started
      when /[a-z0-9]/
        @cmd << c
      when "{"
        self.command
        self.flush
        @format << @format.last.clone
        s = :reading_fragment
      when "}"
        self.command
        self.flush
        @format.pop
        s = :reading_fragment
      else
        self.command
        @fragment << c
        s = :reading_fragment
      end

    when :reading_code
      code << c
      if code.length == 2
        @fragment << [code.to_i(16)].pack("U")
        s = :reading_fragment
      end
    end
  end

  self.command if s == :reading_command
  self.flush

  return @result

end