Skip to content
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
erl_crash.dump
*.beam

src/props_path_parser.erl

deps
ebin
logs
Expand Down
4 changes: 1 addition & 3 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
{stylesheet, ""}]}.

{deps, [{edown, ".*",
{git, "git://github.com/esl/edown.git", "master"}},
{neotoma, "1.5",
{git, "git://github.com/seancribbs/neotoma.git", "master"}}]}.
{git, "git://github.com/esl/edown.git", "master"}}]}.
1 change: 1 addition & 0 deletions src/props.erl
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,4 @@ match(Props, {[{MKey, MVal} | MProps]}) ->
_ ->
false
end.

38 changes: 17 additions & 21 deletions src/props_path_parser.peg → src/props_path_parser.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
%% Property path DSL
%% props_path_parser.peg
%% props_path_parser.erl
%%
%% Copyright 2011-2012 Grey Area
%%
Expand Down Expand Up @@ -28,24 +27,21 @@
%% will work the same.
%%
%% Indexes are 1-based, just like Erlang's lists module.
%%
%% Inspired by:
%% http://www.progski.net/blog/2010/destructuring_json_in_erlang_made_easy.html

path <- var ('[' int ']')? ('.' path)? `
case Node of
[Var, [], []] ->
[{prop, Var}];
[Var, [_, I, _], []] ->
[{prop, Var}, {index, I}];
[Var, [], [_, Rest]] ->
[{prop, Var}] ++ Rest;
[Var, [_, I, _], [_, Rest]] ->
[{prop, Var}, {index, I}] ++ Rest
end`;

int <- [0-9]+ `
list_to_integer(binary_to_list(list_to_binary(Node)))`;
-module(props_path_parser).
-export([parse/1]).

var <- [-_a-zA-Z0-9] [-:\s_a-zA-Z0-9]* `
list_to_binary(Node)`;
-spec parse(binary() | list()) -> [{prop, binary()} | {index, integer()}].
parse(Path) when is_binary(Path) -> parse(binary_to_list(Path));
parse(Path) ->
Tokens = string:tokens(Path, "."),
Parsed = lists:map(fun(Token) ->
case string:tokens(Token, "[]") of
[P, I] ->
{Index, _} = string:to_integer(I),
[{prop, list_to_binary(P)}, {index, Index}];
[P] ->
[{prop, list_to_binary(P)}]
end
end, Tokens),
lists:flatten(Parsed).