Skip to content

Commit

Permalink
Merge pull request #70 from inaka/jfacorro.69.include_token_in_parse_…
Browse files Browse the repository at this point in the history
…tree

[#69] tokens as an attribute of root node.
  • Loading branch information
Brujo Benavides committed Aug 27, 2015
2 parents 7a54972 + 1b37140 commit 266dfef
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/ktn_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,17 @@ parse_tree(IncludeDirs, Source) ->
Children = [to_map(Parsed) || {ok, Parsed} <- ParsedForms],

#{type => root,
attrs => #{},
attrs => #{tokens => lists:map(fun token_to_map/1, Tokens)},
content => to_map(Comments) ++ Children}.

token_to_map({Type, Attrs}) ->
#{type => Type,
attrs => #{text => get_text(Attrs),
locations => get_location(Attrs)}};
token_to_map({Type, Attrs, Value}) ->
Map = token_to_map({Type, Attrs}),
Map#{value => Value}.

%% @doc Evaluates the erlang expression in the string provided.
-spec eval(string() | binary()) -> term().
eval(Source) ->
Expand Down Expand Up @@ -204,7 +212,12 @@ get_location(Attrs) when is_integer(Attrs) ->
get_location(Attrs) when is_list(Attrs) ->
Line = proplists:get_value(line, Attrs),
Column = proplists:get_value(column, Attrs),
{Line, Column};
case {Line, Column} of
{undefined, undefined} ->
proplists:get_value(location, Attrs, {-1, -1});
_ ->
{Line, Column}
end;
get_location(_Attrs) ->
{-1, -1}.

Expand Down

0 comments on commit 266dfef

Please sign in to comment.