Skip to content

Commit

Permalink
[#30] Added node_attrs key with a map to all nodes that have attrib…
Browse files Browse the repository at this point in the history
…utes which are actually nodes.
  • Loading branch information
jfacorro committed Mar 19, 2015
1 parent 3b9d064 commit ab95e41
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/ktn_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-export([
type/1,
attr/2,
meta/2,
node_attr/2,
content/1
]).

Expand Down Expand Up @@ -163,15 +163,15 @@ attr(_Key, Node) when is_map(Node) ->
attr(_Key, undefined) ->
undefined.

-spec meta(term(), tree_node()) -> term() | undefined.
meta(Key, #{meta := Attrs}) ->
case maps:is_key(Key, Attrs) of
true -> maps:get(Key, Attrs);
false -> undefined
end;
meta(_Key, Node) when is_map(Node) ->
undefined;
meta(_Key, undefined) ->
-spec node_attr(term(), tree_node()) -> term() | undefined.
node_attr(Key, #{node_attrs := Attrs}) ->
case maps:is_key(Key, Attrs) of
true -> maps:get(Key, Attrs);
false -> undefined
end;
node_attr(_Key, Node) when is_map(Node) ->
undefined;
node_attr(_Key, undefined) ->
undefined.

-spec content(tree_node()) -> [tree_node()].
Expand Down Expand Up @@ -244,7 +244,7 @@ to_map({clause, Attrs, Patterns, Guards, Body}) ->
#{type => clause,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{pattern => to_map(Patterns),
node_attrs => #{pattern => to_map(Patterns),
guards => to_map(Guards)},
content => to_map(Body)};

Expand Down Expand Up @@ -284,11 +284,11 @@ to_map({bin_element, Attrs, Value, Size, TSL}) ->
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
type_spec_list => TSL},
meta => #{value => to_map(Value),
size => case Size of
default -> #{type => default};
_ -> to_map(Size)
end }};
node_attrs => #{value => to_map(Value),
size => case Size of
default -> #{type => default};
_ -> to_map(Size)
end }};

%% Variables

Expand All @@ -304,15 +304,15 @@ to_map({call, Attrs, Function, Arguments}) ->
#{type => call,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{function => to_map(Function)},
node_attrs => #{function => to_map(Function)},
content => to_map(Arguments)};

to_map({remote, Attrs, Module, Function}) ->
#{type => remote,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{module => to_map(Module),
function => to_map(Function)}};
node_attrs => #{module => to_map(Module),
function => to_map(Function)}};

%% case

Expand All @@ -322,7 +322,7 @@ to_map({'case', Attrs, Expr, Clauses}) ->
#{type => 'case',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{expression => to_map(Expr)},
node_attrs => #{expression => to_map(Expr)},
content => [CaseExpr, CaseClauses]};
to_map({case_expr, Attrs, Expr}) ->
#{type => case_expr,
Expand All @@ -338,11 +338,11 @@ to_map({case_clauses, Attrs, Clauses}) ->
%% fun

to_map({'fun', Attrs, {function, Name, Arity}}) ->
#{type => 'fun',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => Name,
arity => Arity}};
#{type => 'fun',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => Name,
arity => Arity}};

to_map({'fun', Attrs, {function, Module, Name, Arity}}) ->
#{type => 'fun',
Expand Down Expand Up @@ -383,8 +383,8 @@ to_map({'try', Attrs, Body, [], CatchClauses, AfterBody}) ->
#{type => 'try',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{catch_clauses => to_map(CatchClauses),
after_body => to_map(AfterBody)},
node_attrs => #{catch_clauses => to_map(CatchClauses),
after_body => to_map(AfterBody)},
content => TryBody ++ [TryCatch, TryAfter]};

%% try..of..catch..after
Expand All @@ -403,7 +403,7 @@ to_map({try_case, Attrs, Expr, Clauses}) ->
#{type => try_case,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{expression => to_map(Expr)},
node_attrs => #{expression => to_map(Expr)},
content => to_map(Clauses)};

to_map({try_catch, Attrs, Clauses}) ->
Expand Down Expand Up @@ -461,7 +461,7 @@ to_map({receive_after, Attrs, Expr, Body}) ->
#{type => receive_after,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{expression => to_map(Expr)},
node_attrs => #{expression => to_map(Expr)},
content => to_map(Body)};

%% List
Expand All @@ -488,7 +488,7 @@ to_map({map, Attrs, Var, Pairs}) ->
#{type => map,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{var => to_map(Var)},
node_attrs => #{var => to_map(Var)},
content => to_map(Pairs)};

to_map({Type, Attrs, Key, Value}) when
Expand All @@ -497,8 +497,8 @@ to_map({Type, Attrs, Key, Value}) when
#{type => Type,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{key => to_map(Key),
value => to_map(Value)}};
node_attrs => #{key => to_map(Key),
value => to_map(Value)}};

%% List Comprehension

Expand All @@ -514,8 +514,8 @@ to_map({generate, Attrs, Pattern, Expr}) ->
#{type => generate,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{pattern => to_map(Pattern),
expression => to_map(Expr)}};
node_attrs => #{pattern => to_map(Pattern),
expression => to_map(Expr)}};
to_map({lc_expr, Attrs, Expr}) ->
#{type => lc_expr,
attrs => #{location => get_location(Attrs),
Expand All @@ -535,8 +535,8 @@ to_map({b_generate, Attrs, Pattern, Expr}) ->
#{type => b_generate,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{pattern => to_map(Pattern),
expression => to_map(Expr)}};
node_attrs => #{pattern => to_map(Pattern),
expression => to_map(Expr)}};
to_map({bc_expr, Attrs, Expr}) ->
#{type => bc_expr,
attrs => #{location => get_location(Attrs),
Expand Down Expand Up @@ -572,7 +572,7 @@ to_map({record, Attrs, Var, Name, Fields}) ->
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => Name},
meta => #{variable => to_map(Var)},
node_attrs => #{variable => to_map(Var)},
content => to_map(Fields)};

to_map({record_index, Attrs, Name, Field}) ->
Expand All @@ -586,19 +586,19 @@ to_map({record_field, Attrs, Name}) ->
#{type => record_field,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{name => to_map(Name)}};
node_attrs => #{name => to_map(Name)}};
to_map({record_field, Attrs, Name, Default}) ->
#{type => record_field,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{default => to_map(Default),
name => to_map(Name)}};
node_attrs => #{default => to_map(Default),
name => to_map(Name)}};
to_map({record_field, Attrs, Var, Name, Field}) ->
#{type => record_field,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => Name},
meta => #{variable => to_map(Var)},
node_attrs => #{variable => to_map(Var)},
content => [to_map(Field)]};

%% Block
Expand All @@ -623,7 +623,7 @@ to_map({typed_record_field, Field, Type}) ->
attrs => #{location => attr(location, FieldMap),
text => attr(text, FieldMap),
field => FieldMap},
meta => #{type => to_map(Type)}};
node_attrs => #{type => to_map(Type)}};

%% Type

Expand All @@ -645,7 +645,7 @@ to_map({type, Attrs, bounded_fun, [FunType, Defs]}) ->
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => bounded_fun},
meta => #{'fun' => to_map(FunType)},
node_attrs => #{'fun' => to_map(FunType)},
content => to_map(Defs)};
to_map({type, Attrs, Name, any}) ->
to_map({type, Attrs, Name, [any]});
Expand All @@ -668,26 +668,26 @@ to_map({type, Attrs, map_field_assoc, Name, Type}) ->
#{type => type_map_field,
attrs => #{location => Location,
text => Text},
meta => #{key => to_map(Name),
type => to_map(Type)}};
node_attrs => #{key => to_map(Name),
type => to_map(Type)}};
to_map({remote_type, Attrs, [Module, Function, Args]}) ->
#{type => remote_type,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{module => to_map(Module),
function => to_map(Function),
args => to_map(Args)}};
node_attrs => #{module => to_map(Module),
function => to_map(Function),
args => to_map(Args)}};
to_map({ann_type, Attrs, [Var, Type]}) ->
#{type => record_field,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{var => to_map(Var),
type => to_map(Type)}};
node_attrs => #{var => to_map(Var),
type => to_map(Type)}};
to_map({paren_type, Attrs, [Type]}) ->
#{type => record_field,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
meta => #{type => to_map(Type)}};
node_attrs => #{type => to_map(Type)}};
to_map(any) -> %% any()
#{type => any};

Expand All @@ -698,8 +698,8 @@ to_map({attribute, Attrs, type, {Name, Type, Args}}) ->
attrs => #{location => get_location(Attrs),
text => get_text(Attrs),
name => Name},
meta => #{args => to_map(Args),
type => to_map(Type)}};
node_attrs => #{args => to_map(Args),
type => to_map(Type)}};
to_map({attribute, Attrs, spec, {{Name, Arity}, Types}}) ->
#{type => spec,
attrs => #{location => get_location(Attrs),
Expand Down

0 comments on commit ab95e41

Please sign in to comment.