From 3d38e35ef83ce9b1b69ab8d618e14275a4dd5b4b Mon Sep 17 00:00:00 2001 From: belltoy Date: Thu, 17 Nov 2022 17:59:13 +0800 Subject: [PATCH] Fix the optional else block parse in maybe syntax. --- src/ktn_code.erl | 6 ++++++ test/ktn_code_SUITE.erl | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ktn_code.erl b/src/ktn_code.erl index fdb316d..a519436 100644 --- a/src/ktn_code.erl +++ b/src/ktn_code.erl @@ -430,6 +430,12 @@ to_map({try_after, Attrs, AfterBody}) -> #{type => try_after, attrs => #{location => get_location(Attrs), text => get_text(Attrs)}, content => to_map(AfterBody)}; +%% maybe..end +to_map({'maybe', Attrs, Body}) -> + MaybeBody = to_map(Body), + #{type => 'maybe', + attrs => #{location => get_location(Attrs), text => get_text(Attrs)}, + content => MaybeBody}; %% maybe..else..end to_map({'maybe', Attrs, Body, Else}) -> MaybeBody = to_map(Body), diff --git a/test/ktn_code_SUITE.erl b/test/ktn_code_SUITE.erl index 1aa1499..9579c9a 100644 --- a/test/ktn_code_SUITE.erl +++ b/test/ktn_code_SUITE.erl @@ -6,7 +6,7 @@ -if(?OTP_RELEASE >= 25). --export([parse_maybe/1]). +-export([parse_maybe/1, parse_maybe_else/1]). -endif. @@ -116,6 +116,16 @@ to_string(_Config) -> -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 end.">>), + + ok. + +-spec parse_maybe_else(config()) -> ok. +parse_maybe_else(_Config) -> %% Note that to pass this test case, the 'maybe_expr' feature must be enabled. #{type := root, content :=