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.



123
124
125
126
# File 'lib/tins/unit.rb', line 123

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

Instance Method Details

#parseObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tins/unit.rb', line 139

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

#resetObject



128
129
130
131
# File 'lib/tins/unit.rb', line 128

def reset
  super
  @unit_parser.reset
end