Skip to content

Commit

Permalink
Merge pull request #15 from synlay/feature/otp_18_tweaks
Browse files Browse the repository at this point in the history
Fix location extraction from scanner annotations for OTP >= 18.0
  • Loading branch information
danikp committed Aug 27, 2015
2 parents d640562 + 7935882 commit ee1056f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ebin
aleppo_parser.erl
/.settings/
/.project
.rebar
1 change: 0 additions & 1 deletion Emakefile

This file was deleted.

34 changes: 14 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
ERL=erl
ERLC=erlc

PARSER=src/aleppo_parser

all: $(PARSER).erl
-mkdir -p ebin
$(ERL) -make
cp src/aleppo.app.src ebin/aleppo.app

$(PARSER).erl: $(PARSER).yrl
$(ERLC) -o src/ src/aleppo_parser.yrl

run:
$(ERL) -pa ebin
REBAR=./rebar

## dialyzer
PLT_FILE = ~/aleppo.plt
PLT_APPS ?= kernel stdlib erts
DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions -Wunmatched_returns \
-Wunderspecs --verbose --fullpath -n

.PHONY: dialyze
all: compile

compile:
@$(REBAR) compile

run:
@$(REBAR) shell

dialyze: all
@[ -f $(PLT_FILE) ] || $(MAKE) plt
@dialyzer --plt $(PLT_FILE) $(DIALYZER_OPTS) ebin || [ $$? -eq 2 ];

## In case you are missing a plt file for dialyzer,
## you can run/adapt this command
.PHONY: plt
plt:
@echo "Building PLT, may take a few minutes"
@dialyzer --build_plt --output_plt $(PLT_FILE) --apps \
$(PLT_APPS) || [ $$? -eq 2 ];

clean:
rm -fv ebin/*.beam
rm -fv erl_crash.dump $(PARSER).erl
rm -f $(PLT_FILE)
@rm -fv erl_crash.dump
@rm -f $(PLT_FILE)
@$(REBAR) clean

.PHONY: all compile run dialyze plt clean
Binary file added rebar
Binary file not shown.
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{erl_opts, [ {platform_define, "^R|17", pre18} ]}.
45 changes: 41 additions & 4 deletions src/aleppo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ process_tree(ParseTree, Options) ->

try process_tree(ParseTree, TokenAcc, Context) of
{MacroDict, RevTokens} when is_list(RevTokens) ->
FinalTokens = lists:reverse(RevTokens),
FinalTokens = reverse_and_normalize_token_locations(RevTokens),
case proplists:get_value(return_macros, Options, false) of
true -> {ok, FinalTokens, MacroDict};
_ -> {ok, FinalTokens}
Expand Down Expand Up @@ -347,10 +347,10 @@ stringify_tokens(TokenList) ->
stringify_tokens1([], Acc) ->
lists:concat(lists:reverse(Acc));
stringify_tokens1([Token|Rest], []) ->
{symbol, Symbol} = erl_scan:token_info(Token, symbol),
Symbol = get_symbol(Token),
stringify_tokens1(Rest, [Symbol]);
stringify_tokens1([Token|Rest], Acc) ->
{symbol, Symbol} = erl_scan:token_info(Token, symbol),
Symbol = get_symbol(Token),
stringify_tokens1(Rest, [Symbol, " "|Acc]).

insert_comma_tokens(Args, Loc) ->
Expand Down Expand Up @@ -411,7 +411,44 @@ mark_keywords([Other|Rest], Mod, Acc) ->

location(Location = {_Line, _Column}) ->
Location;
location(Attrs) when is_list(Attrs) ->
location(Attrs) ->
location_helper(Attrs).

-ifdef(pre18).
location_helper(Attrs) ->
legacy_location(Attrs).

get_symbol(Token) ->
{symbol, Symbol} = erl_scan:token_info(Token, symbol),
Symbol.
-else.
location_helper(Attrs) ->
case erl_anno:is_anno(Attrs) of
true ->
erl_anno:location(Attrs);
false ->
legacy_location(Attrs)
end.

get_symbol(Token) ->
erl_scan:symbol(Token).
-endif.

legacy_location(Attrs) when is_list(Attrs) ->
Line = proplists:get_value(line, Attrs),
Column = proplists:get_value(column, Attrs),
{Line, Column}.

reverse_and_normalize_token_locations(RevTokens) ->
reverse_and_normalize_token_locations_helper(RevTokens, []).

reverse_and_normalize_token_locations_helper([], Acc) ->
Acc;
reverse_and_normalize_token_locations_helper([{Type, MaybeLocation} | Rest], Acc) when is_tuple(MaybeLocation) orelse
is_list(MaybeLocation) ->
reverse_and_normalize_token_locations_helper(Rest, [{Type, location(MaybeLocation)}|Acc]);
reverse_and_normalize_token_locations_helper([{Type, MaybeLocation, Extra} | Rest], Acc) when is_tuple(MaybeLocation) orelse
is_list(MaybeLocation) ->
reverse_and_normalize_token_locations_helper(Rest, [{Type, location(MaybeLocation), Extra}|Acc]);
reverse_and_normalize_token_locations_helper([Other | Rest], Acc) ->
reverse_and_normalize_token_locations_helper(Rest, [Other | Acc]).

0 comments on commit ee1056f

Please sign in to comment.