1%% ===================================================================== 2%% Licensed under the Apache License, Version 2.0 (the "License"); you may 3%% not use this file except in compliance with the License. You may obtain 4%% a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0> 5%% 6%% Unless required by applicable law or agreed to in writing, software 7%% distributed under the License is distributed on an "AS IS" BASIS, 8%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 9%% See the License for the specific language governing permissions and 10%% limitations under the License. 11%% 12%% Alternatively, you may use this file under the terms of the GNU Lesser 13%% General Public License (the "LGPL") as published by the Free Software 14%% Foundation; either version 2.1, or (at your option) any later version. 15%% If you wish to allow use of your version of this file only under the 16%% terms of the LGPL, you should delete the provisions above and replace 17%% them with the notice and other provisions required by the LGPL; see 18%% <http://www.gnu.org/licenses/>. If you do not delete the provisions 19%% above, a recipient may use your version of this file under the terms of 20%% either the Apache License or the LGPL. 21%% 22%% @private 23%% @copyright 2001-2005 Richard Carlsson 24%% @author Richard Carlsson <carlsson.richard@gmail.com> 25%% @see edoc 26%% @end 27%% ===================================================================== 28 29%% @doc EDoc macro expansion 30 31-module(edoc_macros). 32 33-export([expand_tags/3, std_macros/1, check_defs/1]). 34 35-import(edoc_report, [report/2, error/3, warning/4]). 36 37-include("edoc.hrl"). 38-include("edoc_types.hrl"). 39 40-define(DEFAULT_XML_EXPORT, xmerl_html). 41 42 43std_macros(Env) -> 44 (if Env#env.module =:= [] -> []; 45 true -> [{module, atom_to_list(Env#env.module)}] 46 end 47 ++ 48 [{date, fun date_macro/3}, 49 {docRoot, Env#env.root}, 50 {link, fun link_macro/3}, 51 {section, fun section_macro/3}, 52 {time, fun time_macro/3}, 53 {type, fun type_macro/3}, 54 {version, fun version_macro/3}]). 55 56 57%% Check well-formedness of user-specified list of macro definitions. 58 59check_defs([{K, D} | Ds]) when is_atom(K), is_list(D) -> 60 check_defs(Ds); 61check_defs([X | _Ds]) -> 62 report("bad macro definition: ~P.", [X, 10]), 63 exit(error); 64check_defs([]) -> 65 ok. 66 67%% Code for special macros should throw {error, Line, Reason} for error 68%% reporting, where Reason and Line are passed to edoc_report:error(...) 69%% together with the file name etc. The expanded text must be flat! 70 71date_macro(_S, _Line, _Env) -> 72 edoc_lib:datestr(date()). 73 74time_macro(_S, _Line, _Env) -> 75 edoc_lib:timestr(time()). 76 77version_macro(S, Line, Env) -> 78 date_macro(S, Line, Env) 79 ++ " " ++ time_macro(S, Line, Env). 80 81link_macro(S, Line, Env) -> 82 {S1, S2} = edoc_lib:split_at_stop(S), 83 Ref = edoc_parser:parse_ref(S1, Line), 84 URI = edoc_refs:get_uri(Ref, Env), 85 Txt = if S2 =:= [] -> "<code>" ++ S1 ++ "</code>"; 86 true -> S2 87 end, 88 Target = case edoc_refs:is_top(Ref, Env) of 89 true -> " target=\"_top\""; % note the initial space 90 false -> "" 91 end, 92 lists:flatten(io_lib:fwrite("<a href=\"~ts\"~ts>~ts</a>", 93 [URI, Target, Txt])). 94 95section_macro(S, _Line, _Env) -> 96 S1 = lists:reverse(edoc_lib:strip_space( 97 lists:reverse(edoc_lib:strip_space(S)))), 98 lists:flatten(io_lib:format("<a href=\"#~ts\">~ts</a>", 99 [edoc_lib:to_label(S1), S1])). 100 101type_macro(S, Line, Env) -> 102 S1 = "t()=" ++ S, 103 Def = edoc_parser:parse_typedef(S1, Line), 104 {#t_typedef{type = T}, _} = Def, 105 Txt = edoc_layout:type(edoc_data:type(T, Env)), 106 lists:flatten(io_lib:fwrite("<code>~ts</code>", [Txt])). 107 108 109%% Expand inline macros in tag content. 110 111expand_tags(Ts, Env, Where) -> 112 Defs = dict:from_list(lists:reverse(Env#env.macros)), 113 expand_tags(Ts, Defs, Env, Where). 114 115expand_tags([#tag{data = Cs, line = L} = T | Ts], Defs, Env, Where) -> 116 [T#tag{data = expand_tag(Cs, L, Defs, Env, Where)} 117 | expand_tags(Ts, Defs, Env, Where)]; 118expand_tags([T | Ts], Defs, Env, Where) -> 119 [T | expand_tags(Ts, Defs, Env, Where)]; 120expand_tags([], _, _, _) -> 121 []. 122 123expand_tag(Cs, L, Defs, Env, Where) -> 124 case catch {ok, expand_text(Cs, L, Defs, Env, Where)} of 125 {ok, Cs1} -> 126 lists:reverse(Cs1); 127 {'EXIT', R} -> 128 exit(R); 129 {error, L1, Error} -> 130 error(L1, Where, Error), 131 exit(error); 132 Other -> 133 throw(Other) 134 end. 135 136%% Expand macros in arbitrary lines of text. 137%% The result is in reverse order. 138 139-record(state, {where, env, seen}). 140 141expand_text(Cs, L, Defs, Env, Where) -> 142 St = #state{where = Where, 143 env = Env, 144 seen = sets:new()}, 145 expand(Cs, L, Defs, St, []). 146 147%% Inline macro syntax: "{@name content}" 148%% where 'content' is optional, and separated from 'name' by one or 149%% more whitespace characters. The content is bound to the '{@?}' 150%% parameter variable, and the macro definition is expanded and 151%% substituted for the call. Recursion is detected and reported as an 152%% error, since there are (currently) no control flow operators. 153%% Escape sequences: 154%% "@{" -> "{" 155%% "@}" -> "}" 156%% "@@" -> "@" 157 158expand([$@, $@ | Cs], L, Defs, St, As) -> 159 expand(Cs, L, Defs, St, [$@ | As]); 160expand([$@, ${ | Cs], L, Defs, St, As) -> 161 expand(Cs, L, Defs, St, [${ | As]); 162expand([$@, $} | Cs], L, Defs, St, As) -> 163 expand(Cs, L, Defs, St, [$} | As]); 164expand([${, $@ | Cs], L, Defs, St, As) -> 165 expand_macro(Cs, L, Defs, St, As); 166expand([$\n = C | Cs], L, Defs, St, As) -> 167 expand(Cs, L + 1, Defs, St, [C | As]); 168expand([C | Cs], L, Defs, St, As) -> 169 expand(Cs, L, Defs, St, [C | As]); 170expand([], _, _, _, As) -> 171 As. 172 173expand_macro(Cs, L, Defs, St, As) -> 174 {M, Cs1, L1} = macro_name(Cs, L), 175 {Arg, Cs2, L2} = macro_content(Cs1, L1), 176 As1 = expand_macro_def(M, Arg, L, Defs, St, As), 177 expand(Cs2, L2, Defs, St, As1). 178 179%% The macro argument (the "content") is expanded in the environment of 180%% the call, and the result is bound to the '{@?}' parameter. The result 181%% of the macro expansion is then expanded again. This allows macro 182%% definitions to contain calls to other macros, avoids name capture of 183%% '{@?}', and makes it easier to write handler functions for special 184%% macros such as '{@link ...}', since the argument is already expanded. 185 186expand_macro_def(M, Arg, L, Defs, St, As) -> 187 Seen = St#state.seen, 188 case sets:is_element(M, Seen) of 189 true -> 190 throw_error(L, {"recursive macro expansion of {@~s}.", 191 [M]}); 192 false -> 193 Arg1 = lists:reverse(expand(Arg, L, Defs, St, [])), 194 Defs1 = dict:store('?', Arg1, Defs), 195 St1 = St#state{seen = sets:add_element(M, Seen)}, 196 case dict:find(M, Defs) of 197 {ok, Def} -> 198 Txt = if is_function(Def) -> 199 Def(Arg1, L, St1#state.env); 200 is_list(Def) -> 201 Def 202 end, 203 expand(Txt, L, Defs1, St1, As); 204 error -> 205 warning(L, St1#state.where, 206 "undefined macro {@~s}.", [M]), 207 "??" 208 end 209 end. 210 211%% The macro name ends at the first whitespace or '}' character. The 212%% content, if any, starts at the next non-whitespace character. 213 214%% See edoc_tags:scan_tag/is_name/1 for details on what is a valid 215%% name. In macro names we also allow '?' as the initial character. 216 217macro_name(Cs, L) -> 218 macro_name(Cs, [], L). 219 220macro_name([C | Cs], As, L) when C >= $a, C =< $z -> 221 macro_name_1(Cs, [C | As], L); 222macro_name([C | Cs], As, L) when C >= $A, C =< $Z -> 223 macro_name_1(Cs, [C | As], L); 224macro_name([C | Cs], As, L) when C >= $\300, C =< $\377, 225 C =/= $\327, C =/= $\367 -> 226 macro_name_1(Cs, [C | As], L); 227macro_name([$_ | Cs], As, L) -> 228 macro_name_1(Cs, [$_ | As], L); 229macro_name([$? | Cs], As, L) -> 230 macro_name_1(Cs, [$? | As], L); 231macro_name([$\s | _Cs], _As, L) -> 232 throw_error(L, macro_name); 233macro_name([$\t | _Cs], _As, L) -> 234 throw_error(L, macro_name); 235macro_name([$\n | _Cs], _As, L) -> 236 throw_error(L, macro_name); 237macro_name([C | _Cs], As, L) -> 238 throw_error(L, {macro_name, [C | As]}); 239macro_name([], _As, L) -> 240 throw_error(L, macro_name). 241 242macro_name_1([C | Cs], As, L) when C >= $a, C =< $z -> 243 macro_name_1(Cs, [C | As], L); 244macro_name_1([C | Cs], As, L) when C >= $A, C =< $Z -> 245 macro_name_1(Cs, [C | As], L); 246macro_name_1([C | Cs], As, L) when C >= $0, C =< $9 -> 247 macro_name_1(Cs, [C | As], L); 248macro_name_1([C | Cs], As, L) when C >= $\300, C =< $\377, 249 C =/= $\327, C =/= $\367 -> 250 macro_name_1(Cs, [C | As], L); 251macro_name_1([$_ | Cs], As, L) -> 252 macro_name_1(Cs, [$_ | As], L); 253macro_name_1([$\s | Cs], As, L) -> 254 macro_name_2(Cs, As, L); 255macro_name_1([$\t | Cs], As, L) -> 256 macro_name_2(Cs, As, L); 257macro_name_1([$\n | Cs], As, L) -> 258 macro_name_2(Cs, As, L + 1); 259macro_name_1([$} | _] = Cs, As, L) -> 260 macro_name_3(Cs, As, L); 261macro_name_1([C | _Cs], As, L) -> 262 throw_error(L, {macro_name, [C | As]}); 263macro_name_1([], _As, L) -> 264 throw_error(L, unterminated_macro). 265 266macro_name_2([$\s | Cs], As, L) -> 267 macro_name_2(Cs, As, L); 268macro_name_2([$\t | Cs], As, L) -> 269 macro_name_2(Cs, As, L); 270macro_name_2([$\n | Cs], As, L) -> 271 macro_name_2(Cs, As, L + 1); 272macro_name_2([_ | _] = Cs, As, L) -> 273 macro_name_3(Cs, As, L); 274macro_name_2([], _As, L) -> 275 throw_error(L, unterminated_macro). 276 277macro_name_3(Cs, As, L) -> 278 {list_to_atom(lists:reverse(As)), Cs, L}. 279 280 281%% The macro content ends at the first non-escaped '}' character that is 282%% not balanced by a corresponding non-escaped '{@' sequence. 283%% Escape sequences are those defined above. 284 285macro_content(Cs, L) -> 286 %% If there is an error, we report the start line, not the end line. 287 case catch {ok, macro_content(Cs, [], L, 0)} of 288 {ok, X} -> 289 X; 290 {'EXIT', R} -> 291 exit(R); 292 'end' -> 293 throw_error(L, unterminated_macro); 294 Other -> 295 throw(Other) 296 end. 297 298%% @throws 'end' 299 300macro_content([$@, $@ | Cs], As, L, N) -> 301 macro_content(Cs, [$@, $@ | As], L, N); % escaped '@' 302macro_content([$@, $} | Cs], As, L, N) -> 303 macro_content(Cs, [$}, $@ | As], L, N); % escaped '}' 304macro_content([$@, ${ | Cs], As, L, N) -> 305 macro_content(Cs, [${, $@ | As], L, N); % escaped '{' 306macro_content([${, $@ | Cs], As, L, N) -> 307 macro_content(Cs, [$@, ${ | As], L, N + 1); 308macro_content([$} | Cs], As, L, 0) -> 309 {lists:reverse(As), Cs, L}; 310macro_content([$} | Cs], As, L, N) -> 311 macro_content(Cs, [$} | As], L, N - 1); 312macro_content([$\n = C | Cs], As, L, N) -> 313 macro_content(Cs, [C | As], L + 1, N); 314macro_content([C | Cs], As, L, N) -> 315 macro_content(Cs, [C | As], L, N); 316macro_content([], _As, _L, _N) -> 317 throw('end'). 318 319-type line() :: erl_anno:line(). 320-type err() :: 'unterminated_macro' 321 | 'macro_name' 322 | {'macro_name', string()} 323 | {string(), [string()]}. 324 325-spec throw_error(line(), err()) -> no_return(). 326 327throw_error(L, unterminated_macro) -> 328 throw_error(L, {"unexpected end of macro.", []}); 329throw_error(L, macro_name) -> 330 throw_error(L, {"missing macro name.", []}); 331throw_error(L, {macro_name, S}) -> 332 throw_error(L, {"bad macro name: '@~s...'.", [lists:reverse(S)]}); 333throw_error(L, D) -> 334 throw({error, L, D}). 335