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