diff --git a/.gitignore b/.gitignore index 47e402b..3402482 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,6 @@ erl_crash.dump *.beam -src/props_path_parser.erl - deps ebin logs diff --git a/rebar.config b/rebar.config index bbb96d6..d94ed9a 100644 --- a/rebar.config +++ b/rebar.config @@ -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"}}]}. diff --git a/src/props.erl b/src/props.erl index 69d7cdf..3b71bf0 100644 --- a/src/props.erl +++ b/src/props.erl @@ -476,3 +476,4 @@ match(Props, {[{MKey, MVal} | MProps]}) -> _ -> false end. + diff --git a/src/props_path_parser.peg b/src/props_path_parser.erl similarity index 58% rename from src/props_path_parser.peg rename to src/props_path_parser.erl index 8ab4840..61c02b7 100644 --- a/src/props_path_parser.peg +++ b/src/props_path_parser.erl @@ -1,5 +1,4 @@ -%% Property path DSL -%% props_path_parser.peg +%% props_path_parser.erl %% %% Copyright 2011-2012 Grey Area %% @@ -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).