|
@@ -52,6 +52,7 @@
|
|
|
format/3,
|
|
|
value_ast/5,
|
|
|
resolve_scoped_variable_ast/2,
|
|
|
+ resolve_scoped_variable_ast/3,
|
|
|
interpret_args/3,
|
|
|
unescape_string_literal/1
|
|
|
]).
|
|
@@ -61,24 +62,20 @@
|
|
|
compile(FileOrBinary, Module) ->
|
|
|
compile(FileOrBinary, Module, [verbose, report_errors]).
|
|
|
|
|
|
-compile(Binary, Module, Options0) when is_binary(Binary) ->
|
|
|
- File = "/<text>",
|
|
|
- Options = process_opts(File, Options0),
|
|
|
- Context = init_dtl_context(File, Module, Options),
|
|
|
+compile(Binary, Module, Options) when is_binary(Binary) ->
|
|
|
+ Context = process_opts(undefined, Module, Options),
|
|
|
compile(Context#dtl_context{ bin = Binary });
|
|
|
|
|
|
-compile(File, Module, Options0) ->
|
|
|
- Options = process_opts(File, Options0),
|
|
|
- Context = init_dtl_context(File, Module, Options),
|
|
|
+compile(File, Module, Options) ->
|
|
|
+ Context = process_opts(File, Module, Options),
|
|
|
print("Compile template: ~s~n", [File], Context),
|
|
|
compile(Context).
|
|
|
|
|
|
compile_dir(Dir, Module) ->
|
|
|
- compile_dir(Dir, Module, []).
|
|
|
+ compile_dir(Dir, Module, [verbose, report_errors]).
|
|
|
|
|
|
-compile_dir(Dir, Module, Options0) ->
|
|
|
- Options = process_opts(Dir, Options0),
|
|
|
- Context = init_dtl_context_dir(Dir, Module, Options),
|
|
|
+compile_dir(Dir, Module, Options) ->
|
|
|
+ Context = process_opts(Dir, Module, Options),
|
|
|
%% Find all files in Dir (recursively), matching the regex (no
|
|
|
%% files ending in "~").
|
|
|
Files = filelib:fold_files(Dir, ".+[^~]$", true, fun(F1,Acc1) -> [F1 | Acc1] end, []),
|
|
@@ -113,7 +110,7 @@ parse(Data) ->
|
|
|
parse(Data, #dtl_context{}).
|
|
|
|
|
|
format_error(no_out_dir) ->
|
|
|
- "Compiled template not saved (need outdir option)";
|
|
|
+ "Compiled template not saved (need out_dir option)";
|
|
|
format_error(unexpected_extends_tag) ->
|
|
|
"The extends tag must be at the very top of the template";
|
|
|
format_error(circular_include) ->
|
|
@@ -140,13 +137,27 @@ format_error(Other) ->
|
|
|
%% Internal functions
|
|
|
%%====================================================================
|
|
|
|
|
|
-process_opts(Source, Options) ->
|
|
|
+process_opts(File, Module, Options0) ->
|
|
|
Options1 = proplists:normalize(
|
|
|
- update_defaults(Options),
|
|
|
+ update_defaults(Options0),
|
|
|
[{aliases, [{outdir, out_dir}]}
|
|
|
]),
|
|
|
- [{compiler_options, [{source, Source}]}
|
|
|
- |compiler_opts(Options1, [])].
|
|
|
+ Source = case File of
|
|
|
+ undefined ->
|
|
|
+ filename:join(
|
|
|
+ [proplists:get_value(out_dir, Options1, ""),
|
|
|
+ Module]);
|
|
|
+ {dir, Dir} -> Dir;
|
|
|
+ _ -> File
|
|
|
+ end,
|
|
|
+ Options = [{compiler_options, [{source, lists:concat([Source, ".erl"])}]}
|
|
|
+ |compiler_opts(Options1, [])],
|
|
|
+ case File of
|
|
|
+ {dir, Dir1} ->
|
|
|
+ init_context([], Dir1, Module, Options);
|
|
|
+ _ ->
|
|
|
+ init_context([Source], filename:dirname(Source), Module, Options)
|
|
|
+ end.
|
|
|
|
|
|
compiler_opts([CompilerOption|Os], Acc)
|
|
|
when
|
|
@@ -157,7 +168,8 @@ compiler_opts([CompilerOption|Os], Acc)
|
|
|
CompilerOption =:= report_warnings;
|
|
|
CompilerOption =:= report_errors;
|
|
|
CompilerOption =:= warnings_as_errors;
|
|
|
- CompilerOption =:= verbose ->
|
|
|
+ CompilerOption =:= verbose;
|
|
|
+ CompilerOption =:= debug_info ->
|
|
|
compiler_opts(Os, [CompilerOption, {compiler_options, [CompilerOption]}|Acc]);
|
|
|
compiler_opts([O|Os], Acc) ->
|
|
|
compiler_opts(Os, [O|Acc]);
|
|
@@ -302,7 +314,7 @@ compile_to_binary(DjangoParseTree, CheckSum, Context) ->
|
|
|
end.
|
|
|
|
|
|
compile_forms(Forms, Context) ->
|
|
|
- dump_forms(Forms, Context),
|
|
|
+ maybe_debug_template(Forms, Context),
|
|
|
Options = Context#dtl_context.compiler_options,
|
|
|
case compile:forms(Forms, Options) of
|
|
|
Compiled when element(1, Compiled) =:= ok ->
|
|
@@ -331,7 +343,7 @@ maybe_write(Module, Bin, Context) ->
|
|
|
undefined ->
|
|
|
add_warning(no_out_dir, Context);
|
|
|
OutDir ->
|
|
|
- BeamFile = filename:join([OutDir, atom_to_list(Module) ++ ".beam"]),
|
|
|
+ BeamFile = filename:join([OutDir, [Module, ".beam"]]),
|
|
|
print("Template module: ~w -> ~s\n", [Module, BeamFile], Context),
|
|
|
case file:write_file(BeamFile, Bin) of
|
|
|
ok -> Context;
|
|
@@ -353,23 +365,30 @@ load_code(Module, Bin, Context) ->
|
|
|
Error -> add_warning({load, Error}, Context)
|
|
|
end.
|
|
|
|
|
|
-dump_forms(Forms, Context) ->
|
|
|
- %% undocumented option to debug the compiled Forms
|
|
|
- case proplists:get_value(debug_compiler, Context#dtl_context.all_options) of
|
|
|
+maybe_debug_template(Forms, Context) ->
|
|
|
+ %% undocumented option to debug the compiled template
|
|
|
+ case proplists:get_bool(debug_info, Context#dtl_context.all_options) of
|
|
|
+ false -> nop;
|
|
|
true ->
|
|
|
- io:format("template source:~n~s~n",
|
|
|
- [try
|
|
|
- erl_prettypr:format(erl_syntax:form_list(Forms))
|
|
|
- catch
|
|
|
- error:Err ->
|
|
|
- io_lib:format("Pretty printing failed: ~p~nContext: ~n~p~nForms: ~n~p~n",
|
|
|
- [Err, Context, Forms])
|
|
|
- end
|
|
|
- ]);
|
|
|
- _ -> nop
|
|
|
+ Options = Context#dtl_context.compiler_options,
|
|
|
+ print("Compiler options: ~p~n", [Options], Context),
|
|
|
+ try
|
|
|
+ Source = erl_prettypr:format(erl_syntax:form_list(Forms)),
|
|
|
+ File = proplists:get_value(source, Options),
|
|
|
+ io:format("Saving template source to: ~s.. ~p~n",
|
|
|
+ [File, file:write_file(File, Source)])
|
|
|
+ catch
|
|
|
+ error:Err ->
|
|
|
+ io:format("Pretty printing failed: ~p~n"
|
|
|
+ "Context: ~n~p~n"
|
|
|
+ "Forms: ~n~p~n",
|
|
|
+ [Err, Context, Forms])
|
|
|
+ end
|
|
|
end.
|
|
|
|
|
|
-init_context(IsCompilingDir, ParseTrail, DefDir, Module, Options) ->
|
|
|
+init_context(ParseTrail, DefDir, Module, Options) when is_list(Module) ->
|
|
|
+ init_context(ParseTrail, DefDir, list_to_atom(Module), Options);
|
|
|
+init_context(ParseTrail, DefDir, Module, Options) ->
|
|
|
Ctx = #dtl_context{},
|
|
|
Context = #dtl_context{
|
|
|
all_options = Options,
|
|
@@ -392,7 +411,7 @@ init_context(IsCompilingDir, ParseTrail, DefDir, Module, Options) ->
|
|
|
force_recompile = proplists:get_bool(force_recompile, Options),
|
|
|
locale = proplists:get_value(locale, Options, Ctx#dtl_context.locale),
|
|
|
verbose = proplists:get_value(verbose, Options, Ctx#dtl_context.verbose),
|
|
|
- is_compiling_dir = IsCompilingDir,
|
|
|
+ is_compiling_dir = ParseTrail == [],
|
|
|
extension_module = proplists:get_value(extension_module, Options, Ctx#dtl_context.extension_module),
|
|
|
scanner_module = proplists:get_value(scanner_module, Options, Ctx#dtl_context.scanner_module),
|
|
|
record_info = [{R, lists:zip(I, lists:seq(2, length(I) + 1))}
|
|
@@ -436,16 +455,6 @@ get_error_info_opts(Class, Options) ->
|
|
|
{Value, proplists:get_bool(Key, Options)}
|
|
|
end || Flag <- Flags].
|
|
|
|
|
|
-init_dtl_context(File, Module, Options) when is_list(Module) ->
|
|
|
- init_dtl_context(File, list_to_atom(Module), Options);
|
|
|
-init_dtl_context(File, Module, Options) ->
|
|
|
- init_context(false, [File], filename:dirname(File), Module, Options).
|
|
|
-
|
|
|
-init_dtl_context_dir(Dir, Module, Options) when is_list(Module) ->
|
|
|
- init_dtl_context_dir(Dir, list_to_atom(Module), Options);
|
|
|
-init_dtl_context_dir(Dir, Module, Options) ->
|
|
|
- init_context(true, [], Dir, Module, Options).
|
|
|
-
|
|
|
init_treewalker(Context) ->
|
|
|
TreeWalker = #treewalker{},
|
|
|
case call_extension(Context, init_treewalker, [TreeWalker]) of
|
|
@@ -603,22 +612,24 @@ reset_token_stream(Ts, [_|S]) ->
|
|
|
|
|
|
|
|
|
custom_tags_ast(CustomTags, Context, TreeWalker) ->
|
|
|
- {{CustomTagsClauses, CustomTagsInfo}, TreeWalker1} = custom_tags_clauses_ast(CustomTags, Context, TreeWalker),
|
|
|
- %% This doesn't work since erl_syntax:revert/1 chokes on the airity_qualifier in the -compile attribute..
|
|
|
- %% bug report sent to the erlang-bugs mailing list.
|
|
|
- %% {{erl_syntax:form_list(
|
|
|
- %% [erl_syntax:attribute(
|
|
|
- %% erl_syntax:atom(compile),
|
|
|
- %% [erl_syntax:tuple(
|
|
|
- %% [erl_syntax:atom(nowarn_unused_function),
|
|
|
- %% erl_syntax:arity_qualifier(
|
|
|
- %% erl_syntax:atom(render_tag),
|
|
|
- %% erl_syntax:integer(3))])]),
|
|
|
- %% erl_syntax:function(erl_syntax:atom(render_tag), CustomTagsClauses)]),
|
|
|
- {{erl_syntax:function(erl_syntax:atom(render_tag), CustomTagsClauses),
|
|
|
- CustomTagsInfo},
|
|
|
- TreeWalker1}.
|
|
|
+ %% avoid adding the render_tag/3 fun if it isn't used,
|
|
|
+ %% since we can't add a -compile({nowarn_unused_function, render_tag/3}).
|
|
|
+ %% attribute due to a bug in syntax_tools.
|
|
|
+ case custom_tags_clauses_ast(CustomTags, Context, TreeWalker) of
|
|
|
+ skip ->
|
|
|
+ {{erl_syntax:comment(
|
|
|
+ ["% render_tag/3 is not used in this template."]),
|
|
|
+ #ast_info{}},
|
|
|
+ TreeWalker};
|
|
|
+ {{CustomTagsClauses, CustomTagsInfo}, TreeWalker1} ->
|
|
|
+ {{erl_syntax:function(
|
|
|
+ erl_syntax:atom(render_tag),
|
|
|
+ CustomTagsClauses),
|
|
|
+ CustomTagsInfo},
|
|
|
+ TreeWalker1}
|
|
|
+ end.
|
|
|
|
|
|
+custom_tags_clauses_ast([], _Context, _TreeWalker) -> skip;
|
|
|
custom_tags_clauses_ast(CustomTags, Context, TreeWalker) ->
|
|
|
custom_tags_clauses_ast1(CustomTags, [], [], #ast_info{}, Context, TreeWalker).
|
|
|
|
|
@@ -783,16 +794,16 @@ forms(Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}, Chec
|
|
|
Render1FunctionAst = erl_syntax:function(
|
|
|
erl_syntax:atom(render),
|
|
|
[erl_syntax:clause(
|
|
|
- [erl_syntax:variable("_Variables")],
|
|
|
+ [erl_syntax:variable("Variables")],
|
|
|
none,
|
|
|
[erl_syntax:application(
|
|
|
none, erl_syntax:atom(render),
|
|
|
- [erl_syntax:variable("_Variables"),
|
|
|
+ [erl_syntax:variable("Variables"),
|
|
|
erl_syntax:list([])])
|
|
|
])
|
|
|
]),
|
|
|
Function2 = erl_syntax:application(none, erl_syntax:atom(render_internal),
|
|
|
- [erl_syntax:variable("_Variables"), erl_syntax:variable("RenderOptions")]),
|
|
|
+ [erl_syntax:variable("Variables"), erl_syntax:variable("RenderOptions")]),
|
|
|
ClauseOk = erl_syntax:clause([erl_syntax:variable("Val")],
|
|
|
none,
|
|
|
[erl_syntax:tuple([erl_syntax:atom(ok), erl_syntax:variable("Val")])]),
|
|
@@ -802,7 +813,7 @@ forms(Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}, Chec
|
|
|
Render2FunctionAst = erl_syntax:function(
|
|
|
erl_syntax:atom(render),
|
|
|
[erl_syntax:clause(
|
|
|
- [erl_syntax:variable("_Variables"),
|
|
|
+ [erl_syntax:variable("Variables"),
|
|
|
erl_syntax:variable("RenderOptions")],
|
|
|
none,
|
|
|
[erl_syntax:try_expr([Function2], [ClauseOk], [ClauseCatch])])
|
|
@@ -1460,12 +1471,18 @@ resolve_variable_ast1({variable, {identifier, Pos, VarName}}, Context, TreeWalke
|
|
|
{{VarValue, #ast_info{ var_names=[VarName] }}, TreeWalker}.
|
|
|
|
|
|
resolve_scoped_variable_ast(VarName, Context) ->
|
|
|
- lists:foldl(fun(Scope, Value) ->
|
|
|
- case Value of
|
|
|
- undefined -> proplists:get_value(VarName, Scope);
|
|
|
- _ -> Value
|
|
|
- end
|
|
|
- end, undefined, Context#dtl_context.local_scopes).
|
|
|
+ resolve_scoped_variable_ast(VarName, Context, undefined).
|
|
|
+
|
|
|
+resolve_scoped_variable_ast(VarName, Context, Default) ->
|
|
|
+ lists:foldl(
|
|
|
+ fun (Scope, Res) ->
|
|
|
+ if Res =:= Default ->
|
|
|
+ proplists:get_value(VarName, Scope, Default);
|
|
|
+ true -> Res
|
|
|
+ end
|
|
|
+ end,
|
|
|
+ Default,
|
|
|
+ Context#dtl_context.local_scopes).
|
|
|
|
|
|
format(Ast, Context, TreeWalker) ->
|
|
|
auto_escape(format_number_ast(Ast), Context, TreeWalker).
|
|
@@ -1566,59 +1583,109 @@ to_list_ast(Value, IsReversed, Context, TreeWalker) ->
|
|
|
end.
|
|
|
|
|
|
for_loop_ast(IteratorList, LoopValue, IsReversed, Contents, {EmptyContentsAst, EmptyContentsInfo}, Context, TreeWalker) ->
|
|
|
- Vars = lists:map(fun({identifier, _, Iterator}) ->
|
|
|
- erl_syntax:variable(lists:concat(["Var_", Iterator]))
|
|
|
- end, IteratorList),
|
|
|
- {{InnerAst, Info}, TreeWalker1} =
|
|
|
+ %% create unique namespace for this instance
|
|
|
+ Level = length(Context#dtl_context.local_scopes),
|
|
|
+ {Row, Col} = element(2, hd(IteratorList)),
|
|
|
+ ForId = lists:concat(["/", Level, "_", Row, ":", Col]),
|
|
|
+
|
|
|
+ Counters = lists:concat(["Counters", ForId]),
|
|
|
+ Vars = lists:concat(["Vars", ForId]),
|
|
|
+
|
|
|
+ %% setup
|
|
|
+ VarScope = lists:map(
|
|
|
+ fun({identifier, {R, C}, Iterator}) ->
|
|
|
+ {Iterator, erl_syntax:variable(
|
|
|
+ lists:concat(["Var_", Iterator,
|
|
|
+ "/", Level, "_", R, ":", C
|
|
|
+ ]))}
|
|
|
+ end, IteratorList),
|
|
|
+ {Iterators, IteratorVars} = lists:unzip(VarScope),
|
|
|
+ IteratorCount = length(IteratorVars),
|
|
|
+
|
|
|
+ {{LoopBodyAst, Info}, TreeWalker1} =
|
|
|
body_ast(
|
|
|
Contents,
|
|
|
Context#dtl_context{
|
|
|
local_scopes =
|
|
|
- [[{'forloop', erl_syntax:variable("Counters")}
|
|
|
- | lists:map(
|
|
|
- fun({identifier, _, Iterator}) ->
|
|
|
- {Iterator,
|
|
|
- erl_syntax:variable(
|
|
|
- lists:concat(["Var_", Iterator]))}
|
|
|
- end, IteratorList)
|
|
|
- ]| Context#dtl_context.local_scopes]
|
|
|
+ [[{'forloop', erl_syntax:variable(Counters)} | VarScope]
|
|
|
+ | Context#dtl_context.local_scopes]
|
|
|
},
|
|
|
TreeWalker),
|
|
|
- CounterAst = erl_syntax:application(erl_syntax:atom(erlydtl_runtime),
|
|
|
- erl_syntax:atom(increment_counter_stats), [erl_syntax:variable("Counters")]),
|
|
|
+
|
|
|
+ CounterAst = erl_syntax:application(
|
|
|
+ erl_syntax:atom(erlydtl_runtime),
|
|
|
+ erl_syntax:atom(increment_counter_stats),
|
|
|
+ [erl_syntax:variable(Counters)]),
|
|
|
|
|
|
{{LoopValueAst, LoopValueInfo}, TreeWalker2} = value_ast(LoopValue, false, true, Context, TreeWalker1),
|
|
|
|
|
|
LoopValueAst0 = to_list_ast(LoopValueAst, erl_syntax:atom(IsReversed), Context, TreeWalker2),
|
|
|
|
|
|
- CounterVars0 = case resolve_scoped_variable_ast('forloop', Context) of
|
|
|
- undefined ->
|
|
|
- erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(init_counter_stats), [LoopValueAst0]);
|
|
|
- Value ->
|
|
|
- erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(init_counter_stats), [LoopValueAst0, Value])
|
|
|
- end,
|
|
|
+ ParentLoop = resolve_scoped_variable_ast('forloop', Context, erl_syntax:atom(undefined)),
|
|
|
+
|
|
|
+ %% call for loop
|
|
|
{{erl_syntax:case_expr(
|
|
|
erl_syntax:application(
|
|
|
erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('forloop'),
|
|
|
- [erl_syntax:fun_expr([
|
|
|
- erl_syntax:clause([erl_syntax:tuple(Vars), erl_syntax:variable("Counters")], none,
|
|
|
- [erl_syntax:tuple([InnerAst, CounterAst])]),
|
|
|
- erl_syntax:clause(case Vars of [H] -> [H, erl_syntax:variable("Counters")];
|
|
|
- _ -> [erl_syntax:list(Vars), erl_syntax:variable("Counters")] end, none,
|
|
|
- [erl_syntax:tuple([InnerAst, CounterAst])])
|
|
|
- ]),
|
|
|
- CounterVars0, LoopValueAst0]),
|
|
|
+ [erl_syntax:fun_expr(
|
|
|
+ [erl_syntax:clause(
|
|
|
+ [erl_syntax:variable(Vars),
|
|
|
+ erl_syntax:variable(Counters)],
|
|
|
+ none,
|
|
|
+ [erl_syntax:match_expr(
|
|
|
+ erl_syntax:tuple(IteratorVars),
|
|
|
+ erl_syntax:if_expr(
|
|
|
+ [erl_syntax:clause(
|
|
|
+ [], [erl_syntax:application(none, erl_syntax:atom(is_tuple), [erl_syntax:variable(Vars)]),
|
|
|
+ erl_syntax:infix_expr(
|
|
|
+ erl_syntax:application(none, erl_syntax:atom(size), [erl_syntax:variable(Vars)]),
|
|
|
+ erl_syntax:operator('=='),
|
|
|
+ erl_syntax:integer(IteratorCount))
|
|
|
+ ],
|
|
|
+ [erl_syntax:variable(Vars)])
|
|
|
+ | if IteratorCount > 1 ->
|
|
|
+ [erl_syntax:clause(
|
|
|
+ [], [erl_syntax:application(none, erl_syntax:atom(is_list), [erl_syntax:variable(Vars)]),
|
|
|
+ erl_syntax:infix_expr(
|
|
|
+ erl_syntax:application(none, erl_syntax:atom(length), [erl_syntax:variable(Vars)]),
|
|
|
+ erl_syntax:operator('=='),
|
|
|
+ erl_syntax:integer(IteratorCount))
|
|
|
+ ],
|
|
|
+ [erl_syntax:application(none, erl_syntax:atom(list_to_tuple), [erl_syntax:variable(Vars)])]),
|
|
|
+ erl_syntax:clause(
|
|
|
+ [], [erl_syntax:atom(true)],
|
|
|
+ [erl_syntax:application(
|
|
|
+ none, erl_syntax:atom(throw),
|
|
|
+ [erl_syntax:tuple(
|
|
|
+ [erl_syntax:atom(for_loop),
|
|
|
+ erl_syntax:abstract(Iterators),
|
|
|
+ erl_syntax:variable(Vars)])
|
|
|
+ ])
|
|
|
+ ])
|
|
|
+ ];
|
|
|
+ true ->
|
|
|
+ [erl_syntax:clause(
|
|
|
+ [], [erl_syntax:atom(true)],
|
|
|
+ [erl_syntax:tuple([erl_syntax:variable(Vars)])])
|
|
|
+ ]
|
|
|
+ end
|
|
|
+ ])),
|
|
|
+ erl_syntax:tuple([LoopBodyAst, CounterAst])
|
|
|
+ ])
|
|
|
+ ]),
|
|
|
+ LoopValueAst0, ParentLoop
|
|
|
+ ]),
|
|
|
+ %% of
|
|
|
[erl_syntax:clause(
|
|
|
- [erl_syntax:tuple([erl_syntax:underscore(),
|
|
|
- erl_syntax:list([erl_syntax:tuple([erl_syntax:atom(counter), erl_syntax:integer(1)])],
|
|
|
- erl_syntax:underscore())])],
|
|
|
- none, [EmptyContentsAst]),
|
|
|
+ [erl_syntax:atom(empty)],
|
|
|
+ none,
|
|
|
+ [EmptyContentsAst]),
|
|
|
erl_syntax:clause(
|
|
|
[erl_syntax:tuple([erl_syntax:variable("L"), erl_syntax:underscore()])],
|
|
|
- none, [erl_syntax:variable("L")])]
|
|
|
- ),
|
|
|
- merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)
|
|
|
- }, TreeWalker2}.
|
|
|
+ none, [erl_syntax:variable("L")])
|
|
|
+ ]),
|
|
|
+ merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)},
|
|
|
+ TreeWalker2}.
|
|
|
|
|
|
ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
|
|
|
Info = merge_info(IfContentsInfo, ElseContentsInfo),
|
|
@@ -1645,38 +1712,43 @@ ifchanged_contents_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsA
|
|
|
|
|
|
|
|
|
cycle_ast(Names, Context, TreeWalker) ->
|
|
|
- {NamesTuple, VarNames} = lists:mapfoldl(fun
|
|
|
- ({string_literal, _, Str}, VarNamesAcc) ->
|
|
|
- {{S, _}, _} = string_ast(unescape_string_literal(Str), Context, TreeWalker),
|
|
|
- {S, VarNamesAcc};
|
|
|
- ({variable, _}=Var, VarNamesAcc) ->
|
|
|
- {{V, #ast_info{ var_names=[VarName] }}, _} = resolve_variable_ast(Var, Context, TreeWalker, true),
|
|
|
- {V, [VarName|VarNamesAcc]};
|
|
|
- ({number_literal, _, Num}, VarNamesAcc) ->
|
|
|
- {format(erl_syntax:integer(Num), Context, TreeWalker), VarNamesAcc};
|
|
|
- (_, VarNamesAcc) ->
|
|
|
- {[], VarNamesAcc}
|
|
|
- end, [], Names),
|
|
|
+ {NamesTuple, VarNames}
|
|
|
+ = lists:mapfoldl(
|
|
|
+ fun ({string_literal, _, Str}, VarNamesAcc) ->
|
|
|
+ {{S, _}, _} = string_ast(unescape_string_literal(Str), Context, TreeWalker),
|
|
|
+ {S, VarNamesAcc};
|
|
|
+ ({variable, _}=Var, VarNamesAcc) ->
|
|
|
+ {{V, #ast_info{ var_names=[VarName] }}, _} = resolve_variable_ast(Var, Context, TreeWalker, true),
|
|
|
+ {V, [VarName|VarNamesAcc]};
|
|
|
+ ({number_literal, _, Num}, VarNamesAcc) ->
|
|
|
+ {format(erl_syntax:integer(Num), Context, TreeWalker), VarNamesAcc};
|
|
|
+ (_, VarNamesAcc) ->
|
|
|
+ {[], VarNamesAcc}
|
|
|
+ end, [], Names),
|
|
|
{{erl_syntax:application(
|
|
|
erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
|
|
|
- [erl_syntax:tuple(NamesTuple), erl_syntax:variable("Counters")]), #ast_info{ var_names = VarNames }}, TreeWalker}.
|
|
|
+ [erl_syntax:tuple(NamesTuple), resolve_scoped_variable_ast('forloop', Context)]),
|
|
|
+ #ast_info{ var_names = VarNames }},
|
|
|
+ TreeWalker}.
|
|
|
|
|
|
%% Older Django templates treat cycle with comma-delimited elements as strings
|
|
|
cycle_compat_ast(Names, Context, TreeWalker) ->
|
|
|
- NamesTuple = lists:map(fun
|
|
|
- ({identifier, _, X}) ->
|
|
|
- {{S, _}, _} = string_ast(X, Context, TreeWalker),
|
|
|
- S
|
|
|
- end, Names),
|
|
|
+ NamesTuple = lists:map(
|
|
|
+ fun ({identifier, _, X}) ->
|
|
|
+ {{S, _}, _} = string_ast(X, Context, TreeWalker),
|
|
|
+ S
|
|
|
+ end, Names),
|
|
|
{{erl_syntax:application(
|
|
|
erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
|
|
|
- [erl_syntax:tuple(NamesTuple), erl_syntax:variable("Counters")]), #ast_info{}}, TreeWalker}.
|
|
|
+ [erl_syntax:tuple(NamesTuple), resolve_scoped_variable_ast('forloop', Context)]),
|
|
|
+ #ast_info{}},
|
|
|
+ TreeWalker}.
|
|
|
|
|
|
now_ast(FormatString, Context, TreeWalker) ->
|
|
|
- % Note: we can't use unescape_string_literal here
|
|
|
- % because we want to allow escaping in the format string.
|
|
|
- % We only want to remove the surrounding escapes,
|
|
|
- % i.e. \"foo\" becomes "foo"
|
|
|
+ %% Note: we can't use unescape_string_literal here
|
|
|
+ %% because we want to allow escaping in the format string.
|
|
|
+ %% We only want to remove the surrounding escapes,
|
|
|
+ %% i.e. \"foo\" becomes "foo"
|
|
|
UnescapeOuter = string:strip(FormatString, both, 34),
|
|
|
{{StringAst, Info}, TreeWalker1} = string_ast(UnescapeOuter, Context, TreeWalker),
|
|
|
{{erl_syntax:application(
|