erlydtl_beam_compiler.erl 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. %%%-------------------------------------------------------------------
  2. %%% File: erlydtl_beam_compiler.erl
  3. %%% @author Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
  4. %%% @author Evan Miller <emmiller@gmail.com>
  5. %%% @author Andreas Stenius <kaos@astekk.se>
  6. %%% @copyright 2008 Roberto Saccon, Evan Miller
  7. %%% @copyright 2014 Andreas Stenius
  8. %%% @doc
  9. %%% ErlyDTL template compiler for beam targets.
  10. %%% @end
  11. %%%
  12. %%% The MIT License
  13. %%%
  14. %%% Copyright (c) 2007 Roberto Saccon, Evan Miller
  15. %%% Copyright (c) 2014 Andreas Stenius
  16. %%%
  17. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  18. %%% of this software and associated documentation files (the "Software"), to deal
  19. %%% in the Software without restriction, including without limitation the rights
  20. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. %%% copies of the Software, and to permit persons to whom the Software is
  22. %%% furnished to do so, subject to the following conditions:
  23. %%%
  24. %%% The above copyright notice and this permission notice shall be included in
  25. %%% all copies or substantial portions of the Software.
  26. %%%
  27. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. %%% THE SOFTWARE.
  34. %%%
  35. %%% @since 2007-12-16 by Roberto Saccon, Evan Miller
  36. %%% @since 2014 by Andreas Stenius
  37. %%%-------------------------------------------------------------------
  38. -module(erlydtl_beam_compiler).
  39. -author('rsaccon@gmail.com').
  40. -author('emmiller@gmail.com').
  41. -author('Andreas Stenius <kaos@astekk.se>').
  42. %% --------------------------------------------------------------------
  43. %% Definitions
  44. %% --------------------------------------------------------------------
  45. -export([compile/3, compile_dir/2, format_error/1]).
  46. %% internal use
  47. -export([
  48. is_up_to_date/2,
  49. format/1,
  50. value_ast/4,
  51. interpret_args/2
  52. ]).
  53. -import(erlydtl_compiler, [parse_file/2, do_parse_template/2]).
  54. -import(erlydtl_compiler_utils,
  55. [unescape_string_literal/1, full_path/2, push_scope/2,
  56. restore_scope/2, begin_scope/1, begin_scope/2, end_scope/4,
  57. empty_scope/0, get_current_file/1, add_errors/2,
  58. add_warnings/2, merge_info/2, call_extension/3,
  59. init_treewalker/1, resolve_variable/2, resolve_variable/3,
  60. reset_parse_trail/2, load_library/3, load_library/4,
  61. shorten_filename/2, push_auto_escape/2, pop_auto_escape/1]).
  62. -include_lib("merl/include/merl.hrl").
  63. -include("erlydtl_ext.hrl").
  64. %% --------------------------------------------------------------------
  65. %% API
  66. %% --------------------------------------------------------------------
  67. compile(DjangoParseTree, CheckSum, Context) ->
  68. compile_to_binary(DjangoParseTree, CheckSum, Context).
  69. compile_dir(Dir, Context) ->
  70. do_compile_dir(Dir, Context).
  71. format_error(no_out_dir) ->
  72. "Compiled template not saved (need out_dir option)";
  73. format_error(unexpected_extends_tag) ->
  74. "The extends tag must be at the very top of the template";
  75. format_error(circular_include) ->
  76. "Circular file inclusion!";
  77. format_error({write_file, Error}) ->
  78. io_lib:format(
  79. "Failed to write file: ~s",
  80. [file:format_error(Error)]);
  81. format_error(compile_beam) ->
  82. "Failed to compile template to BEAM code";
  83. format_error({unknown_filter, Name, Arity}) ->
  84. io_lib:format("Unknown filter '~p' (arity ~p)", [Name, Arity]);
  85. format_error({filter_args, Name, {Mod, Fun}, Arity}) ->
  86. io_lib:format("Wrong number of arguments to filter '~p' (~p:~p): ~p", [Name, Mod, Fun, Arity]);
  87. format_error({missing_tag, Name, {Mod, Fun}}) ->
  88. io_lib:format("Custom tag '~p' not exported (~p:~p)", [Name, Mod, Fun]);
  89. format_error({bad_tag, Name, {Mod, Fun}, Arity}) ->
  90. io_lib:format("Invalid tag '~p' (~p:~p/~p)", [Name, Mod, Fun, Arity]);
  91. format_error({load_code, Error}) ->
  92. io_lib:format("Failed to load BEAM code: ~p", [Error]);
  93. format_error({reserved_variable, ReservedName}) ->
  94. io_lib:format("Variable '~s' is reserved for internal use.", [ReservedName]);
  95. format_error({translation_fun, Fun}) ->
  96. io_lib:format("Invalid translation function: ~s~n",
  97. [if is_function(Fun) ->
  98. Info = erlang:fun_info(Fun),
  99. io_lib:format("~s:~s/~p", [proplists:get_value(K, Info) || K <- [module, name, arity]]);
  100. true -> io_lib:format("~p", [Fun])
  101. end]);
  102. format_error(Error) ->
  103. erlydtl_compiler:format_error(Error).
  104. %%====================================================================
  105. %% Internal functions
  106. %%====================================================================
  107. do_compile_dir(Dir, Context) ->
  108. %% Find all files in Dir (recursively), matching the regex (no
  109. %% files ending in "~").
  110. Files = filelib:fold_files(Dir, ".+[^~]$", true, fun(F1,Acc1) -> [F1 | Acc1] end, []),
  111. {ParserResults,
  112. #dtl_context{ errors=#error_info{ list=ParserErrors } }=Context1}
  113. = lists:foldl(
  114. fun (File, {ResultAcc, Ctx}) ->
  115. case filename:basename(File) of
  116. "."++_ ->
  117. {ResultAcc, Ctx};
  118. _ ->
  119. FilePath = filename:absname(File),
  120. case filelib:is_dir(FilePath) of
  121. true ->
  122. {ResultAcc, Ctx};
  123. false ->
  124. case parse_file(FilePath, Ctx) of
  125. up_to_date -> {ResultAcc, Ctx};
  126. {ok, DjangoParseTree, CheckSum} ->
  127. {[{FilePath, DjangoParseTree, CheckSum}|ResultAcc], Ctx};
  128. {error, Reason} -> {ResultAcc, ?ERR(Reason, Ctx)}
  129. end
  130. end
  131. end
  132. end,
  133. {[], Context},
  134. Files),
  135. if length(ParserErrors) == 0 ->
  136. compile_multiple_to_binary(Dir, ParserResults, Context1);
  137. true -> Context1
  138. end.
  139. compile_multiple_to_binary(Dir, ParserResults, Context) ->
  140. MatchAst = options_match_ast(Context),
  141. {Functions,
  142. {AstInfo,
  143. #treewalker{
  144. context=#dtl_context{
  145. errors=#error_info{ list=Errors }
  146. }=Context1 } }
  147. } = lists:mapfoldl(
  148. fun ({File, DjangoParseTree, CheckSum},
  149. {AstInfo, #treewalker{ context=Ctx }=TreeWalker}) ->
  150. try
  151. FilePath = full_path(File, Ctx#dtl_context.doc_root),
  152. {{BodyAst, BodyInfo}, TreeWalker1} = with_dependency(
  153. {FilePath, CheckSum},
  154. body_ast(DjangoParseTree, TreeWalker)),
  155. FunctionName = filename:rootname(filename:basename(File)),
  156. FunctionDefs = ?Q(["'@func'(Variables) -> _@func(Variables, []).",
  157. "'@func'(_Variables, RenderOptions) ->",
  158. " _@MatchAst, _@body."],
  159. [{func, erl_syntax:atom(FunctionName)},
  160. {body, stringify(BodyAst, Ctx)}]),
  161. {{FunctionName, FunctionDefs}, {merge_info(AstInfo, BodyInfo), TreeWalker1}}
  162. catch
  163. throw:Error ->
  164. {error, {AstInfo, TreeWalker#treewalker{ context=?ERR(Error, Ctx) }}}
  165. end
  166. end,
  167. {#ast_info{}, init_treewalker(Context)},
  168. ParserResults),
  169. if length(Errors) == 0 ->
  170. Forms = custom_forms(Dir, Context1#dtl_context.module, Functions, AstInfo),
  171. compile_forms(Forms, Context1);
  172. true ->
  173. Context1
  174. end.
  175. compile_to_binary(DjangoParseTree, CheckSum, Context) ->
  176. try body_ast(DjangoParseTree, init_treewalker(Context)) of
  177. {{BodyAst, BodyInfo}, BodyTreeWalker} ->
  178. try custom_tags_ast(BodyInfo#ast_info.custom_tags, BodyTreeWalker) of
  179. {CustomTags,
  180. #treewalker{
  181. context=#dtl_context{
  182. errors=#error_info{ list=Errors }
  183. } }=CustomTagsTreeWalker}
  184. when length(Errors) == 0 ->
  185. Forms = forms(
  186. {BodyAst, BodyInfo},
  187. CustomTags, CheckSum,
  188. CustomTagsTreeWalker),
  189. compile_forms(Forms, CustomTagsTreeWalker#treewalker.context);
  190. {_, #treewalker{ context=Context1 }} ->
  191. Context1
  192. catch
  193. throw:Error -> ?ERR(Error, BodyTreeWalker#treewalker.context)
  194. end
  195. catch
  196. throw:Error -> ?ERR(Error, Context)
  197. end.
  198. compile_forms(Forms, Context) ->
  199. maybe_debug_template(Forms, Context),
  200. Options = Context#dtl_context.compiler_options,
  201. case compile:forms(Forms, Options) of
  202. Compiled when element(1, Compiled) =:= ok ->
  203. [ok, Module, Bin|Info] = tuple_to_list(Compiled),
  204. lists:foldl(
  205. fun (F, C) -> F(Module, Bin, C) end,
  206. Context#dtl_context{ bin=Bin },
  207. [fun maybe_write/3,
  208. fun maybe_load/3,
  209. fun (_, _, C) ->
  210. case Info of
  211. [Ws] when length(Ws) > 0 ->
  212. add_warnings(Ws, C);
  213. _ -> C
  214. end
  215. end
  216. ]);
  217. error ->
  218. ?ERR(compile_beam, Context);
  219. {error, Es, Ws} ->
  220. add_warnings(Ws, add_errors(Es, Context))
  221. end.
  222. maybe_write(Module, Bin, Context) ->
  223. case proplists:get_value(out_dir, Context#dtl_context.all_options) of
  224. false -> Context;
  225. undefined ->
  226. ?WARN(no_out_dir, Context);
  227. OutDir ->
  228. BeamFile = filename:join([OutDir, [Module, ".beam"]]),
  229. ?LOG_INFO("Template module: ~w -> ~s\n", [Module, BeamFile], Context),
  230. case file:write_file(BeamFile, Bin) of
  231. ok -> Context;
  232. {error, Reason} ->
  233. ?ERR({write_file, Reason}, Context)
  234. end
  235. end.
  236. maybe_load(Module, Bin, Context) ->
  237. case proplists:get_bool(no_load, Context#dtl_context.all_options) of
  238. true -> Context;
  239. false -> load_code(Module, Bin, Context)
  240. end.
  241. load_code(Module, Bin, Context) ->
  242. code:purge(Module),
  243. case code:load_binary(Module, atom_to_list(Module) ++ ".erl", Bin) of
  244. {module, Module} -> Context;
  245. Error -> ?WARN({load_code, Error}, Context)
  246. end.
  247. maybe_debug_template(Forms, Context) ->
  248. case proplists:get_bool(debug_compiler, Context#dtl_context.all_options) of
  249. false -> nop;
  250. true ->
  251. Options = Context#dtl_context.compiler_options,
  252. ?LOG_DEBUG("Compiler options: ~p~n", [Options], Context),
  253. try
  254. Source = erl_prettypr:format(erl_syntax:form_list(Forms)),
  255. SourceFile = lists:concat(
  256. [proplists:get_value(source, Options),".erl"]),
  257. File = case proplists:get_value(
  258. debug_root,
  259. Context#dtl_context.all_options) of
  260. false -> undefined;
  261. undefined -> SourceFile;
  262. Dir ->
  263. Abs = filename:absname(
  264. shorten_filename(
  265. SourceFile,
  266. Context#dtl_context.doc_root),
  267. Dir),
  268. case filelib:is_dir(Dir) of
  269. true -> Abs;
  270. false ->
  271. case filelib:ensure_dir(Abs) of
  272. ok -> Abs;
  273. {error, Reason} ->
  274. io:format(
  275. "Failed to ensure directories for file '~s': ~p~n",
  276. [Abs, Reason]),
  277. undefined
  278. end
  279. end
  280. end,
  281. if File =/= undefined ->
  282. io:format("Saving template source to: ~s.. ~p~n",
  283. [File, file:write_file(File, Source)]);
  284. true -> ok
  285. end
  286. catch
  287. error:Err ->
  288. io:format("Pretty printing failed: ~p~n"
  289. "Context: ~n~p~n"
  290. "Forms: ~n~p~n",
  291. [Err, Context, Forms])
  292. end
  293. end.
  294. is_up_to_date(CheckSum, Context) ->
  295. Module = Context#dtl_context.module,
  296. {M, F} = Context#dtl_context.reader,
  297. case catch Module:source() of
  298. {_, CheckSum} ->
  299. case catch Module:dependencies() of
  300. L when is_list(L) ->
  301. RecompileList = lists:foldl(
  302. fun ({XFile, XCheckSum}, Acc) ->
  303. case catch M:F(XFile) of
  304. {ok, Data} ->
  305. case binary_to_list(erlang:md5(Data)) of
  306. XCheckSum ->
  307. Acc;
  308. _ ->
  309. [recompile | Acc]
  310. end;
  311. _ ->
  312. [recompile | Acc]
  313. end
  314. end, [], L),
  315. case RecompileList of
  316. [] -> true;
  317. _ -> false
  318. end;
  319. _ ->
  320. false
  321. end;
  322. _ ->
  323. false
  324. end.
  325. %%====================================================================
  326. %% AST functions
  327. %%====================================================================
  328. custom_tags_ast(CustomTags, TreeWalker) ->
  329. %% avoid adding the render_tag/3 fun if it isn't used,
  330. %% since we can't add a -compile({nowarn_unused_function, render_tag/3}).
  331. %% attribute due to a bug in syntax_tools.
  332. case custom_tags_clauses_ast(CustomTags, TreeWalker) of
  333. skip ->
  334. {{erl_syntax:comment(
  335. ["% render_tag/3 is not used in this template."]),
  336. #ast_info{}},
  337. TreeWalker};
  338. {{CustomTagsClauses, CustomTagsInfo}, TreeWalker1} ->
  339. {{erl_syntax:function(
  340. erl_syntax:atom(render_tag),
  341. CustomTagsClauses),
  342. CustomTagsInfo},
  343. TreeWalker1}
  344. end.
  345. custom_tags_clauses_ast([], _TreeWalker) -> skip;
  346. custom_tags_clauses_ast(CustomTags, TreeWalker) ->
  347. custom_tags_clauses_ast1(CustomTags, [], [], #ast_info{}, TreeWalker).
  348. custom_tags_clauses_ast1([], _ExcludeTags, ClauseAcc, InfoAcc, TreeWalker) ->
  349. {{DefaultAst, DefaultInfo}, TreeWalker1} =
  350. case call_extension(TreeWalker, custom_tag_ast, [TreeWalker]) of
  351. undefined ->
  352. {{?Q("(_TagName, _, _) -> []"), InfoAcc}, TreeWalker};
  353. {{ExtAst, ExtInfo}, ExtTreeWalker} ->
  354. Clause = ?Q("(TagName, _Variables, RenderOptions) -> _@tag",
  355. [{tag, options_match_ast(ExtTreeWalker) ++ [ExtAst]}]),
  356. {{Clause, merge_info(ExtInfo, InfoAcc)}, ExtTreeWalker}
  357. end,
  358. {{lists:reverse([DefaultAst|ClauseAcc]), DefaultInfo}, TreeWalker1};
  359. custom_tags_clauses_ast1([Tag|CustomTags], ExcludeTags, ClauseAcc, InfoAcc, TreeWalker) ->
  360. case lists:member(Tag, ExcludeTags) of
  361. true ->
  362. custom_tags_clauses_ast1(CustomTags, ExcludeTags, ClauseAcc, InfoAcc, TreeWalker);
  363. false ->
  364. Context = TreeWalker#treewalker.context,
  365. CustomTagFile = full_path(Tag, Context#dtl_context.custom_tags_dir),
  366. case filelib:is_file(CustomTagFile) of
  367. true ->
  368. case parse_file(CustomTagFile, Context) of
  369. {ok, DjangoParseTree, CheckSum} ->
  370. {{BodyAst, BodyAstInfo}, TreeWalker1} = with_dependency(
  371. {CustomTagFile, CheckSum},
  372. body_ast(DjangoParseTree, TreeWalker)),
  373. MatchAst = options_match_ast(TreeWalker1),
  374. Clause = ?Q("(_@Tag@, _Variables, RenderOptions) -> _@MatchAst, _@BodyAst"),
  375. custom_tags_clauses_ast1(
  376. CustomTags, [Tag|ExcludeTags], [Clause|ClauseAcc],
  377. merge_info(BodyAstInfo, InfoAcc), TreeWalker1);
  378. {error, Reason} ->
  379. empty_ast(?ERR(Reason, TreeWalker))
  380. end;
  381. false ->
  382. case call_extension(TreeWalker, custom_tag_ast, [Tag, TreeWalker]) of
  383. undefined ->
  384. custom_tags_clauses_ast1(
  385. CustomTags, [Tag | ExcludeTags],
  386. ClauseAcc, InfoAcc, TreeWalker);
  387. {{Ast, Info}, TW} ->
  388. Clause = ?Q("(_@Tag@, _Variables, RenderOptions) -> _@match, _@Ast",
  389. [{match, options_match_ast(TW)}]),
  390. custom_tags_clauses_ast1(
  391. CustomTags, [Tag | ExcludeTags], [Clause|ClauseAcc],
  392. merge_info(Info, InfoAcc), TW)
  393. end
  394. end
  395. end.
  396. custom_forms(Dir, Module, Functions, AstInfo) ->
  397. Dependencies = AstInfo#ast_info.dependencies,
  398. TranslatableStrings = AstInfo#ast_info.translatable_strings,
  399. erl_syntax:revert_forms(
  400. lists:flatten(
  401. ?Q(["-module('@Module@').",
  402. "-export([source_dir/0, dependencies/0, translatable_strings/0,",
  403. " render/1, render/2, render/3]).",
  404. "-export(['@__export_functions'/0]).",
  405. "source_dir() -> _@Dir@.",
  406. "dependencies() -> _@Dependencies@.",
  407. "translatable_strings() -> _@TranslatableStrings@.",
  408. "render(Tag) -> render(Tag, [], []).",
  409. "render(Tag, Vars) -> render(Tag, Vars, []).",
  410. "render(Tag, Vars, Opts) ->",
  411. " try '@Module@':Tag(Vars, Opts) of",
  412. " Val -> {ok, Val}",
  413. " catch",
  414. " Err -> {error, Err}",
  415. " end.",
  416. "'@_functions'() -> _."
  417. ],
  418. [{export_functions,
  419. erl_syntax:list(
  420. [erl_syntax:arity_qualifier(erl_syntax:atom(FName), erl_syntax:integer(Arity))
  421. || {FName, _} <- Functions, Arity <- [1, 2]])},
  422. {functions, [Ast || {_, Ast} <- Functions]}
  423. ]))
  424. ).
  425. stringify(BodyAst, #dtl_context{ binary_strings=BinaryStrings }) ->
  426. [?Q("erlydtl_runtime:stringify_final(_@BodyAst, '@BinaryStrings@')")].
  427. forms({BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}, CheckSum,
  428. #treewalker{
  429. context=#dtl_context{
  430. module=Module,
  431. parse_trail=[File|_]
  432. }=Context
  433. }=TreeWalker) ->
  434. MergedInfo = merge_info(BodyInfo, CustomTagsInfo),
  435. Dependencies = MergedInfo#ast_info.dependencies,
  436. TranslatableStrings = MergedInfo#ast_info.translatable_strings,
  437. TranslatedBlocks = MergedInfo#ast_info.translated_blocks,
  438. Variables = lists:usort(MergedInfo#ast_info.var_names),
  439. DefaultVariables = lists:usort(MergedInfo#ast_info.def_names),
  440. Constants = lists:usort(MergedInfo#ast_info.const_names),
  441. FinalBodyAst = options_match_ast(TreeWalker) ++ stringify(BodyAst, Context),
  442. erl_syntax:revert_forms(
  443. ?Q(["-module('@Module@').",
  444. "-export([render/0, render/1, render/2, source/0, dependencies/0,",
  445. " translatable_strings/0, translated_blocks/0, variables/0,",
  446. " default_variables/0, constants/0]).",
  447. "source() -> {_@File@, _@CheckSum@}.",
  448. "dependencies() -> _@Dependencies@.",
  449. "variables() -> _@Variables@.",
  450. "default_variables() -> _@DefaultVariables@.",
  451. "constants() -> _@Constants@.",
  452. "translatable_strings() -> _@TranslatableStrings@.",
  453. "translated_blocks() -> _@TranslatedBlocks@.",
  454. "'@_CustomTagsFunctionAst'() -> _.",
  455. "render() -> render([], []).",
  456. "render(Variables) -> render(Variables, []).",
  457. "render(Variables, RenderOptions) ->",
  458. " try render_internal(Variables, RenderOptions) of",
  459. " Val -> {ok, Val}",
  460. " catch",
  461. " Err -> {error, Err}",
  462. " end.",
  463. "render_internal(_Variables, RenderOptions) -> _@FinalBodyAst."
  464. ])).
  465. options_match_ast(#treewalker{ context=Context }=TreeWalker) ->
  466. options_match_ast(Context, TreeWalker);
  467. options_match_ast(Context) ->
  468. options_match_ast(Context, undefined).
  469. options_match_ast(Context, TreeWalker) ->
  470. [
  471. ?Q(["_TranslationFun = erlydtl_runtime:init_translation(",
  472. " proplists:get_value(translation_fun, RenderOptions, none))"]),
  473. ?Q("_CurrentLocale = proplists:get_value(locale, RenderOptions, default)"),
  474. ?Q("_RecordInfo = _@info", [{info, merl:term(Context#dtl_context.record_info)}])
  475. | case call_extension(Context, setup_render_ast, [Context, TreeWalker]) of
  476. undefined -> [];
  477. Ast when is_list(Ast) -> Ast
  478. end].
  479. %% child templates should only consist of blocks at the top level
  480. body_ast([{'extends', {string_literal, _Pos, String}} | ThisParseTree], #treewalker{ context=Context }=TreeWalker) ->
  481. File = full_path(unescape_string_literal(String), Context#dtl_context.doc_root),
  482. case lists:member(File, Context#dtl_context.parse_trail) of
  483. true ->
  484. empty_ast(?ERR(circular_include, TreeWalker));
  485. _ ->
  486. case parse_file(File, Context) of
  487. {ok, ParentParseTree, CheckSum} ->
  488. BlockDict = lists:foldl(
  489. fun ({block, {identifier, _, Name}, Contents}, Dict) ->
  490. dict:store(Name, Contents, Dict);
  491. (_, Dict) -> Dict
  492. end,
  493. dict:new(),
  494. ThisParseTree),
  495. {Info, TreeWalker1} = with_dependency(
  496. {File, CheckSum},
  497. body_ast(
  498. ParentParseTree,
  499. TreeWalker#treewalker{
  500. context=Context#dtl_context{
  501. block_dict = dict:merge(
  502. fun(_Key, _ParentVal, ChildVal) -> ChildVal end,
  503. BlockDict, Context#dtl_context.block_dict),
  504. parse_trail = [File | Context#dtl_context.parse_trail]
  505. }
  506. })),
  507. {Info, reset_parse_trail(Context#dtl_context.parse_trail, TreeWalker1)};
  508. {error, Reason} ->
  509. empty_ast(?ERR(Reason, TreeWalker))
  510. end
  511. end;
  512. body_ast(DjangoParseTree, TreeWalker) ->
  513. body_ast(DjangoParseTree, empty_scope(), TreeWalker).
  514. body_ast(DjangoParseTree, BodyScope, TreeWalker) ->
  515. {ScopeId, TreeWalkerScope} = begin_scope(BodyScope, TreeWalker),
  516. BodyFun =
  517. fun ({'autoescape', {identifier, _, OnOrOff}, Contents}, TW) ->
  518. {Info, BodyTW} = body_ast(Contents, push_auto_escape(OnOrOff, TW)),
  519. {Info, pop_auto_escape(BodyTW)};
  520. ({'block', {identifier, Pos, Name}, Contents}, #treewalker{ context=Context }=TW) ->
  521. {Block, BlockScope} =
  522. case dict:find(Name, Context#dtl_context.block_dict) of
  523. {ok, ChildBlock} ->
  524. {{ContentsAst, _ContentsInfo}, _ContentsTW} = body_ast(Contents, TW),
  525. {ChildBlock,
  526. create_scope(
  527. [{block, ?Q("[{super, _@ContentsAst}]")}],
  528. Pos, TW)
  529. };
  530. _ ->
  531. {Contents, empty_scope()}
  532. end,
  533. body_ast(Block, BlockScope, TW);
  534. ({'blocktrans', Args, Contents, PluralContents}, TW) ->
  535. blocktrans_ast(Args, Contents, PluralContents, TW);
  536. ({'call', {identifier, _, Name}}, TW) ->
  537. call_ast(Name, TW);
  538. ({'call', {identifier, _, Name}, With}, TW) ->
  539. call_with_ast(Name, With, TW);
  540. ({'comment', _Contents}, TW) ->
  541. empty_ast(TW);
  542. ({'comment_tag', _, _}, TW) ->
  543. empty_ast(TW);
  544. ({'cycle', Names}, TW) ->
  545. cycle_ast(Names, TW);
  546. ({'cycle_compat', Names}, TW) ->
  547. cycle_compat_ast(Names, TW);
  548. ({'date', 'now', {string_literal, _Pos, FormatString}}, TW) ->
  549. now_ast(FormatString, TW);
  550. ({'filter', FilterList, Contents}, TW) ->
  551. filter_tag_ast(FilterList, Contents, TW);
  552. ({'firstof', Vars}, TW) ->
  553. firstof_ast(Vars, TW);
  554. ({'for', {'in', IteratorList, Variable, Reversed}, Contents}, TW) ->
  555. {EmptyAstInfo, TW1} = empty_ast(TW),
  556. for_loop_ast(IteratorList, Variable, Reversed, Contents, EmptyAstInfo, TW1);
  557. ({'for', {'in', IteratorList, Variable, Reversed}, Contents, EmptyPartContents}, TW) ->
  558. {EmptyAstInfo, TW1} = body_ast(EmptyPartContents, TW),
  559. for_loop_ast(IteratorList, Variable, Reversed, Contents, EmptyAstInfo, TW1);
  560. ({'if', Expression, Contents, Elif}, TW) ->
  561. {IfAstInfo, TW1} = body_ast(Contents, TW),
  562. {ElifAstInfo, TW2} = body_ast(Elif, TW1),
  563. ifelse_ast(Expression, IfAstInfo, ElifAstInfo, TW2);
  564. ({'if', Expression, Contents}, TW) ->
  565. {IfAstInfo, TW1} = body_ast(Contents, TW),
  566. {ElseAstInfo, TW2} = empty_ast(TW1),
  567. ifelse_ast(Expression, IfAstInfo, ElseAstInfo, TW2);
  568. ({'ifchanged', '$undefined', Contents}, TW) ->
  569. {IfAstInfo, TW1} = body_ast(Contents, TW),
  570. {ElseAstInfo, TW2} = empty_ast(TW1),
  571. ifchanged_contents_ast(Contents, IfAstInfo, ElseAstInfo, TW2);
  572. ({'ifchanged', Values, Contents}, TW) ->
  573. {IfAstInfo, TW1} = body_ast(Contents, TW),
  574. {ElseAstInfo, TW2} = empty_ast(TW1),
  575. ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, TW2);
  576. ({'ifchangedelse', '$undefined', IfContents, ElseContents}, TW) ->
  577. {IfAstInfo, TW1} = body_ast(IfContents, TW),
  578. {ElseAstInfo, TW2} = body_ast(ElseContents, TW1),
  579. ifchanged_contents_ast(IfContents, IfAstInfo, ElseAstInfo, TW2);
  580. ({'ifchangedelse', Values, IfContents, ElseContents}, TW) ->
  581. {IfAstInfo, TW1} = body_ast(IfContents, TW),
  582. {ElseAstInfo, TW2} = body_ast(ElseContents, TW1),
  583. ifchanged_values_ast(Values, IfAstInfo, ElseAstInfo, TW2);
  584. ({'ifelse', Expression, IfContents, ElseContents}, TW) ->
  585. {IfAstInfo, TW1} = body_ast(IfContents, TW),
  586. {ElseAstInfo, TW2} = body_ast(ElseContents, TW1),
  587. ifelse_ast(Expression, IfAstInfo, ElseAstInfo, TW2);
  588. ({'ifequal', [Arg1, Arg2], Contents}, TW) ->
  589. {IfAstInfo, TW1} = body_ast(Contents, TW),
  590. {ElseAstInfo, TW2} = empty_ast(TW1),
  591. ifelse_ast({'expr', "eq", Arg1, Arg2}, IfAstInfo, ElseAstInfo, TW2);
  592. ({'ifequalelse', [Arg1, Arg2], IfContents, ElseContents}, TW) ->
  593. {IfAstInfo, TW1} = body_ast(IfContents, TW),
  594. {ElseAstInfo, TW2} = body_ast(ElseContents,TW1),
  595. ifelse_ast({'expr', "eq", Arg1, Arg2}, IfAstInfo, ElseAstInfo, TW2);
  596. ({'ifnotequal', [Arg1, Arg2], Contents}, TW) ->
  597. {IfAstInfo, TW1} = body_ast(Contents, TW),
  598. {ElseAstInfo, TW2} = empty_ast(TW1),
  599. ifelse_ast({'expr', "ne", Arg1, Arg2}, IfAstInfo, ElseAstInfo, TW2);
  600. ({'ifnotequalelse', [Arg1, Arg2], IfContents, ElseContents}, TW) ->
  601. {IfAstInfo, TW1} = body_ast(IfContents, TW),
  602. {ElseAstInfo, TW2} = body_ast(ElseContents, TW1),
  603. ifelse_ast({'expr', "ne", Arg1, Arg2}, IfAstInfo, ElseAstInfo, TW2);
  604. ({'include', {string_literal, _, File}, Args}, #treewalker{ context=Context }=TW) ->
  605. include_ast(unescape_string_literal(File), Args, Context#dtl_context.local_scopes, TW);
  606. ({'include_only', {string_literal, _, File}, Args}, TW) ->
  607. {Info, IncTW} = include_ast(unescape_string_literal(File), Args, [], TW),
  608. {Info, restore_scope(TW, IncTW)};
  609. ({'load_libs', Libs}, TW) ->
  610. load_libs_ast(Libs, TW);
  611. ({'load_from_lib', What, Lib}, TW) ->
  612. load_from_lib_ast(What, Lib, TW);
  613. ({'regroup', {ListVariable, Grouper, {identifier, _, NewVariable}}}, TW) ->
  614. regroup_ast(ListVariable, Grouper, NewVariable, TW);
  615. ('end_regroup', TW) ->
  616. {{end_scope, #ast_info{}}, TW};
  617. ({'spaceless', Contents}, TW) ->
  618. spaceless_ast(Contents, TW);
  619. ({'ssi', Arg}, TW) ->
  620. ssi_ast(Arg, TW);
  621. ({'ssi_parsed', {string_literal, _, FileName}}, #treewalker{ context=Context }=TW) ->
  622. include_ast(unescape_string_literal(FileName), [], Context#dtl_context.local_scopes, TW);
  623. ({'string', _Pos, String}, TW) ->
  624. string_ast(String, TW);
  625. ({'tag', Name, Args}, TW) ->
  626. tag_ast(Name, Args, TW);
  627. ({'templatetag', {_, _, TagName}}, TW) ->
  628. templatetag_ast(TagName, TW);
  629. ({'trans', Value}, TW) ->
  630. translated_ast(Value, TW);
  631. ({'trans', Value, Context}, TW) ->
  632. translated_ast(Value, Context, TW);
  633. ({'widthratio', Numerator, Denominator, Scale}, TW) ->
  634. widthratio_ast(Numerator, Denominator, Scale, TW);
  635. ({'with', Args, Contents}, TW) ->
  636. with_ast(Args, Contents, TW);
  637. ({'scope_as', {identifier, _, Name}, Contents}, TW) ->
  638. scope_as(Name, Contents, TW);
  639. ({'extension', Tag}, TW) ->
  640. extension_ast(Tag, TW);
  641. ({'extends', _}, TW) ->
  642. empty_ast(?ERR(unexpected_extends_tag, TW));
  643. (ValueToken, TW) ->
  644. format(value_ast(ValueToken, true, true, TW))
  645. end,
  646. {AstInfoList, TreeWalker1} = lists:mapfoldl(BodyFun, TreeWalkerScope, DjangoParseTree),
  647. {AstList, Info} =
  648. lists:mapfoldl(
  649. fun ({Ast, Info}, InfoAcc) ->
  650. {Ast, merge_info(Info, InfoAcc)}
  651. end, #ast_info{}, AstInfoList),
  652. {Ast, TreeWalker2} = end_scope(
  653. fun ([ScopeVars|ScopeBody]) -> [?Q("begin _@ScopeVars, [_@ScopeBody] end")] end,
  654. ScopeId, AstList, TreeWalker1),
  655. {{erl_syntax:list(Ast), Info}, TreeWalker2}.
  656. value_ast(ValueToken, AsString, EmptyIfUndefined, TreeWalker) ->
  657. case ValueToken of
  658. {'expr', Operator, Value} ->
  659. {{ValueAst,InfoValue}, TreeWalker1} = value_ast(Value, false, EmptyIfUndefined, TreeWalker),
  660. Op = list_to_atom(Operator),
  661. Ast = ?Q("erlydtl_runtime:_@Op@(_@ValueAst)"),
  662. {{Ast, InfoValue}, TreeWalker1};
  663. {'expr', Operator, Value1, Value2} ->
  664. {{Value1Ast,InfoValue1}, TreeWalker1} = value_ast(Value1, false, EmptyIfUndefined, TreeWalker),
  665. {{Value2Ast,InfoValue2}, TreeWalker2} = value_ast(Value2, false, EmptyIfUndefined, TreeWalker1),
  666. Op = list_to_atom(Operator),
  667. Ast = ?Q("erlydtl_runtime:_@Op@(_@Value1Ast, _@Value2Ast)"),
  668. {{Ast, merge_info(InfoValue1,InfoValue2)}, TreeWalker2};
  669. {'string_literal', _Pos, String} ->
  670. string_ast(unescape_string_literal(String), TreeWalker);
  671. {'number_literal', _Pos, Number} ->
  672. case AsString of
  673. true -> string_ast(Number, TreeWalker);
  674. false -> {{erl_syntax:integer(Number), #ast_info{}}, TreeWalker}
  675. end;
  676. {'apply_filter', Variable, Filter} ->
  677. filter_ast(Variable, Filter, TreeWalker);
  678. {'attribute', _} = Variable ->
  679. resolve_variable_ast(Variable, EmptyIfUndefined, TreeWalker);
  680. {'variable', _} = Variable ->
  681. resolve_variable_ast(Variable, EmptyIfUndefined, TreeWalker);
  682. {extension, Tag} ->
  683. extension_ast(Tag, TreeWalker)
  684. end.
  685. extension_ast(Tag, TreeWalker) ->
  686. case call_extension(TreeWalker, compile_ast, [Tag, TreeWalker]) of
  687. undefined ->
  688. empty_ast(?WARN({unknown_extension, Tag}, TreeWalker));
  689. Result ->
  690. Result
  691. end.
  692. with_dependencies([], Args) ->
  693. Args;
  694. with_dependencies([Dependency | Rest], Args) ->
  695. with_dependencies(Rest, with_dependency(Dependency, Args)).
  696. with_dependency(FilePath, {{Ast, Info}, TreeWalker}) ->
  697. {{Ast, Info#ast_info{dependencies = [FilePath | Info#ast_info.dependencies]}}, TreeWalker}.
  698. empty_ast(TreeWalker) ->
  699. {{erl_syntax:list([]), #ast_info{}}, TreeWalker}.
  700. %%% Note: Context here refers to the translation context, not the #dtl_context{} record
  701. blocktrans_ast(Args, Contents, PluralContents, TreeWalker) ->
  702. %% get args, count and context
  703. ArgList = [{Name, Value}
  704. || {{identifier, _, Name}, Value}
  705. <- proplists:get_value(args, Args, [])],
  706. Count = proplists:get_value(count, Args),
  707. Context = case proplists:get_value(context, Args) of
  708. undefined -> undefined;
  709. {string_literal, _, S} ->
  710. unescape_string_literal(S)
  711. end,
  712. %% add new scope using 'with' values
  713. {NewScope, {ArgInfo, TreeWalker1}} =
  714. lists:mapfoldl(
  715. fun ({LocalVarName, Value}, {AstInfoAcc, TreeWalkerAcc}) ->
  716. {{Ast, Info}, TW} = value_ast(Value, false, false, TreeWalkerAcc),
  717. {{LocalVarName, Ast}, {merge_info(AstInfoAcc, Info), TW}}
  718. end,
  719. {#ast_info{}, TreeWalker},
  720. case Count of
  721. {{identifier, _, Name}, Value} ->
  722. [{Name, Value}|ArgList];
  723. _ ->
  724. ArgList
  725. end),
  726. TreeWalker2 = push_scope(NewScope, TreeWalker1),
  727. %% key for translation lookup
  728. SourceText = erlydtl_unparser:unparse(Contents),
  729. {{DefaultAst, AstInfo}, TreeWalker3} = body_ast(Contents, TreeWalker2),
  730. MergedInfo = merge_info(AstInfo, ArgInfo),
  731. #dtl_context{
  732. trans_fun = TFun,
  733. trans_locales = TLocales } = TreeWalker3#treewalker.context,
  734. if TFun =:= none; PluralContents =/= undefined ->
  735. %% translate in runtime
  736. {FinalAst, FinalTW} = blocktrans_runtime_ast(
  737. {DefaultAst, MergedInfo}, SourceText, Contents, Context,
  738. plural_contents(PluralContents, Count, TreeWalker3)),
  739. {FinalAst, restore_scope(TreeWalker1, FinalTW)};
  740. is_function(TFun, 2) ->
  741. %% translate in compile-time
  742. {FinalAstInfo, FinalTreeWalker, Clauses} =
  743. lists:foldr(
  744. fun (Locale, {AstInfoAcc, TreeWalkerAcc, ClauseAcc}) ->
  745. case TFun(SourceText, phrase_locale(Locale, Context)) of
  746. default ->
  747. {AstInfoAcc, TreeWalkerAcc, ClauseAcc};
  748. Body ->
  749. {ok, DjangoParseTree} = do_parse_template(Body, TreeWalkerAcc#treewalker.context),
  750. {{BodyAst, BodyInfo}, BodyTreeWalker} = body_ast(DjangoParseTree, TreeWalkerAcc),
  751. {merge_info(BodyInfo, AstInfoAcc), BodyTreeWalker,
  752. [?Q("_@Locale@ -> _@BodyAst")|ClauseAcc]}
  753. end
  754. end,
  755. {MergedInfo, TreeWalker3, []}, TLocales),
  756. FinalAst = ?Q("case _CurrentLocale of _@_Clauses -> _; _ -> _@DefaultAst end"),
  757. {{FinalAst, FinalAstInfo#ast_info{ translated_blocks = [SourceText] }},
  758. restore_scope(TreeWalker1, FinalTreeWalker)};
  759. true ->
  760. empty_ast(?ERR({translation_fun, TFun}, TreeWalker3))
  761. end.
  762. blocktrans_runtime_ast({DefaultAst, Info}, SourceText, Contents, Context, {Plural, TreeWalker}) ->
  763. %% Contents is flat - only strings and '{{var}}' allowed.
  764. %% build sorted list (orddict) of pre-resolved variables to pass to runtime translation function
  765. USortedVariables = lists:usort(fun({variable, {identifier, _, A}},
  766. {variable, {identifier, _, B}}) ->
  767. A =< B
  768. end, [Var || {variable, _}=Var
  769. <- Contents ++ maybe_plural_contents(Plural)]),
  770. VarBuilder = fun({variable, {identifier, _, Name}}=Var, TW) ->
  771. {{VarAst, _VarInfo}, VarTW} = resolve_variable_ast(Var, false, TW),
  772. {?Q("{_@name, _@VarAst}", [{name, merl:term(atom_to_list(Name))}]), VarTW}
  773. end,
  774. {VarAsts, TreeWalker1} = lists:mapfoldl(VarBuilder, TreeWalker, USortedVariables),
  775. VarListAst = erl_syntax:list(VarAsts),
  776. BlockTransAst = ?Q(["begin",
  777. " case erlydtl_runtime:translate_block(",
  778. " _@phrase, _@locale,",
  779. " _@VarListAst, _TranslationFun) of",
  780. " default -> _@DefaultAst;",
  781. " Text -> Text",
  782. " end",
  783. "end"],
  784. [{phrase, phrase_ast(SourceText, Plural)},
  785. {locale, phrase_locale_ast(Context)}]),
  786. {{BlockTransAst, merge_count_info(Info, Plural)}, TreeWalker1}.
  787. maybe_plural_contents(undefined) -> [];
  788. maybe_plural_contents({Contents, _}) -> Contents.
  789. merge_count_info(Info, undefined) -> Info;
  790. merge_count_info(Info, {_Contents, {_CountAst, CountInfo}}) ->
  791. merge_info(Info, CountInfo).
  792. plural_contents(undefined, _, TreeWalker) -> {undefined, TreeWalker};
  793. plural_contents(Contents, {_CountVarName, Value}, TreeWalker) ->
  794. {CountAst, TW} = value_ast(Value, false, false, TreeWalker),
  795. {{Contents, CountAst}, TW}.
  796. phrase_ast(Text, undefined) -> merl:term(Text);
  797. phrase_ast(Text, {Contents, {CountAst, _CountInfo}}) ->
  798. erl_syntax:tuple(
  799. [merl:term(Text),
  800. erl_syntax:tuple(
  801. [merl:term(erlydtl_unparser:unparse(Contents)),
  802. CountAst])
  803. ]).
  804. phrase_locale_ast(undefined) -> merl:var('_CurrentLocale');
  805. phrase_locale_ast(Context) -> erl_syntax:tuple([merl:var('_CurrentLocale'), merl:term(Context)]).
  806. phrase_locale(Locale, undefined) -> Locale;
  807. phrase_locale(Locale, Context) -> {Locale, Context}.
  808. translated_ast(Text, TreeWalker) ->
  809. translated_ast(Text, undefined, TreeWalker).
  810. translated_ast({noop, Value}, _, TreeWalker) ->
  811. value_ast(Value, true, true, TreeWalker);
  812. translated_ast(Text, {string_literal, _, Context}, TreeWalker) ->
  813. translated_ast(Text, unescape_string_literal(Context), TreeWalker);
  814. translated_ast({string_literal, _, String}, Context, TreeWalker) ->
  815. Text = unescape_string_literal(String),
  816. case call_extension(TreeWalker, translate_ast, [Text, Context, TreeWalker]) of
  817. undefined ->
  818. case TreeWalker#treewalker.context#dtl_context.trans_fun of
  819. none -> runtime_trans_ast(Text, Context, TreeWalker);
  820. Fun when is_function(Fun, 2) ->
  821. compiletime_trans_ast(Fun, Text, Context, TreeWalker);
  822. Fun when is_function(Fun, 1) ->
  823. compiletime_trans_ast(fun (T, _) -> Fun(T) end,
  824. Text, Context, TreeWalker);
  825. Fun ->
  826. empty_ast(?ERR({translation_fun, Fun}, TreeWalker))
  827. end;
  828. TranslatedAst ->
  829. TranslatedAst
  830. end;
  831. translated_ast(Value, Context, TreeWalker) ->
  832. runtime_trans_ast(value_ast(Value, true, false, TreeWalker), Context).
  833. runtime_trans_ast(Text, Context, TreeWalker) ->
  834. Info = #ast_info{ translatable_strings = [Text] },
  835. runtime_trans_ast({{merl:term(Text), Info}, TreeWalker}, Context).
  836. runtime_trans_ast({{ValueAst, AstInfo}, TreeWalker}, undefined) ->
  837. {{?Q("erlydtl_runtime:translate(_@ValueAst, _CurrentLocale, _TranslationFun)"),
  838. AstInfo}, TreeWalker};
  839. runtime_trans_ast({{ValueAst, AstInfo}, TreeWalker}, Context) ->
  840. {{?Q("erlydtl_runtime:translate(_@ValueAst, {_CurrentLocale, _@Context@}, _TranslationFun)"),
  841. AstInfo}, TreeWalker}.
  842. compiletime_trans_ast(TFun, Text, LContext,
  843. #treewalker{
  844. context=#dtl_context{
  845. trans_locales=TLocales
  846. }=Context
  847. }=TreeWalker) ->
  848. ClAst = lists:foldl(
  849. fun(Locale, ClausesAcc) ->
  850. [?Q("_@Locale@ -> _@translated",
  851. [{translated, case TFun(Text, phrase_locale(Locale, LContext)) of
  852. default -> string_ast(Text, Context);
  853. Translated -> string_ast(Translated, Context)
  854. end}])
  855. |ClausesAcc]
  856. end,
  857. [], TLocales),
  858. {{?Q(["case _CurrentLocale of",
  859. " _@_ClAst -> _;",
  860. " _ -> _@string",
  861. "end"],
  862. [{string, string_ast(Text, Context)}]),
  863. #ast_info{ translatable_strings = [Text] }},
  864. TreeWalker}.
  865. %%% end of context being translation context
  866. %% Completely unnecessary in ErlyDTL (use {{ "{%" }} etc), but implemented for compatibility.
  867. templatetag_ast("openblock", TreeWalker) ->
  868. string_ast("{%", TreeWalker);
  869. templatetag_ast("closeblock", TreeWalker) ->
  870. string_ast("%}", TreeWalker);
  871. templatetag_ast("openvariable", TreeWalker) ->
  872. string_ast("{{", TreeWalker);
  873. templatetag_ast("closevariable", TreeWalker) ->
  874. string_ast("}}", TreeWalker);
  875. templatetag_ast("openbrace", TreeWalker) ->
  876. string_ast("{", TreeWalker);
  877. templatetag_ast("closebrace", TreeWalker) ->
  878. string_ast("}", TreeWalker);
  879. templatetag_ast("opencomment", TreeWalker) ->
  880. string_ast("{#", TreeWalker);
  881. templatetag_ast("closecomment", TreeWalker) ->
  882. string_ast("#}", TreeWalker).
  883. widthratio_ast(Numerator, Denominator, Scale, TreeWalker) ->
  884. {{NumAst, NumInfo}, TreeWalker1} = value_ast(Numerator, false, true, TreeWalker),
  885. {{DenAst, DenInfo}, TreeWalker2} = value_ast(Denominator, false, true, TreeWalker1),
  886. {{ScaleAst, ScaleInfo}, TreeWalker3} = value_ast(Scale, false, true, TreeWalker2),
  887. {{format_number_ast(?Q("erlydtl_runtime:widthratio(_@NumAst, _@DenAst, _@ScaleAst)")),
  888. merge_info(ScaleInfo, merge_info(NumInfo, DenInfo))},
  889. TreeWalker3}.
  890. string_ast(Arg, #treewalker{ context=Context }=TreeWalker) ->
  891. {{string_ast(Arg, Context), #ast_info{}}, TreeWalker};
  892. string_ast(Arg, Context) ->
  893. merl:term(erlydtl_compiler_utils:to_string(Arg, Context)).
  894. include_ast(File, ArgList, Scopes, #treewalker{ context=Context }=TreeWalker) ->
  895. FilePath = full_path(File, Context#dtl_context.doc_root),
  896. case parse_file(FilePath, Context) of
  897. {ok, InclusionParseTree, CheckSum} ->
  898. {NewScope, {ArgInfo, TreeWalker1}}
  899. = lists:mapfoldl(
  900. fun ({{identifier, _, LocalVarName}, Value}, {AstInfoAcc, TreeWalkerAcc}) ->
  901. {{Ast, Info}, TW} = value_ast(Value, false, false, TreeWalkerAcc),
  902. {{LocalVarName, Ast}, {merge_info(AstInfoAcc, Info), TW}}
  903. end, {#ast_info{}, TreeWalker}, ArgList),
  904. C = TreeWalker1#treewalker.context,
  905. {{BodyAst, BodyInfo}, TreeWalker2} = with_dependency(
  906. {FilePath, CheckSum},
  907. body_ast(
  908. InclusionParseTree,
  909. TreeWalker1#treewalker{
  910. context=C#dtl_context{
  911. parse_trail = [FilePath | C#dtl_context.parse_trail],
  912. local_scopes = [NewScope|Scopes]
  913. }
  914. })),
  915. {{BodyAst, merge_info(BodyInfo, ArgInfo)},
  916. reset_parse_trail(C#dtl_context.parse_trail, TreeWalker2)};
  917. {error, Reason} ->
  918. empty_ast(?ERR(Reason, TreeWalker))
  919. end.
  920. %% include at run-time
  921. ssi_ast(FileName, #treewalker{
  922. context=#dtl_context{
  923. reader = {Mod, Fun},
  924. doc_root = Dir
  925. }
  926. }=TreeWalker) ->
  927. {{FileAst, Info}, TreeWalker1} = value_ast(FileName, true, true, TreeWalker),
  928. {{?Q("erlydtl_runtime:read_file(_@Mod@, _@Fun@, _@Dir@, _@FileAst)"), Info}, TreeWalker1}.
  929. filter_tag_ast(FilterList, Contents, TreeWalker) ->
  930. {{InnerAst, Info}, TreeWalker1} = body_ast(Contents, push_auto_escape(did, TreeWalker)),
  931. {{FilteredAst, FilteredInfo}, TreeWalker2} =
  932. lists:foldl(
  933. fun ({{identifier, _, Name}, []}, {{AstAcc, InfoAcc}, TreeWalkerAcc})
  934. when Name =:= 'escape'; Name =:= 'safe'; Name =:= 'safeseq' ->
  935. {{AstAcc, InfoAcc}, TreeWalkerAcc#treewalker{ safe = true }};
  936. (Filter, {{AstAcc, InfoAcc}, TreeWalkerAcc}) ->
  937. {{Ast, AstInfo}, TW} = filter_ast1(Filter, AstAcc, TreeWalkerAcc),
  938. {{Ast, merge_info(InfoAcc, AstInfo)}, TW}
  939. end,
  940. {{?Q("erlang:iolist_to_binary(_@InnerAst)"), Info},
  941. pop_auto_escape(TreeWalker1)},
  942. FilterList),
  943. EscapedAst = case search_for_escape_filter(
  944. lists:reverse(FilterList),
  945. TreeWalker2#treewalker.context) of
  946. on -> ?Q("erlydtl_filters:force_escape(_@FilteredAst)");
  947. _ -> FilteredAst
  948. end,
  949. {{EscapedAst, FilteredInfo}, TreeWalker2}.
  950. search_for_escape_filter(FilterList, #dtl_context{auto_escape = [on|_]}) ->
  951. search_for_safe_filter(FilterList);
  952. search_for_escape_filter(_, #dtl_context{auto_escape = [did|_]}) -> off;
  953. search_for_escape_filter([{{identifier, _, 'escape'}, []}|Rest], _Context) ->
  954. search_for_safe_filter(Rest);
  955. search_for_escape_filter([_|Rest], Context) ->
  956. search_for_escape_filter(Rest, Context);
  957. search_for_escape_filter([], _Context) -> off.
  958. search_for_safe_filter([{{identifier, _, Name}, []}|_])
  959. when Name =:= 'safe'; Name =:= 'safeseq' -> off;
  960. search_for_safe_filter([_|Rest]) -> search_for_safe_filter(Rest);
  961. search_for_safe_filter([]) -> on.
  962. filter_ast(Variable, Filter, TreeWalker) ->
  963. %% the escape filter is special; it is always applied last, so we have to go digging for it
  964. %% AutoEscape = 'did' means we (will have) decided whether to escape the current variable,
  965. %% so don't do any more escaping
  966. {{UnescapedAst, Info}, TreeWalker1} =
  967. filter_ast_noescape(
  968. Variable, Filter,
  969. push_auto_escape(did, TreeWalker)),
  970. {EscapedAst, TreeWalker2} =
  971. case search_for_escape_filter(Variable, Filter, TreeWalker#treewalker.context) of
  972. on -> {?Q("erlydtl_filters:force_escape(_@UnescapedAst)"),
  973. TreeWalker1#treewalker{ safe = true }};
  974. _ -> {UnescapedAst, TreeWalker1}
  975. end,
  976. {{EscapedAst, Info}, pop_auto_escape(TreeWalker2)}.
  977. filter_ast_noescape(Variable, {{identifier, _, Name}, []}, TreeWalker)
  978. when Name =:= 'escape'; Name =:= 'safe'; Name =:= 'safeseq' ->
  979. value_ast(Variable, true, false, TreeWalker#treewalker{safe = true});
  980. filter_ast_noescape(Variable, Filter, TreeWalker) ->
  981. {{ValueAst, Info1}, TreeWalker2} = value_ast(Variable, true, false, TreeWalker),
  982. {{VarValue, Info2}, TreeWalker3} = filter_ast1(Filter, ValueAst, TreeWalker2),
  983. {{VarValue, merge_info(Info1, Info2)}, TreeWalker3}.
  984. filter_ast1({{identifier, Pos, Name}, Args}, ValueAst, TreeWalker) ->
  985. {{ArgsAst, ArgsInfo}, TreeWalker1} =
  986. lists:foldr(
  987. fun (Arg, {{AccAst, AccInfo}, AccTreeWalker}) ->
  988. {{ArgAst, ArgInfo}, ArgTreeWalker} = value_ast(Arg, false, false, AccTreeWalker),
  989. {{[ArgAst|AccAst], merge_info(ArgInfo, AccInfo)}, ArgTreeWalker}
  990. end,
  991. {{[], #ast_info{}}, TreeWalker},
  992. Args),
  993. case filter_ast2(Name, [ValueAst|ArgsAst], TreeWalker1#treewalker.context) of
  994. {ok, FilterAst} ->
  995. {{FilterAst, ArgsInfo}, TreeWalker1};
  996. Error ->
  997. empty_ast(?WARN({Pos, Error}, TreeWalker1))
  998. end.
  999. filter_ast2(Name, Args, #dtl_context{ filters = Filters }) ->
  1000. case proplists:get_value(Name, Filters) of
  1001. {Mod, Fun}=Filter ->
  1002. case erlang:function_exported(Mod, Fun, length(Args)) of
  1003. true -> {ok, ?Q("'@Mod@':'@Fun@'(_@Args)")};
  1004. false ->
  1005. {filter_args, Name, Filter, length(Args)}
  1006. end;
  1007. undefined ->
  1008. {unknown_filter, Name, length(Args)}
  1009. end.
  1010. search_for_escape_filter(Variable, Filter, #dtl_context{auto_escape = [on|_]}) ->
  1011. search_for_safe_filter(Variable, Filter);
  1012. search_for_escape_filter(_, _, #dtl_context{auto_escape = [did|_]}) ->
  1013. off;
  1014. search_for_escape_filter(Variable, {{identifier, _, 'escape'}, []} = Filter, _Context) ->
  1015. search_for_safe_filter(Variable, Filter);
  1016. search_for_escape_filter({apply_filter, Variable, Filter}, _, Context) ->
  1017. search_for_escape_filter(Variable, Filter, Context);
  1018. search_for_escape_filter(_Variable, _Filter, _Context) ->
  1019. off.
  1020. search_for_safe_filter(_, {{identifier, _, 'safe'}, []}) ->
  1021. off;
  1022. search_for_safe_filter(_, {{identifier, _, 'safeseq'}, []}) ->
  1023. off;
  1024. search_for_safe_filter({apply_filter, Variable, Filter}, _) ->
  1025. search_for_safe_filter(Variable, Filter);
  1026. search_for_safe_filter(_Variable, _Filter) ->
  1027. on.
  1028. finder_function(true) -> {erlydtl_runtime, fetch_value};
  1029. finder_function(false) -> {erlydtl_runtime, find_value}.
  1030. finder_function(EmptyIfUndefined, TreeWalker) ->
  1031. case call_extension(TreeWalker, finder_function, [EmptyIfUndefined]) of
  1032. undefined -> finder_function(EmptyIfUndefined);
  1033. Result -> Result
  1034. end.
  1035. resolve_variable_ast({extension, Tag}, _, TreeWalker) ->
  1036. extension_ast(Tag, TreeWalker);
  1037. resolve_variable_ast(VarTuple, EmptyIfUndefined, TreeWalker)
  1038. when is_boolean(EmptyIfUndefined) ->
  1039. resolve_variable_ast(VarTuple, finder_function(EmptyIfUndefined, TreeWalker), TreeWalker);
  1040. resolve_variable_ast(VarTuple, FinderFunction, TreeWalker) ->
  1041. resolve_variable_ast1(VarTuple, FinderFunction, TreeWalker).
  1042. resolve_variable_ast1({attribute, {{_, Pos, Attr}, Variable}}, {Runtime, Finder}=FinderFunction, TreeWalker) ->
  1043. {{VarAst, VarInfo}, TreeWalker1} = resolve_variable_ast(Variable, FinderFunction, TreeWalker),
  1044. #treewalker{
  1045. context=#dtl_context{
  1046. lists_0_based = Lists0Based,
  1047. tuples_0_based = Tuples0Based
  1048. }
  1049. } = TreeWalker,
  1050. FileName = get_current_file(TreeWalker1),
  1051. {{?Q(["'@Runtime@':'@Finder@'(",
  1052. " _@Attr@, _@VarAst,",
  1053. " [{lists_0_based, _@Lists0Based@},",
  1054. " {tuples_0_based, _@Tuples0Based@},",
  1055. " {render_options, RenderOptions},",
  1056. " {record_info, _RecordInfo},",
  1057. " {filename, _@FileName@},",
  1058. " {pos, _@Pos@}",
  1059. " ])"]),
  1060. VarInfo},
  1061. TreeWalker1};
  1062. resolve_variable_ast1({variable, {identifier, Pos, VarName}}, {Runtime, Finder}, TreeWalker) ->
  1063. Ast = case resolve_variable(VarName, TreeWalker) of
  1064. {_, undefined} ->
  1065. FileName = get_current_file(TreeWalker),
  1066. {?Q(["'@Runtime@':'@Finder@'(",
  1067. " _@VarName@, _Variables,",
  1068. " [{filename, _@FileName@},",
  1069. " {pos, _@Pos@},",
  1070. " {record_info, _RecordInfo},",
  1071. " {render_options, RenderOptions}])"
  1072. ]),
  1073. #ast_info{ var_names=[VarName] }};
  1074. {default_vars, Val} ->
  1075. FileName = get_current_file(TreeWalker),
  1076. {?Q(["'@Runtime@':fetch_value(",
  1077. " _@VarName@, _Variables,",
  1078. " [{filename, _@FileName@},",
  1079. " {pos, _@Pos@},",
  1080. " {record_info, _RecordInfo},",
  1081. " {render_options, RenderOptions}],",
  1082. " _@val)"
  1083. ],
  1084. [{val, merl:term(erlydtl_filters:format_number(Val))}]),
  1085. #ast_info{ var_names=[VarName], def_names=[VarName] }};
  1086. {constant, Val} ->
  1087. {merl:term(erlydtl_filters:format_number(Val)),
  1088. #ast_info{ const_names=[VarName] }};
  1089. {scope, Val} ->
  1090. {Val, #ast_info{}}
  1091. end,
  1092. {Ast, TreeWalker}.
  1093. resolve_reserved_variable(ReservedName, TreeWalker) ->
  1094. resolve_reserved_variable(ReservedName, merl:term(undefined), TreeWalker).
  1095. resolve_reserved_variable(ReservedName, Default, TreeWalker) ->
  1096. case resolve_variable(ReservedName, Default, TreeWalker) of
  1097. {Src, Value} when Src =:= scope; Value =:= Default ->
  1098. {Value, TreeWalker};
  1099. _ ->
  1100. {Default, ?ERR({reserved_variable, ReservedName}, TreeWalker)}
  1101. end.
  1102. format({{Ast, Info}, TreeWalker}) ->
  1103. auto_escape({{format_number_ast(Ast), Info}, TreeWalker}).
  1104. format_number_ast(Ast) ->
  1105. ?Q("erlydtl_filters:format_number(_@Ast)").
  1106. auto_escape({AstInfo, #treewalker{ safe = true }=TW}) ->
  1107. {AstInfo, TW#treewalker{ safe = false }};
  1108. auto_escape({{Value, Info}, #treewalker{ context=#dtl_context{auto_escape=[on|_]} }=TW}) ->
  1109. {{?Q("erlydtl_filters:force_escape(_@Value)"), Info}, TW};
  1110. auto_escape(Value) -> Value.
  1111. firstof_ast(Vars, TreeWalker) ->
  1112. body_ast(
  1113. [lists:foldr(
  1114. fun ({L, _, _}=Var, [])
  1115. when L=:=string_literal;L=:=number_literal ->
  1116. Var;
  1117. ({L, _, _}, _)
  1118. when L=:=string_literal;L=:=number_literal ->
  1119. erlang:error(errbadliteral);
  1120. (Var, []) ->
  1121. {'ifelse', Var, [Var], []};
  1122. (Var, Acc) ->
  1123. {'ifelse', Var, [Var], [Acc]}
  1124. end,
  1125. [], Vars)
  1126. ],
  1127. TreeWalker).
  1128. ifelse_ast(Expression, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, TreeWalker) ->
  1129. Info = merge_info(IfContentsInfo, ElseContentsInfo),
  1130. {{Ast, ExpressionInfo}, TreeWalker1} = value_ast(Expression, false, false, TreeWalker),
  1131. {{?Q(["case erlydtl_runtime:is_true(_@Ast) of",
  1132. " true -> _@IfContentsAst;",
  1133. " _ -> _@ElseContentsAst",
  1134. "end"]),
  1135. merge_info(ExpressionInfo, Info)},
  1136. TreeWalker1}.
  1137. with_ast(ArgList, Contents, TreeWalker) ->
  1138. {ArgAstList, {ArgInfo, TreeWalker1}} =
  1139. lists:mapfoldl(
  1140. fun ({{identifier, _, _LocalVarName}, Value}, {AstInfoAcc, TreeWalkerAcc}) ->
  1141. {{Ast, Info}, TW} = value_ast(Value, false, false, TreeWalkerAcc),
  1142. {Ast, {merge_info(AstInfoAcc, Info), TW}}
  1143. end,
  1144. {#ast_info{}, TreeWalker},
  1145. ArgList),
  1146. NewScope = lists:map(
  1147. fun ({{identifier, _, LocalVarName}, _Value}) ->
  1148. {LocalVarName, varname_ast(LocalVarName)}
  1149. end,
  1150. ArgList),
  1151. {{InnerAst, InnerInfo}, TreeWalker2} =
  1152. body_ast(
  1153. Contents,
  1154. push_scope(NewScope, TreeWalker1)),
  1155. {{?Q("fun (_@args) -> _@InnerAst end (_@ArgAstList)",
  1156. [{args, element(2, lists:unzip(NewScope))}]),
  1157. merge_info(ArgInfo, InnerInfo)},
  1158. restore_scope(TreeWalker1, TreeWalker2)}.
  1159. scope_as(VarName, Contents, TreeWalker) ->
  1160. {{ContentsAst, ContentsInfo}, TreeWalker1} = body_ast(Contents, TreeWalker),
  1161. VarAst = varname_ast(VarName),
  1162. {Id, TreeWalker2} = begin_scope(
  1163. {[{VarName, VarAst}],
  1164. [?Q("_@VarAst = _@ContentsAst")]},
  1165. TreeWalker1),
  1166. {{Id, ContentsInfo}, TreeWalker2}.
  1167. regroup_ast(ListVariable, GrouperVariable, LocalVarName, TreeWalker) ->
  1168. {{ListAst, ListInfo}, TreeWalker1} = value_ast(ListVariable, false, true, TreeWalker),
  1169. LocalVarAst = varname_ast(LocalVarName),
  1170. {Id, TreeWalker2} = begin_scope(
  1171. {[{LocalVarName, LocalVarAst}],
  1172. [?Q("_@LocalVarAst = erlydtl_runtime:regroup(_@ListAst, _@regroup)",
  1173. [{regroup, regroup_filter(GrouperVariable, [])}])
  1174. ]},
  1175. TreeWalker1),
  1176. {{Id, ListInfo}, TreeWalker2}.
  1177. regroup_filter({attribute,{{identifier,_,Ident},Next}},Acc) ->
  1178. regroup_filter(Next,[erl_syntax:atom(Ident)|Acc]);
  1179. regroup_filter({variable,{identifier,_,Var}},Acc) ->
  1180. erl_syntax:list([erl_syntax:atom(Var)|Acc]).
  1181. to_list_ast(Value, IsReversed) ->
  1182. ?Q("erlydtl_runtime:to_list(_@Value, _@IsReversed)").
  1183. to_list_ast(Value, IsReversed, TreeWalker) ->
  1184. case call_extension(TreeWalker, to_list_ast, [Value, IsReversed, TreeWalker]) of
  1185. undefined -> to_list_ast(Value, IsReversed);
  1186. Result -> Result
  1187. end.
  1188. for_loop_ast(IteratorList, LoopValue, IsReversed, Contents,
  1189. {EmptyContentsAst, EmptyContentsInfo},
  1190. #treewalker{ context=Context }=TreeWalker) ->
  1191. %% create unique namespace for this instance
  1192. Level = length(Context#dtl_context.local_scopes),
  1193. {Row, Col} = element(2, hd(IteratorList)),
  1194. ForId = lists:concat(["/", Level, "_", Row, ":", Col]),
  1195. Counters = merl:var(lists:concat(["Counters", ForId])),
  1196. Vars = merl:var(lists:concat(["Vars", ForId])),
  1197. %% setup
  1198. VarScope = lists:map(
  1199. fun({identifier, {R, C}, Iterator}) ->
  1200. {Iterator, varname_ast(lists:concat([
  1201. Iterator,"/", Level, "_", R, ":", C]))}
  1202. end, IteratorList),
  1203. {Iterators, IteratorVars} = lists:unzip(VarScope),
  1204. IteratorCount = length(IteratorVars),
  1205. {{LoopBodyAst, Info}, TreeWalker1} =
  1206. body_ast(
  1207. Contents,
  1208. push_scope([{'forloop', Counters} | VarScope],
  1209. TreeWalker)),
  1210. {{LoopValueAst, LoopValueInfo}, TreeWalker2} =
  1211. value_ast(LoopValue, false, true, restore_scope(TreeWalker, TreeWalker1)),
  1212. LoopValueAst0 = to_list_ast(LoopValueAst, merl:term(IsReversed), TreeWalker2),
  1213. {ParentLoop, TreeWalker3} = resolve_reserved_variable('forloop', TreeWalker2),
  1214. %% call for loop
  1215. {{?Q(["case erlydtl_runtime:forloop(",
  1216. " fun (_@Vars, _@Counters) ->",
  1217. " {_@IteratorVars} = if is_tuple(_@Vars), size(_@Vars) == _@IteratorCount@ -> _@Vars;",
  1218. " _@___ifclauses -> _",
  1219. " end,",
  1220. " {_@LoopBodyAst, erlydtl_runtime:increment_counter_stats(_@Counters)}",
  1221. " end,",
  1222. " _@LoopValueAst0, _@ParentLoop)",
  1223. "of",
  1224. " empty -> _@EmptyContentsAst;",
  1225. " {L, _} -> L",
  1226. "end"],
  1227. [{ifclauses, if IteratorCount > 1 ->
  1228. ?Q(["() when is_list(_@Vars), length(_@Vars) == _@IteratorCount@ ->",
  1229. " list_to_tuple(_@Vars);",
  1230. "() when true -> throw({for_loop, _@Iterators@, _@Vars})"]);
  1231. true ->
  1232. ?Q("() when true -> {_@Vars}")
  1233. end}]),
  1234. merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)},
  1235. TreeWalker3}.
  1236. ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, TreeWalker) ->
  1237. Info = merge_info(IfContentsInfo, ElseContentsInfo),
  1238. ValueAstFun = fun (Expr, {LTreeWalker, LInfo, Acc}) ->
  1239. {{EAst, EInfo}, ETw} = value_ast(Expr, false, true, LTreeWalker),
  1240. {ETw, merge_info(LInfo, EInfo),
  1241. [?Q("{_@hash, _@EAst}",
  1242. [{hash, merl:term(erlang:phash2(Expr))}])
  1243. |Acc]}
  1244. end,
  1245. {TreeWalker1, MergedInfo, Changed} = lists:foldl(ValueAstFun, {TreeWalker, Info, []}, Values),
  1246. {{?Q(["case erlydtl_runtime:ifchanged([_@Changed]) of",
  1247. " true -> _@IfContentsAst;",
  1248. " _ -> _@ElseContentsAst",
  1249. "end"]),
  1250. MergedInfo},
  1251. TreeWalker1}.
  1252. ifchanged_contents_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, TreeWalker) ->
  1253. {{?Q(["case erlydtl_runtime:ifchanged([{_@hash, _@IfContentsAst}]) of",
  1254. " true -> _@IfContentsAst;",
  1255. " _ -> _@ElseContentsAst",
  1256. "end"],
  1257. [{hash, merl:term(erlang:phash2(Contents))}]),
  1258. merge_info(IfContentsInfo, ElseContentsInfo)},
  1259. TreeWalker}.
  1260. cycle_ast(Names, #treewalker{ context=Context }=TreeWalker) ->
  1261. {NamesTuple, VarNames}
  1262. = lists:mapfoldl(
  1263. fun ({string_literal, _, Str}, VarNamesAcc) ->
  1264. S = string_ast(unescape_string_literal(Str), Context),
  1265. {S, VarNamesAcc};
  1266. ({variable, _}=Var, VarNamesAcc) ->
  1267. {{V, #ast_info{ var_names=[VarName] }}, _} = resolve_variable_ast(Var, true, TreeWalker),
  1268. {V, [VarName|VarNamesAcc]};
  1269. ({number_literal, _, Num}, VarNamesAcc) ->
  1270. {format_number_ast(erl_syntax:integer(Num)), VarNamesAcc};
  1271. (_, VarNamesAcc) ->
  1272. {[], VarNamesAcc}
  1273. end, [], Names),
  1274. {ForLoop, TreeWalker1} = resolve_reserved_variable('forloop', TreeWalker),
  1275. {{?Q("erlydtl_runtime:cycle({_@NamesTuple}, _@ForLoop)"),
  1276. #ast_info{ var_names = VarNames }},
  1277. TreeWalker1}.
  1278. %% Older Django templates treat cycle with comma-delimited elements as strings
  1279. cycle_compat_ast(Names, #treewalker{ context=Context }=TreeWalker) ->
  1280. NamesTuple = lists:map(
  1281. fun ({identifier, _, X}) ->
  1282. string_ast(X, Context)
  1283. end, Names),
  1284. {ForLoop, TreeWalker1} = resolve_reserved_variable('forloop', TreeWalker),
  1285. {{?Q("erlydtl_runtime:cycle({_@NamesTuple}, _@ForLoop)"),
  1286. #ast_info{}},
  1287. TreeWalker1}.
  1288. now_ast(FormatString, TreeWalker) ->
  1289. %% Note: we can't use unescape_string_literal here
  1290. %% because we want to allow escaping in the format string.
  1291. %% We only want to remove the surrounding escapes,
  1292. %% i.e. \"foo\" becomes "foo"
  1293. UnescapeOuter = string:strip(FormatString, both, 34),
  1294. {{StringAst, Info}, TreeWalker1} = string_ast(UnescapeOuter, TreeWalker),
  1295. {{?Q("erlydtl_dateformat:format(_@StringAst)"), Info}, TreeWalker1}.
  1296. spaceless_ast(Contents, TreeWalker) ->
  1297. {{Ast, Info}, TreeWalker1} = body_ast(Contents, TreeWalker),
  1298. {{?Q("erlydtl_runtime:spaceless(_@Ast)"), Info}, TreeWalker1}.
  1299. load_libs_ast(Libs, TreeWalker) ->
  1300. TreeWalker1 = lists:foldl(
  1301. fun ({identifier, Pos, Lib}, TW) ->
  1302. load_library(Pos, Lib, TW)
  1303. end,
  1304. TreeWalker, Libs),
  1305. empty_ast(TreeWalker1).
  1306. load_from_lib_ast(What, {identifier, Pos, Lib}, TreeWalker) ->
  1307. Names = lists:foldl(
  1308. fun ({identifier, _, Name}, Acc) -> [Name|Acc] end,
  1309. [], What),
  1310. empty_ast(load_library(Pos, Lib, Names, TreeWalker)).
  1311. %%-------------------------------------------------------------------
  1312. %% Custom tags
  1313. %%-------------------------------------------------------------------
  1314. interpret_value({trans, StringLiteral}, TreeWalker) ->
  1315. translated_ast(StringLiteral, TreeWalker);
  1316. interpret_value(Value, TreeWalker) ->
  1317. value_ast(Value, false, false, TreeWalker).
  1318. interpret_args(Args, TreeWalker) ->
  1319. lists:foldr(
  1320. fun ({{identifier, _, Key}, Value}, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
  1321. {{Ast0, AstInfo0}, TreeWalker0} = interpret_value(Value, TreeWalkerAcc),
  1322. {{[?Q("{_@Key@, _@Ast0}")|ArgsAcc], merge_info(AstInfo0, AstInfoAcc)}, TreeWalker0};
  1323. (Value, {{ArgsAcc, AstInfoAcc}, TreeWalkerAcc}) ->
  1324. {{Ast0, AstInfo0}, TreeWalker0} = value_ast(Value, false, false, TreeWalkerAcc),
  1325. {{[Ast0|ArgsAcc], merge_info(AstInfo0, AstInfoAcc)}, TreeWalker0}
  1326. end, {{[], #ast_info{}}, TreeWalker}, Args).
  1327. tag_ast(Name, Args, TreeWalker) ->
  1328. {{InterpretedArgs, AstInfo1}, TreeWalker1} = interpret_args(Args, TreeWalker),
  1329. {{RenderAst, RenderInfo}, TreeWalker2} = custom_tags_modules_ast(Name, InterpretedArgs, TreeWalker1),
  1330. {{RenderAst, merge_info(AstInfo1, RenderInfo)}, TreeWalker2}.
  1331. custom_tags_modules_ast({identifier, Pos, Name}, InterpretedArgs,
  1332. #treewalker{
  1333. context=#dtl_context{
  1334. tags = Tags,
  1335. module = Module,
  1336. is_compiling_dir=IsCompilingDir
  1337. }
  1338. }=TreeWalker) ->
  1339. case proplists:get_value(Name, Tags) of
  1340. {Mod, Fun}=Tag ->
  1341. case lists:max([-1] ++ [I || {N,I} <- Mod:module_info(exports), N =:= Fun]) of
  1342. 2 ->
  1343. {{?Q("'@Mod@':'@Fun@'([_@InterpretedArgs], RenderOptions)"),
  1344. #ast_info{}}, TreeWalker};
  1345. 1 ->
  1346. {{?Q("'@Mod@':'@Fun@'([_@InterpretedArgs])"),
  1347. #ast_info{}}, TreeWalker};
  1348. -1 ->
  1349. empty_ast(?WARN({Pos, {missing_tag, Name, Tag}}, TreeWalker));
  1350. I ->
  1351. empty_ast(?WARN({Pos, {bad_tag, Name, Tag, I}}, TreeWalker))
  1352. end;
  1353. undefined ->
  1354. if IsCompilingDir =/= false ->
  1355. {{?Q("'@Module@':'@Name@'([_@InterpretedArgs], RenderOptions)"),
  1356. #ast_info{ custom_tags = [Name] }}, TreeWalker};
  1357. true ->
  1358. {{?Q("render_tag(_@Name@, [_@InterpretedArgs], RenderOptions)"),
  1359. #ast_info{ custom_tags = [Name] }}, TreeWalker}
  1360. end
  1361. end.
  1362. call_ast(Module, TreeWalker) ->
  1363. call_ast(Module, merl:var("_Variables"), #ast_info{}, TreeWalker).
  1364. call_with_ast(Module, Variable, TreeWalker) ->
  1365. {{VarAst, VarInfo}, TreeWalker2} = resolve_variable_ast(Variable, false, TreeWalker),
  1366. call_ast(Module, VarAst, VarInfo, TreeWalker2).
  1367. call_ast(Module, Variable, AstInfo, TreeWalker) ->
  1368. Ast = ?Q(["case '@Module@':render(_@Variable, RenderOptions) of",
  1369. " {ok, Rendered} -> Rendered;",
  1370. " {error, Reason} -> io_lib:format(\"error: ~p\", [Reason])",
  1371. "end"]),
  1372. with_dependencies(Module:dependencies(), {{Ast, AstInfo}, TreeWalker}).
  1373. create_scope(Vars, VarScope) ->
  1374. {Scope, Values} =
  1375. lists:foldl(
  1376. fun ({Name, Value}, {VarAcc, ValueAcc}) ->
  1377. NameAst = varname_ast(lists:concat(["_", Name, VarScope])),
  1378. {[{Name, NameAst}|VarAcc],
  1379. [?Q("_@NameAst = _@Value")|ValueAcc]
  1380. }
  1381. end,
  1382. empty_scope(),
  1383. Vars),
  1384. {Scope, [Values]}.
  1385. create_scope(Vars, {Row, Col}, #treewalker{ context=Context }) ->
  1386. Level = length(Context#dtl_context.local_scopes),
  1387. create_scope(Vars, lists:concat(["/", Level, "_", Row, ":", Col])).
  1388. varname_ast([$_|VarName]) ->
  1389. merl:var(lists:concat(["_Var__", VarName]));
  1390. varname_ast(VarName) ->
  1391. merl:var(lists:concat(["Var_", VarName])).