Permalink
Browse files

Add support for one more expression type

This leads to another Erlang grammar addition.
  • Loading branch information...
1 parent 96f1e0b commit b013cdedd73aa835c76f97d2fcd769fef6cf4ad8 @alavrik committed Jul 9, 2011
Showing with 22 additions and 3 deletions.
  1. +2 −0 src/erl_parse.yrl
  2. +19 −3 src/erlson_parse_transform.erl
  3. +1 −0 src/erlson_tests.erl
View
2 src/erl_parse.yrl
@@ -352,6 +352,8 @@ record_expr -> expr_max '#' record_tuple :
{record,?line('$2'),'$1','','$3'}.
record_expr -> record_expr '#' atom '.' atom :
{record_field,?line('$2'),'$1',element(3, '$3'),'$5'}.
+record_expr -> record_expr '.' atom :
+ {record_field,?line('$2'),'$1','','$3'}.
record_expr -> record_expr '#' atom record_tuple :
{record,?line('$2'),'$1',element(3, '$3'),'$4'}.
record_expr -> record_expr '#' record_tuple :
View
22 src/erlson_parse_transform.erl
@@ -151,6 +151,14 @@ make_dict_store_1(InitDict, [H|T]) ->
Res.
+make_dict_new(LINE) ->
+ make_call(LINE, 'dict', 'new', []).
+
+
+make_dict_fetch(LINE, FieldName, Dict) ->
+ make_call(LINE, 'dict', 'fetch', [FieldName, Dict]).
+
+
make_call(LINE, ModName, FunName, Args) ->
Mod = {'atom',LINE,ModName},
Fun = {'atom',LINE,FunName},
@@ -162,15 +170,23 @@ make_call(LINE, ModName, FunName, Args) ->
%
% common function for traversing expressions, patterns and guard elements
%
+
+% this node was returned by the customized Erlang parser
expr({record,LINE,'',L}, S) when ?is_context(body) ->
- % convert #{...} to dict:store(Key, Value, dict:store(...))
- InitDict = make_call(LINE, 'dict', 'new', []),
+ % convert #{...} to dict:store(Key, Value, dict:store(..., dict:new()))
+ InitDict = make_dict_new(LINE),
make_dict_store(InitDict, ?field_list(L));
+% this node was returned by the customized Erlang parser
expr({record,_LINE,E,'',L}, S) when ?is_context(body) ->
% convert D#{...} to dict:store(Key, Value, dict:store(..., D))
make_dict_store(_InitDict = ?expr(E), ?field_list(L));
+% this node was returned by the customized Erlang parser
+expr({record_field,LINE,E,'',F}, S) when ?is_context(body) ->
+ % convert #X.foo to dict:fetch(foo, X)
+ make_dict_fetch(LINE, F, ?expr(E));
+
% NOTE: reusing Mensia field access syntax for accessing dict members
% Original use: If E is E_0.Field, a Mnesia record access inside a query.
expr({record_field,LINE,E,F}, S) when ?is_context(body),
@@ -179,7 +195,7 @@ expr({record_field,LINE,E,F}, S) when ?is_context(body),
andalso element(3, E) == '') ->
% convert X.foo to dict:fetch(foo, X)
%?PRINT("record_field: ~p~n", [E]),
- make_call(LINE, 'dict', 'fetch', [F, ?expr(E)]);
+ make_dict_fetch(LINE, F, ?expr(E));
%
% end of special handling of 'dict' records
View
1 src/erlson_tests.erl
@@ -33,6 +33,7 @@ basic_test() ->
% associating foo with 1
D = #{foo = 1},
1 = D.foo,
+ 1 = #{foo = 1}.foo,
% setting foo to foo + 1 and baz to 100
D1 = D#{foo = D.foo + 1}#{baz = 100},

0 comments on commit b013cde

Please sign in to comment.