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-2020, University of Amsterdam
    7                              CWI, 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(prolog_dialect,
   37          [ expects_dialect/1,          % +Dialect
   38            exists_source/1,            % +Source
   39            source_exports/2            % +Source, ?Export
   40          ]).   41:- autoload(library(error),[must_be/2]).   42:- autoload(library(lists),[member/2]).   43
   44
   45/** <module> Support multiple Prolog dialects
   46
   47The idea for this predicate  was  raised   by  Vitor  Santos  Costa in a
   48discussion to reach as a portability   framework  between SWI-Prolog and
   49YAP.
   50
   51This library defines :- expects_dialect/1, telling  the system for which
   52Prolog dialect was written,  as  well   as  useful  tests in conditional
   53compilation:
   54
   55        * exists_source/1
   56        * source_exports/2
   57
   58@see    if/1, require/1, term_expansion/2, goal_expansion/2.
   59@author Jan Wielemaker
   60@author Vitor Santos Costa
   61*/
   62
   63%!  expects_dialect(+Dialect:atom) is det.
   64%
   65%   Tell Prolog all subsequent code to the   end  of the file or the
   66%   next :- expects_dialect/1 directive is written for the indicated
   67%   Dialect.   The   current   dialect     is    available   through
   68%   prolog_load_context/2.
   69%
   70%   @tbd    Should we setup the dialect module only as autoload for
   71%           the current module?
   72
   73expects_dialect(Dialect) :-
   74    must_be(atom, Dialect),
   75    (   Dialect == swi
   76    ->  true
   77    ;   attach_dialect(Dialect)
   78    ),
   79    set_prolog_flag(emulated_dialect, Dialect).
   80
   81attach_dialect(Dialect) :-
   82    exists_source(library(dialect/Dialect)),
   83    !,
   84    prolog_load_context(module, Module),
   85    use_module(Module:library(dialect/Dialect)),
   86    (   current_predicate(Dialect:setup_dialect/0)
   87    ->  Dialect:setup_dialect
   88    ;   true
   89    ).
   90attach_dialect(_).
   91
   92%!  source_exports(+Source, +Export) is semidet.
   93%!  source_exports(+Source, -Export) is nondet.
   94%
   95%   True if Source exports Export. Fails   without  error if this is
   96%   not the case.  See also exists_source/1.
   97%
   98%   @tbd    Should we also allow for source_exports(-Source, +Export)?
   99
  100source_exports(Source, Export) :-
  101    open_source(Source, In),
  102    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  103    (   ground(Export)
  104    ->  memberchk(Export, Exports)
  105    ;   member(Export, Exports)
  106    ).
  107
  108%!  open_source(+Source, -In:stream) is semidet.
  109%
  110%   Open a source location.
  111
  112open_source(File, In) :-
  113    exists_source(File, Path),
  114    open(Path, read, In),
  115    (   peek_char(In, #)
  116    ->  skip(In, 10)
  117    ;   true
  118    ).
  119
  120exports(In, Exports) :-
  121    read(In, Term),
  122    Term = (:- module(_Name, Exports))