summaryrefslogtreecommitdiff
path: root/src/rebar_string.erl
diff options
context:
space:
mode:
authorFred Hebert <mononcqc@ferd.ca>2019-03-29 11:49:14 -0400
committerGitHub <noreply@github.com>2019-03-29 11:49:14 -0400
commite42eecc93203e37f96db60b628356f718f9eb1f6 (patch)
tree884b8abe552bbe09f76148aa95e9d0431f22a8d7 /src/rebar_string.erl
parentcc788f1ff3c2c845cda19f27291309effb94511a (diff)
parent86d261dbf1653a7cc074f7749a19b211e53cca0a (diff)
Merge pull request #2036 from ferd/shell-support-cfg-src
Attempt at support sys_config_src in shell
Diffstat (limited to 'src/rebar_string.erl')
-rw-r--r--src/rebar_string.erl29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/rebar_string.erl b/src/rebar_string.erl
index d03b14e..79867f5 100644
--- a/src/rebar_string.erl
+++ b/src/rebar_string.erl
@@ -1,7 +1,12 @@
%%% @doc Compatibility module for string functionality
%%% for pre- and post-unicode support.
+%%%
+%%% Also contains other useful string functionality.
-module(rebar_string).
+%% Compatibility exports
-export([join/2, split/2, lexemes/2, trim/3, uppercase/1, lowercase/1, chr/2]).
+%% Util exports
+-export([consult/1]).
-ifdef(unicode_str).
@@ -42,3 +47,27 @@ uppercase(Str) -> string:to_upper(Str).
lowercase(Str) -> string:to_lower(Str).
chr(Str, Char) -> string:chr(Str, Char).
-endif.
+
+%% @doc
+%% Given a string or binary, parse it into a list of terms, ala file:consult/1
+-spec consult(unicode:chardata()) -> {error, term()} | [term()].
+consult(Str) ->
+ consult([], unicode:characters_to_list(Str), []).
+
+consult(Cont, Str, Acc) ->
+ case erl_scan:tokens(Cont, Str, 0) of
+ {done, Result, Remaining} ->
+ case Result of
+ {ok, Tokens, _} ->
+ case erl_parse:parse_term(Tokens) of
+ {ok, Term} -> consult([], Remaining, [Term | Acc]);
+ {error, Reason} -> {error, Reason}
+ end;
+ {eof, _Other} ->
+ lists:reverse(Acc);
+ {error, Info, _} ->
+ {error, Info}
+ end;
+ {more, Cont1} ->
+ consult(Cont1, eof, Acc)
+ end.