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