mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-24 04:27:59 +01:00
pass old parser tests
This commit is contained in:
parent
c46cceab03
commit
c0379ec056
3 changed files with 28 additions and 18 deletions
|
@ -1,11 +1,16 @@
|
||||||
defmodule Search.LiteralParser do
|
defmodule Search.LiteralParser do
|
||||||
import NimbleParsec
|
import NimbleParsec
|
||||||
|
import Search.Helpers
|
||||||
|
|
||||||
defp trim([term]), do: String.trim(term)
|
float =
|
||||||
|
ascii_string([?0..?9], min: 1)
|
||||||
|
|> optional(ascii_char('.') |> ascii_string([?0..?9], min: 1))
|
||||||
|
|> reduce({List, :to_string, []})
|
||||||
|
|> reduce(:to_number)
|
||||||
|
|
||||||
edit_distance =
|
edit_distance =
|
||||||
ignore(string("~"))
|
ignore(string("~"))
|
||||||
|> integer(min: 1)
|
|> concat(float)
|
||||||
|> unwrap_and_tag(:fuzz)
|
|> unwrap_and_tag(:fuzz)
|
||||||
|> eos()
|
|> eos()
|
||||||
|
|
||||||
|
@ -24,7 +29,6 @@ defmodule Search.LiteralParser do
|
||||||
])
|
])
|
||||||
|> repeat()
|
|> repeat()
|
||||||
|> reduce({List, :to_string, []})
|
|> reduce({List, :to_string, []})
|
||||||
|> reduce(:trim)
|
|
||||||
|> unwrap_and_tag(:literal)
|
|> unwrap_and_tag(:literal)
|
||||||
|> optional(edit_distance)
|
|> optional(edit_distance)
|
||||||
|> eos()
|
|> eos()
|
||||||
|
@ -40,7 +44,6 @@ defmodule Search.LiteralParser do
|
||||||
])
|
])
|
||||||
|> repeat()
|
|> repeat()
|
||||||
|> reduce({List, :to_string, []})
|
|> reduce({List, :to_string, []})
|
||||||
|> reduce(:trim)
|
|
||||||
|> unwrap_and_tag(:wildcard)
|
|> unwrap_and_tag(:wildcard)
|
||||||
|> ignore(optional(edit_distance))
|
|> ignore(optional(edit_distance))
|
||||||
|> eos()
|
|> eos()
|
||||||
|
|
|
@ -42,7 +42,14 @@ defmodule Search.Parser do
|
||||||
%{parser | __fields__: Map.new(fields)}
|
%{parser | __fields__: Map.new(fields)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(%Parser{} = parser, input, context \\ nil) do
|
def parse(parser, input, context \\ nil)
|
||||||
|
|
||||||
|
# Empty search should emit a match_none.
|
||||||
|
def parse(_parser, "", _context) do
|
||||||
|
{:ok, %{match_none: %{}}}
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(%Parser{} = parser, input, context) do
|
||||||
parser = %{parser | __data__: context}
|
parser = %{parser | __data__: context}
|
||||||
|
|
||||||
with {:ok, tokens, _1, _2, _3, _4} <- Lexer.lex(input),
|
with {:ok, tokens, _1, _2, _3, _4} <- Lexer.lex(input),
|
||||||
|
@ -51,7 +58,7 @@ defmodule Search.Parser do
|
||||||
{:ok, tree}
|
{:ok, tree}
|
||||||
else
|
else
|
||||||
{:ok, {_tree, tokens}} ->
|
{:ok, {_tree, tokens}} ->
|
||||||
{:error, "Junk at end of expression: " <> debug_tokens(tokens)}
|
{:error, "junk at end of expression: " <> debug_tokens(tokens)}
|
||||||
|
|
||||||
{:error, msg, start_pos, _1, _2, _3} ->
|
{:error, msg, start_pos, _1, _2, _3} ->
|
||||||
{:error, msg <> ", starting at: " <> start_pos}
|
{:error, msg <> ", starting at: " <> start_pos}
|
||||||
|
@ -60,7 +67,7 @@ defmodule Search.Parser do
|
||||||
{:error, msg}
|
{:error, msg}
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
{:error, "Search parsing error."}
|
{:error, "unknown parsing error"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,8 +120,8 @@ defmodule Search.Parser do
|
||||||
{:ok, {child, r_tokens}} ->
|
{:ok, {child, r_tokens}} ->
|
||||||
{:ok, {%{bool: %{must_not: child}}, r_tokens}}
|
{:ok, {%{bool: %{must_not: child}}, r_tokens}}
|
||||||
|
|
||||||
err ->
|
value ->
|
||||||
err
|
value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -128,8 +135,8 @@ defmodule Search.Parser do
|
||||||
{:ok, {_child, _tokens}} ->
|
{:ok, {_child, _tokens}} ->
|
||||||
{:error, "Imbalanced parentheses."}
|
{:error, "Imbalanced parentheses."}
|
||||||
|
|
||||||
err ->
|
value ->
|
||||||
err
|
value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -245,7 +252,7 @@ defmodule Search.Parser do
|
||||||
|
|
||||||
|
|
||||||
defp field_type(parser, [{DateParser, field_name}, range: :eq, date: [lower, upper]]),
|
defp field_type(parser, [{DateParser, field_name}, range: :eq, date: [lower, upper]]),
|
||||||
do: {:ok, {%{range: %{field(parser, field_name) => %{gte: lower, lte: upper}}}, []}}
|
do: {:ok, {%{range: %{field(parser, field_name) => %{gte: lower, lt: upper}}}, []}}
|
||||||
|
|
||||||
defp field_type(parser, [{DateParser, field_name}, range: r, date: [_lower, upper]]) when r in [:lte, :gt],
|
defp field_type(parser, [{DateParser, field_name}, range: r, date: [_lower, upper]]) when r in [:lte, :gt],
|
||||||
do: {:ok, {%{range: %{field(parser, field_name) => %{r => upper}}}, []}}
|
do: {:ok, {%{range: %{field(parser, field_name) => %{r => upper}}}, []}}
|
||||||
|
|
|
@ -18,17 +18,17 @@ defmodule Search.TermRangeParser do
|
||||||
|
|
||||||
case input do
|
case input do
|
||||||
<<^field_name::binary-size(field_sz), ":", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ":", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :eq, value: value]
|
[{field_parser, field_name}, range: :eq, value: String.trim(value)]
|
||||||
<<^field_name::binary-size(field_sz), ".eq:", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ".eq:", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :eq, value: value]
|
[{field_parser, field_name}, range: :eq, value: String.trim(value)]
|
||||||
<<^field_name::binary-size(field_sz), ".gt:", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ".gt:", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :gt, value: value]
|
[{field_parser, field_name}, range: :gt, value: String.trim(value)]
|
||||||
<<^field_name::binary-size(field_sz), ".gte:", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ".gte:", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :gte, value: value]
|
[{field_parser, field_name}, range: :gte, value: String.trim(value)]
|
||||||
<<^field_name::binary-size(field_sz), ".lt:", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ".lt:", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :lt, value: value]
|
[{field_parser, field_name}, range: :lt, value: String.trim(value)]
|
||||||
<<^field_name::binary-size(field_sz), ".lte:", value::binary>> ->
|
<<^field_name::binary-size(field_sz), ".lte:", value::binary>> ->
|
||||||
[{field_parser, field_name}, range: :lte, value: value]
|
[{field_parser, field_name}, range: :lte, value: String.trim(value)]
|
||||||
_ ->
|
_ ->
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue