1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2006-2020, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(pldoc_process,
[ doc_comment/4, % ?Object, ?Pos, ?Summary, ?Comment
doc_file_has_comments/1, % +File
is_structured_comment/2, % +Comment, -Prefixes
parse_comment/3, % +Comment, +FilePos, -Parsed
comment_modes/2, % +Comment, -Synopsis
process_comments/3, % +Comments, +StartTermPos, +File
doc_file_name/3, % +Source, -Doc, +Options
doc_clean/1 % +Module
]).
:- dynamic user:file_search_path/2.
:- multifile user:file_search_path/2.
user:file_search_path(pldoc, library(pldoc)).
:- use_module(pldoc(doc_register)).
:- use_module(pldoc(doc_modes)).
:- use_module(pldoc(doc_wiki)).
:- use_module(library(debug)).
:- use_module(library(option)).
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(operators)).
:- use_module(library(prolog_source)).
/** <module> Process source documentation
The pldoc module processes structured comments in Prolog source files into
well formatted HTML documents.
@author Jan Wielemaker
@license GPL
*/
:- predicate_options(doc_file_name/3, 3,
[ format(oneof([html,tex]))
]).
%! prolog:predicate_summary(+PI, -Summary) is semidet.
%
% Provide predicate summaries to the XPCE class
% =prolog_predicate=, used by the IDE tools.
:- multifile
prolog:predicate_summary/2. % ?PI, -Summary
%! is_structured_comment(+Comment:string,
%! -Prefixes:list(codes)) is semidet.
%
% True if Comment is a structured comment that should use Prefixes
% to extract the plain text using indented_lines/3.
is_structured_comment(Comment, Prefixes) :-
is_structured_comment(Comment, Prefixes, _Style).
is_structured_comment(_Pos-Comment, Prefixes, Style) :-
!,
is_structured_comment(Comment, Prefixes, Style).
is_structured_comment(Comment, Prefixes, Style) :-
is_list(Comment),
!,
( phrase(structured_comment(Prefixes, Style), Comment, _)
-> true
).
is_structured_comment(Comment, Prefixes, Style) :-
atom_string(CommentA, Comment),
structured_command_start(Start, Prefixes, Style),
sub_atom(CommentA, 0, Len, _, Start),
!,
sub_atom(CommentA, Len, 1, _, Space),
char_type(Space, space),
( Style == block
-> true
; \+ blanks_to_nl(CommentA)
).
structured_command_start('%%', ["%"], percent_percent). % Deprecated
structured_command_start('%!', ["%"], percent_bang). % New style
structured_command_start('/**', ["/**", " *"], block). % block
blanks_to_nl(CommentA) :-
sub_atom(CommentA, At, 1, _, Char),
At >= 2,
( char_type(Char, end_of_line)
-> !
; ( char_type(Char, space)
; Char == '%'
)
-> fail
; !, fail
).
blanks_to_nl(_).
%! structured_comment(-Prefixes:list(codes), -Style) is semidet.
%
% Grammar rule version of the above. Avoids the need for
% conversion.
structured_comment(["%"], percent_percent) -->
"%%", space,
\+ separator_line.
structured_comment(["%"], percent_bang) -->
"%!", space.
structured_comment(Prefixes, block) -->
"/**", space,
{ Prefixes = ["/**", " *"]
}.
space -->
[H],
{ code_type(H, space) }.
%! separator_line// is semidet.
%
% Matches a line like %% SWI or %%%%%%%%%%%%%%%%%%%%%%%%%, etc.
separator_line -->
string(S), "\n",
!,
{ maplist(blank_or_percent, S)
; contains(S, " SWI ")
; contains(S, " SICStus ")
; contains(S, " Mats ")
}.
string([]) --> [].
string([H|T]) --> [H], string(T).
blank_or_percent(0'%) :- !.
blank_or_percent(C) :-
code_type(C, space).
contains(Haystack, Needle) :-
string_codes(Needle, NeedleCodes),
append(_, Start, Haystack),
append(NeedleCodes, _, Start),
!.
%! doc_file_name(+Source:atom, -Doc:atom, +Options:list) is det.
%
% Doc is the name of the file for documenting Source.
%
% @param Source Prolog source to be documented
% @param Doc the name of the file documenting Source.
% @param Options Option list:
%
% * format(+Format)
% Output format. One of =html= or =tex=
%
% @error permission_error(overwrite, Source)
doc_file_name(Source, Doc, Options) :-
option(format(Format), Options, html),
file_name_extension(Base, _Ext, Source),
file_name_extension(Base, Format, Doc),
( Source == Doc
-> throw(error(permission_error(overwrite, Source), _))
; true
).
%! doc_file_has_comments(+Source:atom) is semidet.
%
% True if we have loaded comments from Source.
doc_file_has_comments(Source) :-
source_file_property(Source, module(M)),
locally_defined(M:'$pldoc'/4),
M:'$pldoc'(_, _, _, _),
!.
%! doc_comment(?Objects, -Pos,
%! -Summary:string, -Comment:string) is nondet.
%
% True if Comment is the comment describing object. Comment is
% returned as a string object containing the original from the
% source-code. Object is one of
%
% * Name/Arity
% Predicate indicator
%
% * Name//Arity
% DCG rule indicator. Same as Name/Arity+2
%
% * module(ModuleTitle)
% Comment appearing in a module.
%
% If Object is unbound and multiple objects share the same
% description, Object is unified with a list of terms described
% above.
%
% @param Summary First sentence. Normalised spacing.
% @param Comment Comment string from the source-code (untranslated)
doc_comment(Object, Pos, Summary, Comment) :-
var(Object),
!,
locally_defined(M:'$pldoc'/4),
M:'$pldoc'(Obj, Pos, Summary, Comment),
qualify(M, Obj, Object0),
( locally_defined(M:'$pldoc_link'/2),
findall(L, M:'$pldoc_link'(L, Obj), Ls), Ls \== []
-> maplist(qualify(M), Ls, QLs),
Object = [Object0|QLs]
; Object = Object0
).
doc_comment(M:Object, Pos, Summary, Comment) :-
!,
locally_defined(M:'$pldoc'/4),
( M:'$pldoc'(Object, Pos, Summary, Comment)
; locally_defined(M:'$pldoc_link'/2),
M:'$pldoc_link'(Object, Obj2),
M:'$pldoc'(Obj2, Pos, Summary, Comment)
).
doc_comment(Name/Arity, Pos, Summary, Comment) :-
system_module(M),
doc_comment(M:Name/Arity, Pos, Summary, Comment).
locally_defined(M:Name/Arity) :-
current_predicate(M:Name/Arity),
functor(Head, Name, Arity),
% \+ predicate_property(M:Head, imported_from(_)).
\+ '$get_predicate_attribute'(M:Head, imported, _).
qualify(M, H, H) :- system_module(M), !.
qualify(M, H, H) :- sub_atom(M, 0, _, _, $), !.
qualify(M, H, M:H).
system_module(user).
system_module(system).
% Make the summary available to external tools on plugin basis.
prolog:predicate_summary(PI, Summary) :-
doc_comment(PI, _, Summary, _).
/*******************************
* CALL-BACK COLLECT *
*******************************/
%! process_comments(+Comments:list, +TermPos, +File) is det.
%
% Processes comments returned by read_term/3 using the =comments=
% option. It creates clauses of the form
%
% * '$mode'(Head, Det)
% * '$pldoc'(Id, Pos, Summary, Comment)
% * '$pldoc_link'(Id0, Id)
%
% where Id is one of
%
% * module(Title)
% Generated from /** <module> Title */
% * Name/Arity
% Generated from Name(Arg, ...)
% * Name//Arity
% Generated from Name(Arg, ...)//
%
% @param Comments is a list Pos-Comment returned by read_term/3
% @param TermPos is the start-location of the actual term
% @param File is the file that is being loaded.
process_comments([], _, _).
process_comments([Pos-Comment|T], TermPos, File) :-
( Pos @> TermPos % comments inside term
-> true
; process_comment(Pos, Comment, File),
process_comments(T, TermPos, File)
).
process_comment(Pos, Comment, File) :-
is_structured_comment(Comment, Prefixes, Style),
!,
stream_position_data(line_count, Pos, Line),
FilePos = File:Line,
process_structured_comment(FilePos, Comment, Prefixes, Style).
process_comment(_, _, _).
%! parse_comment(+Comment, +FilePos, -Parsed) is semidet.
%
% True when Comment is a structured comment and Parsed is its
% parsed representation. Parsed is a list of the following terms:
%
% * section(Id, Title, Comment)
% Generated from /** <module> Title Comment */ comments.
% * predicate(PI, Summary, Comment)
% Comment for predicate PI
% * link(FromPI, ToPI)
% Indicate that FromPI shares its comment with ToPI. The actual
% comment is in ToPI.
% * mode(Head, Determinism)
% Mode declaration. Head is a term with Mode(Type) terms and
% Determinism describes the associated determinism (=det=,
% etc.).
parse_comment(Comment, FilePos, Parsed) :-
is_structured_comment(Comment, Prefixes),
!,
compile_comment(Comment, FilePos, Prefixes, Parsed).
%! comment_modes(+Comment, -Modes:list) is semidet.
comment_modes(Comment, Modes) :-
is_structured_comment(Comment, Prefixes),
string_codes(Comment, CommentCodes),
indented_lines(CommentCodes, Prefixes, Lines),
( prolog_load_context(module, Module)
-> true
; Module = user
),
process_modes(Lines, Module, dummy:0, Modes0, _Vars, _),
maplist(bind_mode, Modes0, Modes).
bind_mode(mode(Mode, Bindings), Mode) :-
maplist(bind_var, Bindings).
bind_var(Name=Name).
%! process_structured_comment(+FilePos,
%! +Comment:string,
%! +Prefixed:list,
%! +Style) is det.
%
% Proccess a structured comment, adding the documentation facts to
% the database. This predicate verifies that the comment has not
% already been loaded.
%
% @tbd Note that as of version 7.3.12 clauses from a file being
% reloaded are not wiped before the reloading and therefore we
% cannot test the clause while reloading a file. Ultimately we
% need a better test for this.
process_structured_comment(FilePos, Comment, _, _) :- % already processed
prolog_load_context(module, M),
locally_defined(M:'$pldoc'/4),
catch(M:'$pldoc'(_, FilePos, _, Comment), _, fail),
( FilePos = File:_,
source_file_property(File, reloading)
-> debug(pldoc(reload), 'Reloading ~q', [FilePos]),
fail
; true
),
!.
process_structured_comment(FilePos, Comment, Prefixes, Style) :-
catch(compile_comment(Comment, FilePos, Prefixes, Compiled), E,
comment_warning(Style, E)),
maplist(store_comment(FilePos), Compiled).
process_structured_comment(FilePos, Comment, _Prefixes, Style) :-
comment_style_warning_level(Style, Level),
print_message(Level,
pldoc(invalid_comment(FilePos, Comment))).
comment_style_warning_level(percent_percent, silent) :- !.
comment_style_warning_level(_, warning).
%! comment_warning(+Style, +Error) is failure.
%
% Print a warning on structured comments that could not be
% processed. Since the recommended magic sequence is now =|%!|=,
% we remain silent about comments that start with =|%%|=.
comment_warning(Style, E) :-
comment_style_warning_level(Style, Level),
print_message(Level, E),
fail.
%! compile_comment(+Comment, +FilePos, +Prefixes, -Compiled) is semidet.
%
% Compile structured Comment into a list of terms that describe
% the comment.
%
% @see parse_comment/3 for the terms in Compiled.
compile_comment(Comment, FilePos, Prefixes, Compiled) :-
string_codes(Comment, CommentCodes),
indented_lines(CommentCodes, Prefixes, Lines),
( section_comment_header(Lines, Header, _RestLines)
-> Header = \section(Type, Title),
Id =.. [Type,Title],
Compiled = [section(Id, Title, Comment)]
; prolog_load_context(module, Module),
process_modes(Lines, Module, FilePos, Modes, _, RestLines)
-> maplist(compile_mode, Modes, ModeDecls),
modes_to_predicate_indicators(Modes, AllPIs),
decl_module(AllPIs, M, [PI0|PIs]),
maplist(link_term(M:PI0), PIs, Links),
summary_from_lines(RestLines, Codes),
string_codes(Summary, Codes),
append([ ModeDecls,
[ predicate(M:PI0, Summary, Comment) ],
Links
], Compiled)
),
!.
store_comment(Pos, section(Id, Title, Comment)) :-
!,
compile_clause('$pldoc'(Id, Pos, Title, Comment), Pos).
store_comment(Pos, predicate(M:PI, Summary, Comment)) :-
!,
compile_clause(M:'$pldoc'(PI, Pos, Summary, Comment), Pos).
store_comment(Pos, link(PI, M:PI0)) :-
!,
compile_clause(M:'$pldoc_link'(PI, PI0), Pos).
store_comment(Pos, mode(Head, Det)) :-
!,
compile_clause('$mode'(Head, Det), Pos).
store_comment(_, Term) :-
type_error(pldoc_term, Term).
link_term(To, From, link(From,To)).
decl_module([], M, []) :-
( var(M)
-> prolog_load_context(module, M)
; true
).
decl_module([H0|T0], M, [H|T]) :-
( H0 = M1:H
-> M = M1
; H = H0
),
decl_module(T0, M, T).
%! doc_clean(+Module) is det.
%
% Clean documentation for Module.
doc_clean(Module) :-
abolish(Module:'$mode'/2),
abolish(Module:'$pldoc'/4),
abolish(Module:'$pldoc_link'/2).
/*******************************
* MESSAGES *
*******************************/
:- multifile
prolog:message//1.
prolog:message(pldoc(invalid_comment(File:Line, Comment))) -->
[ url(File:Line), ': PlDoc: failed to process structured comment:~n~s~n'-
[Comment]
].
|