mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
33 lines
810 B
Elixir
33 lines
810 B
Elixir
defmodule PhilomenaQuery.Parse.IntParser do
|
|
@moduledoc false
|
|
|
|
import NimbleParsec
|
|
|
|
defp to_int(input), do: PhilomenaQuery.Parse.Helpers.to_int(input)
|
|
defp range(input), do: PhilomenaQuery.Parse.Helpers.range(input)
|
|
|
|
space =
|
|
choice([string(" "), string("\t"), string("\n"), string("\r"), string("\v"), string("\f")])
|
|
|> ignore()
|
|
|
|
fuzz =
|
|
string("~")
|
|
|> ignore()
|
|
|
|
int =
|
|
optional(ascii_char(~c"-+"))
|
|
|> ascii_string([?0..?9], min: 1)
|
|
|> reduce({List, :to_string, []})
|
|
|> reduce(:to_int)
|
|
|
|
int_parser =
|
|
choice([
|
|
int |> concat(fuzz) |> integer(min: 1) |> reduce(:range) |> unwrap_and_tag(:int_range),
|
|
int |> unwrap_and_tag(:int)
|
|
])
|
|
|> repeat(space)
|
|
|> eos()
|
|
|> label("an integer, like `3' or `-10'")
|
|
|
|
defparsec(:parse, int_parser)
|
|
end
|