Parses CLDR Unit of Measure identifier strings into structured ASTs.
Implements the unit identifier syntax defined in Unicode TR35.
Summary
Functions
Parses a CLDR unit identifier string.
Parses a CLDR unit identifier string, raising on error.
Parses the given binary as a CLDR unit identifier, returning the
raw NimbleParsec result tuple.
Functions
@spec parse(String.t()) :: {:ok, tuple()} | {:error, Exception.t()}
Parses a CLDR unit identifier string.
Arguments
inputis a unit identifier string such as"meter-per-second"or"length-kilometer".
Returns
{:ok, ast}whereastis the parsed unit structure, or{:error, reason}if the input cannot be parsed.
Examples
iex> Localize.Unit.Parser.parse("meter")
{:ok, {:unit, type: nil, numerator: [{:single_unit, prefix: nil, power: nil, base: "meter"}], denominator: []}}
iex> Localize.Unit.Parser.parse("meter-per-second")
{:ok, {:unit, type: nil, numerator: [{:single_unit, prefix: nil, power: nil, base: "meter"}], denominator: [{:single_unit, prefix: nil, power: nil, base: "second"}]}}
Parses a CLDR unit identifier string, raising on error.
Same as parse/1 but returns the AST directly or raises
ArgumentError.
Arguments
inputis a unit identifier string.
Returns
- The parsed unit AST.
Examples
iex> Localize.Unit.Parser.parse!("meter")
{:unit, type: nil, numerator: [{:single_unit, prefix: nil, power: nil, base: "meter"}], denominator: []}
@spec unit_identifier(String.t(), Keyword.t()) :: {:ok, list(), String.t(), map(), {pos_integer(), non_neg_integer()}, non_neg_integer()} | {:error, String.t(), String.t(), map(), {pos_integer(), non_neg_integer()}, non_neg_integer()}
Parses the given binary as a CLDR unit identifier, returning the
raw NimbleParsec result tuple.
This is the low-level parsec entry point. Most callers should use
parse/1, which unwraps the result and returns tagged
{:ok, ast} / {:error, exception} tuples.
Arguments
binaryis a unit identifier string such as"meter".optionsis a keyword list of NimbleParsec options (:byte_offset,:line, and:context).
Returns
{:ok, [token], rest, context, position, byte_offset}on success.{:error, reason, rest, context, line, byte_offset}on failure, including abinarythat is not a string.
Examples
iex> Localize.Unit.Parser.unit_identifier("meter")
{:ok, [unit: [type: nil, numerator: [single_unit: [prefix: nil, power: nil, base: "meter"]], denominator: []]], "", %{}, {1, 0}, 5}