Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case insensitive string using i suffix #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion priv/neotoma_parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ nonterminal <- alpha_char alphanumeric_char*
{nonterminal, Symbol}
`;

terminal <- regexp_string / quoted_string / character_class / anything_symbol ~;
terminal <- regexp_string / case_insensitive_string / quoted_string / character_class / anything_symbol ~;

regexp_string <- '#' string:(!'#' ('\\#' / .))+ '#'
`
Expand All @@ -171,6 +171,15 @@ double_quoted_string <- '"' string:(!'"' ("\\\\" / '\\"' / .))* '"' ~;

single_quoted_string <- "'" string:(!"'" ("\\\\" / "\\'" / .))* "'" ~;

case_insensitive_string <- target:(double_quoted_string / single_quoted_string) 'i'
`
used_combinator(p_case_insensitive),
Str = string:lowercase(proplists:get_value(string, proplists:get_value(target, Node))),
lists:flatten(["p_case_insensitive(<<\"",
escape_string(unicode:characters_to_list(Str)),
"\">>)"])
`;

character_class <- '[' characters:(!']' ('\\\\' . / !'\\\\' .))+ ']'
`
used_combinator(p_charclass),
Expand Down
16 changes: 16 additions & 0 deletions priv/peg_includes.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ p_string(S) ->
end.
-endif.

-ifdef(p_case_insensitive).
-spec p_case_insensitive(binary()) -> parse_fun().
p_case_insensitive(S) ->
Length = erlang:byte_size(S),
SLower = string:lowercase(S),
fun(Input, Index) ->
try
<<Sc:Length/binary, Rest/binary>> = Input,
SLower = string:lowercase(Sc),
{SLower, Rest, p_advance_index(S, Index)}
catch
error:{badmatch,_} -> {fail, {expected, {string, S}, Index}}
end
end.
-endif.

-ifdef(p_anything).
-spec p_anything() -> parse_fun().
p_anything() ->
Expand Down
2 changes: 1 addition & 1 deletion src/neotoma.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

-define(ALL_COMBINATORS, [p_eof, p_optional, p_not, p_assert, p_seq,
p_choose, p_zero_or_more, p_one_or_more, p_label, p_scan,
p_string, p_anything, p_charclass, p_regexp, line, column]).
p_string, p_case_insensitive, p_anything, p_charclass, p_regexp, line, column]).

-type option() :: {module, atom()} | {output, file:filename()} | {transform_module, atom()} |
{neotoma_priv_dir, file:filename()}.
Expand Down
28 changes: 27 additions & 1 deletion src/neotoma_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ end

-spec 'terminal'(input(), index()) -> parse_result().
'terminal'(Input, Index) ->
p(Input, Index, 'terminal', fun(I,D) -> (p_choose([fun 'regexp_string'/2, fun 'quoted_string'/2, fun 'character_class'/2, fun 'anything_symbol'/2]))(I,D) end, fun(Node, _Idx) ->Node end).
p(Input, Index, 'terminal', fun(I,D) -> (p_choose([fun 'regexp_string'/2, fun 'case_insensitive_string'/2, fun 'quoted_string'/2, fun 'character_class'/2, fun 'anything_symbol'/2]))(I,D) end, fun(Node, _Idx) ->Node end).

-spec 'regexp_string'(input(), index()) -> parse_result().
'regexp_string'(Input, Index) ->
Expand Down Expand Up @@ -326,6 +326,16 @@ end
'single_quoted_string'(Input, Index) ->
p(Input, Index, 'single_quoted_string', fun(I,D) -> (p_seq([p_string(<<"\'">>), p_label('string', p_zero_or_more(p_seq([p_not(p_string(<<"\'">>)), p_choose([p_string(<<"\\\\">>), p_string(<<"\\\'">>), p_anything()])]))), p_string(<<"\'">>)]))(I,D) end, fun(Node, _Idx) ->Node end).

-spec 'case_insensitive_string'(input(), index()) -> parse_result().
'case_insensitive_string'(Input, Index) ->
p(Input, Index, 'case_insensitive_string', fun(I,D) -> (p_seq([p_label('target', p_choose([fun 'double_quoted_string'/2, fun 'single_quoted_string'/2])), p_string(<<"i">>)]))(I,D) end, fun(Node, _Idx) ->
used_combinator(p_case_insensitive),
Str = string:lowercase(proplists:get_value(string, proplists:get_value(target, Node))),
lists:flatten(["p_case_insensitive(<<\"",
escape_string(unicode:characters_to_list(Str)),
"\">>)"])
end).

-spec 'character_class'(input(), index()) -> parse_result().
'character_class'(Input, Index) ->
p(Input, Index, 'character_class', fun(I,D) -> (p_seq([p_string(<<"[">>), p_label('characters', p_one_or_more(p_seq([p_not(p_string(<<"]">>)), p_choose([p_seq([p_string(<<"\\\\">>), p_anything()]), p_seq([p_not(p_string(<<"\\\\">>)), p_anything()])])]))), p_string(<<"]">>)]))(I,D) end, fun(Node, _Idx) ->
Expand Down Expand Up @@ -564,6 +574,22 @@ p_string(S) ->
end.
-endif.

-ifdef(p_case_insensitive).
-spec p_case_insensitive(binary()) -> parse_fun().
p_case_insensitive(S) ->
Length = erlang:byte_size(S),
SLower = string:lowercase(S),
fun(Input, Index) ->
try
<<Sc:Length/binary, Rest/binary>> = Input,
SLower = string:lowercase(Sc),
{SLower, Rest, p_advance_index(S, Index)}
catch
error:{badmatch,_} -> {fail, {expected, {string, S}, Index}}
end
end.
-endif.

-ifdef(p_anything).
-spec p_anything() -> parse_fun().
p_anything() ->
Expand Down
3 changes: 2 additions & 1 deletion test/neotoma_peg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-define(p_label,true).
-define(p_scan,true).
-define(p_string,true).
-define(p_case_insensitive,true).
-define(p_anything,true).
-define(p_charclass,true).
-define(p_regexp,true).
Expand All @@ -25,6 +26,6 @@
-export([p/5]).
-export([setup_memo/0, release_memo/0]).

-export([p_eof/0, p_optional/1, p_not/1, p_assert/1, p_seq/1, p_choose/1, p_zero_or_more/1, p_one_or_more/1, p_label/2, p_string/1, p_anything/0, p_charclass/1, p_regexp/1, line/1, column/1]).
-export([p_eof/0, p_optional/1, p_not/1, p_assert/1, p_seq/1, p_choose/1, p_zero_or_more/1, p_one_or_more/1, p_label/2, p_string/1, p_case_insensitive/1, p_anything/0, p_charclass/1, p_regexp/1, line/1, column/1]).

-include("priv/peg_includes.hrl").
6 changes: 6 additions & 0 deletions test/test_combinators.erl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ string_test_() ->
?_assertEqual({fail,{expected, {string, <<"abc">>}, ?STARTINDEX}}, (neotoma_peg:p_string(<<"abc">>))(<<"defabc">>,?STARTINDEX))
].

case_insensitive_test_() ->
[
?_assertEqual({<<"abc">>, <<"def">>, {{line,1}, {column, 4}}}, (neotoma_peg:p_case_insensitive(<<"abc">>))(<<"aBCdef">>,?STARTINDEX)),
?_assertEqual({fail,{expected, {string, <<"abc">>}, ?STARTINDEX}}, (neotoma_peg:p_case_insensitive(<<"abc">>))(<<"defabc">>,?STARTINDEX))
].

anything_test_() ->
[
?_assertEqual({<<"a">>,<<"bcde">>,{{line,1},{column,2}}}, (neotoma_peg:p_anything())(<<"abcde">>,?STARTINDEX)),
Expand Down
15 changes: 15 additions & 0 deletions test/test_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ parser_test() ->
_:_ -> ?assert(false)
end.

case_insensitive_test() ->
% so we don't have to copy test.peg to .eunit
Data = "rule <- \"caSe\"i;",
file:write_file("test_parser.peg", io_lib:fwrite("~s\n", [Data])),
neotoma:file("test_parser.peg"),
compile:file("test_parser.erl", []),
try
TestString = "caSE",
Result = test_parser:parse(TestString),
?assertEqual(4, length(Result)),
StringResult = lists:flatten(io_lib:format("~ts", [Result])),
?assertEqual(TestString, StringResult)
catch
_:_ -> ?assert(false)
end.