Class: Tins::Unit::FormatParser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/tins/unit.rb

Instance Method Summary collapse

Constructor Details

#initialize(format, unit_parser) ⇒ FormatParser

Returns a new instance of FormatParser.



116
117
118
119
# File 'lib/tins/unit.rb', line 116

def initialize(format, unit_parser)
  super format
  @unit_parser = unit_parser
end

Instance Method Details

#parseObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tins/unit.rb', line 132

def parse
  reset
  until eos? || @unit_parser.eos?
    case
    when scan(/%f/)
      @unit_parser.scan_number or
        raise ArgumentError, "\"%f\" expected at #{location}"
    when scan(/%U/)
      @unit_parser.scan_unit or
        raise ArgumentError, "\"%U\" expected at #{location}"
    when scan(/%%/)
      @unit_parser.scan_char(?%) or
        raise ArgumentError, "#{?%.inspect} expected at #{location}"
    else
      char = scan(/./)
      @unit_parser.scan_char(char) or
        raise ArgumentError, "#{char.inspect} expected at #{location}"
    end
  end
  unless eos? && @unit_parser.eos?
    raise ArgumentError,
      "format #{string.inspect} and string "\
        "#{@unit_parser.string.inspect} do not match"
  end
  @unit_parser.number
end

#resetObject



121
122
123
124
# File 'lib/tins/unit.rb', line 121

def reset
  super
  @unit_parser.reset
end