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    set_prolog_flag(emulated_dialect, Dialect),
   76    (   Dialect == swi
   77    ->  true
   78    ;   attach_dialect(Dialect)
   79    ).
   80
   81
   82attach_dialect(Dialect) :-
   83    exists_source(library(dialect/Dialect)),
   84    !,
   85    prolog_load_context(module, Module),
   86    use_module(Module:library(dialect/Dialect)),
   87    (   current_predicate(Dialect:setup_dialect/0)
   88    ->  Dialect:setup_dialect
   89    ;   true
   90    ).
   91attach_dialect(_).
   92
   93%!  source_exports(+Source, +Export) is semidet.
   94%!  source_exports(+Source, -Export) is nondet.
   95%
   96%   True if Source exports Export. Fails   without  error if this is
   97%   not the case.  See also exists_source/1.
   98%
   99%   @tbd    Should we also allow for source_exports(-Source, +Export)?
  100
  101source_exports(Source, Export) :-
  102    open_source(Source, In),
  103    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  104    (   ground(Export)
  105    ->  memberchk(Export, Exports)
  106    ;   member(Export, Exports)
  107    ).
  108
  109%!  open_source(+Source, -In:stream) is semidet.
  110%
  111%   Open a source location.
  112
  113open_source(File, In) :-
  114    exists_source(File, Path),
  115    open(Path, read, In),
  116    (   peek_char(In, #)
  117    ->  skip(In, 10)
  118    ;   true
  119    ).
  120
  121exports(In, Exports) :-
  122    read(In, Term),
  123    Term = (:- module(_Name, Exports))