Skip to content

Commit

Permalink
Merge pull request #2 from Kyorai/gh-1-line-with-whitespace-only
Browse files Browse the repository at this point in the history
Ignore whitespace-only lines
  • Loading branch information
michaelklishin authored Aug 3, 2019
2 parents 6948112 + 2caf452 commit fff866a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
22 changes: 20 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 All @@ -91,11 +92,28 @@ file_test() ->
ok.

utf8_test() ->
Conf = conf_parse:parse("setting = thingŒ\n"),
Conf = conf_parse:parse("setting = thing" ++ [338] ++ "\n"),
?assertEqual([{["setting"],
{error, {conf_to_latin1, 1}}
}], Conf),
ok.

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 @@ -176,7 +194,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
20 changes: 19 additions & 1 deletion 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 @@ -161,5 +162,22 @@ utf8_test() ->
{error, {conf_to_latin1, 1}}
}], Conf),
ok.

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),
?assertMatch({errorlist,[{error, {file_open, {"test/nonent.conf", _}}}]}, Conf),
ok.

files_incomplete_parse_test() ->
Expand Down

0 comments on commit fff866a

Please sign in to comment.