Skip to content

Commit

Permalink
allow to handle unicode strings with single/double quotes in parser #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Colombo committed Jan 8, 2024
1 parent b3cd425 commit 9ec08bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/fiql_lexer.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ list_to_bool(Chars) ->
to_bool(string:lowercase(list_to_binary(Chars))).
from_double_quoted(Chars) ->
binary_to_list(re:replace(string:slice(list_to_binary(Chars), 1, string:length(list_to_binary(Chars))-2), "\\\\\"", "\"", [global, {return, binary}])).
unicode:characters_to_list(re:replace(string:slice(unicode:characters_to_binary(Chars), 1, string:length(unicode:characters_to_binary(Chars))-2), "\\\\\"", "\"", [global, {return, binary}])).

from_single_quoted(Chars) ->
binary_to_list(re:replace(string:slice(list_to_binary(Chars), 1, string:length(list_to_binary(Chars))-2), "\\\\'", "'", [global, {return, binary}])).
unicode:characters_to_list(re:replace(string:slice(unicode:characters_to_binary(Chars), 1, string:length(unicode:characters_to_binary(Chars))-2), "\\\\'", "'", [global, {return, binary}])).

strip_equals(Chars) ->
binary_to_list(string:slice(list_to_binary(Chars), 1, string:length(list_to_binary(Chars))-2)).
binary_to_list(string:slice(list_to_binary(Chars), 1, string:length(list_to_binary(Chars))-2)).
24 changes: 24 additions & 0 deletions test/fiql_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ defmodule FIQLExParserTest do
{:ok, {:op, {:selector_and_value, "my_selector", :equal, "àèìòù"}}}
end

test "Selector with non ascii value and with single quotes" do
payload = "my_selector=='àèìòù'"
{:ok, tokens, _} = :fiql_lexer.string(to_charlist(payload))

assert :fiql_parser.parse(tokens) ==
{:ok, {:op, {:selector_and_value, "my_selector", :equal, "àèìòù"}}}
end

test "Selector with non ascii value and with double quotes" do
payload = "my_selector==\"àèìòù\""
{:ok, tokens, _} = :fiql_lexer.string(to_charlist(payload))

assert :fiql_parser.parse(tokens) ==
{:ok, {:op, {:selector_and_value, "my_selector", :equal, "àèìòù"}}}
end

test "Selector with unicode value" do
payload = "my_selector==€€€"
{:ok, tokens, _} = :fiql_lexer.string(to_charlist(payload))
Expand All @@ -147,6 +163,14 @@ defmodule FIQLExParserTest do
{:ok, {:op, {:selector_and_value, "my_selector", :equal, "€€€"}}}
end

test "Selector with unicode value single quoted" do
payload = "my_selector=='ф'"
{:ok, tokens, _} = :fiql_lexer.string(to_charlist(payload))

assert :fiql_parser.parse(tokens) ==
{:ok, {:op, {:selector_and_value, "my_selector", :equal, "ф"}}}
end

test "Selector with list value" do
payload = "my_selector==(1, \"hello world\", (true, false))"
{:ok, tokens, _} = :fiql_lexer.string(to_charlist(payload))
Expand Down

0 comments on commit 9ec08bd

Please sign in to comment.