From 1b37140b1de69c549e08b19e4bcf670b57c4f8fd Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Thu, 27 Aug 2015 14:36:06 -0300 Subject: [PATCH] [#69] tokens as an attribute of root node. --- src/ktn_code.erl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ktn_code.erl b/src/ktn_code.erl index 7c59cf3..e41b4ed 100644 --- a/src/ktn_code.erl +++ b/src/ktn_code.erl @@ -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) -> @@ -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}.