34
35:- module(yaml,
36 [ yaml_read/2, 37 yaml_write/2, 38 yaml_write/3 39 ]). 40:- autoload(library(apply),[maplist/3,exclude/3]). 41:- autoload(library(base64),[base64/3]). 42:- use_module(library(debug),[debug/3]). 43:- autoload(library(error),[instantiation_error/1]). 44:- autoload(library(option),[option/2,option/3]). 45:- autoload(library(terms),[term_factorized/3]). 46
47:- use_foreign_library(foreign(yaml4pl)).
58:- multifile
59 tagged/3. 60
61:- predicate_options(yaml_write/3, 3,
62 [ canonical(boolean),
63 unicode(boolean),
64 implicit(boolean),
65 factorize(boolean)
66 ]).
101yaml_read(In, DOM) :-
102 setup_call_cleanup(
103 yaml_open(In, Stream, Close),
104 yaml_parse_stream(Stream, DOM0),
105 Close),
106 finalize_dom(DOM0, DOM).
107
108yaml_open(Stream, Stream, Close) :-
109 is_stream(Stream),
110 !,
111 stream_property(Stream, eof_action(EOF0)),
112 ( EOF0 == eof_code
113 -> Close = true
114 ; set_stream(Stream, eof_action(eof_code)),
115 Close = set_stream(Stream, eof_action(EOF0))
116 ).
117yaml_open(string(Data), Stream, close(Stream)) :-
118 open_string(Data, Stream),
119 set_stream(Stream, eof_action(eof_code)).
120yaml_open(File, Stream, close(Stream)) :-
121 open(File, read, Stream,
122 [ eof_action(eof_code)
123 ]).
124
125finalize_dom(Var, _) :-
126 var(Var), 127 !.
128finalize_dom(sequence(Elems0, Done, Elems), Elems) :-
129 !,
130 ( var(Done)
131 -> Done = true,
132 maplist(finalize_dom, Elems0, Elems)
133 ; true
134 ).
135finalize_dom(mapping(Attrs0, Done, Dict), Dict) :-
136 !,
137 ( var(Done)
138 -> Done = true,
139 maplist(mapping_pair, Attrs0, Pairs),
140 dict_pairs(Dict, yaml, Pairs)
141 ; true
142 ).
143finalize_dom(tag(Tag, ValueIn), Value) :-
144 !,
145 ( string(ValueIn)
146 -> ( yalm_tagged(Tag, ValueIn, Value0)
147 -> Value = Value0
148 ; debug(yaml(tag), 'Ignored tag ~p for ~p', [Tag, ValueIn]),
149 Value = tag(Tag, ValueIn)
150 )
151 ; finalize_dom(ValueIn, ValueOut),
152 Value = tag(Tag, ValueOut)
153 ).
154finalize_dom(Value, Value).
155
156mapping_pair(Name=Value0, Name-Value) :-
157 finalize_dom(Value0, Value).
158
159yalm_tagged(Tag, String, Value) :-
160 tagged(Tag, String, Value), !.
161yalm_tagged('tag:yaml.org,2002:binary', Base64, Data) :-
162 string_codes(Base64, EncCodes0),
163 exclude(whitespace, EncCodes0, EncCodes),
164 phrase(base64(PlainCodes), EncCodes),
165 string_codes(Data, PlainCodes).
166yalm_tagged('tag:yaml.org,2002:str', String, String).
167yalm_tagged('tag:yaml.org,2002:null', "null", null).
168yalm_tagged('tag:yaml.org,2002:bool', "true", true).
169yalm_tagged('tag:yaml.org,2002:bool', "false", false).
170yalm_tagged('tag:yaml.org,2002:int', String, Int) :-
171 catch(number_string(Int, String), error(syntax_error(_),_), fail),
172 integer(Int).
173yalm_tagged('tag:yaml.org,2002:float', String, Float) :-
174 ( special_float(String, Float)
175 -> true
176 ; catch(number_string(Float0, String), error(syntax_error(_),_), fail),
177 Float is float(Float0)
178 ).
179
180special_float(".nan", NaN) :- NaN is nan.
181special_float(".NaN", NaN) :- NaN is nan.
182special_float(".NAN", NaN) :- NaN is nan.
183special_float(".inf", Inf) :- Inf is inf.
184special_float(".Inf", Inf) :- Inf is inf.
185special_float(".INF", Inf) :- Inf is inf.
186special_float("-.inf", Inf) :- Inf is -inf.
187special_float("-.Inf", Inf) :- Inf is -inf.
188special_float("-.INF", Inf) :- Inf is -inf.
189
190whitespace(0'\s).
191whitespace(0'\t).
192whitespace(0'\r).
193whitespace(0'\n).
194
195
218yaml_write(To, DOM) :-
219 yaml_write(To, DOM, []).
220
221yaml_write(To, DOM, Options) :-
222 ( option(factorize(true), Options)
223 -> true
224 ; cyclic_term(DOM)
225 ),
226 !,
227 term_factorized(DOM, Skeleton, Substitutions),
228 assign_anchors(Substitutions, 1),
229 yaml_write2(To, Skeleton, Options).
230yaml_write(To, DOM, Options) :-
231 yaml_write2(To, DOM, Options).
232
233assign_anchors([], _).
234assign_anchors([anchored(Anchor,_Done,Term)=Term|T], I) :-
235 string_concat("a", I, Anchor),
236 I2 is I + 1,
237 assign_anchors(T, I2).
238
239yaml_write2(To, DOM, Options) :-
240 option(implicit(Implicit), Options, true),
241 yaml_emitter_create(Emitter, To, Options),
242 yaml_emit_event(Emitter, stream_start),
243 yaml_emit_event(Emitter, document_start(Implicit)),
244 yaml_emit(DOM, Emitter, Options),
245 yaml_emit_event(Emitter, document_end(Implicit)),
246 yaml_emit_event(Emitter, stream_end).
247
248yaml_emit(Var, _, _) :-
249 var(Var),
250 !,
251 instantiation_error(Var).
252yaml_emit(anchored(Anchor, Done, Term), Emitter, Options) :-
253 !,
254 ( var(Done)
255 -> Done = true,
256 yaml_emit(Term, Emitter, Anchor, Options)
257 ; yaml_emit_event(Emitter, alias(Anchor))
258 ).
259yaml_emit(Term, Emitter, Options) :-
260 yaml_emit(Term, Emitter, _Anchor, Options).
261
262yaml_emit(List, Emitter, Anchor, Options) :-
263 is_list(List),
264 !,
265 yaml_emit_event(Emitter, sequence_start(Anchor, _Tag)),
266 yaml_emit_list_elements(List, Emitter, Options),
267 yaml_emit_event(Emitter, sequence_end).
268yaml_emit(Dict, Emitter, Anchor, Options) :-
269 is_dict(Dict, _),
270 !,
271 dict_pairs(Dict, _, Pairs),
272 emit_mapping(Pairs, Emitter, Anchor, Options).
273yaml_emit(json(Pairs), Emitter, Anchor, Options) :-
274 !,
275 emit_mapping(Pairs, Emitter, Anchor, Options).
276yaml_emit(yaml(Pairs), Emitter, Anchor, Options) :-
277 !,
278 emit_mapping(Pairs, Emitter, Anchor, Options).
279yaml_emit(Scalar, Emitter, Anchor, _Options) :-
280 yaml_emit_event(Emitter, scalar(Scalar, _Tag, Anchor, plain)).
281
282yaml_emit_list_elements([], _, _).
283yaml_emit_list_elements([H|T], Emitter, Options) :-
284 yaml_emit(H, Emitter, Options),
285 yaml_emit_list_elements(T, Emitter, Options).
286
287emit_mapping(Pairs, Emitter, Anchor, Options) :-
288 yaml_emit_event(Emitter, mapping_start(Anchor, _Tag)),
289 yaml_emit_mapping_elements(Pairs, Emitter, Options),
290 yaml_emit_event(Emitter, mapping_end).
291
292yaml_emit_mapping_elements([], _, _).
293yaml_emit_mapping_elements([H|T], Emitter, Options) :-
294 name_value(H, Name, Value),
295 yaml_emit(Name, Emitter, Options),
296 yaml_emit(Value, Emitter, Options),
297 yaml_emit_mapping_elements(T, Emitter, Options).
298
299name_value(Name-Value, Name, Value) :- !.
300name_value(Name=Value, Name, Value) :- !.
301name_value(NameValue, Name, Value) :-
302 NameValue =.. [Name,Value].
303
304
305
Process YAML data
This module parses YAML serialized data into a Prolog term with structure that is compatible with the JSON library. This library is a wrapper around the C library
libyaml. This library forms the basis of the YAML support in several languages and thus guarantees compatibility of our YAML support with other languages.