Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Support arbitrary number of arguments to ifchanged

  • Loading branch information...
commit 165b1bfebb034a1ff7a3412a588c1f2c1d063901 1 parent 2c62bd0
Evan Miller authored

Showing 2 changed files with 18 additions and 32 deletions. Show diff stats Hide diff stats

  1. +17 18 src/erlydtl_compiler.erl
  2. +1 14 src/erlydtl_parser.yrl
35 src/erlydtl_compiler.erl
@@ -545,24 +545,22 @@ body_ast(DjangoParseTree, Context, TreeWalker) ->
545 545 {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
546 546 {ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
547 547 ifelse_ast(Expression, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
548   - ({'ifchanged', Expression, Contents}, TreeWalkerAcc) ->
  548 + ({'ifchanged', '$undefined', Contents}, TreeWalkerAcc) ->
549 549 {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
550 550 {ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
551   - case Expression of
552   - '$undefined' ->
553   - ifchanged_ast(Contents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
554   - _ ->
555   - ifchanged_ast({expr, Expression}, IfAstInfo, ElseAstInfo, Context, TreeWalker2)
556   - end;
557   - ({'ifchangedelse', Expression, IfContents, ElseContents}, TreeWalkerAcc) ->
  551 + ifchanged_contents_ast(Contents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
  552 + ({'ifchanged', Values, Contents}, TreeWalkerAcc) ->
  553 + {IfAstInfo, TreeWalker1} = body_ast(Contents, Context, TreeWalkerAcc),
  554 + {ElseAstInfo, TreeWalker2} = empty_ast(TreeWalker1),
  555 + ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
  556 + ({'ifchangedelse', '$undefined', IfContents, ElseContents}, TreeWalkerAcc) ->
558 557 {IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
559 558 {ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
560   - case Expression of
561   - '$undefined' ->
562   - ifchanged_ast(IfContents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
563   - _ ->
564   - ifchanged_ast({expr, Expression}, IfAstInfo, ElseAstInfo, Context, TreeWalker2)
565   - end;
  559 + ifchanged_contents_ast(IfContents, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
  560 + ({'ifchangedelse', Values, IfContents, ElseContents}, TreeWalkerAcc) ->
  561 + {IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
  562 + {ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
  563 + ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, Context, TreeWalker2);
566 564 ({'ifelse', Expression, IfContents, ElseContents}, TreeWalkerAcc) ->
567 565 {IfAstInfo, TreeWalker1} = body_ast(IfContents, Context, TreeWalkerAcc),
568 566 {ElseAstInfo, TreeWalker2} = body_ast(ElseContents, Context, TreeWalker1),
@@ -1099,19 +1097,20 @@ for_loop_ast(IteratorList, LoopValue, Contents, {EmptyContentsAst, EmptyContents
1099 1097 merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)
1100 1098 }, TreeWalker2}.
1101 1099
1102   -ifchanged_ast({expr, Expressions}, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
  1100 +ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
1103 1101 Info = merge_info(IfContentsInfo, ElseContentsInfo),
1104 1102 ValueAstFun = fun(Expr, {LTreeWalker, LInfo, Acc}) ->
1105 1103 {{EAst, EInfo}, ETw} = value_ast(Expr, false, Context, LTreeWalker),
1106 1104 {ETw, merge_info(LInfo, EInfo), [erl_syntax:tuple([erl_syntax:integer(erlang:phash2(Expr)), EAst])|Acc]} end,
1107   - {TreeWalker1, MergedInfo, Changed} = lists:foldl(ValueAstFun, {TreeWalker, Info, []}, Expressions),
  1105 + {TreeWalker1, MergedInfo, Changed} = lists:foldl(ValueAstFun, {TreeWalker, Info, []}, Values),
1108 1106 {{erl_syntax:case_expr(erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(ifchanged), [erl_syntax:list(Changed)]),
1109 1107 [erl_syntax:clause([erl_syntax:atom(true)], none,
1110 1108 [IfContentsAst]),
1111 1109 erl_syntax:clause([erl_syntax:underscore()], none,
1112 1110 [ElseContentsAst])
1113   - ]), MergedInfo}, TreeWalker1};
1114   -ifchanged_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, _Context, TreeWalker) ->
  1111 + ]), MergedInfo}, TreeWalker1}.
  1112 +
  1113 +ifchanged_contents_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, _Context, TreeWalker) ->
1115 1114 Info = merge_info(IfContentsInfo, ElseContentsInfo),
1116 1115 Key = erl_syntax:integer(erlang:phash2(Contents)),
1117 1116 {{erl_syntax:case_expr(erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(ifchanged), [erl_syntax:list([erl_syntax:tuple([Key, IfContentsAst])])]),
15 src/erlydtl_parser.yrl
@@ -85,11 +85,6 @@ Nonterminals
85 85
86 86 IfChangedBlock
87 87 IfChangedBraced
88   - IfChangedExpression
89   - IfChangedExpression1
90   - IfChangedExpression2
91   - IfChangedExpression3
92   - IfChangedExpression4
93 88 EndIfChangedBraced
94 89
95 90 IfEqualBlock
@@ -325,15 +320,7 @@ EndIfBraced -> open_tag endif_keyword close_tag.
325 320 IfChangedBlock -> IfChangedBraced Elements ElseBraced Elements EndIfChangedBraced : {ifchangedelse, '$1', '$2', '$4'}.
326 321 IfChangedBlock -> IfChangedBraced Elements EndIfChangedBraced : {ifchanged, '$1', '$2'}.
327 322 IfChangedBraced -> open_tag ifchanged_keyword close_tag.
328   -IfChangedBraced -> open_tag ifchanged_keyword IfChangedExpression close_tag : '$3'.
329   -IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 : ['$1', '$2'].
330   -IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 IfChangedExpression3 : ['$1', '$2', '$3'].
331   -IfChangedExpression -> IfChangedExpression1 IfChangedExpression2 IfChangedExpression3 IfChangedExpression4: ['$1', '$2', '$3', '$4'].
332   -IfChangedExpression -> Value : ['$1'].
333   -IfChangedExpression1 -> Value : '$1'.
334   -IfChangedExpression2 -> Value : '$1'.
335   -IfChangedExpression3 -> Value : '$1'.
336   -IfChangedExpression4 -> Value : '$1'.
  323 +IfChangedBraced -> open_tag ifchanged_keyword Values close_tag : '$3'.
337 324 EndIfChangedBraced -> open_tag endifchanged_keyword close_tag.
338 325
339 326 IfEqualBlock -> IfEqualBraced Elements ElseBraced Elements EndIfEqualBraced : {ifequalelse, '$1', '$2', '$4'}.

0 comments on commit 165b1bf

Please sign in to comment.
Something went wrong with that request. Please try again.