Search documentation of Localize

# Localize.Number.Format.Compiler (Localize v0.39.0)

Copy Markdown View Source

Compiles number format patterns into metadata for fast runtime interpretation.

Number format patterns like `"#,##0.###"` or `"¤#,##0.00"` are parsed using a leex/yecc lexer-parser and then analysed to extract formatting metadata (digit counts, grouping, rounding, etc.) into a `Localize.Number.Format.Meta` struct.

## Summary

### Functions

compile(definition)

Compiles a number format definition into metadata.

format_to_metadata(format)

Extracts metadata from a parsed format.

format_to_metadata!(format)

Same as `format_to_metadata/1` but raises on error.

number_match_regex()

Returns a regex that can be used to split a number format or number string into integer, fraction, and exponent parts.

parse(tokens)

Parses a number format definition into a keyword list of positive and negative format elements.

tokenize(definition)

Tokenizes a number format definition string.

## Functions

# compile(definition)

    @spec compile(String.t()) ::
      {:ok, Localize.Number.Format.Meta.t()} | {:error, String.t()}

Compiles a number format definition into metadata.

Parses the format string, analyses it, and returns the metadata struct used to drive number formatting.

### Arguments

- `definition` is a number format pattern string.

### Returns

- `{:ok, meta}` where `meta` is a `Localize.Number.Format.Meta.t()`.

- `{:error, reason}` if parsing fails.

# format_to_metadata(format)

    @spec format_to_metadata(String.t() | Keyword.t()) ::
      {:ok, Localize.Number.Format.Meta.t()} | {:error, String.t()}

Extracts metadata from a parsed format.

### Arguments

- `format` is either a format pattern string or a parsed keyword list from `parse/1`.

### Returns

- `{:ok, meta}` where `meta` is a `Localize.Number.Format.Meta.t()`.

# format_to_metadata!(format)

    @spec format_to_metadata!(String.t() | Keyword.t()) :: Localize.Number.Format.Meta.t()

Same as `format_to_metadata/1` but raises on error.

### Arguments

- `format` is either a format pattern string or a parsed keyword list.

### Returns

- A `Localize.Number.Format.Meta.t()` struct.

### Raises

- Raises `ArgumentError` if the format cannot be parsed.

# number_match_regex()

Returns a regex that can be used to split a number format or number string into integer, fraction, and exponent parts.

# parse(tokens)

    @spec parse(String.t() | list()) :: {:ok, Keyword.t()} | {:error, term()}

Parses a number format definition into a keyword list of positive and negative format elements.

### Arguments

- `definition` is a number format pattern string or a list of tokens from `tokenize/1`.

### Returns

- `{:ok, format}` where `format` is a keyword list with `:positive` and `:negative` keys.

- `{:error, reason}` if parsing fails.

### Examples

    iex> {:ok, parsed} = Localize.Number.Format.Compiler.parse("#,##0.###")
    iex> parsed[:positive]
    [format: "#,##0.###"]

# tokenize(definition)

    @spec tokenize(String.t()) :: {:ok, list(), integer()} | {:error, term(), integer()}

Tokenizes a number format definition string.

### Arguments

- `definition` is a number format pattern string.

### Returns

- `{:ok, tokens, end_line}` or an error tuple.