View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Michael Hendricks
    4    E-mail:        michael@ndrix.org
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2013,2020, Michael Hendricks
    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 are
   11    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 HOLDER 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_format,
   36          [ format_spec/2,                      % +Format, -Spec
   37            format_spec//1,                     % -Spec
   38            format_types/2                      % +Format, -Types
   39          ]).   40:- autoload(library(error),[existence_error/2]).   41:- autoload(library(dcg/basics),[eos//0,string_without//2,integer//1]).   42
   43
   44/** <module> Analyse format specifications
   45
   46This library parses the format specification  used by format/1, format/2
   47and format/3. The parsed  specification  can   be  used  to validate the
   48consistency of the  format  string  and   the  provided  arguments.  For
   49example:
   50
   51  ==
   52  ?- format_types('~d bottles of beer', Types).
   53  Types = [integer].
   54  ==
   55
   56@tbd    The current implementation does not support format_predicate/2.
   57@see    http://www.swi-prolog.org/pack/list?p=format_spec
   58@author Michael Hendricks
   59*/
   60
   61%%  format_spec(+Format, -Spec:list) is det.
   62%
   63%   Parse a format string. Each element of Spec is one of the following:
   64%
   65%    * text(Text)
   66%    Text sent to the output as is
   67%    * escape(Num,Colon,Action)
   68%    A format escape. Num represents the optional numeric portion of
   69%    an esape. Colon represents the optional colon in an escape.
   70%    Action is an atom representing the action to be take by this
   71%    escape.
   72
   73format_spec(Format, Spec) :-
   74    (   is_list(Format)
   75    ->  Codes = Format
   76    ;   string_codes(Format, Codes)
   77    ),
   78    phrase(format_spec(Spec), Codes).
   79
   80%%  format_spec(-Spec)// is det.
   81%
   82%   DCG for parsing format  strings.  It   doesn't  yet  generate format
   83%   strings from a spec. See format_spec/2 for details.
   84
   85format_spec([escape(Numeric,Modifier,Action)|Rest]) -->
   86    "~",
   87    !,
   88    numeric_argument(Numeric),
   89    modifier_argument(Modifier),
   90    action(Action),
   91    format_spec(Rest).
   92format_spec([text(String)|Rest]) -->
   93    string_without("~", Codes),
   94    { Codes \== [],
   95      !,
   96      string_codes(String, Codes)
   97    },
   98    format_spec(Rest).
   99format_spec([]) -->
  100    [].
  101
  102%%  format_types(+Format:text, -Types:list) is det.
  103%
  104%   True when Format requires an argument list   with  terms of the type
  105%   specified by Types. The  length  of  this   list  is  the  number of
  106%   arguments required. Each value of Types is   a  type as described by
  107%   error:has_type/2.
  108
  109format_types(Format, Types) :-
  110    format_spec(Format, Spec),
  111    spec_types(Spec, Types).
  112
  113%%  spec_types(+FormatSpec, -Types:list(type)) is det.
  114%
  115%   True if FormatSpec requires format/2  to   have  arguments of Types.
  116%   Each value of Types is a type as described by error:has_type/2. This
  117%   notion of types is compatible with library(mavis).
  118
  119spec_types(Spec, Types) :-
  120    phrase(spec_types(Spec), Types).
  121
  122spec_types([]) -->
  123    [].
  124spec_types([Item|Items]) -->
  125    item_types(Item),
  126    spec_types(Items).
  127
  128item_types(text(_)) -->
  129    [].
  130item_types(escape(Numeric,_,Action)) -->
  131    numeric_types(Numeric),
  132    action_types(Action).
  133
  134numeric_types(number(_)) -->
  135    [].
  136numeric_types(character(_)) -->
  137    [].
  138numeric_types(star) -->
  139    [number].
  140numeric_types(nothing) -->
  141    [].
  142
  143action_types(Action) -->
  144    { atom_codes(Action, [Code]) },
  145    { action_types(Code, Types) },
  146    phrase(Types).
  147
  148numeric_argument(number(N)) -->
  149    integer(N),
  150    !.
  151numeric_argument(character(C)) -->
  152    "`",
  153    !,
  154    [C].
  155numeric_argument(star) -->
  156    "*",
  157    !.
  158numeric_argument(nothing) -->
  159    "".
  160
  161modifier_argument(colon) -->
  162    ":".
  163modifier_argument(no_colon) -->
  164    \+ ":".
  165
  166action(Char) -->
  167    [C],
  168    { char_code(Char, C),
  169      (   is_action(C)
  170      ->  true
  171      ;   existence_error(format_character, Char)
  172      )
  173    }.
  174
  175%%  is_action(+Action:integer) is semidet.
  176%%  is_action(-Action:integer) is multi.
  177%
  178%   True if Action is a valid   format/2  action character. Iterates all
  179%   acceptable action characters, if Action is unbound.
  180
  181is_action(Action) :-
  182    action_types(Action, _).
  183
  184%%  action_types(?Action:integer, ?Types:list(type))
  185%
  186%   True if Action consumes arguments matching   Types.  An action (like
  187%   `~`), which consumes no arguments, has `Types=[]`. For example,
  188%
  189%       ?- action_types(0'~, Types).
  190%       Types = [].
  191%       ?- action_types(0'a, Types).
  192%       Types = [atom].
  193
  194action_types(0'~, []).
  195action_types(0'a, [atom]).
  196action_types(0'c, [integer]).  % specifically, a code
  197action_types(0'd, [integer]).
  198action_types(0'D, [integer]).
  199action_types(0'e, [float]).
  200action_types(0'E, [float]).
  201action_types(0'f, [float]).
  202action_types(0'g, [float]).
  203action_types(0'G, [float]).
  204action_types(0'i, [any]).
  205action_types(0'I, [integer]).
  206action_types(0'k, [any]).
  207action_types(0'n, []).
  208action_types(0'N, []).
  209action_types(0'p, [any]).
  210action_types(0'q, [any]).
  211action_types(0'r, [integer]).
  212action_types(0'R, [integer]).
  213action_types(0's, [text]).
  214action_types(0'@, [callable]).
  215action_types(0't, []).
  216action_types(0'|, []).
  217action_types(0'+, []).
  218action_types(0'w, [any]).
  219action_types(0'W, [any, list])