philomena/lib/search/float_parser.ex

31 lines
805 B
Elixir
Raw Normal View History

2019-11-02 19:34:25 +01:00
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()
2019-11-02 21:31:55 +01:00
|> label("a real number, like `2.7182818' or `-10'")
2019-11-02 19:34:25 +01:00
defparsec :parse, float_parser
end