File: prolog_format.pl

package info (click to toggle)
swi-prolog 8.0.2+dfsg-3+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (222 lines) | stat: -rw-r--r-- 6,736 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*  Part of SWI-Prolog

    Author:        Michael Hendricks
    E-mail:        michael@ndrix.org
    WWW:           http://www.swi-prolog.org
    Copyright (C): 2013,2014, Michael Hendricks
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(prolog_format,
          [ format_spec/2,                      % +Format, -Spec
            format_spec//1,                     % -Spec
            format_types/2                      % +Format, -Types
          ]).
:- use_module(library(dcg/basics), [eos//0, integer//1, string_without//2]).
:- use_module(library(when), [when/2]).

/** <module> Analyse format specifications

This library parses the format specification  used by format/1, format/2
and format/3. The parsed  specification  can   be  used  to validate the
consistency of the  format  string  and   the  provided  arguments.  For
example:

  ==
  ?- format_types('~d bottles of beer', Types).
  Types = [integer].
  ==

@tbd    The current implementation does not support format_predicate/2.
@see    http://www.swi-prolog.org/pack/list?p=format_spec
@author Michael Hendricks
*/

%%  format_spec(+Format, -Spec:list) is semidet.
%
%   Parse a format string. Each element of Spec is one of the following:
%
%    * text(Text)
%    Text sent to the output as is
%    * escape(Num,Colon,Action)
%    A format escape. Num represents the optional numeric portion of
%    an esape. Colon represents the optional colon in an escape.
%    Action is an atom representing the action to be take by this
%    escape.

format_spec(Format, Spec) :-
    when((ground(Format);ground(Codes)),text_codes(Format, Codes)),
    once(phrase(format_spec(Spec), Codes, [])).

%%  format_spec(-Spec)//
%
%   DCG for parsing format  strings.  It   doesn't  yet  generate format
%   strings from a spec. See format_spec/2 for details.

format_spec([]) -->
    eos.
format_spec([escape(Numeric,Modifier,Action)|Rest]) -->
    "~",
    numeric_argument(Numeric),
    modifier_argument(Modifier),
    action(Action),
    format_spec(Rest).
format_spec([text(String)|Rest]) -->
    { when((ground(String);ground(Codes)),string_codes(String, Codes)) },
    string_without("~", Codes),
    { Codes \= [] },
    format_spec(Rest).

%%  format_types(+Format:text, -Types:list) is det.
%
%   True when Format requires an argument list   with  terms of the type
%   specified by Types. The  length  of  this   list  is  the  number of
%   arguments required. Each value of Types is   a  type as described by
%   error:has_type/2.

format_types(Format, Types) :-
    format_spec(Format, Spec),
    spec_types(Spec, Types).

%%  spec_types(+FormatSpec, -Types:list(type)) is det.
%
%   True if FormatSpec requires format/2  to   have  arguments of Types.
%   Each value of Types is a type as described by error:has_type/2. This
%   notion of types is compatible with library(mavis).

spec_types(Spec, Types) :-
    phrase(spec_types(Spec), Types).

spec_types([]) -->
    [].
spec_types([Item|Items]) -->
    item_types(Item),
    spec_types(Items).

item_types(text(_)) -->
    [].
item_types(escape(Numeric,_,Action)) -->
    numeric_types(Numeric),
    action_types(Action).

numeric_types(number(_)) -->
    [].
numeric_types(character(_)) -->
    [].
numeric_types(star) -->
    [number].
numeric_types(nothing) -->
    [].

action_types(Action) -->
    { atom_codes(Action, [Code]) },
    { action_types(Code, Types) },
    phrase(Types).

%% text_codes(Text:text, Codes:codes).
text_codes(Var, Codes) :-
    var(Var),
    !,
    string_codes(Var, Codes).
text_codes(Atom, Codes) :-
    atom(Atom),
    !,
    atom_codes(Atom, Codes).
text_codes(String, Codes) :-
    string(String),
    !,
    string_codes(String, Codes).
text_codes(Codes, Codes) :-
    is_of_type(codes, Codes).


numeric_argument(number(N)) -->
    integer(N).
numeric_argument(character(C)) -->
    "`",
    [C].
numeric_argument(star) -->
    "*".
numeric_argument(nothing) -->
    "".

modifier_argument(colon) -->
    ":".
modifier_argument(no_colon) -->
    \+ ":".

action(Action) -->
    [C],
    { is_action(C) },
    { atom_codes(Action, [C]) }.

%%  is_action(+Action:integer) is semidet.
%%  is_action(-Action:integer) is multi.
%
%   True if Action is a valid   format/2  action character. Iterates all
%   acceptable action characters, if Action is unbound.

is_action(Action) :-
    action_types(Action, _).

%%  action_types(?Action:integer, ?Types:list(type))
%
%   True if Action consumes arguments matching   Types.  An action (like
%   `~`), which consumes no arguments, has `Types=[]`. For example,
%
%       ?- action_types(0'~, Types).
%       Types = [].
%       ?- action_types(0'a, Types).
%       Types = [atom].

action_types(0'~, []).
action_types(0'a, [atom]).
action_types(0'c, [integer]).  % specifically, a code
action_types(0'd, [integer]).
action_types(0'D, [integer]).
action_types(0'e, [float]).
action_types(0'E, [float]).
action_types(0'f, [float]).
action_types(0'g, [float]).
action_types(0'G, [float]).
action_types(0'i, [any]).
action_types(0'I, [integer]).
action_types(0'k, [any]).
action_types(0'n, []).
action_types(0'N, []).
action_types(0'p, [any]).
action_types(0'q, [any]).
action_types(0'r, [integer]).
action_types(0'R, [integer]).
action_types(0's, [text]).
action_types(0'@, [callable]).
action_types(0't, []).
action_types(0'|, []).
action_types(0'+, []).
action_types(0'w, [any]).
action_types(0'W, [any, list]).