Class: Tins::Unit::FormatParser

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

Overview

A parser for unit specifications that extends StringScanner

This class is responsible for parsing strings that contain numerical values followed by unit specifications, supporting various prefix types and unit formats for flexible unit parsing.

Instance Method Summary collapse

Constructor Details

#initialize(format, unit_parser) ⇒ Tins::Unit::FormatParser

The initialize method sets up a new UnitParser instance with the given format and unit parser.

parsing units with the provided parameters

Parameters:



224
225
226
227
# File 'lib/tins/unit.rb', line 224

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

Instance Method Details

#locationString (private)

The location method returns a string representation of the current parsing position by peeking at the next 10 characters from the unit parser and inspecting them

Returns:

  • (String)

    the inspected representation of the next 10 characters from the parser



243
244
245
# File 'lib/tins/unit.rb', line 243

def location
  @unit_parser.peek(10).inspect
end

#parseFloat

The parse method parses a format string using a unit parser and returns the parsed number.

This method processes a format template by scanning for specific pattern directives (%f for numbers, %U for units, %% for literal percent signs) and validates that the input string matches the expected format. It handles parsing errors by raising ParserError exceptions with descriptive messages about mismatches.

Returns:

  • (Float)

    the parsed numerical value with units applied

Raises:

  • (ParserError)

    if the format string or input string doesn’t match the expected pattern

  • (ParserError)

    if a required number or unit is missing at a specific location

  • (ParserError)

    if literal percent signs don’t match expected positions



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tins/unit.rb', line 261

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

The reset method resets the unit parser state.

This method calls the superclass reset implementation and then resets the internal unit parser instance to its initial state.



233
234
235
236
# File 'lib/tins/unit.rb', line 233

def reset
  super
  @unit_parser.reset
end