split off optional

This commit is contained in:
Liam P. White 2019-08-23 13:09:33 -04:00
parent c794336557
commit 968a636eec
2 changed files with 47 additions and 20 deletions

View file

@ -0,0 +1,23 @@
defmodule Philomena.Search.Helpers do
import NimbleParsec
def to_number(term) do
{float_val, _} = :string.to_float(term)
{int_val, _} = :string.to_integer(term)
cond do
is_float(float_val) ->
float_val
is_integer(int_val) ->
int_val
end
end
def reverse_times(combinator \\ empty(), to_repeat, opts) do
max = opts[:max]
combinator
|> choice((for x <- max..1, do: times(to_repeat, x)) ++ [empty()])
end
end

View file

@ -1,18 +1,6 @@
defmodule Philomena.Search.Lexer do defmodule Philomena.Search.Lexer do
import NimbleParsec import NimbleParsec
import Philomena.Search.Helpers
defp to_number(term) do
{float_val, _} = :string.to_float(term)
{int_val, _} = :string.to_integer(term)
cond do
is_float(float_val) ->
float_val
is_integer(int_val) ->
int_val
end
end
l_and = l_and =
choice([string("AND"), string("&&"), string(",")]) choice([string("AND"), string("&&"), string(",")])
@ -76,13 +64,27 @@ defmodule Philomena.Search.Lexer do
choice([ choice([
times(ipv6_fragment, 6) |> concat(ipv6_ls32), times(ipv6_fragment, 6) |> concat(ipv6_ls32),
string("::") |> times(ipv6_fragment, 5) |> concat(ipv6_ls32), string("::") |> times(ipv6_fragment, 5) |> concat(ipv6_ls32),
optional(ipv6_hexadectet) |> string("::") |> times(ipv6_fragment, 4) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 1) |> concat(ipv6_hexadectet)) |> string("::") |> times(ipv6_fragment, 3) |> concat(ipv6_ls32), ipv6_hexadectet |> string("::") |> times(ipv6_fragment, 4) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 2) |> concat(ipv6_hexadectet)) |> string("::") |> times(ipv6_fragment, 2) |> concat(ipv6_ls32), string("::") |> times(ipv6_fragment, 4) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 3) |> concat(ipv6_hexadectet)) |> string("::") |> concat(ipv6_fragment) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 4) |> concat(ipv6_hexadectet)) |> string("::") |> concat(ipv6_ls32), times(ipv6_fragment, max: 1) |> concat(ipv6_hexadectet) |> string("::") |> times(ipv6_fragment, 3) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 5) |> concat(ipv6_hexadectet)) |> string("::") |> concat(ipv6_hexadectet), string("::") |> times(ipv6_fragment, 3) |> concat(ipv6_ls32),
optional(times(ipv6_fragment, max: 6) |> concat(ipv6_hexadectet)) |> string("::")
times(ipv6_fragment, max: 2) |> concat(ipv6_hexadectet) |> string("::") |> times(ipv6_fragment, 2) |> concat(ipv6_ls32),
string("::") |> times(ipv6_fragment, 2) |> concat(ipv6_ls32),
times(ipv6_fragment, max: 3) |> concat(ipv6_hexadectet) |> string("::") |> concat(ipv6_fragment) |> concat(ipv6_ls32),
string("::") |> concat(ipv6_fragment) |> concat(ipv6_ls32),
times(ipv6_fragment, max: 4) |> concat(ipv6_hexadectet) |> string("::") |> concat(ipv6_ls32),
string("::") |> concat(ipv6_ls32),
times(ipv6_fragment, max: 5) |> concat(ipv6_hexadectet) |> string("::") |> concat(ipv6_hexadectet),
string("::") |> concat(ipv6_hexadectet),
times(ipv6_fragment, max: 6) |> concat(ipv6_hexadectet) |> string("::"),
string("::")
]) ])
cidr_prefix = cidr_prefix =
@ -99,6 +101,8 @@ defmodule Philomena.Search.Lexer do
|> label("a valid IPv4 or IPv6 address and optional CIDR prefix") |> label("a valid IPv4 or IPv6 address and optional CIDR prefix")
|> unwrap_and_tag(:ip) |> unwrap_and_tag(:ip)
defparsec :ip, ipv6_address
year = integer(4) year = integer(4)
month = integer(2) month = integer(2)
day = integer(2) day = integer(2)