Skip to content
Open
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
76 changes: 54 additions & 22 deletions native/src/driver_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
-behaviour(application).

%% Application callbacks
-export([start/2, stop/1,loop/0,parseExpr/1,tokenize/1,parse/1,format/1]).
-export([start/2, stop/1,loop/0,splitByDots/1]).
-include_lib("eunit/include/eunit.hrl").



start(_StartType, _StartArgs) ->
try
%If you are modifying the code comment the next line to show the exceptions
%error_logger:tty(false),
error_logger:tty(false),
loop()
catch
throw:{eofErr,_}->
Expand Down Expand Up @@ -60,15 +60,14 @@ process() ->
{error,BadJSON} ->
throw(BadJSON)
end,
ExprList = tokenize(Content),
{ok,ParseList} = parse(ExprList),
TokExprList = tokenize(binary_to_list(Content)),
{ok,ParseList} = parse(TokExprList),
FormatParse = format(ParseList),
JSON = jsx:encode([{<<"status">>,<<"ok">>},{<<"ast">>,FormatParse}]),
io:format("~s~n",[binary_to_list(JSON)])
end.

decode(InputSrt) ->

SubS = string:substr(InputSrt,1,string:len(InputSrt)-1),
case jsx:is_json(list_to_binary(SubS)) of
true->Data= jsx:decode(list_to_binary(SubS)),
Expand All @@ -79,27 +78,22 @@ decode(InputSrt) ->
end;
false->
{error,{json,<<"Input is not a valid JSON">>}}
end.
end.

tokenize(Content) ->
Formated= string:join(lists:map(fun erlang:binary_to_list/1,[Content]),""),
string:tokens(Formated,".").
TokExprList = case erl_scan:string(Content) of
{ok,Tokens,_} ->
splitByDots(Tokens);
{error,BadScan,_}-> throw({scan,BadScan})
end,
TokExprList.

parse(ExprList) ->
List = lists:foldl(fun (Expr,ParseList)->
case string:equal("\n",Expr) of
true ->
lists:append(ParseList,"");
false ->
ExprDot = string:concat(Expr,"."),
case erl_scan:string(ExprDot) of
{ok,Tokens,_} ->
case parseExpr(Tokens) of
{ok,AST} -> lists:append(ParseList,AST);
{error,BadParse} -> throw({parse,BadParse})
end;
{error,BadScan,_}-> throw({scan,BadScan})
end
end
case parseExpr(Expr) of
{ok,AST} -> lists:append(ParseList,AST);
{error,BadParse} -> throw({parse,BadParse})
end
end,[],ExprList),
{ok,List}.

Expand All @@ -113,6 +107,26 @@ parseExpr(Tokens) ->
end
end.

splitByDots(TokenList)->
splitByDots(TokenList,[],[]).

splitByDots([H|T],Acc,Final)->
if
element(1,H) == dot ->
SubList = [lists:append(Acc,[H])],
splitByDots(T,[],lists:append(Final,SubList));
true ->
splitByDots(T,lists:append(Acc,[H]),Final)
end;
splitByDots([],Acc,Final)->
if
length(Acc) == 0 ->
Res = Final;
true ->
Res =lists:append(Final,[Acc])
end,
Res.

%% Format do a conversion of erlang tuples to list, also change strings to binaries
format(T) when is_list(T)->
format(list_to_tuple(T));
Expand Down Expand Up @@ -174,3 +188,21 @@ format_test_()->
?_assert(format({"hello","world"})=:= [<<"hello">>,<<"world">>]),
?_assert(format({a,b,[c,"hello",{"world",["this","is","a","test"]}]})=:= [a,b,[c,<<"hello">>,[<<"world">>,[<<"this">>,<<"is">>,<<"a">>,<<"test">>]]]]),
?_assert(format({"hello",<<"world">>})=:= [<<"hello">>,<<"world">>])].

splitByDots_test_()->
String1 = "ok . ok . ok . ok",
String2 = ". . . . ok",
String3 = "ok",
String4 = ".",
{_,Tokens1,_} = erl_scan:string(String1),
{_,Tokens2,_} = erl_scan:string(String2),
{_,Tokens3,_} = erl_scan:string(String3),
{_,Tokens4,_} = erl_scan:string(String4),
Res1 = length(splitByDots(Tokens1)),
Res2 = length(splitByDots(Tokens2)),
Res3 = length(splitByDots(Tokens3)),
Res4 = length(splitByDots(Tokens4)),
[?_assertEqual(4,Res1),
?_assertEqual(5,Res2),
?_assertEqual(1,Res3),
?_assertEqual(1,Res4)].