erlydtl_compiler.erl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. %%%-------------------------------------------------------------------
  2. %%% File: erlydtl_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
  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_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_file/3, compile_template/3, compile_dir/3,
  46. format_error/1, default_options/0]).
  47. %% internal use
  48. -export([parse_file/2, parse_template/2, do_parse_template/2]).
  49. -import(erlydtl_compiler_utils,
  50. [add_filters/2, add_tags/2, call_extension/3,
  51. load_library/2, shorten_filename/1, get_current_file/1]).
  52. -include("erlydtl_ext.hrl").
  53. %% --------------------------------------------------------------------
  54. %% API
  55. %% --------------------------------------------------------------------
  56. default_options() -> [verbose, report].
  57. compile_template(Template, Module, Options) ->
  58. Context = process_opts(undefined, Module, Options),
  59. Bin = iolist_to_binary(Template),
  60. ?LOG_INFO("Compile template: ~32s~s~n",
  61. [Bin, if size(Bin) > 32 -> "...";
  62. true -> ""
  63. end],
  64. Context),
  65. compile(Context#dtl_context{ bin = Bin }).
  66. compile_file(File, Module, Options) ->
  67. Context = process_opts(File, Module, Options),
  68. ?LOG_INFO("Compile file: ~s~n", [File], Context),
  69. compile(Context).
  70. compile_dir(Dir, Module, Options) ->
  71. Context = process_opts({dir, Dir}, Module, Options),
  72. ?LOG_INFO("Compile directory: ~s~n", [Dir], Context),
  73. compile(Context).
  74. format_error({read_file, Error}) ->
  75. io_lib:format(
  76. "Failed to read file: ~s",
  77. [file:format_error(Error)]);
  78. format_error({read_file, File, Error}) ->
  79. io_lib:format(
  80. "Failed to include file ~s: ~s",
  81. [File, file:format_error(Error)]);
  82. format_error(Error) ->
  83. erlydtl_compiler_utils:format_error(Error).
  84. %%====================================================================
  85. %% Internal functions
  86. %%====================================================================
  87. process_opts(File, Module, Options0) ->
  88. Options1 = proplists:normalize(
  89. update_defaults(Options0),
  90. [{aliases, [{outdir, out_dir}]}
  91. ]),
  92. Source0 = filename:absname(
  93. case File of
  94. undefined ->
  95. filename:join(
  96. [case proplists:get_value(out_dir, Options1, false) of
  97. false -> ".";
  98. OutDir -> OutDir
  99. end,
  100. Module]);
  101. {dir, Dir} ->
  102. Dir;
  103. _ ->
  104. File
  105. end),
  106. Source = shorten_filename(Source0),
  107. Options = [{compiler_options, [{source, Source}]}
  108. |compiler_opts(Options1, [])],
  109. case File of
  110. {dir, _} ->
  111. init_context([], Source, Module, Options);
  112. _ ->
  113. init_context([Source], filename:dirname(Source), Module, Options)
  114. end.
  115. compiler_opts([CompilerOption|Os], Acc)
  116. when
  117. CompilerOption =:= return;
  118. CompilerOption =:= return_warnings;
  119. CompilerOption =:= return_errors;
  120. CompilerOption =:= report;
  121. CompilerOption =:= report_warnings;
  122. CompilerOption =:= report_errors;
  123. CompilerOption =:= warnings_as_errors;
  124. CompilerOption =:= verbose;
  125. CompilerOption =:= debug_info ->
  126. compiler_opts(Os, [CompilerOption, {compiler_options, [CompilerOption]}|Acc]);
  127. compiler_opts([O|Os], Acc) ->
  128. compiler_opts(Os, [O|Acc]);
  129. compiler_opts([], Acc) ->
  130. lists:reverse(Acc).
  131. update_defaults(Options) ->
  132. maybe_add_env_default_opts(Options).
  133. maybe_add_env_default_opts(Options) ->
  134. case proplists:get_bool(no_env, Options) of
  135. true -> Options;
  136. _ -> Options ++ env_default_opts()
  137. end.
  138. %% shamelessly borrowed from:
  139. %% https://github.com/erlang/otp/blob/21095e6830f37676dd29c33a590851ba2c76499b/\
  140. %% lib/compiler/src/compile.erl#L128
  141. env_default_opts() ->
  142. Key = "ERLYDTL_COMPILER_OPTIONS",
  143. case os:getenv(Key) of
  144. false -> [];
  145. Str when is_list(Str) ->
  146. case erl_scan:string(Str) of
  147. {ok,Tokens,_} ->
  148. case erl_parse:parse_term(Tokens ++ [{dot, 1}]) of
  149. {ok,List} when is_list(List) -> List;
  150. {ok,Term} -> [Term];
  151. {error,_Reason} ->
  152. io:format("Ignoring bad term in ~s\n", [Key]),
  153. []
  154. end;
  155. {error, {_,_,_Reason}, _} ->
  156. io:format("Ignoring bad term in ~s\n", [Key]),
  157. []
  158. end
  159. end.
  160. compile(Context) ->
  161. Context1 = do_compile(Context),
  162. collect_result(Context1).
  163. collect_result(#dtl_context{
  164. module=Module,
  165. errors=#error_info{ list=[] },
  166. warnings=Ws }=Context) ->
  167. Info = case Ws of
  168. #error_info{ return=true, list=Warnings } ->
  169. [pack_error_list(Warnings)];
  170. _ ->
  171. []
  172. end,
  173. Res = case proplists:get_bool(binary, Context#dtl_context.all_options) of
  174. true ->
  175. [ok, Module, Context#dtl_context.bin | Info];
  176. false ->
  177. [ok, Module | Info]
  178. end,
  179. list_to_tuple(Res);
  180. collect_result(#dtl_context{ errors=Es, warnings=Ws }) ->
  181. if Es#error_info.return ->
  182. {error,
  183. pack_error_list(Es#error_info.list),
  184. case Ws of
  185. #error_info{ list=L } ->
  186. pack_error_list(L);
  187. _ ->
  188. []
  189. end};
  190. true -> error
  191. end.
  192. init_context(ParseTrail, DefDir, Module, Options) when is_list(Module) ->
  193. init_context(ParseTrail, DefDir, list_to_atom(Module), Options);
  194. init_context(ParseTrail, DefDir, Module, Options) ->
  195. Ctx = #dtl_context{},
  196. Locale = proplists:get_value(locale, Options),
  197. BlocktransLocales = proplists:get_value(blocktrans_locales, Options),
  198. TransLocales = case {Locale, BlocktransLocales} of
  199. {undefined, undefined} -> Ctx#dtl_context.trans_locales;
  200. {undefined, Val} -> Val;
  201. {Val, undefined} -> [Val];
  202. _ -> lists:usort([Locale | BlocktransLocales])
  203. end,
  204. Context0 =
  205. #dtl_context{
  206. all_options = Options,
  207. auto_escape = case proplists:get_value(auto_escape, Options, true) of
  208. true -> [on];
  209. _ -> [off]
  210. end,
  211. parse_trail = ParseTrail,
  212. module = Module,
  213. doc_root = proplists:get_value(doc_root, Options, DefDir),
  214. libraries = proplists:get_value(libraries, Options, Ctx#dtl_context.libraries),
  215. custom_tags_dir = proplists:get_value(
  216. custom_tags_dir, Options,
  217. filename:join([erlydtl_deps:get_base_dir(), "priv", "custom_tags"])),
  218. trans_fun = proplists:get_value(blocktrans_fun, Options, Ctx#dtl_context.trans_fun),
  219. trans_locales = TransLocales,
  220. vars = proplists:get_value(vars, Options, Ctx#dtl_context.vars),
  221. reader = proplists:get_value(reader, Options, Ctx#dtl_context.reader),
  222. compiler_options = proplists:append_values(compiler_options, Options),
  223. binary_strings = proplists:get_value(binary_strings, Options, Ctx#dtl_context.binary_strings),
  224. force_recompile = proplists:get_bool(force_recompile, Options),
  225. verbose = length(proplists:get_all_values(verbose, Options)),
  226. is_compiling_dir = if ParseTrail == [] -> DefDir; true -> false end,
  227. extension_module = proplists:get_value(extension_module, Options, Ctx#dtl_context.extension_module),
  228. scanner_module = proplists:get_value(scanner_module, Options, Ctx#dtl_context.scanner_module),
  229. record_info = [{R, lists:zip(I, lists:seq(2, length(I) + 1))}
  230. || {R, I} <- proplists:get_value(record_info, Options, Ctx#dtl_context.record_info)],
  231. errors = init_error_info(errors, Ctx#dtl_context.errors, Options),
  232. warnings = init_error_info(warnings, Ctx#dtl_context.warnings, Options)
  233. },
  234. Context = load_libraries(proplists:get_value(default_libraries, Options, []), Context0),
  235. case call_extension(Context, init_context, [Context]) of
  236. {ok, C} when is_record(C, dtl_context) -> C;
  237. undefined -> Context
  238. end.
  239. init_error_info(warnings, Ei, Options) ->
  240. case proplists:get_bool(warnings_as_errors, Options) of
  241. true -> warnings_as_errors;
  242. false ->
  243. init_error_info(get_error_info_opts(warnings, Options), Ei)
  244. end;
  245. init_error_info(Class, Ei, Options) ->
  246. init_error_info(get_error_info_opts(Class, Options), Ei).
  247. init_error_info([{return, true}|Flags], #error_info{ return = false }=Ei) ->
  248. init_error_info(Flags, Ei#error_info{ return = true });
  249. init_error_info([{report, true}|Flags], #error_info{ report = false }=Ei) ->
  250. init_error_info(Flags, Ei#error_info{ report = true });
  251. init_error_info([_|Flags], Ei) ->
  252. init_error_info(Flags, Ei);
  253. init_error_info([], Ei) -> Ei.
  254. get_error_info_opts(Class, Options) ->
  255. Flags = case Class of
  256. errors ->
  257. [return, report, {return_errors, return}, {report_errors, report}];
  258. warnings ->
  259. [return, report, {return_warnings, return}, {report_warnings, report}]
  260. end,
  261. [begin
  262. {Key, Value} = if is_atom(Flag) -> {Flag, Flag};
  263. true -> Flag
  264. end,
  265. {Value, proplists:get_bool(Key, Options)}
  266. end || Flag <- Flags].
  267. load_libraries([], #dtl_context{ all_options=Options }=Context) ->
  268. %% Load filters and tags passed using the old options
  269. Filters = proplists:get_value(custom_filters_modules, Options, []) ++ [erlydtl_filters],
  270. Tags = proplists:get_value(custom_tags_modules, Options, []),
  271. load_legacy_filters(Filters, load_legacy_tags(Tags, Context));
  272. load_libraries([Lib|Libs], Context) ->
  273. load_libraries(Libs, load_library(Lib, Context)).
  274. load_legacy_filters([], Context) -> Context;
  275. load_legacy_filters([Mod|Mods], Context) ->
  276. {Filters, Context1} = read_legacy_library(Mod, Context),
  277. load_legacy_filters(Mods, add_filters(Filters, Context1)).
  278. load_legacy_tags([], Context) -> Context;
  279. load_legacy_tags([Mod|Mods], Context) ->
  280. {Tags, Context1} = read_legacy_library(Mod, Context),
  281. load_legacy_tags(Mods, add_tags(Tags, Context1)).
  282. read_legacy_library(Mod, Context) ->
  283. case code:ensure_loaded(Mod) of
  284. {module, Mod} ->
  285. {[{Name, {Mod, Name}}
  286. || {Name, _} <- lists:ukeysort(1, Mod:module_info(exports)),
  287. Name =/= module_info
  288. ], Context};
  289. {error, Reason} ->
  290. {[], ?WARN({load_library, '(custom-legacy)', Mod, Reason}, Context)}
  291. end.
  292. is_up_to_date(_, #dtl_context{force_recompile = true}) ->
  293. false;
  294. is_up_to_date(CheckSum, Context) ->
  295. erlydtl_beam_compiler:is_up_to_date(CheckSum, Context).
  296. parse_file(File, Context) ->
  297. {M, F} = Context#dtl_context.reader,
  298. case catch M:F(File) of
  299. {ok, Data} ->
  300. parse_template(Data, Context);
  301. {error, Reason} ->
  302. {error, {read_file, File, Reason}}
  303. end.
  304. parse_template(Data, Context) ->
  305. CheckSum = binary_to_list(erlang:md5(Data)),
  306. case is_up_to_date(CheckSum, Context) of
  307. true -> up_to_date;
  308. false ->
  309. case do_parse_template(Data, Context) of
  310. {ok, Val} -> {ok, Val, CheckSum};
  311. Err -> Err
  312. end
  313. end.
  314. do_parse_template(Data, #dtl_context{ scanner_module=Scanner }=Context) ->
  315. check_scan(
  316. apply(Scanner, scan, [Data]),
  317. Context).
  318. check_scan({ok, Tokens}, Context) ->
  319. Tokens1 = case call_extension(Context, post_scan, [Tokens]) of
  320. undefined -> Tokens;
  321. {ok, T} -> T
  322. end,
  323. check_parse(erlydtl_parser:parse(Tokens1), [], Context#dtl_context{ scanned_tokens=Tokens1 });
  324. check_scan({error, Err, State}, Context) ->
  325. case call_extension(Context, scan, [State]) of
  326. undefined ->
  327. {error, Err};
  328. {ok, NewState} ->
  329. check_scan(apply(Context#dtl_context.scanner_module, resume, [NewState]), Context);
  330. ExtRes ->
  331. ExtRes
  332. end;
  333. check_scan({error, _}=Error, _Context) ->
  334. Error.
  335. check_parse({ok, _}=Ok, [], _Context) -> Ok;
  336. check_parse({ok, Parsed}, Acc, _Context) -> {ok, Acc ++ Parsed};
  337. check_parse({error, _}=Err, _, _Context) -> Err;
  338. check_parse({error, Err, State}, Acc, Context) ->
  339. {State1, Parsed} = reset_parse_state(State, Context),
  340. case call_extension(Context, parse, [State1]) of
  341. undefined ->
  342. {error, Err};
  343. {ok, ExtParsed} ->
  344. {ok, Acc ++ Parsed ++ ExtParsed};
  345. {error, ExtErr, ExtState} ->
  346. case reset_parse_state(ExtState, Context) of
  347. {_, []} ->
  348. %% todo: see if this is indeed a sensible ext error,
  349. %% or if we should rather present the original Err message
  350. {error, ExtErr};
  351. {State2, ExtParsed} ->
  352. check_parse(erlydtl_parser:resume(State2), Acc ++ Parsed ++ ExtParsed, Context)
  353. end;
  354. ExtRes ->
  355. ExtRes
  356. end.
  357. %% backtrack up to the nearest opening tag, and keep the value stack parsed ok so far
  358. reset_parse_state([[{Tag, _, _}|_]=Ts, Tzr, _, _, Stack], Context)
  359. when Tag==open_tag; Tag==open_var ->
  360. %% reached opening tag, so the stack should be sensible here
  361. {[reset_token_stream(Ts, Context#dtl_context.scanned_tokens),
  362. Tzr, 0, [], []], lists:flatten(Stack)};
  363. reset_parse_state([_, _, 0, [], []]=State, _Context) ->
  364. %% top of (empty) stack
  365. {State, []};
  366. reset_parse_state([Ts, Tzr, _, [0 | []], [Parsed | []]], Context)
  367. when is_list(Parsed) ->
  368. %% top of good stack
  369. {[reset_token_stream(Ts, Context#dtl_context.scanned_tokens),
  370. Tzr, 0, [], []], Parsed};
  371. reset_parse_state([Ts, Tzr, _, [S | Ss], [T | Stack]], Context) ->
  372. %% backtrack...
  373. reset_parse_state([[T|Ts], Tzr, S, Ss, Stack], Context).
  374. reset_token_stream([T|_], [T|Ts]) -> [T|Ts];
  375. reset_token_stream(Ts, [_|S]) ->
  376. reset_token_stream(Ts, S).
  377. %% we should find the next token in the list of scanned tokens, or something is real fishy
  378. pack_error_list(Es) ->
  379. collect_error_info([], Es, []).
  380. collect_error_info([], [], Acc) ->
  381. lists:reverse(Acc);
  382. collect_error_info([{File, ErrorInfo}|Es], Rest, [{File, FEs}|Acc]) ->
  383. collect_error_info(Es, Rest, [{File, ErrorInfo ++ FEs}|Acc]);
  384. collect_error_info([E|Es], Rest, Acc) ->
  385. collect_error_info(Es, [E|Rest], Acc);
  386. collect_error_info([], Rest, Acc) ->
  387. case lists:reverse(Rest) of
  388. [E|Es] ->
  389. collect_error_info(Es, [], [E|Acc])
  390. end.
  391. do_compile(#dtl_context{ is_compiling_dir=false, bin=undefined }=Context) ->
  392. compile_output(parse_file(get_current_file(Context), Context), Context);
  393. do_compile(#dtl_context{ is_compiling_dir=false, bin=Template }=Context) ->
  394. compile_output(parse_template(Template, Context), Context);
  395. do_compile(#dtl_context{ is_compiling_dir=Dir }=Context) ->
  396. erlydtl_beam_compiler:compile_dir(Dir, Context).
  397. compile_output(up_to_date, Context) -> Context;
  398. compile_output({ok, DjangoParseTree, CheckSum}, Context) ->
  399. erlydtl_beam_compiler:compile(DjangoParseTree, CheckSum, Context#dtl_context{ bin=undefined });
  400. compile_output({error, Reason}, Context) -> ?ERR(Reason, Context).