View source with formatted 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)  2007-2011, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(prolog_dialect,
   36          [ expects_dialect/1,          % +Dialect
   37            exists_source/1,            % +Source
   38            source_exports/2            % +Source, ?Export
   39          ]).   40:- use_module(library(error)).   41:- use_module(library(lists)).   42
   43/**     <module> Support multiple Prolog dialects
   44
   45The idea for this predicate  was  raised   by  Vitor  Santos  Costa in a
   46discussion to reach as a portability   framework  between SWI-Prolog and
   47YAP.
   48
   49This library defines :- expects_dialect/1, telling  the system for which
   50Prolog dialect was written,  as  well   as  useful  tests in conditional
   51compilation:
   52
   53        * exists_source/1
   54        * source_exports/2
   55
   56@see    if/1, require/1, term_expansion/2, goal_expansion/2.
   57@author Jan Wielemaker
   58@author Vitor Santos Costa
   59*/
   60
   61%!  expects_dialect(+Dialect:atom) is det.
   62%
   63%   Tell Prolog all subsequent code to the   end  of the file or the
   64%   next :- expects_dialect/1 directive is written for the indicated
   65%   Dialect.   The   current   dialect     is    available   through
   66%   prolog_load_context/2.
   67%
   68%   @tbd    Should we setup the dialect module only as autoload for
   69%           the current module?
   70
   71expects_dialect(Dialect) :-
   72    must_be(atom, Dialect),
   73    set_prolog_flag(emulated_dialect, Dialect),
   74    (   Dialect == swi
   75    ->  true
   76    ;   attach_dialect(Dialect)
   77    ).
   78
   79
   80attach_dialect(Dialect) :-
   81    exists_source(library(dialect/Dialect)),
   82    !,
   83    prolog_load_context(module, Module),
   84    use_module(Module:library(dialect/Dialect)),
   85    (   current_predicate(Dialect:setup_dialect/0)
   86    ->  Dialect:setup_dialect
   87    ;   true
   88    ).
   89attach_dialect(_).
   90
   91
   92%!  exists_source(+Source) is semidet.
   93%
   94%   True if Source (a term  valid   for  load_files/2) exists. Fails
   95%   without error if this is not the case. The predicate is intended
   96%   to be used with  :-  if,  as   in  the  example  below. See also
   97%   source_exports/2.
   98%
   99%   ==
  100%   :- if(exists_source(library(error))).
  101%   :- use_module_library(error).
  102%   :- endif.
  103%   ==
  104
  105exists_source(Source) :-
  106    exists_source(Source, _Path).
  107
  108exists_source(Source, Path) :-
  109    absolute_file_name(Source, Path,
  110                       [ file_type(prolog),
  111                         access(read),
  112                         file_errors(fail)
  113                       ]).
  114
  115%!  source_exports(+Source, +Export) is semidet.
  116%!  source_exports(+Source, -Export) is nondet.
  117%
  118%   True if Source exports Export. Fails   without  error if this is
  119%   not the case.  See also exists_source/1.
  120%
  121%   @tbd    Should we also allow for source_exports(-Source, +Export)?
  122
  123source_exports(Source, Export) :-
  124    open_source(Source, In),
  125    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  126    (   ground(Export)
  127    ->  memberchk(Export, Exports)
  128    ;   member(Export, Exports)
  129    ).
  130
  131%!  open_source(+Source, -In:stream) is semidet.
  132%
  133%   Open a source location.
  134
  135open_source(File, In) :-
  136    exists_source(File, Path),
  137    open(Path, read, In),
  138    (   peek_char(In, #)
  139    ->  skip(In, 10)
  140    ;   true
  141    ).
  142
  143exports(In, Exports) :-
  144    read(In, Term),
  145    Term = (:- module(_Name, Exports))