Class: Tins::Unit::FormatParser
- 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
-
#initialize(format, unit_parser) ⇒ Tins::Unit::FormatParser
constructor
The initialize method sets up a new UnitParser instance with the given format and unit parser.
-
#location ⇒ String
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.
-
#parse ⇒ Float
The parse method parses a format string using a unit parser and returns the parsed number.
-
#reset ⇒ Object
The reset method resets the unit parser state.
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
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
#location ⇒ String (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
243 244 245 |
# File 'lib/tins/unit.rb', line 243 def location @unit_parser.peek(10).inspect end |
#parse ⇒ Float
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.
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 |
#reset ⇒ Object
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 |