Skip to content

Commit

Permalink
Merge pull request #69 from sile/support-maybe
Browse files Browse the repository at this point in the history
Support maybe syntax
  • Loading branch information
elbrujohalcon committed May 10, 2022
2 parents 7b27798 + b688540 commit b69d0e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/ktn_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ to_map({match, Attrs, Left, Right}) ->
text => get_text(Attrs)},
content => to_map([Left, Right])};

to_map({maybe_match, Attrs, Left, Right}) ->
#{type => maybe_match,
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
content => to_map([Left, Right])};

to_map({tuple, Attrs, Elements}) ->
#{type => tuple,
attrs => #{location => get_location(Attrs),
Expand Down Expand Up @@ -497,6 +503,22 @@ to_map({try_after, Attrs, AfterBody}) ->
text => get_text(Attrs)},
content => to_map(AfterBody)};

%% maybe..else..end

to_map({'maybe', Attrs, Body, Else}) ->
MaybeBody = to_map(Body),
MaybeElse = to_map(Else),
#{type => 'maybe',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
content => MaybeBody ++ [MaybeElse]};

to_map({'else', Attrs, Clauses}) ->
#{type => 'else',
attrs => #{location => get_location(Attrs),
text => get_text(Attrs)},
content => to_map(Clauses)};

%% if

to_map({'if', Attrs, IfClauses}) ->
Expand Down
23 changes: 23 additions & 0 deletions test/ktn_code_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
to_string/1
]).

-if(?OTP_RELEASE >= 25).
-export([
parse_maybe/1
]).
-endif.

-define(EXCLUDED_FUNS,
[
module_info,
Expand Down Expand Up @@ -126,6 +132,23 @@ to_string(_Config) ->

ok.

-if(?OTP_RELEASE >= 25).
-spec parse_maybe(config()) -> ok.
parse_maybe(_Config) ->
%% Note that to pass this test case, the 'maybe_expr' feature must be enabled.

#{ type := root
, content :=
[#{ type := function
, content :=
[#{ type := clause
, content := [#{type := 'maybe'}]}]
}]
} = ktn_code:parse_tree(<<"foo() -> maybe ok ?= ok else _ -> ng end.">>),

ok.
-endif.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down

0 comments on commit b69d0e9

Please sign in to comment.