From 4703b64dc87465e0d505b9997ca5e91f5cb12002 Mon Sep 17 00:00:00 2001 From: Andrey Tsirulev Date: Sun, 3 Aug 2014 01:58:09 +0400 Subject: Allow Name/Arity syntax in maps inside attributes Currently Name/Arity syntax is supported in list and tuple terms. This change makes it possible to use this syntax in map terms for consistency and convenience. Example: -custom(#{test1 => init/2, test2 => [val/1, val/2]}). --- lib/stdlib/src/erl_parse.yrl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/stdlib/src/erl_parse.yrl b/lib/stdlib/src/erl_parse.yrl index e1ae3b7aea..039cc88b9c 100644 --- a/lib/stdlib/src/erl_parse.yrl +++ b/lib/stdlib/src/erl_parse.yrl @@ -753,6 +753,9 @@ attribute_farity({cons,L,H,T}) -> attribute_farity({tuple,L,Args0}) -> Args = attribute_farity_list(Args0), {tuple,L,Args}; +attribute_farity({map,L,Args0}) -> + Args = attribute_farity_map(Args0), + {map,L,Args}; attribute_farity({op,L,'/',{atom,_,_}=Name,{integer,_,_}=Arity}) -> {tuple,L,[Name,Arity]}; attribute_farity(Other) -> Other. @@ -760,6 +763,14 @@ attribute_farity(Other) -> Other. attribute_farity_list(Args) -> [attribute_farity(A) || A <- Args]. +attribute_farity_map(Args) -> + [attribute_farity_map_field(A) || A <- Args]. + +attribute_farity_map_field({map_field_assoc,L,K,V}) -> + {map_field_assoc,L,attribute_farity(K),attribute_farity(V)}; +attribute_farity_map_field({map_field_exact,L,K,V}) -> + {map_field_exact,L,attribute_farity(K),attribute_farity(V)}. + -spec error_bad_decl(integer(), attributes()) -> no_return(). error_bad_decl(L, S) -> -- cgit v1.2.3 From 588df549e708b68ffb2b1becb5efecd20a7e6224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn-Egil=20Dahlberg?= Date: Tue, 30 Sep 2014 14:42:37 +0200 Subject: stdlib: Refactor Maps farity attributes --- lib/stdlib/src/erl_parse.yrl | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/stdlib/src/erl_parse.yrl b/lib/stdlib/src/erl_parse.yrl index 039cc88b9c..1d70b1286f 100644 --- a/lib/stdlib/src/erl_parse.yrl +++ b/lib/stdlib/src/erl_parse.yrl @@ -763,13 +763,9 @@ attribute_farity(Other) -> Other. attribute_farity_list(Args) -> [attribute_farity(A) || A <- Args]. +%% It is not meaningful to have farity keys. attribute_farity_map(Args) -> - [attribute_farity_map_field(A) || A <- Args]. - -attribute_farity_map_field({map_field_assoc,L,K,V}) -> - {map_field_assoc,L,attribute_farity(K),attribute_farity(V)}; -attribute_farity_map_field({map_field_exact,L,K,V}) -> - {map_field_exact,L,attribute_farity(K),attribute_farity(V)}. + [{Op,L,K,attribute_farity(V)} || {Op,L,K,V} <- Args]. -spec error_bad_decl(integer(), attributes()) -> no_return(). -- cgit v1.2.3 From 14a570b5ad9ad47027d0006fb0eab20b78ae100f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn-Egil=20Dahlberg?= Date: Tue, 30 Sep 2014 14:51:34 +0200 Subject: stdlib: Refactor ?line in erl_pp_SUITE:misc_attrs --- lib/stdlib/test/erl_pp_SUITE.erl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/stdlib/test/erl_pp_SUITE.erl b/lib/stdlib/test/erl_pp_SUITE.erl index babf3a49eb..04c1dd3898 100644 --- a/lib/stdlib/test/erl_pp_SUITE.erl +++ b/lib/stdlib/test/erl_pp_SUITE.erl @@ -604,20 +604,19 @@ import_export(Config) when is_list(Config) -> misc_attrs(suite) -> []; misc_attrs(Config) when is_list(Config) -> - ?line ok = pp_forms(<<"-module(m). ">>), - ?line ok = pp_forms(<<"-module(m, [Aafjlksfjdlsjflsdfjlsdjflkdsfjlk," - "Blsjfdlslfjsdf]). ">>), - ?line ok = pp_forms(<<"-export([]). ">>), - ?line ok = pp_forms(<<"-export([foo/2, bar/0]). ">>), - ?line ok = pp_forms(<<"-export([bar/0]). ">>), - ?line ok = pp_forms(<<"-import(lists, []). ">>), - ?line ok = pp_forms(<<"-import(lists, [map/2]). ">>), - ?line ok = pp_forms(<<"-import(lists, [map/2, foreach/2]). ">>), - ?line ok = pp_forms(<<"-'wild '({attr2,3}). ">>), - ?line ok = pp_forms(<<"-record(a, {b,c}). ">>), - ?line ok = pp_forms(<<"-record(' a ', {}). ">>), - ?line ok = pp_forms(<<"-record(' a ', {foo = foo:bar()}). ">>), - + ok = pp_forms(<<"-module(m). ">>), + ok = pp_forms(<<"-module(m, [Aafjlksfjdlsjflsdfjlsdjflkdsfjlk," + "Blsjfdlslfjsdf]). ">>), + ok = pp_forms(<<"-export([]). ">>), + ok = pp_forms(<<"-export([foo/2, bar/0]). ">>), + ok = pp_forms(<<"-export([bar/0]). ">>), + ok = pp_forms(<<"-import(lists, []). ">>), + ok = pp_forms(<<"-import(lists, [map/2]). ">>), + ok = pp_forms(<<"-import(lists, [map/2, foreach/2]). ">>), + ok = pp_forms(<<"-'wild '({attr2,3}). ">>), + ok = pp_forms(<<"-record(a, {b,c}). ">>), + ok = pp_forms(<<"-record(' a ', {}). ">>), + ok = pp_forms(<<"-record(' a ', {foo = foo:bar()}). ">>), ok. dialyzer_attrs(suite) -> -- cgit v1.2.3 From 4196af1031b1cb9e410de77118a52bfbc3363b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn-Egil=20Dahlberg?= Date: Tue, 30 Sep 2014 15:26:09 +0200 Subject: stdlib: erl_parse abstract Maps --- lib/stdlib/src/erl_parse.yrl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/stdlib/src/erl_parse.yrl b/lib/stdlib/src/erl_parse.yrl index 1d70b1286f..1d4a2a1fef 100644 --- a/lib/stdlib/src/erl_parse.yrl +++ b/lib/stdlib/src/erl_parse.yrl @@ -961,7 +961,9 @@ abstract([H|T], L, none=E) -> abstract(List, L, E) when is_list(List) -> abstract_list(List, [], L, E); abstract(Tuple, L, E) when is_tuple(Tuple) -> - {tuple,L,abstract_tuple_list(tuple_to_list(Tuple), L, E)}. + {tuple,L,abstract_tuple_list(tuple_to_list(Tuple), L, E)}; +abstract(Map, L, E) when is_map(Map) -> + {map,L,abstract_map_fields(maps:to_list(Map),L,E)}. abstract_list([H|T], String, L, E) -> case is_integer(H) andalso H >= 0 andalso E(H) of @@ -986,6 +988,9 @@ abstract_tuple_list([H|T], L, E) -> abstract_tuple_list([], _L, _E) -> []. +abstract_map_fields(Fs,L,E) -> + [{map_field_assoc,L,abstract(K,L,E),abstract(V,L,E)}||{K,V}<-Fs]. + abstract_byte(Byte, L) when is_integer(Byte) -> {bin_element, L, {integer, L, Byte}, default, default}; abstract_byte(Bits, L) -> -- cgit v1.2.3 From f515622d0ce3a4dd444745f9bee5cb39d5b7f0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn-Egil=20Dahlberg?= Date: Tue, 30 Sep 2014 15:30:44 +0200 Subject: stdlib: Test Map attributes via erl_pp --- lib/stdlib/test/erl_pp_SUITE.erl | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/stdlib/test/erl_pp_SUITE.erl b/lib/stdlib/test/erl_pp_SUITE.erl index 04c1dd3898..927fe0b595 100644 --- a/lib/stdlib/test/erl_pp_SUITE.erl +++ b/lib/stdlib/test/erl_pp_SUITE.erl @@ -617,6 +617,7 @@ misc_attrs(Config) when is_list(Config) -> ok = pp_forms(<<"-record(a, {b,c}). ">>), ok = pp_forms(<<"-record(' a ', {}). ">>), ok = pp_forms(<<"-record(' a ', {foo = foo:bar()}). ">>), + ok = pp_forms(<<"-custom1(#{test1 => init/2, test2 => [val/1, val/2]}). ">>), ok. dialyzer_attrs(suite) -> -- cgit v1.2.3