Skip to content

Commit

Permalink
Fix PEG grammar to consider whitespace as one or more whitespace char…
Browse files Browse the repository at this point in the history
…acters, fix test in cuttlefish_conf
  • Loading branch information
lukebakken committed Aug 3, 2019
1 parent ef90390 commit 143dcad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/conf_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

%% @doc Only let through lines that are not comments or whitespace.
is_setting(ws) -> false;
is_setting([ws]) -> false;
is_setting(comment) -> false;
is_setting(_) -> true.

Expand Down Expand Up @@ -97,14 +98,22 @@ utf8_test() ->
}], Conf),
ok.

gh_1_only_whitespace_test() ->
gh_1_two_tab_test() ->
Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"),
?assertEqual([
{["setting0"],"thing0"},
{["setting1"],"thing1"}
], Conf),
ok.

gh_1_three_tab_test() ->
Conf = conf_parse:parse("setting0 = thing0\n\t\t\t\nsetting1 = thing1\n"),
?assertEqual([
{["setting0"],"thing0"},
{["setting1"],"thing1"}
], Conf),
ok.

-endif.

-spec file(file:name()) -> any().
Expand Down Expand Up @@ -180,7 +189,7 @@ parse(Input) when is_binary(Input) ->

-spec 'ws'(input(), index()) -> parse_result().
'ws'(Input, Index) ->
p(Input, Index, 'ws', fun(I,D) -> (p_charclass(<<"[\s\t]">>))(I,D) end, fun(_Node, _Idx) ->ws end).
p(Input, Index, 'ws', fun(I,D) -> (p_one_or_more(p_charclass(<<"[\s\t]">>)))(I,D) end, fun(_Node, _Idx) ->ws end).



Expand Down
13 changes: 11 additions & 2 deletions src/conf_parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ crlf <- "\r"? "\n" `ws`;
eof <- !. `ws`;

%% Whitespace is either spaces or tabs.
ws <- [ \t] `ws`;
ws <- [ \t]+ `ws`;

% Erlang code
%{
Expand Down Expand Up @@ -131,6 +131,7 @@ ws <- [ \t] `ws`;

%% @doc Only let through lines that are not comments or whitespace.
is_setting(ws) -> false;
is_setting([ws]) -> false;
is_setting(comment) -> false;
is_setting(_) -> true.

Expand Down Expand Up @@ -162,13 +163,21 @@ utf8_test() ->
}], Conf),
ok.

gh_1_only_whitespace_test() ->
gh_1_two_tab_test() ->
Conf = conf_parse:parse("setting0 = thing0\n\t\t\nsetting1 = thing1\n"),
?assertEqual([
{["setting0"],"thing0"},
{["setting1"],"thing1"}
], Conf),
ok.

gh_1_three_tab_test() ->
Conf = conf_parse:parse("setting0 = thing0\n\t\t\t\nsetting1 = thing1\n"),
?assertEqual([
{["setting0"],"thing0"},
{["setting1"],"thing1"}
], Conf),
ok.

-endif.
%}
2 changes: 1 addition & 1 deletion src/cuttlefish_conf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ duplicates_multi_test() ->

files_one_nonent_test() ->
Conf = files(["test/multi1.conf", "test/nonent.conf"]),
?assertEqual({errorlist,[{error, {file_open, {"test/nonent.conf", undefined}}}]}, Conf),
?assertEqual({errorlist,[{error, {file_open, {"test/nonent.conf", enoent}}}]}, Conf),
ok.

files_incomplete_parse_test() ->
Expand Down

0 comments on commit 143dcad

Please sign in to comment.