erlydtl_compiler.erl 17 KB

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