File 3503-Add-a-poor-man-s-implementation-of-enable_feature-Fe.patch of Package erlang

From e025971d73273476e88e4bff385275b7f14ddc39 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Tue, 2 Nov 2021 07:08:54 +0100
Subject: [PATCH 03/12] Add a poor man's implementation of
 `{enable_feature,Feature}`

There is ongoing work, which will result in an EEP and a pull request,
for a way to conditionally enable new or experimental features in
the compiler and runtime system.

This commit implements this feature in a crude way, to allow us to
selectively make `maybe` and `else` keywords instead of atoms. That
will allow us to compile source code that use `maybe` and `else`
as atoms. (They are quite a lot of code in OTP that uses `maybe` as
an atom.)

Note that as implemented in this commit, for the
`{enable_feature,maybe_expr}` to have any effect, it must be given as an
option to the compiler, **not** in the source code for the module.
---
 lib/compiler/src/compile.erl | 13 +++++++++++++
 lib/stdlib/src/epp.erl       | 14 ++++++++++----
 2 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/lib/compiler/src/compile.erl b/lib/compiler/src/compile.erl
index ec0414cdda..847ea484ba 100644
--- a/lib/compiler/src/compile.erl
+++ b/lib/compiler/src/compile.erl
@@ -1018,12 +1018,25 @@ do_parse_module(DefEncoding, #compile{ifile=File,options=Opts,dir=Dir}=St) ->
                         false ->
                             1
                     end,
+
+    %% FIXME: Rewrite this when the enable feature EEP has been implemented.
+    ResWordFun = case proplists:get_value(enable_feature, Opts, []) of
+                     maybe_expr ->
+                         fun('maybe') -> true;
+                            ('else') -> true;
+                            (Other) -> erl_scan:reserved_word(Other)
+                         end;
+                     _ ->
+                         fun erl_scan:reserved_word/1
+                 end,
+
     R = epp:parse_file(File,
                        [{includes,[".",Dir|inc_paths(Opts)]},
                         {source_name, SourceName},
                         {macros,pre_defs(Opts)},
                         {default_encoding,DefEncoding},
                         {location,StartLocation},
+                        {reserved_word_fun,ResWordFun},
                         extra]),
     case R of
 	{ok,Forms0,Extra} ->
diff --git a/lib/stdlib/src/epp.erl b/lib/stdlib/src/epp.erl
index 185deaf7b4..1f14133039 100644
--- a/lib/stdlib/src/epp.erl
+++ b/lib/stdlib/src/epp.erl
@@ -73,7 +73,8 @@
 	            :: #{name() => [{argspec(), [used()]}]},
               default_encoding = ?DEFAULT_ENCODING :: source_encoding(),
 	      pre_opened = false :: boolean(),
-              fname = [] :: function_name_type()
+              fname = [] :: function_name_type(),
+              erl_scan_opts = [] :: erl_scan:options()
 	     }).
 
 %% open(Options)
@@ -604,11 +605,16 @@ init_server(Pid, FileName, Options, St0) ->
             %% first in path
             Path = [filename:dirname(FileName) |
                     proplists:get_value(includes, Options, [])],
+
+            ResWordFun = proplists:get_value(reserved_word_fun, Options,
+                                             fun erl_scan:reserved_word/1),
+
             %% the default location is 1 for backwards compatibility, not {1,1}
             AtLocation = proplists:get_value(location, Options, 1),
             St = St0#epp{delta=0, name=SourceName, name2=SourceName,
 			 path=Path, location=AtLocation, macs=Ms1,
-			 default_encoding=DefEncoding},
+			 default_encoding=DefEncoding,
+                         erl_scan_opts=[{reserved_word_fun,ResWordFun}]},
             From = wait_request(St),
             Anno = erl_anno:new(AtLocation),
             enter_file_reply(From, file_name(SourceName), Anno,
@@ -806,7 +812,7 @@ leave_file(From, St) ->
 %% scan_toks(Tokens, From, EppState)
 
 scan_toks(From, St) ->
-    case io:scan_erl_form(St#epp.file, '', St#epp.location) of
+    case io:scan_erl_form(St#epp.file, '', St#epp.location, St#epp.erl_scan_opts) of
 	{ok,Toks,Cl} ->
 	    scan_toks(Toks, From, St#epp{location=Cl});
 	{error,E,Cl} ->
@@ -1322,7 +1328,7 @@ new_location(Ln, {Le,_}, {Lf,_}) ->
 %%  nested conditionals and repeated 'else's.
 
 skip_toks(From, St, [I|Sis]) ->
-    case io:scan_erl_form(St#epp.file, '', St#epp.location) of
+    case io:scan_erl_form(St#epp.file, '', St#epp.location, St#epp.erl_scan_opts) of
 	{ok,[{'-',_Ah},{atom,_Ai,ifdef}|_Toks],Cl} ->
 	    skip_toks(From, St#epp{location=Cl}, [ifdef,I|Sis]);
 	{ok,[{'-',_Ah},{atom,_Ai,ifndef}|_Toks],Cl} ->
-- 
2.34.1

openSUSE Build Service is sponsored by