From 7845ab4818fddbdf6928114e06488a9e7ccc629a Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 22 Aug 2019 00:45:38 -0400 Subject: [PATCH] working date parsing --- lib/philomena/search/lexer.ex | 59 ++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/philomena/search/lexer.ex b/lib/philomena/search/lexer.ex index 6e9c521b..ebc8858c 100644 --- a/lib/philomena/search/lexer.ex +++ b/lib/philomena/search/lexer.ex @@ -91,37 +91,54 @@ defmodule Philomena.Search.Lexer do ymd_sep = ignore(string("-")) hms_sep = ignore(string(":")) iso8601_sep = ignore(choice([string("T"), string("t"), space])) - iso8601_tzsep = choice([string("+"), string("-")]) + iso8601_tzsep = + choice([ + string("+") |> replace(1), + string("-") |> replace(-1) + ]) zulu = ignore(choice([string("Z"), string("z")])) - """ - absolute_date = + date_part = year |> optional( - concat(ymd_sep, month) + ymd_sep + |> concat(month) |> optional( - concat(ymd_sep, day) + ymd_sep + |> concat(day) |> optional( - concat(iso8601_sep, hour) + iso8601_sep |> optional( - concat(hms_sep, minute) + hour |> optional( - concat(hms_sep, second) + hms_sep + |> concat(minute) + |> optional( + concat(hms_sep, second) + ) ) ) ) ) ) - |> optional( - choice([ - concat(iso8601_tzsep, tz_hour) - |> optional( - concat(hms_sep, tz_minute) - ), - zulu - ]) - ) - """ + |> tag(:date) + + timezone_part = + choice([ + iso8601_tzsep + |> concat(tz_hour) + |> optional( + hms_sep + |> concat(tz_minute) + ) + |> tag(:timezone), + zulu + ]) + + absolute_date = + date_part + |> optional(timezone_part) + |> tag(:absolute_date) relative_date = integer(min: 1) @@ -139,10 +156,10 @@ defmodule Philomena.Search.Lexer do |> tag(:relative_date) date = - # choice([ - # absolute_date, + choice([ + absolute_date, relative_date - # ]) + ]) boost = ignore(string("^")) |> unwrap_and_tag(number, :boost) fuzz = ignore(string("~")) |> unwrap_and_tag(number, :fuzz)