Skip to content

Commit

Permalink
Update Travis OTP vsns (21 and later), fix try/catch and some edoc (#43)
Browse files Browse the repository at this point in the history
* Update Travis OTP vsns (21 and later), fix try/catch and some edoc

* Fix some markup in overview.edoc

* Adjusting OTP versions for Travis CI

* Trying Travis with OTP 23.1

* Add minimum_otp_vsn
  • Loading branch information
uwiger authored Feb 5, 2021
1 parent 4376d81 commit 91f0243
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: erlang
otp_release:
- 21.0
- 20.3
- 19.3
- 18.3
- 23.1.2
- 22.3.4
- 21.3.8
script:
- rebar3 eunit
notifications:
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

# The parse_trans application #

__Authors:__ Ulf Wiger ([`[email protected]`](mailto:[email protected])).

Parse_transform utilities

[![Build Status](https://travis-ci.org/uwiger/parse_trans.svg)
[![Hex pm](http://img.shields.io/hexpm/v/parse_trans.svg?style=flat)](https://hex.pm/packages/parse_trans)


## Introduction ##

Parse_trans was written in order to capture some useful patterns in parse transformation
and code generation for Erlang.

Most notably, perhaps, the module [`exprecs`](http://github.com/uwiger/parse_trans/blob/master/doc/exprecs.md) generates standardized accessor
functions for records, and [`ct_expand`](http://github.com/uwiger/parse_trans/blob/master/doc/ct_expand.md) makes it possible to evaluate an
expression at compile-time and substitute the result as a compile-time constant.

Less known modules, perhaps:
* [`parse_trans_pp`](http://github.com/uwiger/parse_trans/blob/master/doc/parse_trans_pp.md) can be called with escript to pretty-print source from
debug-compiled .beam files.
* [`parse_trans_codegen`](http://github.com/uwiger/parse_trans/blob/master/doc/parse_trans_codegen.md) provides pseudo-functions that can be used for
simple code generation.
* [`parse_trans`](http://github.com/uwiger/parse_trans/blob/master/doc/parse_trans.md) provides various helper functions for traversing code and
managing complex parse transforms


## Modules ##

Expand Down
25 changes: 25 additions & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

# The parse_trans application #

__Authors:__ Ulf Wiger ([`[email protected]`](mailto:[email protected])).

Parse_transform utilities

[![Build Status](https://travis-ci.org/uwiger/parse_trans.svg)
[![Hex pm](http://img.shields.io/hexpm/v/parse_trans.svg?style=flat)](https://hex.pm/packages/parse_trans)


## Introduction ##

Parse_trans was written in order to capture some useful patterns in parse transformation
and code generation for Erlang.

Most notably, perhaps, the module [`exprecs`](exprecs.md) generates standardized accessor
functions for records, and [`ct_expand`](ct_expand.md) makes it possible to evaluate an
expression at compile-time and substitute the result as a compile-time constant.

Less known modules, perhaps:
* [`parse_trans_pp`](parse_trans_pp.md) can be called with escript to pretty-print source from
debug-compiled .beam files.
* [`parse_trans_codegen`](parse_trans_codegen.md) provides pseudo-functions that can be used for
simple code generation.
* [`parse_trans`](parse_trans.md) provides various helper functions for traversing code and
managing complex parse transforms


## Modules ##

Expand Down
25 changes: 25 additions & 0 deletions doc/overview.edoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@author Ulf Wiger <[email protected]>

@doc Parse_transform utilities

[![Build Status](https://travis-ci.org/uwiger/parse_trans.svg)
[![Hex pm](http://img.shields.io/hexpm/v/parse_trans.svg?style=flat)](https://hex.pm/packages/parse_trans)

<h2>Introduction</h2>

Parse_trans was written in order to capture some useful patterns in parse transformation
and code generation for Erlang.

Most notably, perhaps, the module {@link exprecs} generates standardized accessor
functions for records, and {@link ct_expand} makes it possible to evaluate an
expression at compile-time and substitute the result as a compile-time constant.

Less known modules, perhaps:
* {@link parse_trans_pp} can be called with escript to pretty-print source from
debug-compiled .beam files.
* {@link parse_trans_codegen} provides pseudo-functions that can be used for
simple code generation.
* {@link parse_trans} provides various helper functions for traversing code and
managing complex parse transforms

@end
4 changes: 2 additions & 2 deletions doc/parse_trans.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ xform_f_rec() = fun((<a href="#type-type">type()</a>, <a href="#type-form">form(
<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#context-2">context/2</a></td><td>
Accessor function for the Context record.</td></tr><tr><td valign="top"><a href="#depth_first-4">depth_first/4</a></td><td></td></tr><tr><td valign="top"><a href="#do_depth_first-4">do_depth_first/4</a></td><td></td></tr><tr><td valign="top"><a href="#do_insert_forms-4">do_insert_forms/4</a></td><td></td></tr><tr><td valign="top"><a href="#do_inspect-4">do_inspect/4</a></td><td></td></tr><tr><td valign="top"><a href="#do_transform-4">do_transform/4</a></td><td></td></tr><tr><td valign="top"><a href="#error-3">error/3</a></td><td>.</td></tr><tr><td valign="top"><a href="#export_function-3">export_function/3</a></td><td></td></tr><tr><td valign="top"><a href="#format_error-1">format_error/1</a></td><td></td></tr><tr><td valign="top"><a href="#format_exception-2">format_exception/2</a></td><td>Equivalent to <a href="#format_exception-3"><tt>format_exception(Class, Reason, 4)</tt></a>.</td></tr><tr><td valign="top"><a href="#format_exception-3">format_exception/3</a></td><td>Produces a few lines of user-friendly formatting of exception info.</td></tr><tr><td valign="top"><a href="#function_exists-3">function_exists/3</a></td><td>
Checks whether the given function is defined in Forms.</td></tr><tr><td valign="top"><a href="#get_attribute-2">get_attribute/2</a></td><td>
Returns the value of the first occurence of attribute A.</td></tr><tr><td valign="top"><a href="#get_attribute-3">get_attribute/3</a></td><td></td></tr><tr><td valign="top"><a href="#get_file-1">get_file/1</a></td><td>
Returns the value of the first occurrence of attribute A.</td></tr><tr><td valign="top"><a href="#get_attribute-3">get_attribute/3</a></td><td></td></tr><tr><td valign="top"><a href="#get_file-1">get_file/1</a></td><td>
Returns the name of the file being compiled.</td></tr><tr><td valign="top"><a href="#get_module-1">get_module/1</a></td><td>
Returns the name of the module being compiled.</td></tr><tr><td valign="top"><a href="#get_orig_syntax_tree-1">get_orig_syntax_tree/1</a></td><td>.</td></tr><tr><td valign="top"><a href="#get_pos-1">get_pos/1</a></td><td>
Tries to retrieve the line number from an erl_syntax form.</td></tr><tr><td valign="top"><a href="#initial_context-2">initial_context/2</a></td><td>
Expand Down Expand Up @@ -253,7 +253,7 @@ get_attribute(A, Forms) -&gt; any()

<ul class="definitions"><li><code>A = atom()</code></li></ul>

Returns the value of the first occurence of attribute A.
Returns the value of the first occurrence of attribute A.

<a name="get_attribute-3"></a>

Expand Down
5 changes: 4 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
%% under the License.
%% --------------------------------------------------

{minimum_otp_vsn, "21.0"}.

{erl_first_files, ["src/parse_trans.erl",
"src/parse_trans_pp.erl",
"src/parse_trans_codegen.erl"]}.
Expand All @@ -30,7 +32,8 @@
{edoc_opts, [{doclet, edown_doclet},
{top_level_readme,
{"./README.md",
"http://github.com/uwiger/parse_trans"}}]}
"http://github.com/uwiger/parse_trans",
"master"}}]}
]},
{test,
[
Expand Down
30 changes: 19 additions & 11 deletions src/parse_trans.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
type/1
]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-record(context, {module,
function,
arity,
Expand All @@ -100,12 +104,6 @@
throw({error,get_pos(I),{R, Trace}})
end).

-ifdef(OTP_RELEASE).
-define(WITH_STACKTRACE(T, R, S), T:R:S ->).
-else.
-define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),).
-endif.

-export_type([forms/0]).

%% Typedefs
Expand Down Expand Up @@ -329,7 +327,7 @@ do(Transform, Fun, Acc, Forms, Options) ->
optionally_pretty_print(NewForms1, Options, Context),
{NewForms1, Acc1}
catch
?WITH_STACKTRACE(error, Reason, ST)
error:Reason:ST ->
{error,
[{File, [{?DUMMY_LINE, ?MODULE,
{Reason, ST}}]}]};
Expand All @@ -349,7 +347,7 @@ top(F, Forms, Options) ->
optionally_pretty_print(NewForms1, Options, Context),
NewForms1
catch
?WITH_STACKTRACE(error, Reason, ST)
error:Reason:ST ->
{error,
[{File, [{?DUMMY_LINE, ?MODULE,
{Reason, ST}}]}]};
Expand Down Expand Up @@ -748,14 +746,14 @@ format_exception(Class, Reason) ->
%%% Note that a stacktrace is generated inside this function.
%%% @end
format_exception(Class, Reason, Lines) ->
ST = erlang:process_info(self(), current_stacktrace),
{current_stacktrace, ST} = erlang:process_info(self(), current_stacktrace),
PrintF = fun(Term, I) ->
io_lib_pretty:print(
Term, I, columns(), ?LINEMAX, ?CHAR_MAX,
record_print_fun())
end,
StackF = fun(_, _, _) -> false end,
lines(Lines, lib:format_exception(
lines(Lines, erl_error:format_exception(
1, Class, Reason, ST, StackF, PrintF)).

columns() ->
Expand Down Expand Up @@ -886,7 +884,7 @@ this_form_df(F, Form, Context, Acc) ->
apply_F(F, Type, Form, Context, Acc) ->
try F(Type, Form, Context, Acc)
catch
?WITH_STACKTRACE(error, Reason, ST)
error:Reason:ST ->
?ERROR(Reason,
?HERE,
[{type, Type},
Expand Down Expand Up @@ -957,3 +955,13 @@ format_error(Error) ->

format_error_(Error) ->
lists:flatten(io_lib:fwrite("~p", [Error])).


%% EUnit
-ifdef(TEST).

format_exeption_test() ->
[_,_,_] = format_exception(error, {error, foo}, 3),
ok.

-endif.

0 comments on commit 91f0243

Please sign in to comment.