View source with raw comments or as raw
   1/*  Part of SWI-Prolog
   2
   3    Author:        Jan Wielemaker
   4    E-mail:        J.Wielemaker@vu.nl
   5    WWW:           http://www.swi-prolog.org
   6    Copyright (c)  2006-2013, University of Amsterdam
   7                              VU University Amsterdam
   8    All rights reserved.
   9
  10    Redistribution and use in source and binary forms, with or without
  11    modification, are permitted provided that the following conditions
  12    are met:
  13
  14    1. Redistributions of source code must retain the above copyright
  15       notice, this list of conditions and the following disclaimer.
  16
  17    2. Redistributions in binary form must reproduce the above copyright
  18       notice, this list of conditions and the following disclaimer in
  19       the documentation and/or other materials provided with the
  20       distribution.
  21
  22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33    POSSIBILITY OF SUCH DAMAGE.
  34*/
  35
  36:- module(pldoc_register,
  37          [ process_stored_comments/0
  38          ]).
  39:- use_module(doc_process).
  40:- use_module(library(pldoc)).
  41:- set_prolog_flag(generate_debug_info, false).
  42
  43pldoc_module(pldoc_modes).              % avoid recursive behaviour
  44pldoc_module(pldoc_process).
  45pldoc_module(pldoc_wiki).
  46pldoc_module(pldoc).
  47pldoc_module(lists).
  48pldoc_module(apply).
  49pldoc_module(pairs).
  50
  51                 /*******************************
  52                 *            REGISTER          *
  53                 *******************************/
  54
  55:- multifile
  56    prolog:comment_hook/3,
  57    user:message_hook/3.
  58
  59:- dynamic
  60    mydoc/3.                        %  +Comments, +TermPos, +File
  61
  62do_comment_hook(_, _, _, _) :-
  63    current_prolog_flag(pldoc_collecting, false),
  64    !.
  65do_comment_hook(Comments, TermPos, File, _) :-
  66    pldoc_loading,
  67    !,
  68    assert(mydoc(Comments, TermPos, File)).
  69do_comment_hook(Comments, TermPos, File, _Term) :-
  70    prolog_load_context(module, Module),
  71    pldoc_module(Module),
  72    !,
  73    assert(mydoc(Comments, TermPos, File)).
  74do_comment_hook(Comments, TermPos, File, _) :-
  75    process_comments(Comments, TermPos, File).
  76
  77user:message_hook(load_file(done(0, _F, _A, _M, _T, _H)), _, _) :-
  78    (   mydoc(_, _, _)
  79    ->  debug(pldoc, 'Processing delayed comments', []),
  80        process_stored_comments
  81    ),
  82    fail.
  83
  84%!  prolog:comment_hook(+Comments, +TermPos, +Term) is det.
  85%
  86%   Hook called by the compiler and cross-referencer. In addition to
  87%   the comment, it passes the  term  itself   to  see  what term is
  88%   commented  as  well  as  the  start-position   of  the  term  to
  89%   distinguish between comments before the term and inside it.
  90%
  91%   @param Comments List of comments read before the end of Term
  92%   @param TermPos  Start location of Term
  93%   @param Term     Actual term read
  94
  95prolog:comment_hook(Comments, TermPos, Term) :-
  96    source_location(File, _TermLine),
  97    setup_call_cleanup(
  98        '$push_input_context'(pldoc),  % Preserve input file and line
  99        do_comment_hook(Comments, TermPos, File, Term),
 100        '$pop_input_context').
 101
 102process_stored_comments :-
 103    forall(retract(mydoc(Comments, TermPos, File)),
 104           delayed_process(Comments, TermPos, File)).
 105
 106delayed_process(Comments, TermPos, File) :-
 107    module_property(Module, file(File)),
 108    setup_call_cleanup(
 109        '$set_source_module'(Old, Module),
 110        process_comments(Comments, TermPos, File),
 111        '$set_source_module'(_, Old)).