-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.erl
100 lines (85 loc) · 2.2 KB
/
checker.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%% ---
%% Excerpted from "Programming Erlang",
%% published by The Pragmatic Bookshelf.
%% Copyrights apply to this code. It may not be used to create training material,
%% courses, books, articles, and the like. Contact us if you are in doubt.
%% We make no guarantees that this code is fit for any purpose.
%% Visit http://www.pragmaticprogrammer.com/titles/jaerlang for more book information.
%%---
-module(checker).
-compile(export_all).
-import(lists, [all/2, foreach/2, member/2]).
%% checker:start()
%% checks consistency of names
start() ->
F = lib_find:files(".", "*.erl", true),
foreach(fun(I) -> check(I) end, F).
test() ->
check("./checker.erl").
check(File) ->
io:format("Check:~p~n",[File]),
case epp:parse_file(File, ".", []) of
{ok, Forms} ->
chk(File, Forms);
_ ->
true
end.
chk(File, F) ->
try do(F)
catch
_:W ->
io:format("File:~p ~p~n",[File,W])
end.
do({function,_,Name,Arity,Clauses}) ->
%% io:format("checfun ~p~n",[Name]),
isFuname(Name),
do(Clauses);
do({call,_,{remote,_,A,B},Args}) ->
is_valid_function_name(A),
is_valid_function_name(B),
do(Args);
do({call,_,F,Args}) ->
is_valid_function_name(F),
do(Args);
do({var,_,V}) ->
is_valid_variable_name(V);
do(T) when tuple(T) ->
foreach(fun do/1, tuple_to_list(T));
do(T) when list(T) ->
foreach(fun do/1, T);
do(_) ->
true.
is_valid_function_name({atom,_,N}) ->
isFuname(N);
is_valid_function_name(X) ->
do(X).
isFuname(N) ->
%% io:format("Checking:~p~n",[N]),
case is_valid_funname(atom_to_list(N)) of
false -> io:format("** invalid function name:~p~n",[N]);
_ -> void
end.
-include("stdmacros.hrl").
is_valid_funname([H1,H2|T]) when ?IN(H1,$a,$z),
?IN(H2,$A,$Z) ->
false;
is_valid_funname([_|T]) ->
is_valid_funname(T);
is_valid_funname([]) ->
true.
is_valid_variable_name(V_n) ->
L = atom_to_list(V_n),
case L of
"_" ++ T ->
true;
_ ->
case (not member($_,L)) of
false -> io:format("** invalid variable name:~p~n",[L]);
_ -> void
end
end.
is_fun_name_char($_) -> true;
is_fun_name_char(X) when $a =< X, X =< $z -> true;
is_fun_name_char(_) -> false.
doIt(A) ->
a.