<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc Common library utility functions
-module(nklib_parse_trans_vsn).
-author('<NAME> <<EMAIL>>').

-export([parse_transform/2]).


%% ===================================================================
%% Public
%% ===================================================================

%% @doc Replaces do_fun/1 for a version compatible with erlang pre and post 21
parse_transform(Forms, _Opts) ->
    Forms2 = forms_replace_fun(do_try, 1,  make_try_fun(), Forms),
    Forms3 = forms_replace_fun(do_config_get, 1,  make_get_fun(), Forms2),
    Forms4 = forms_replace_fun(do_config_put, 2,  make_put_fun(), Forms3),
    Forms4.


%% @private
make_try_fun() ->
    Exp = case is_21() of
        true ->
            "
                do_try(Fun) ->
                    try Fun()
                catch
                    throw:Throw -> {exception, {throw, {Throw, []}}};
                    Class:Error:Trace -> {exception, {Class, {Error, Trace}}}
                end.
            ";
        false ->
            "
                do_try(Fun) ->
                    try Fun()
                catch
                    throw:Throw -> {exception, {throw, {Throw, []}}};
                    Class:Error -> {exception, {Class, {Error, erlang:get_stacktrace()}}}
                end.
            "
    end,
    forms_expression(Exp).


%% @private
make_get_fun() ->
    Exp = case is_21() of
        true ->
            "
                do_config_get(Key) ->
                    persistent_term:get(Key).
            ";
        false ->
            "
                do_config_get(Key) ->
                    nklib_config:get(nklib_trans_comp, Key).
            "
    end,
    forms_expression(Exp).


%% @private
make_put_fun() ->
    Exp = case is_21() of
        true ->
            "
                do_config_put(Key, Value) ->
                    persistent_term:put(Key, Value).
            ";
        false ->
            "
                do_config_put(Key, Value) ->
                    nklib_config:put(nklib_trans_comp, Key, Value).
            "
    end,
    forms_expression(Exp).


%% @private
is_21() ->
    erlang:system_info(otp_release) >= "21".


%% @private
forms_replace_fun(Name, Arity, Spec, Forms) ->
    forms_replace_fun(Forms, Name, Arity, Spec, []).


%% @private<commit_msg>Fix detection of Erlang > 21 =< 21.2<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc Common library utility functions
-module(nklib_parse_trans_vsn).
-author('<NAME> <<EMAIL>>').

-export([parse_transform/2]).


%% ===================================================================
%% Public
%% ===================================================================

%% @doc Replaces do_fun/1 for a version compatible with erlang pre and post 21
parse_transform(Forms, _Opts) ->
    Forms2 = forms_replace_fun(do_try, 1,  make_try_fun(), Forms),
    Forms3 = forms_replace_fun(do_config_get, 1,  make_get_fun(), Forms2),
    Forms4 = forms_replace_fun(do_config_put, 2,  make_put_fun(), Forms3),
    Forms4.


%% @private
make_try_fun() ->
    Exp = case is_21() of
        true ->
            "
                do_try(Fun) ->
                    try Fun()
                catch
                    throw:Throw -> {exception, {throw, {Throw, []}}};
                    Class:Error:Trace -> {exception, {Class, {Error, Trace}}}
                end.
            ";
        false ->
            "
                do_try(Fun) ->
                    try Fun()
                catch
                    throw:Throw -> {exception, {throw, {Throw, []}}};
                    Class:Error -> {exception, {Class, {Error, erlang:get_stacktrace()}}}
                end.
            "
    end,
    forms_expression(Exp).


%% @private
make_get_fun() ->
    Exp = case is_21_2() of
        true ->
            "
                do_config_get(Key) ->
                    persistent_term:get(Key).
            ";
        false ->
            "
                do_config_get(Key) ->
                    nklib_config:get(nklib_trans_comp, Key).
            "
    end,
    forms_expression(Exp).


%% @private
make_put_fun() ->
    Exp = case is_21_2() of
        true ->
            "
                do_config_put(Key, Value) ->
                    persistent_term:put(Key, Value).
            ";
        false ->
            "
                do_config_put(Key, Value) ->
                    nklib_config:put(nklib_trans_comp, Key, Value).
            "
    end,
    forms_expression(Exp).



%% @private
is_21() ->
    erlang:system_info(otp_release) >= "21".


%% @private
is_21_2() ->
    % Erlang 21.0: 10.0.8
    % Erlang 21.1: 10.1.8
    % Erlang 21.2: 10.2.1
    is_21() andalso erlang:system_info(version) >= "10.2.1".


%% @private
forms_replace_fun(Name, Arity, Spec, Forms) ->
    forms_replace_fun(Forms, Name, Arity, Spec, []).


%% @private
{'fraction_non_alphanumeric': 0.20112605567719738, 'fraction_numerical': 0.015483265561463873, 'mean_word_length': 1.7756076388888888, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '641178', 'n_tokens_mistral': 1891, 'n_tokens_neox': 1747, 'n_words': 455}