Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Python-style empty last array item; improved error-handling; fixes; typos #4

Merged
merged 4 commits into from

2 participants

Uwe Dauernheim Carlos González Florido
Uwe Dauernheim

A bunch of smaller tweaks. Haven't written testcases yet, will do later. And maybe the test should be extended to run against BurntSushi's test set.

Carlos González Florido
Owner

Thanks, I'll have a look as soon as I can

Carlos González Florido
Owner

Thanks.
Most of the changes are ok.
However, the case "a=[]" doesn't work correctly. Also, I think the "Fix to apply list string instead of binary" is not needed, as that part wasn't needed in my original code. I have reworked it a bit and the final parse_array/3 would be:

parse_array(Rest, Line, Acc) ->
    case parse_value(Rest, Line) of
        % Empty array
        {undefined, {[$]|Rest1], Line1}} ->
            {lists:reverse(Acc), parse_space(Rest1, Line1)};
        {undefined, _} ->
            throw({invalid_array, Line});
        {Value, {[$]|Rest1], Line1}} ->
            {lists:reverse([Value|Acc]), parse_space(Rest1, Line1)};
        {Value, {[$,|Rest1], Line1}} ->
            case parse_space(Rest1, Line1) of
                {[$]|Rest2], Line2} -> %% "Python-style" empty last array item
                    {lists:reverse([Value|Acc]), parse_space(Rest2, Line2)};
                {Rest2, Line2} ->
                    parse_array(Rest2, Line2, [Value|Acc])
            end;
        _ ->
            throw({invalid_array, Line})
    end.

I will add these changes to a djui-master branch to test it a bit more.

Carlos González Florido kalta merged commit f601a94 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 12 deletions.
  1. +4 −2 .gitignore
  2. +20 −10 src/etoml.erl
6 .gitignore
View
@@ -1,2 +1,4 @@
-*.beam
-.eunit
+/.eunit
+/.test
+/ebin/*.beam
+/ebin/*.app
30 src/etoml.erl
View
@@ -51,7 +51,7 @@
when Error :: {invalid_key, integer()} | {invalid_group, integer()} |
{invalid_date, integer()} | {invalid_number, integer()} |
{invalid_array, integer()} | {invalid_string, integer()} |
- {duplicated_key, binary()}.
+ {undefined_value, integer()} | {duplicated_key, binary()}.
parse(Msg) ->
try
@@ -68,7 +68,8 @@ parse(Msg) ->
[{Keys::[binary()], Value::element()}] | {error, Error}
when Error :: {invalid_key, integer()} | {invalid_group, integer()} |
{invalid_date, integer()} | {invalid_number, integer()} |
- {invalid_array, integer()} | {invalid_string, integer()}.
+ {invalid_array, integer()} | {invalid_string, integer()} |
+ {undefined_value, integer()} | {duplicated_key, binary()}.
parse2(Msg) when is_binary(Msg) ->
parse2(binary_to_list(Msg));
@@ -121,8 +122,12 @@ parse_key(Rest, Line) ->
case parse_text(Rest, Line, []) of
{Key, {[$=|Rest1], Line1}} ->
{Rest2, Line2} = parse_space(Rest1, Line1),
- {Value, {Rest3, Line3}} = parse_value(Rest2, Line2),
- {{list_to_binary(Key), Value}, {Rest3, Line3}};
+ case parse_value(Rest2, Line2) of
+ {undefined, {_Rest3, Line3}} ->
+ throw({undefined_value, Line3});
+ {Value, {Rest3, Line3}} ->
+ {{list_to_binary(Key), Value}, {Rest3, Line3}}
+ end;
_ ->
throw({invalid_key, Line})
end.
@@ -138,6 +143,7 @@ parse_value(Rest, Line) ->
{"true", Pos} -> {true, Pos};
{"false", Pos} -> {false, Pos};
{[_,_,_,_,$-|_]=Date, Pos} -> {parse_date(Date, Line), Pos};
+ {"", Pos} -> {undefined, Pos};
{Number, Pos} -> {parse_number(Number, Line), Pos}
end.
@@ -164,15 +170,19 @@ parse_string([], Line, _) ->
%% @private
parse_array(Rest, Line, Acc) ->
case parse_value(Rest, Line) of
- {<<>>, {[$[|Rest1], Line1}} ->
+ {"", {[$[|Rest1], Line1}} ->
{Value, {Rest2, Line2}} = parse_array(Rest1, Line1, []),
parse_array(Rest2, Line2, [Value|Acc]);
- {<<>>, {[$,|_], _}} ->
+ {"", {[$,|_], _}} ->
throw({invalid_array, Line});
- {Value, {[$,|Rest1], Line1}} ->
- {Rest2, Line2} = parse_space(Rest1, Line1),
- parse_array(Rest2, Line2, [Value|Acc]);
- {<<>>, {[$]|Rest1], Line1}} ->
+ {Value, {[$,|Rest1], Line1}} when Value =/= undefined ->
+ case parse_space(Rest1, Line1) of
+ {[$]|Rest2], Line2} -> %% "Python-style" empty last array item
+ {lists:reverse([Value|Acc]), parse_space(Rest2, Line2)};
+ {Rest2, Line2} ->
+ parse_array(Rest2, Line2, [Value|Acc])
+ end;
+ {"", {[$]|Rest1], Line1}} ->
{lists:reverse(Acc), parse_space(Rest1, Line1)};
{Value, {[$]|Rest1], Line1}} ->
{lists:reverse([Value|Acc]), parse_space(Rest1, Line1)};
Something went wrong with that request. Please try again.