# `Localize.Number.Format.Compiler`
[🔗](https://github.com/elixir-localize/localize/blob/v0.39.0/lib/localize/number/format/compiler.ex#L1)

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.

# `compile`

```elixir
@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`

```elixir
@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!`

```elixir
@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`

```elixir
@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`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
