mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 04:27:59 +01:00
30 lines
No EOL
748 B
Elixir
30 lines
No EOL
748 B
Elixir
defmodule Search.FloatParser do
|
|
import NimbleParsec
|
|
import Search.Helpers
|
|
|
|
fuzz =
|
|
string("~")
|
|
|> ignore()
|
|
|
|
unsigned_float =
|
|
ascii_string([?0..?9], min: 1)
|
|
|> optional(ascii_char('.') |> ascii_string([?0..?9], min: 1))
|
|
|> reduce({List, :to_string, []})
|
|
|> reduce(:to_number)
|
|
|
|
float =
|
|
optional(ascii_char('-+'))
|
|
|> ascii_string([?0..?9], min: 1)
|
|
|> optional(ascii_char('.') |> ascii_string([?0..?9], min: 1))
|
|
|> reduce({List, :to_string, []})
|
|
|> reduce(:to_number)
|
|
|
|
float_parser =
|
|
choice([
|
|
float |> concat(fuzz) |> concat(unsigned_float) |> reduce(:range) |> unwrap_and_tag(:float_range),
|
|
float |> unwrap_and_tag(:float)
|
|
])
|
|
|> eos()
|
|
|
|
defparsec :parse, float_parser
|
|
end |