-
Notifications
You must be signed in to change notification settings - Fork 0
Add ff_pipeline:do parse transform #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,16 +7,21 @@ | |
|
|
||
| -module(ff_pipeline). | ||
|
|
||
| -include_lib("syntax_tools/include/merl.hrl"). | ||
|
|
||
| -export([do/1]). | ||
| -export([do/2]). | ||
| -export([unwrap/1]). | ||
| -export([unwrap/2]). | ||
| -export([wrap/1]). | ||
| -export([expect/2]). | ||
| -export([flip/1]). | ||
| -export([valid/2]). | ||
|
|
||
| -export([with/3]). | ||
|
|
||
| -export([parse_transform/2]). | ||
|
|
||
| %% | ||
|
|
||
| -type thrown(_E) :: | ||
|
|
@@ -42,7 +47,7 @@ do(Fun) -> | |
| ok | result(T, {Tag, E}). | ||
|
|
||
| do(Tag, Fun) -> | ||
| do(fun () -> unwrap(Tag, do(Fun)) end). | ||
| ff_pipeline:do(fun () -> unwrap(Tag, do(Fun)) end). | ||
|
|
||
| -spec unwrap | ||
| (ok) -> ok; | ||
|
|
@@ -56,6 +61,14 @@ unwrap({ok, V}) -> | |
| unwrap({error, E}) -> | ||
| throw(E). | ||
|
|
||
| -spec wrap(any()) -> | ||
| ok | {ok, any()}. | ||
|
|
||
| wrap(ok) -> | ||
| ok; | ||
| wrap(V) -> | ||
| {ok, V}. | ||
|
|
||
| -spec expect | ||
| (_E, ok) -> ok; | ||
| (_E, {ok, V}) -> V; | ||
|
|
@@ -117,3 +130,82 @@ with(Model, St, F) -> | |
| {error, Reason} -> | ||
| {error, {Model, Reason}} | ||
| end. | ||
|
|
||
| %% Parse transform | ||
| % -import(ff_pipeline, [do/1, unwrap/1]). | ||
| % f() -> | ||
| % do(fun() -> | ||
| % R0 = 1, | ||
| % R1 = unwrap(do_smth(R0)), | ||
| % R2 = ff_pipeline:unwrap(do_smth2(R1)), | ||
| % R2 + 1 | ||
| % end). | ||
| % To | ||
| % -import(ff_pipeline, [unwrap/1]). | ||
| % f() -> | ||
| % try | ||
| % ff_pipeline:wrap(begin | ||
| % R0 = 1, | ||
| % R1 = unwrap(do_smth(R0)), | ||
| % R2 = ff_pipeline:unwrap(do_smth2(R1)), | ||
| % R2 + 1 | ||
| % end) | ||
| % catch | ||
| % Thrown -> {error, Thrown} | ||
| % end. | ||
|
|
||
| -spec parse_transform(Forms, [compile:option()]) -> Forms when | ||
| Forms :: [erl_parse:abstract_form() | erl_parse:form_info()]. | ||
| parse_transform(Forms, _Options) -> | ||
| [erl_syntax:revert(erl_syntax_lib:map(fun transform_do/1, Form)) || Form <- Forms]. | ||
|
|
||
| transform_do(Tree) -> | ||
| TreePos = erl_syntax:get_pos(Tree), | ||
| case Tree of | ||
| ?Q("do(_@Tag, fun() -> _@@Body end)") -> | ||
| build_do_statement(TreePos, Tag, Body); | ||
| ?Q("ff_pipeline:do(_@Tag, fun() -> _@@Body end)") -> | ||
| build_do_statement(TreePos, Tag, Body); | ||
| ?Q("do(fun() -> _@@Body end)") -> | ||
| build_do_statement(TreePos, Body); | ||
| ?Q("ff_pipeline:do(fun() -> _@@Body end)") -> | ||
| build_do_statement(TreePos, Body); | ||
| ?Q("-import(ff_pipeline, ['@_@Imports'/0]).") -> | ||
| build_import_statement(TreePos, Imports); | ||
| _ -> | ||
| Tree | ||
| end. | ||
|
|
||
| build_do_statement(Pos, Body) -> | ||
| ThrownVar = build_var(<<"Thrown">>, Pos), | ||
| build_do_statement(Pos, ThrownVar, ThrownVar, Body). | ||
|
|
||
| build_do_statement(Pos, Tag, Body) -> | ||
| ThrownVar = build_var(<<"Thrown">>, Pos), | ||
| Error = ?Q("{_@Tag, _@ThrownVar}"), | ||
| build_do_statement(Pos, ThrownVar, Error, Body). | ||
|
|
||
| build_do_statement(_Pos, ThrownVar, Error, Body) -> | ||
| ?Q([ | ||
| "try ff_pipeline:wrap(begin _@@Body end)", | ||
| "catch _@ThrownVar -> {error, _@Error} end" | ||
| ]). | ||
|
|
||
| build_import_statement(_Pos, Imports) -> | ||
| FilteredImports = lists:filter(fun is_import_not_replaced/1, Imports), | ||
| ?Q("-import(ff_pipeline, ['@_@FilteredImports'/0])."). | ||
|
|
||
| is_import_not_replaced(ImportItem) -> | ||
| case ?Q("-export(['@_ImportItem'/0]).") of | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Очень долго думал, почему тут
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, добавлю. Или посмотрю, чем из Это нужно потому, что грамматика языка не позволяет однозначно распарсить |
||
| ?Q("-export([do/1]).") -> | ||
| false; | ||
| ?Q("-export([do/2]).") -> | ||
| false; | ||
| _ -> | ||
| true | ||
| end. | ||
|
|
||
| build_var(Name, Line) -> | ||
| LineStr = erlang:integer_to_binary(Line), | ||
| VarName = erlang:binary_to_atom(<<"$Pipeline", Name/binary, "-", LineStr/binary>>, latin1), | ||
| merl:var(VarName). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [do/1, unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [do/1, unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [do/1, unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [do/1, unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
|
|
||
| %% Pipeline | ||
|
|
||
| -compile({parse_transform, ff_pipeline}). | ||
| -import(ff_pipeline, [do/1, unwrap/1]). | ||
|
|
||
| %% | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Мне кажется не очень корректным перефигачивать конструкции с
do(...), даже в том случае, если эту псевдофункцию не импортировали в модуль. Кажется лучше было бы пойти по одному из двух путей:do, не требуя никаких имортов, только включения parse transform (не пытаться сделать вид, чтоdo‒ это функция в модулеff_pipeline).Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да, согласен, это не достаточно явно.
Попробую сделать.
На это агрятся IDE. Плагин, что я использовал, ожидаемо не смог обработать parse transform.
Ну и, справедливости ради,
doэто все же функцияff_pipeline, тут делается что-то вроде инлайнинга частных случаев.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Забавно, я не считаю это ожидаемым поведением. Это же просто ещё одна опция компиляции.
О, это я упустил.