Skip to content

Commit

Permalink
Untabify
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Klionsky committed Mar 10, 2014
1 parent fa86ba9 commit 5c6f3df
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 248 deletions.
4 changes: 2 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{erl_opts, [
warn_export_all,
warn_missing_spec
warn_export_all,
warn_missing_spec
]}.
{escript_name, "syntaxerl"}.
26 changes: 13 additions & 13 deletions src/syntaxerl.app.src
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{application, syntaxerl, [
{description, "Syntax checker for Erlang"},
{vsn, "0.0.5"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{env, []},
{handlers, [
{".*\\.erl$", syntaxerl_erl},
{".*\\.hrl$", syntaxerl_hrl},
{".*", syntaxerl_terms}
]}
{description, "Syntax checker for Erlang"},
{vsn, "0.0.5"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{env, []},
{handlers, [
{".*\\.erl$", syntaxerl_erl},
{".*\\.hrl$", syntaxerl_hrl},
{".*", syntaxerl_terms}
]}
]}.
130 changes: 65 additions & 65 deletions src/syntaxerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,96 @@

-spec behaviour_info(callbacks | any()) -> [{atom(), arity()}] | undefined.
behaviour_info(callbacks) ->
[{check_syntax, 2}];
[{check_syntax, 2}];
behaviour_info(_) ->
undefined.
undefined.

-spec main([string()]) -> no_return().
main([""]) ->
usage();
usage();
main(["-h"]) ->
usage();
usage();
main(["--help"]) ->
usage();
usage();
main(["-d"]) ->
usage();
usage();
main(["--debug"]) ->
usage();
usage();
main([FileName]) ->
check_syntax(FileName, false);
check_syntax(FileName, false);
main([FileName, "-d"]) ->
check_syntax(FileName, true);
check_syntax(FileName, true);
main(["-d", FileName]) ->
check_syntax(FileName, true);
check_syntax(FileName, true);
main([FileName, "--debug"]) ->
check_syntax(FileName, true);
check_syntax(FileName, true);
main(["--debug", FileName]) ->
check_syntax(FileName, true);
check_syntax(FileName, true);
main(_) ->
usage().

check_syntax(FileName, Debug) ->
ScriptName = escript:script_name(),
Handlers = handlers(ScriptName),
syntaxerl_logger:debug(Debug, "Handlers: ~p", [Handlers]),
Handler = get_handler(FileName, Handlers),
syntaxerl_logger:debug(Debug, "Selected handler: ~p", [Handler]),
case Handler:check_syntax(FileName, Debug) of
{ok, Issues} ->
syntaxerl_utils:print_issues(FileName, Issues);
{error, Issues} ->
syntaxerl_utils:print_issues(FileName, Issues)
end.
ScriptName = escript:script_name(),
Handlers = handlers(ScriptName),
syntaxerl_logger:debug(Debug, "Handlers: ~p", [Handlers]),
Handler = get_handler(FileName, Handlers),
syntaxerl_logger:debug(Debug, "Selected handler: ~p", [Handler]),
case Handler:check_syntax(FileName, Debug) of
{ok, Issues} ->
syntaxerl_utils:print_issues(FileName, Issues);
{error, Issues} ->
syntaxerl_utils:print_issues(FileName, Issues)
end.

usage() ->
ScriptName = escript:script_name(),
BaseName = filename:basename(ScriptName),
ScriptName = escript:script_name(),
BaseName = filename:basename(ScriptName),
io:format("Usage: ~s [-d] filename~n", [BaseName]),
io:format("Usage: ~s [-h]~n", [BaseName]),
case description_vsn(ScriptName) of
{Description, Vsn} ->
io:format("~s (~s)~n~n", [Description, Vsn]);
_ ->
io:format("~n")
end,
io:format(" -d, --debug Enable debug output~n"),
io:format(" -h, --help Show this message~n~n").
io:format("Usage: ~s [-h]~n", [BaseName]),
case description_vsn(ScriptName) of
{Description, Vsn} ->
io:format("~s (~s)~n~n", [Description, Vsn]);
_ ->
io:format("~n")
end,
io:format(" -d, --debug Enable debug output~n"),
io:format(" -h, --help Show this message~n~n").

script_options(ScriptName) ->
{ok, Sections} = escript:extract(ScriptName, []),
ZipArchive = proplists:get_value(archive, Sections),
AppName = lists:flatten(io_lib:format("~p.app", [?MODULE])),
case zip:extract(ZipArchive, [{file_list, [AppName]}, memory]) of
{ok, [{AppName, Binary}]} ->
{ok, Tokens, _} = erl_scan:string(binary_to_list(Binary)),
{ok, {application, ?MODULE, Options}} = erl_parse:parse_term(Tokens),
Options;
_ ->
undefined
end.
{ok, Sections} = escript:extract(ScriptName, []),
ZipArchive = proplists:get_value(archive, Sections),
AppName = lists:flatten(io_lib:format("~p.app", [?MODULE])),
case zip:extract(ZipArchive, [{file_list, [AppName]}, memory]) of
{ok, [{AppName, Binary}]} ->
{ok, Tokens, _} = erl_scan:string(binary_to_list(Binary)),
{ok, {application, ?MODULE, Options}} = erl_parse:parse_term(Tokens),
Options;
_ ->
undefined
end.

description_vsn(ScriptName) ->
case script_options(ScriptName) of
undefined ->
undefined;
Options ->
Description = proplists:get_value(description, Options),
Vsn = proplists:get_value(vsn, Options),
{Description, Vsn}
end.
case script_options(ScriptName) of
undefined ->
undefined;
Options ->
Description = proplists:get_value(description, Options),
Vsn = proplists:get_value(vsn, Options),
{Description, Vsn}
end.

handlers(ScriptName) ->
case script_options(ScriptName) of
undefined ->
undefined;
Options ->
proplists:get_value(handlers, Options)
end.
case script_options(ScriptName) of
undefined ->
undefined;
Options ->
proplists:get_value(handlers, Options)
end.

get_handler(FileName, [{Pattern, Handler} | Patterns]) ->
case re:run(FileName, Pattern, [{capture, none}]) of
match ->
Handler;
nomatch ->
get_handler(FileName, Patterns)
end.
case re:run(FileName, Pattern, [{capture, none}]) of
match ->
Handler;
nomatch ->
get_handler(FileName, Patterns)
end.
54 changes: 27 additions & 27 deletions src/syntaxerl_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
-include("check_syntax_spec.hrl").

check_syntax(FileName, Debug) ->
{InclDirs, DepsDirs, ErlcOpts} = syntaxerl_utils:incls_deps_opts(FileName),
syntaxerl_logger:debug(Debug, "Include dirs: ~p", [InclDirs]),
syntaxerl_logger:debug(Debug, "Deps dirs: ~p", [DepsDirs]),
syntaxerl_logger:debug(Debug, "Erlc opts: ~p", [ErlcOpts]),
{InclDirs, DepsDirs, ErlcOpts} = syntaxerl_utils:incls_deps_opts(FileName),
syntaxerl_logger:debug(Debug, "Include dirs: ~p", [InclDirs]),
syntaxerl_logger:debug(Debug, "Deps dirs: ~p", [DepsDirs]),
syntaxerl_logger:debug(Debug, "Erlc opts: ~p", [ErlcOpts]),

code:add_paths(DepsDirs),
code:add_paths(DepsDirs),

Result = compile:file(FileName, ErlcOpts ++ InclDirs),
syntaxerl_logger:debug(Debug, "Compile result: ~p", [Result]),
Result = compile:file(FileName, ErlcOpts ++ InclDirs),
syntaxerl_logger:debug(Debug, "Compile result: ~p", [Result]),

case Result of
{ok, _ModuleName} ->
{ok, []};
{ok, _ModuleName, Warnings} ->
{ok, format_warnings(Warnings)};
{error, Errors, Warnings} ->
{error, format_errors(Errors) ++ format_warnings(Warnings)}
end.
case Result of
{ok, _ModuleName} ->
{ok, []};
{ok, _ModuleName, Warnings} ->
{ok, format_warnings(Warnings)};
{error, Errors, Warnings} ->
{error, format_errors(Errors) ++ format_warnings(Warnings)}
end.

%%% Internal

Expand All @@ -36,23 +36,23 @@ fix_line_number(none) -> 1;
fix_line_number(Line) -> Line.

format_errors([]) ->
[];
[];
format_errors([{_FileName, Errors} | _]) ->
FilteredErrors = lists:filter(fun output_error/1, Errors),
lists:map(fun format_error/1, FilteredErrors).
FilteredErrors = lists:filter(fun output_error/1, Errors),
lists:map(fun format_error/1, FilteredErrors).

format_error({Line, _Mod, _Term} = Error) ->
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{error, FixedLine, Description}.
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{error, FixedLine, Description}.

format_warnings([]) ->
[];
[];
format_warnings([{_FileName, Warnings} | _]) ->
FilteredWarnings = lists:filter(fun output_warning/1, Warnings),
lists:map(fun format_warning/1, FilteredWarnings).
FilteredWarnings = lists:filter(fun output_warning/1, Warnings),
lists:map(fun format_warning/1, FilteredWarnings).

format_warning({Line, _Mod, _Term} = Error) ->
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{warning, FixedLine, Description}.
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{warning, FixedLine, Description}.
90 changes: 45 additions & 45 deletions src/syntaxerl_hrl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@
-include("check_syntax_spec.hrl").

check_syntax(FileName, Debug) ->
case file:read_file(FileName) of
{ok, HrlContent} ->
%% precede with the module name, so now this is a real erlang module.
ErlContent = <<
<<"-module(test_inc).\n">>/binary,
HrlContent/binary
>>,
%% append an erlang extention, so the `compile:file' won't complain.
NewFileName = FileName ++ ".erl",
case file:write_file(NewFileName, ErlContent) of
ok ->
{InclDirs, DepsDirs, ErlcOpts} = syntaxerl_utils:incls_deps_opts(FileName),
syntaxerl_logger:debug(Debug, "Include dirs: ~p", [InclDirs]),
syntaxerl_logger:debug(Debug, "Deps dirs: ~p", [DepsDirs]),
syntaxerl_logger:debug(Debug, "Erlc opts: ~p", [ErlcOpts]),
case file:read_file(FileName) of
{ok, HrlContent} ->
%% precede with the module name, so now this is a real erlang module.
ErlContent = <<
<<"-module(test_inc).\n">>/binary,
HrlContent/binary
>>,
%% append an erlang extention, so the `compile:file' won't complain.
NewFileName = FileName ++ ".erl",
case file:write_file(NewFileName, ErlContent) of
ok ->
{InclDirs, DepsDirs, ErlcOpts} = syntaxerl_utils:incls_deps_opts(FileName),
syntaxerl_logger:debug(Debug, "Include dirs: ~p", [InclDirs]),
syntaxerl_logger:debug(Debug, "Deps dirs: ~p", [DepsDirs]),
syntaxerl_logger:debug(Debug, "Erlc opts: ~p", [ErlcOpts]),

code:add_paths(DepsDirs),
code:add_paths(DepsDirs),

Result = compile:file(NewFileName, ErlcOpts ++ InclDirs),
syntaxerl_logger:debug(Debug, "Compile result: ~p", [Result]),
Result = compile:file(NewFileName, ErlcOpts ++ InclDirs),
syntaxerl_logger:debug(Debug, "Compile result: ~p", [Result]),

file:delete(NewFileName),
file:delete(NewFileName),

case Result of
{ok, _ModuleName} ->
{ok, []};
{ok, _ModuleName, Warnings} ->
{ok, format_warnings(Warnings)};
{error, Errors, Warnings} ->
{error, format_errors(Errors) ++ format_warnings(Warnings)}
end;
{error, Reason} ->
{error, [{error, file:format_error(Reason)}]}
end;
{error, Reason} ->
{error, [{error, file:format_error(Reason)}]}
end.
case Result of
{ok, _ModuleName} ->
{ok, []};
{ok, _ModuleName, Warnings} ->
{ok, format_warnings(Warnings)};
{error, Errors, Warnings} ->
{error, format_errors(Errors) ++ format_warnings(Warnings)}
end;
{error, Reason} ->
{error, [{error, file:format_error(Reason)}]}
end;
{error, Reason} ->
{error, [{error, file:format_error(Reason)}]}
end.

%%% Internal

Expand All @@ -61,23 +61,23 @@ fix_line_number(none) -> 1;
fix_line_number(Line) -> Line - 1.

format_errors([]) ->
[];
[];
format_errors([{_FileName, Errors} | _]) ->
FilteredErrors = lists:filter(fun output_error/1, Errors),
lists:map(fun format_error/1, FilteredErrors).
FilteredErrors = lists:filter(fun output_error/1, Errors),
lists:map(fun format_error/1, FilteredErrors).

format_error({Line, _Mod, _Term} = Error) ->
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{error, FixedLine, Description}.
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{error, FixedLine, Description}.

format_warnings([]) ->
[];
[];
format_warnings([{_FileName, Warnings} | _]) ->
FilteredWarnings = lists:filter(fun output_warning/1, Warnings),
lists:map(fun format_warning/1, FilteredWarnings).
FilteredWarnings = lists:filter(fun output_warning/1, Warnings),
lists:map(fun format_warning/1, FilteredWarnings).

format_warning({Line, _Mod, _Term} = Error) ->
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{warning, FixedLine, Description}.
Description = syntaxerl_utils:error_description(Error),
FixedLine = fix_line_number(Line),
{warning, FixedLine, Description}.
6 changes: 2 additions & 4 deletions src/syntaxerl_logger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

-spec debug(Output::boolean(), Fmt::string(), Args::[term()]) -> ok.
debug(true, Fmt, Args) ->
io:format("DEBUG: " ++ Fmt ++ "~n", Args);
io:format("DEBUG: " ++ Fmt ++ "~n", Args);
debug(false, _Fmt, _Args) ->
ok.


ok.
Loading

0 comments on commit 5c6f3df

Please sign in to comment.