From 8dae60de92647a83fc7c08feb0acf260e01f6899 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 31 Mar 2020 13:27:53 -0400 Subject: [PATCH] require spaces around textual search operators, fixes #37 --- lib/search/lexer.ex | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/search/lexer.ex b/lib/search/lexer.ex index 7b56573e..0a2bab7b 100644 --- a/lib/search/lexer.ex +++ b/lib/search/lexer.ex @@ -3,6 +3,10 @@ defmodule Search.Lexer do defp to_number(input), do: Search.Helpers.to_number(input) + space = + choice([string(" "), string("\t"), string("\n"), string("\r"), string("\v"), string("\f")]) + |> ignore() + float = optional(ascii_char('-+')) |> ascii_string([?0..?9], min: 1) @@ -11,24 +15,33 @@ defmodule Search.Lexer do |> reduce(:to_number) l_and = - choice([string("AND"), string("&&"), string(",")]) + times(space, min: 1) + |> choice([string("AND"), string("&&")]) + |> times(space, min: 1) + |> unwrap_and_tag(:and) + + l_comma = + string(",") |> unwrap_and_tag(:and) l_or = - choice([string("OR"), string("||")]) + times(space, min: 1) + |> choice([string("OR"), string("||")]) + |> times(space, min: 1) |> unwrap_and_tag(:or) l_not = - choice([string("NOT"), string("!"), string("-")]) + string("NOT") + |> times(space, min: 1) + |> unwrap_and_tag(:not) + + l_negate = + choice([string("!"), string("-")]) |> unwrap_and_tag(:not) lparen = string("(") |> unwrap_and_tag(:lparen) rparen = string(")") |> unwrap_and_tag(:rparen) - space = - choice([string(" "), string("\t"), string("\n"), string("\r"), string("\v"), string("\f")]) - |> ignore() - quot = string("\"") boost = @@ -37,12 +50,12 @@ defmodule Search.Lexer do |> unwrap_and_tag(:boost) stop_words = - repeat(space) - |> choice([ + choice([ + l_comma, l_and, l_or, - rparen, - boost + repeat(space) |> concat(rparen), + repeat(space) |> concat(boost) ]) defcombinatorp( @@ -85,6 +98,8 @@ defmodule Search.Lexer do outer = choice([ + l_comma, + l_negate, l_and, l_or, l_not,