1 % (c) 2009-2024 Lehrstuhl fuer Softwaretechnik und Programmiersprachen,
2 % Heinrich Heine Universitaet Duesseldorf
3 % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html)
4
5 :- module(parsercall, [load_b_machine_as_term/3,
6 load_b_machine_probfile_as_term/2, % a way to directly load a .prob file
7 load_b_machine_probfile_as_term/3,
8 load_b_machine_list_of_facts_as_term/2,
9 get_parser_version/1,
10 get_parser_version/2,
11 get_parser_version/6,
12 get_java_command_path/1,
13 check_java_version/2,
14 get_java_fullversion/1, get_java_fullversion/3, get_java_version/1,
15 ensure_console_parser_launched/0, % just make sure the console parser is up and running
16 connect_to_external_console_parser_on_port/1, % connect to a separately started parser
17 release_console_parser/0,
18 console_parser_jar_available_in_lib/0,
19 call_ltl_parser/3,
20 call_tla2b_parser/1, tla2b_filename/2,
21 %call_promela_parser/1, promela_prolog_filename/2,
22 call_alloy2pl_parser/2,
23 tla2prob_filename/2,
24 parse/3,
25 parse_at_position_in_file/5,
26 parse_formula/2, parse_predicate/2, parse_expression/2,
27 parse_substitution/2,
28 transform_string_template/3,
29 call_fuzz_parser/2,
30 register_parsing_call_back/1, deregister_parsing_call_back/0,
31 set_default_filenumber/2, reset_default_filenumber/2
32 ]).
33
34 :- meta_predicate register_parsing_call_back(4).
35
36 :- use_module(module_information,[module_info/2]).
37 :- module_info(group,typechecker).
38 :- module_info(description,'This module takes care of calling the Java B Parser if necessary.').
39
40 :- use_module(library(lists)).
41 :- use_module(library(process)).
42 :- use_module(library(file_systems)).
43 :- use_module(library(codesio)).
44 :- use_module(library(system)).
45 :- use_module(library(sockets)).
46
47 :- use_module(error_manager,[add_error/2, add_error/3, add_error/4, add_error_fail/3,
48 add_failed_call_error/1, add_internal_error/2, add_warning/3, add_warning/4, real_error_occurred/0,
49 add_message/3, add_message/4, extract_line_col/5, add_all_perrors/3]).
50 :- use_module(self_check).
51 :- use_module(preferences).
52 :- use_module(tools,[host_platform/1, split_filename/3, same_file_name/2,
53 get_PROBPATH/1, safe_atom_chars/3, platform_is_64_bit/0]).
54 :- use_module(tools_strings,[ajoin_with_sep/3, ajoin/2]).
55 :- use_module(tools_printing,[print_error/1, format_with_colour_nl/4]).
56 :- use_module(debug).
57 :- use_module(bmachine,[b_get_definition/5, b_machine_is_loaded/0]).
58 :- use_module(specfile, [b_or_z_mode/0]).
59
60 :- set_prolog_flag(double_quotes, codes).
61
62 :- dynamic java_parser_version/6.
63
64 % stores the java process of the parser, consisting of ProcessIdentifier, Socket Stream, STDErrStream
65 :- dynamic java_parser_process/4.
66 :- volatile java_parser_process/4.
67
68
69 send_definition(Stream,def(Name,Type,Arity)) :-
70 write(Stream,'definition'), nl(Stream),
71 format(Stream,'~w\n~w\n~w\n',[Name,Type,Arity]).
72
73 :- dynamic definitions_already_sent/0.
74 :- volatile definitions_already_sent/0.
75
76 send_definitions(ToParser) :-
77 b_or_z_mode, b_machine_is_loaded,
78 \+ definitions_already_sent,
79 !,
80 findall_definitions(L),
81 % format('~nSending definitions ~w~n~n',[L]),
82 maplist(send_definition(ToParser),L),
83 assertz(definitions_already_sent).
84 send_definitions(_).
85
86 findall_definitions(DefList) :-
87 findall(def(Name,Type,Arity),(b_get_definition(Name,Type,Args,_,_),length(Args,Arity)),DefList).
88
89 get_definitions(L) :- b_or_z_mode, b_machine_is_loaded,!, findall_definitions(L).
90 get_definitions([]).
91
92 reset_definitions :-
93 retractall(definitions_already_sent),
94 parser_is_launched, % otherwise no need to reset; inside prob2_kernel there is no Java parser for probcli
95 (debug_mode(off) -> true
96 ; get_parser_version(Version),
97 format('Resetting DEFINITIONS for parser version ~w ~n',[Version])
98 ),
99 % TO DO: check if parser Version is at least 2.9.27
100 get_console_parser(ParserStream,Out,Err),!,
101 write(ParserStream,'resetdefinitions'), nl(ParserStream), % only supported in more recent versions of the parser
102 flush_output(ParserStream),
103 display_pending_outputs(Out,user_output),
104 display_pending_outputs(Err,user_error).
105 reset_definitions.
106
107 % update preferences of parser using new PREPL commands
108 update_jvm_parser_preferences :-
109 get_preference(jvm_parser_fastrw,FAST),
110 try_set_parser_option_nc(fastPrologOutput,FAST,_),
111 get_preference(jvm_parser_position_infos,LINENO),
112 try_set_parser_option_nc(addLineNumbers,LINENO,_),
113 (debug_mode(off) -> VERBOSE=false ; VERBOSE=true),
114 try_set_parser_option_nc(verbose,VERBOSE,_).
115
116
117 :- dynamic cur_defaultFileNumber/1.
118
119 try_set_parser_option(defaultFileNumber,Value,Res) :- !,
120 (cur_defaultFileNumber(Value) % cache last value to avoid unnecessary parser calls
121 -> Res=prev_value(Value) % nothing to do
122 ; retractall(cur_defaultFileNumber(_)),
123 assert(cur_defaultFileNumber(Value)),
124 try_set_parser_option_nc(defaultFileNumber,Value,Res)
125 ).
126 try_set_parser_option(Name,Value,Res) :-
127 try_set_parser_option_nc(Name,Value,Res).
128
129 reset_parser_option_cache :- retractall(cur_defaultFileNumber(_)).
130
131 % try_set_parser_option_nc: non-caching version
132 try_set_parser_option_nc(Name,Value,Res) :-
133 parser_command_supported(setoption),
134 !,
135 debug_format(4,'Updating parser option: ~q=~q~n',[Name,Value]),
136 get_console_parser(Stream,_Out,Err),
137 write(Stream,setoption), nl(Stream),
138 write(Stream,Name), nl(Stream),
139 write(Stream,Value), nl(Stream),
140 flush_output(Stream),
141 read_line(Stream,CodesIn),
142 my_read_from_codes(CodesIn,Res,Stream,Err).
143 try_set_parser_option_nc(Name,Value,Res) :-
144 old_option_command(Name,Command),
145 parser_command_supported(Command),
146 !,
147 debug_format(19,'Updating JVM parser option (using old command ~q): ~q=~q~n',[Command,Name,Value]),
148 get_console_parser(Stream,_Out,_Err),
149 write(Stream,Command), nl(Stream),
150 write(Stream,Value), nl(Stream),
151 flush_output(Stream),
152 Res = changed. % prev_value isn't returned here
153 try_set_parser_option_nc(Name,Value,unsupported) :-
154 debug_format(19,'Parser too old to change option at runtime: ~q=~q~n',[Name,Value]).
155
156
157
158 old_option_command(addLineNumbers,lineno).
159 old_option_command(verbose,verbose).
160 old_option_command(fastPrologOutput,fastprolog).
161 old_option_command(compactPrologPositions,compactpos).
162 old_option_command(machineNameMustMatchFileName,checkname).
163
164 ?reset_old_parser_option_value(Name,Value,prev_value(Old)) :- \+ unchanged(Value,Old), !,
165 try_set_parser_option(Name,Old,_).
166 reset_old_parser_option_value(_,_,_).
167
168 % the Java values are stored as strings, even for number attributes
169 unchanged(V,V).
170 unchanged(N,A) :- number(N), atom(A), number_codes(N,C), atom_codes(A,C).
171
172 reset_parser :- reset_definitions, reset_parser_option_cache.
173 :- use_module(probsrc(eventhandling),[register_event_listener/3]).
174 :- register_event_listener(clear_specification,reset_parser,
175 'Remove DEFINITIONS from parser cache and reset parer options cache.').
176
177 crlf(10).
178 crlf(13).
179
180 % remove newlines in a B formula so that we can pass the formula as a single line to the parser
181 cleanup_newlines([],[]).
182 cleanup_newlines([H|T],CleanCodes) :- crlf(H),!,
183 % this could also be inside a multi-line string; the string will be modified by this conversion
184 CleanCodes=[8232|TC], % 8232 \x2028 is Unicode line separator
185 cleanup_newlines(T,TC).
186 %cleanup_newlines([47,47|T],CleanCodes) :-
187 % % a B Comment '//': we used to skip until end of line
188 % % but this could be inside a string ! see test 2236
189 % CleanCodes=[8232|TC], skip_until_crlf(T,T2), !, cleanup_newlines(T2,TC).
190 cleanup_newlines([H|T],[H|R]) :- cleanup_newlines(T,R).
191
192 %skip_until_crlf([],[]).
193 %skip_until_crlf([H|T],T) :- crlf(H),!.
194 %skip_until_crlf([_|T],R) :- skip_until_crlf(T,R).
195
196 % register parsing call back from prob_socket_server within ProB2
197 :- dynamic prob2_call_back_available/1.
198 register_parsing_call_back(CallBackPred4) :- deregister_parsing_call_back,
199 assertz(prob2_call_back_available(CallBackPred4)).
200 deregister_parsing_call_back :-
201 retractall(prob2_call_back_available(_)).
202
203 % ---------
204
205 %! parse_x(+Kind,+Codes,-Tree)
206 % Kind is formula, expression, predicate, substitution
207 %:- use_module(prob_socketserver,[prob2_call_back_available/0, prob2_call_back/2]).
208 parse_x(Kind,CodesWithNewlines,Tree) :-
209 parse_simple(Kind,CodesWithNewlines,Res),!,
210 % format('Parse simple ~s : ~w~n',[CodesWithNewlines,Res]),
211 Tree=Res.
212 parse_x(Kind,CodesWithNewlines,Tree) :-
213 parse_y(Kind,CodesWithNewlines,Tree).
214
215 % a version which does not attempt simple parsing without calling Java:
216 parse_y(Kind,CodesWithNewlines,Tree) :-
217 prob2_call_back_available(ParseCallBackPred),
218 cleanup_newlines(CodesWithNewlines,Codes), atom_codes(Formula,Codes),
219 debug_format(19,'Parsing ~w via call_back: ~w~n',[Kind,Formula]),
220 get_definitions(DefList),
221 call(ParseCallBackPred,Kind,DefList,Formula,Tree),
222 % TO DO: deal with substitutions
223 debug_println(19,prob2_call_back_available_parse_result(Tree)),
224 (Tree = call_back_not_supported ->
225 % Parsing callback isn't implemented (e. g. ProB Rodin plugin) - don't try to use it again in the future.
226 deregister_parsing_call_back,
227 fail
228 ; true),
229 !,
230 (Tree = parse_error(Exception)
231 -> handle_parser_exception(Exception)
232 ; functor(Tree,parse_error,_) % deprecated
233 -> throw(parse_errors([error('Parse error occurred via ProB2 call_back:',unknown)]))
234 ; true).
235 parse_y(Kind,CodesWithNewlines,Tree) :-
236 % Java parser expects only one line of input -> remove newlines
237 cleanup_newlines(CodesWithNewlines,Codes),
238 % statistics(walltime,[Tot,Delta]), format('~s~n~w~n0 ~w (Delta ~w) ms ~n',[Codes,Kind,Tot,Delta]),
239 get_console_parser(Stream,Out,Err),
240 send_definitions(Stream),
241 write(Stream,Kind),
242 nl(Stream),
243 format(Stream,'~s',[Codes]), %format(user_output,'sent: ~s~n',[Codes]),
244 nl(Stream),
245 flush_output(Stream),
246 read_line(Stream,CodesIn),
247 %format('read: ~s~n',[CodesIn]),
248 catch(
249 (my_read_from_codes(CodesIn,Tree,Stream,Err),handle_parser_exception(Tree)),
250 error(_,_),
251 (append("Illegal parser result exception: ",CodesIn,ErrMsgCodes),
252 atom_codes(ErrMsg,ErrMsgCodes),
253 throw(parse_errors([internal_error(ErrMsg,none)])))),
254 display_pending_outputs(Out,user_output).
255 %statistics(walltime,[Tot4,Delta4]), format('4 ~w (Delta ~w) ms ~n ~n',[Tot4,Delta4]).
256
257 % a few very simple cases, which can be parsed without accessing Java parser:
258 % common e.g., in JSON trace files or Latex files to avoid overhead of calling parser
259 parse_simple(Kind,Codes,AST) :-
260 (Kind=expression -> true ; Kind=formula),
261 (cur_defaultFileNumber(FN) -> true ; FN = -1),
262 parse_simple_codes(Codes,FN,1,1,AST).
263
264 % ---------------------------
265
266 % benchmarking code:
267 % statistics(walltime,[_W1,_]),(for(I,1,10000) do parsercall:parse_formula("123",Tree)),statistics(walltime,[_W2,_]),D is _W2-_W1.
268 % | ?- findall("|-> 123",between(2,10000,X),L), append(["123 "|L],Str), statistics(walltime,[_W1,_]),parsercall:parse_expression(Str,Tree), statistics(walltime,[_W2,_]),D is _W2-_W1.
269
270 parse_simple_codes(Codes,FileNr,SL,SC,AST) :-
271 parse_int_codes(Codes,Len,Val), !,
272 L2 is Len+SC, % EndColumn of p4 position (same line)
273 AST=integer(p4(FileNr,SL,SC,L2),Val).
274 parse_simple_codes("TRUE",FileNr,SL,SC,AST) :- !, EC is SC+4, AST=boolean_true(p4(FileNr,SL,SC,EC)).
275 parse_simple_codes("FALSE",FileNr,SL,SC,AST) :- !, EC is SC+5, AST=boolean_false(p4(FileNr,SL,SC,EC)).
276 parse_simple_codes("{}",FileNr,SL,SC,AST) :- !, EC is SC+2, AST=empty_set(p4(FileNr,SL,SC,EC)).
277 ?parse_simple_codes(Codes,FileNr,SL,SC,AST) :- is_real_literal_codes(Codes),!,
278 length(Codes,Len), L2 is Len+SC,
279 atom_codes(Atom,Codes),
280 AST=real(p4(FileNr,SL,SC,L2),Atom).
281 % TO DO: maybe simple string values, simple identifiers: but we need to know the keyword list for this
282
283 :- assert_must_succeed((parsercall:parse_int_codes("0",Len,Res), Len==1, Res==0)).
284 :- assert_must_succeed((parsercall:parse_int_codes("1",Len,Res), Len==1, Res==1)).
285 :- assert_must_succeed((parsercall:parse_int_codes("12",Len,Res), Len==2, Res==12)).
286 :- assert_must_succeed((parsercall:parse_int_codes("931",Len,Res), Len==3, Res==931)).
287 :- assert_must_succeed((parsercall:parse_int_codes("100931",Len,Res), Len==6, Res==100931)).
288 parse_int_codes([Digit|T],Len,Res) :- digit_code_2_int(Digit,DVal),
289 (DVal=0 -> T=[], Len=1, Res=0
290 ; parse_int2(T,DVal,Res,1,Len)).
291
292 parse_int2([],Acc,Acc,LenAcc,LenAcc).
293 parse_int2([Digit|T],Acc,Res,LenAcc,Len) :-
294 digit_code_2_int(Digit,DVal),
295 NewAcc is Acc*10 + DVal,
296 L1 is LenAcc+1,
297 parse_int2(T,NewAcc,Res,L1,Len).
298
299 digit_code_2_int(X,Val) :- X >= 48, X =< 57, Val is X - 48.
300 is_digit(X) :- X >= 48, X =< 57.
301
302 :- assert_must_succeed(parsercall:is_real_literal_codes("0.0")).
303 :- assert_must_succeed(parsercall:is_real_literal_codes("112300.0010")).
304 :- assert_must_succeed(parsercall:is_real_literal_codes("1234567890.9876543210")).
305 :- assert_must_succeed(parsercall:is_real_literal_codes("001.0")). % is accepted by parser
306 :- assert_must_fail(parsercall:is_real_literal_codes("0")).
307 :- assert_must_fail(parsercall:is_real_literal_codes("11.")).
308 :- assert_must_fail(parsercall:is_real_literal_codes(".12")).
309 :- assert_must_fail(parsercall:is_real_literal_codes("12a.0")).
310 % TODO: we could return length and merge with parse_int_codes to do only one traversal
311
312 ?is_real_literal_codes([Digit|T]) :- is_digit(Digit), is_real_lit1(T).
313 is_real_lit1([0'.,Digit|T]) :- is_digit(Digit), is_real_lit2(T).
314 is_real_lit1([Digit|T]) :- is_digit(Digit), is_real_lit1(T).
315 is_real_lit2([]).
316 is_real_lit2([Digit|T]) :- is_digit(Digit), is_real_lit2(T).
317
318 % ---------------------
319
320 my_read_from_codes(end_of_file,_,_,Err) :- !,
321 read_line_if_ready(Err,Err1Codes), % we just read one line
322 safe_name(ErrMsg,Err1Codes),
323 add_error(parsercall,'Abnormal termination of parser: ',ErrMsg),
324 missing_parser_diagnostics,
325 ajoin(['Parser not available (',ErrMsg,')'],FullErrMsg),
326 read_lines_and_add_as_error(Err), % add other info on error stream as single error
327 throw(parse_errors([error(FullErrMsg,none)])).
328 my_read_from_codes(ErrorCodes,_Tree,Out,_) :-
329 append("Error",_,ErrorCodes),!,
330 atom_codes(ErrMsg,ErrorCodes),
331 add_error(parsercall,'Error running/starting parser: ',ErrMsg),
332 read_lines_and_add_as_error(Out),
333 fail.
334 my_read_from_codes(CodesIn,Tree,_,_) :- read_from_codes(CodesIn,Tree).
335
336 % a version of my_read_from_codes which has no access to error stream
337 my_read_from_codes(end_of_file,_) :- !,
338 missing_parser_diagnostics,
339 throw(parse_errors([error('Parser not available (end_of_file on input stream)',none)])).
340 my_read_from_codes(ErrorCodes,_Tree) :-
341 append("Error",_,ErrorCodes),!,
342 atom_codes(ErrMsg,ErrorCodes),
343 add_error(parsercall,'Error running/starting parser: ',ErrMsg),
344 fail.
345 my_read_from_codes(CodesIn,Tree) :- read_from_codes(CodesIn,Tree).
346
347 :- use_module(probsrc(preferences),[get_prob_application_type/1]).
348 missing_parser_diagnostics :-
349 parser_location(Classpath), debug_println(9,classpath(Classpath)),fail.
350 missing_parser_diagnostics :- runtime_application_path(P),
351 atom_concat(P,'/lib/probcliparser.jar',ConParser), % TODO: adapt for GraalVM cliparser
352 (file_exists(ConParser)
353 -> add_error(parsercall,'The Java B parser (probcliparser.jar) cannot be launched: ',ConParser),
354 (get_prob_application_type(tcltk) ->
355 add_error(parsercall,'Please check that Java and B Parser are available using the "Check Java and Parser Version" command in the Debug menu.')
356 ; host_platform(windows) ->
357 add_error(parsercall,'Please check that Java and B Parser are available, e.g., using "probcli.exe -check_java_version" command.')
358 ; add_error(parsercall,'Please check that Java and B Parser are available, e.g., using "probcli -check_java_version" command.')
359 % application_type
360 % TODO: avoid printing the message when the user is already calling check_java_version
361 ),
362 (get_preference(jvm_parser_additional_args,ExtraArgStr), ExtraArgStr\=''
363 -> add_error(parsercall,'Also check your JVM_PARSER_ARGS preference value:',ExtraArgStr)
364 ; true)
365 ; add_internal_error('Java B parser (probcliparser.jar) is missing from lib directory in: ',P)
366 ).
367
368 % ProB2 cli builds do not have the cli parser bundled
369 % check if there is a jar available in the lib folder:
370 % should only happen for get_prob_application_type(probcli) in ProB2
371 console_parser_jar_available_in_lib :-
372 runtime_application_path(P),
373 atom_concat(P,'/lib/probcliparser.jar',ConParser),
374 file_exists(ConParser).
375
376 handle_parser_exception(Exception) :-
377 ? handle_parser_exception(Exception,ExAuxs,[]),
378 !,
379 throw(parse_errors(ExAuxs)).
380 handle_parser_exception(_).
381
382 handle_parser_exception(compound_exception([])) --> [].
383 handle_parser_exception(compound_exception([Ex|MoreEx])) -->
384 handle_parser_exception(Ex),
385 handle_parser_exception(compound_exception(MoreEx)).
386 % Non-list version of parse_exception is handled in handle_parser_exception_aux.
387 % (Note: The non-list version is no longer generated by the parser -
388 % see comments on handle_console_parser_result below.)
389 handle_parser_exception(parse_exception([],_Msg)) --> [].
390 handle_parser_exception(parse_exception([Pos|MorePos],Msg)) -->
391 [error(Msg,Pos)],
392 ? handle_parser_exception(parse_exception(MorePos,Msg)).
393 handle_parser_exception(Ex) -->
394 {handle_parser_exception_aux(Ex,ExAux)},
395 [ExAux].
396
397 handle_parser_exception_aux(parse_exception(Pos,Msg),error(SanitizedMsg,Pos)) :-
398 remove_msg_posinfo_known(Msg,SanitizedMsg).
399 handle_parser_exception_aux(io_exception(Pos,Msg),error(Msg,Pos)).
400 handle_parser_exception_aux(io_exception(Msg),error(Msg,unknown)).
401 handle_parser_exception_aux(exception(Msg),error(Msg,unknown)). % TO DO: get Pos from Java parser !
402
403 %! parse_formula(+Codes,-Tree)
404 parse_formula(Codes,Tree) :-
405 parse_x(formula,Codes,Tree).
406
407 %! parse_expression(+Codes,-Tree)
408 parse_expression(Codes,Tree) :-
409 parse_x(expression,Codes,Tree).
410
411 %! parse_predicate(+Codes,-Tree)
412 parse_predicate(Codes,Tree) :-
413 parse_x(predicate,Codes,Tree).
414
415 %! parse_substitution(+Codes,-Tree)
416 parse_substitution(Codes,Tree) :-
417 parse_x(substitution,Codes,Tree).
418
419 % parse in the context of particular files and position within the file (e.g., VisB JSON file)
420
421 parse_at_position_in_file(Kind,Codes,Tree,Span,Filenumber) :- Span \= unknown,
422 %format('Parse @ ~w~n ~s~n',[Span,Codes]),
423 extract_line_col_for_b_parser(Span,Line,Col),!,
424 parse_at_position_in_file2(Kind,Codes,Tree,Filenumber,Line,Col).
425 parse_at_position_in_file(Kind,Codes,Tree,_,_) :-
426 parse_x(Kind,Codes,Tree).
427
428 parse_at_position_in_file2(Kind,Codes,Tree,Filenumber,Line,Col) :-
429 (Kind=expression -> true ; Kind=formula),
430 % special case to avoid setting parser options if parser not called in parse_x !
431 parse_simple_codes(Codes,Filenumber,Line,Col,AST),!,
432 Tree=AST.
433 parse_at_position_in_file2(Kind,Codes,Tree,Filenumber,Line,Col) :-
434 (Line \= 1 -> try_set_parser_option_nc(startLineNumber,Line,_OldLine)
435 ; true), % default in ParsingBehaviour.java: 1
436 (Col \= 1 -> try_set_parser_option_nc(startColumnNumber,Col,_OldCol)
437 ; true), % default in ParsingBehaviour.java: 1
438 (integer(Filenumber) -> Fnr=Filenumber ; format('Invalid filenumber : ~w~n',[Filenumber]), Fnr = -1),
439 try_set_parser_option(defaultFileNumber,Fnr,OldFile),
440 % Note: Filenumber will be used in AST, but parse errors will have null as filename
441 % format('Parsing at line ~w col ~w in file ~w (old ~w:~w in ~w)~n',[Line,Col,Filenumber,OldLine,OldCol,OldFile]),
442 call_cleanup(parse_y(Kind,Codes,Tree),
443 (%reset_old_parser_option_value(startLineNumber,Line,OldLine), % not required anymore, is now volatile
444 %reset_old_parser_option_value(startColumnNumber,Col,OldCol), % not required anymore, is now volatile
445 % TODO: should we check at startup whether the parser has volatilePosOptions feature?
446 reset_old_parser_option_value(defaultFileNumber,Filenumber,OldFile))).
447
448 extract_line_col_for_b_parser(Span,Line,Col1) :-
449 extract_line_col(Span,Line,Col,_EndLine,_EndCol),
450 Col1 is Col+1. % the BParser starts column numbering at 1
451
452 set_default_filenumber(Filenumber,OldFile) :- try_set_parser_option(defaultFileNumber,Filenumber,OldFile).
453 reset_default_filenumber(Filenumber,OldFile) :- reset_old_parser_option_value(defaultFileNumber,Filenumber,OldFile).
454
455 %! parse(+Kind,+Codes,-Tree)
456 parse(Kind,Codes,Tree) :- parse_x(Kind,Codes,Tree).
457
458 %! parse_temporal_formula(+Kind,+LanguageExtension,+Formula,-Tree)
459 parse_temporal_formula(Kind,LanguageExtension,Formula,Tree) :-
460 write_to_codes(Formula,CodesWithNewlines),
461 % Java parser expects only one line of input -> remove newlines
462 cleanup_newlines(CodesWithNewlines,Codes),
463 get_console_parser(Stream,Out,Err),
464 send_definitions(Stream),
465 format(Stream,'~s', [Kind]), nl(Stream),
466 format(Stream,'~s', [LanguageExtension]), nl(Stream),
467 format(Stream,'~s',[Codes]), nl(Stream),
468 flush_output(Stream),
469 % format('Parsing temporal formula, kind=~s, lge=~s, formula="~s"~n',[Kind,LanguageExtension,Codes]),
470 read_line(Stream,CodesIn), % TODO: Improve feedback from java!
471 catch(my_read_from_codes(CodesIn,Tree,Stream,Err),_,throw(parse_errors([error(exception,none)]))),
472 display_pending_outputs(Out,user_output).
473
474 % throws parse_errors(Errors) in case of syntax errors
475 load_b_machine_as_term(Filename, Machine,Options) :- %print(load_b_machine(Filename)),nl,
476 prob_filename(Filename, ProBFile),
477 debug_println(9,prob_filename(Filename,ProBFile)),
478 ( get_preference(jvm_parser_force_parsing,false),
479 dont_need_compilation(Filename,ProBFile,Machine,LoadResult,Options) ->
480 % no compilation is needed, .prob file was loaded
481 release_parser_if_requested(Options),
482 (LoadResult=loaded -> debug_println(20,'.prob file up-to-date')
483 ; print_error('*** Loading failed *** '),nl,fail)
484 ; % we need to parse and generate .prob file
485 (debug:debug_mode(on) ->
486 time_with_msg('generating .prob file with Java parser',call_console_parser(Filename,ProBFile))
487 ; call_console_parser(Filename,ProBFile)
488 ),
489 release_parser_if_requested(Options),
490 load_b_machine_probfile_as_term(ProBFile, Options, Machine)
491 ).
492 ?release_parser_if_requested(Options) :- (member(release_java_parser,Options) -> release_console_parser ; true).
493
494 % converts the filename of the machine into a filename for the parsed machine
495 prob_filename(Filename, ProB) :-
496 split_filename(Filename,Basename,_Extension),
497 safe_atom_chars(Basename,BasenameC,prob_filename1),
498 append(BasenameC,['.','p','r','o','b'],ProBC),
499 safe_atom_chars(ProB,ProBC,prob_filename2),!.
500 prob_filename(Filename, ProB) :-
501 add_failed_call_error(prob_filename(Filename,ProB)),fail.
502
503 check_version_and_read_machine(Filename,ProBFile, ProbTime, Version, GitSha, Machine,Options) :-
504 (Version='$UNKNOWN'
505 -> add_warning(parsercall,'*** Unknown parser version number. Trying to load parsed file: ',ProBFile)
506 /* keep CheckVersionNr as variable */
507 ; CheckVersionNr=Version),
508 read_machine_term_from_file(ProBFile,prob_parser_version(CheckVersionNr,GitSha),
509 [check_if_up_to_date(Filename,ProbTime)|Options],Machine).
510
511 read_machine_term_from_file(ProBFile,VersionTerm,Options,CompleteMachine) :-
512 (debug:debug_mode(on)
513 -> time_with_msg('loading .prob file',
514 read_machine_term_from_file_aux(ProBFile,VersionTerm,Options,CompleteMachine))
515 ; read_machine_term_from_file_aux(ProBFile,VersionTerm,Options,CompleteMachine)).
516
517 read_machine_term_from_file_aux(ProBFile,VersionTerm,Options,complete_machine(MainName,Machines,Files)) :-
518 open_probfile(ProBFile,Options,Stream,Opt2),
519 call_cleanup(( safe_read_term(Stream,Opt2,ProBFile,parser_version,VersionTermInFile),
520 debug_format(9,'Parser version term in file: ~w~n',[VersionTermInFile]),
521 same_parser_version(VersionTermInFile,VersionTerm), % CHECK correct version
522 read_machines(Stream,ProBFile,Opt2,Machines,MainName,Files),
523 (var(MainName)
524 -> add_internal_error('Missing classical_b fact',ProBFile), MainName=unknown,Files=[]
525 ; true)
526 ),
527 close(Stream)).
528
529 % open a .prob file:
530 open_probfile(ProBFile,Options,NewStream,NewOptions) :-
531 open(ProBFile, read, Stream),
532 file_property(ProBFile,size_in_bytes,Size),
533 check_prob_file_not_empty(Stream,Size,ProBFile,Options),
534 peek_code(Stream,Code),
535 open_probfile_aux(Code,Size,Stream,ProBFile,Options,NewStream,NewOptions).
536
537 open_probfile_aux(Code,_,Stream,ProBFile,Options,NewStream,NewOptions) :-
538 fastrw_start_code(Code),!,
539 close(Stream),
540 add_message(parsercall,'Reading .prob file using fastrw library: ',ProBFile),
541 open(ProBFile, read, NewStream, [type(binary)]),
542 % delete(use_fastread)
543 NewOptions = [use_fastrw|Options].
544 open_probfile_aux(Code,Size,Stream,ProBFile,Options,Stream,NewOptions) :-
545 check_prob_file_first_code(Code,Stream,ProBFile,Options),
546 update_options(Options,Size,NewOptions).
547
548 % automatically use fastread if file large
549 update_options(Options,Size,Opt2) :- nonmember(use_fastread,Options),
550 nonmember(use_fastrw,Options),
551 Size>1000000, % greater than 1 MB
552 !, Opt2 = [use_fastread|Options],
553 debug_println(19,automatically_using_fastread(Size)).
554 update_options(Opts,_,Opts).
555
556 % check if the file starts out in an expected way
557 check_prob_file_not_empty(Stream,Size,ProBFile,Options) :- Size=<0,!,
558 close(Stream),
559 (member(check_if_up_to_date(_,_),Options)
560 -> add_warning(parsercall,'Re-running parser as .prob file is empty: ',ProBFile)
561 % due to possible crash or CTRL-C of previous parser run
562 ; add_internal_error('Generated .prob file is empty',ProBFile)
563 ),fail.
564 check_prob_file_not_empty(_,_,_,_).
565
566 % check first character code
567 check_prob_file_first_code(Code,Stream,ProBFile,Options) :-
568 (valid_start_code(Code) -> true
569 ; fastrw_start_code(Code)
570 -> close(Stream),
571 % we need to do: open(ProBFile,read,S,[type(binary)]),
572 (member(check_if_up_to_date(_,_),Options)
573 -> add_warning(parsercall,'Re-running parser as .prob file seems to be in new fast-rw format: ',ProBFile)
574 ; add_error(parsercall,'Cannot load .prob file; it seems to be in new fast-rw format:',ProBFile)
575 ),
576 fail
577 ; add_message(parsercall,'Strange start character code in .prob file: ',[Code]) % probably syntax error will happen
578 ).
579
580 fastrw_start_code(0'D). % typical start DSparser_version
581
582 valid_start_code(0'p). % from parser_version(_) term
583 valid_start_code(37). % percentage sign; appears when parser called with verbose flag
584 valid_start_code(47). % start of comment slash (inserted by user)
585 valid_start_code(39). % ' (inserted by user)
586 valid_start_code(32). % whitespace (inserted by user)
587 valid_start_code(8). % tab (inserted by user)
588 valid_start_code(10). % lf (inserted by user)
589 valid_start_code(13). % cr (inserted by user)
590
591 % --------------------
592
593 % use various read_term mechanisms depending on Options
594
595 safe_read_term(Stream,Options,File,Expecting,T) :-
596 catch(
597 safe_read_term2(Options,Stream,File,Expecting,T),
598 error(permission_error(_,_,_),ERR),
599 (
600 ajoin(['Permission error in .prob file trying to read ',Expecting,' term in ',File,':'],Msg),
601 add_internal_error(Msg,ERR),
602 fail
603 )).
604
605 :- use_module(tools_fastread,[read_term_from_stream/2, fastrw_read/3]).
606
607 safe_read_term2([use_fastrw|_],Stream,File,Expecting,Term) :- !,
608 fastrw_read(Stream,Term,Error),
609 (Error=false -> true
610 ; ajoin(['.prob (fastrw) file has error in ',Expecting,' term:'],Msg),
611 add_internal_error(Msg,File),
612 fail
613 ), functor(Term,F,N),debug_println(19,fast_read_term(F,N)).
614 safe_read_term2([use_fastread|_],Stream,File,Expecting,Term) :- !,
615 (read_term_from_stream(Stream,Term) -> true
616 ; ajoin(['.prob file has Prolog syntax error in ',Expecting,' term:'],Msg),
617 add_internal_error(Msg,File), fail
618 ).
619 safe_read_term2([_|Opts],Stream,File,Expecting,T) :- !,
620 safe_read_term2(Opts,Stream,File,Expecting,T).
621 safe_read_term2([],Stream,File,Expecting,T) :- % use regular SICStus Prolog reader
622 catch(read(Stream,T), error(E1,E2), (
623 ajoin(['.prob file (',File,') has error in ',Expecting,' term:'],Msg),
624 add_error(parser_call,Msg,error(E1,E2)),
625 fail
626 )).
627
628
629
630 % ------------------
631
632 % check if file was parsed with same parser version as currently in use by ProB
633 same_parser_version(parser_version(V), prob_parser_version(VB,GitSha)) :- !,
634 (V=VB -> true ; V=GitSha).
635 same_parser_version(end_of_file,_) :- !, debug_format(19,'.prob file is empty~n',[]).
636 same_parser_version(T,_) :-
637 add_internal_error('.prob file contains illegal parser_version term:',T),fail.
638
639 load_b_machine_list_of_facts_as_term(Facts,Machine) :-
640 read_machine_term_from_list_of_facts(Facts,_,Machine).
641 read_machine_term_from_list_of_facts(Facts,VersionTerm,complete_machine(MainName,Machines,FileList)) :-
642 (select(parser_version(VERS),Facts,F2) -> VersionTerm = parser_version(VERS)
643 ; add_internal_error('No parser_version available',read_machine_from_list_of_facts),
644 F2=Facts, VersionTerm = unknown),
645 (select(classical_b(MainName,FileList),F2,F3) -> true
646 ; add_internal_error('No classical_b file information available',read_machine_from_list_of_facts),
647 fail),
648 debug_println(9,loading_classical_b(VersionTerm,MainName,FileList)),
649 include(is_machine_term,F3,F4),
650 maplist(get_machine_term,F4,Machines).
651
652 is_machine_term(machine(_M)).
653 get_machine_term(machine(M),M).
654
655 load_b_machine_probfile_as_term(ProBFile, Machine) :-
656 load_b_machine_probfile_as_term(ProBFile, [], Machine).
657
658 load_b_machine_probfile_as_term(ProBFile, Options, Machine) :-
659 debug_println(9,consulting(ProBFile)),
660 ( read_machine_term_from_file(ProBFile,_VersionTerm,Options,Machine) -> true
661 ; add_error(parsercall,'Failed to read parser output: wrong format?',ProBFile),fail).
662
663 read_machines(S,ProBFile,Options,Machines,MainName,Files) :-
664 safe_read_term(S,Options,ProBFile,machine,Term),
665 %, write_term(Term,[max_depth(5),numbervars(true)]),nl,
666 ( Term == end_of_file ->
667 Machines = []
668 ; Term = classical_b(M,F) ->
669 ((M,F)=(MainName,Files) -> true ; add_internal_error('Inconsistent classical_b fact: ',classical_b(M,F))),
670 ? (member(check_if_up_to_date(MainFileName,ProbTime),Options)
671 -> debug_format(9,'Checking if .prob file up-to-date for subsidiary files and if it was generated for ~w~n',[MainFileName]),
672 all_older(Files, ProbTime), % avoid reading rest if subsidiary source files not all_older !
673 (Files = [File1|_],
674 same_file_name(MainFileName,File1) -> true
675 ; format('Re-parsing: .prob file created for different source file:~n ~w~n',[MainFileName]),
676 % this can happen, e.g., when we have M.def and M.mch and we previously loaded the other file
677 Files = [File1|_],
678 format(' ~w~n',[File1]),
679 fail
680 )
681 ; true),
682 read_machines(S,ProBFile,Options,Machines,MainName,Files)
683 ; Term = machine(M) ->
684 Machines = [M|Mrest],
685 read_machines(S,ProBFile,Options,Mrest,MainName,Files)
686 ).
687
688
689 % checks if all files that the prob-file depends on
690 % are older than the prob-file
691 dont_need_compilation(Filename,ProB,Machine,LoadResult,Options) :-
692 file_exists(Filename),
693 catch( ( get_parser_version(Version,GitSha),
694 file_property(Filename, modify_timestamp, BTime),
695 debug_format(9, 'Parser Version: ~w, Filename Timestamp: ~w : ~w~n',[Version,Filename,BTime]),
696 file_exists(ProB), % the .prob file exists
697 file_property(ProB, modify_timestamp, ProbTime),
698 debug_format(9, '.prob Timestamp: ~w : ~w~n',[ProB,ProbTime]),
699 % print(time_stamps(ProB,ProbTime,Filename,BTime)),nl,
700 BTime < ProbTime, % .mch file is older than .prob file
701 file_property(ProB,size_in_bytes,Size),
702 (Size>0 -> true
703 ; debug_mode(on),
704 add_message(parsercall,'.prob file is empty; parsing again',ProB),
705 % possibly due to crash or error before, cf second run of test 254
706 fail),
707 check_version_and_read_machine(Filename,ProB, ProbTime, Version, GitSha, Machine,Options),
708 %Machine = complete_machine(_,_,_),
709 LoadResult = loaded),
710 error(A,B),
711 (debug_println(9,error(A,B)),
712 (A=resource_error(memory)
713 -> add_error(load_b_machine,'Insufficient memory to load file.\nTry starting up ProB with more memory\n (e.g., setting GLOBALSTKSIZE=500M before starting ProB).'),
714 LoadResult=A
715 ; fail))).
716
717 % check that all included files are older than the .prob file (ProbTime):
718 all_older([],_).
719 all_older([File|Rest],ProbTime) :-
720 (file_exists(File)
721 -> file_property(File, modify_timestamp, BTime),
722 debug_format(9,' Subsidiary File Timestamp: ~w:~w~n',[File,BTime]),
723 ( BTime < ProbTime -> true
724 ; format('File has changed : ~w : ~w~n',[File,BTime]),
725 fail )
726 ; virtual_dsl_file(File) -> debug_format(9,'Virtual file generated by DSL translator: ~w~n',[File])
727 ; debug_format(20,'File does not exist anymore: ~w; calling parser.~n',[File]),fail),
728 all_older(Rest,ProbTime).
729
730 virtual_dsl_file(File) :- atom_codes(File,Codes),
731 Codes = [95|_], % file name starts with _ underscore
732 nonmember(47,Codes), % no slash
733 nonmember(92,Codes). % no backslash
734
735 :- use_module(system_call).
736
737 get_extra_args_list(ExtraArgs) :-
738 get_preference(jvm_parser_additional_args,ExtraArgsStr),
739 ExtraArgsStr \= '',
740 atom_codes(ExtraArgsStr,C),
741 split_chars(C," ",CA),
742 maplist(atom_codes,ExtraArgs,CA).
743
744 jvm_options -->
745 ({get_preference(jvm_parser_heap_size_mb,Heap), Heap>0} ->
746 {ajoin(['-Xmx',Heap,'m'],HeapOpt)},
747 [HeapOpt]
748 ;
749 % comment in to simulate low-memory situations
750 % use -Xmx221500000m instead to force exception and test ProB's response
751 [] %['-Xmx20m']
752 ),
753
754 ({get_extra_args_list(ExtraArgs)} ->
755 {debug_format(19,'Using additional arguments for JVM (JVM_PARSER_ARGS): ~w~n',[ExtraArgs])},
756 ExtraArgs
757 % use the former LARGE_JVM / use_large_jvm_for_parser by default
758 % if we are running on a 64-bit system
759 % FIXME Is it intentional that this option is not added if JVM_PARSER_ARGS is set?
760 ; {platform_is_64_bit} -> ['-Xss5m'] % default stacksize; set to -Xss150k to mimic low stack setting
761 ; []
762 ).
763
764 parser_jvm_options -->
765 {get_PROBPATH(PROBPATH), atom_concat('-Dprob.stdlib=', PROBPATH, Stdlibpref)},
766 [Stdlibpref],
767 jvm_options.
768
769
770 % we could do something like this: or allow user to provide list of JVM options
771 %get_gc_options([]) :- !,get_preference(jvm_parser_aggressive_gc,true).
772 %get_gc_options(['-XX:GCTimeRatio=19', '-XX:MinHeapFreeRatio=20', '-XX:MaxHeapFreeRatio=30']).
773 % see https://stackoverflow.com/questions/30458195/does-gc-release-back-memory-to-os
774
775 parser_cli_options -->
776 %% ['-v'], % comment in for verbose, -time for timing info
777 ({debug_mode(on)} -> ['-v', '-time'] ; []),
778
779 % useful for very large data validation machines
780 ({get_preference(jvm_parser_position_infos,true)} -> ['-lineno'] ; []),
781
782 %( get_preference(jvm_parser_fastrw,false) ; parser_version_at_least(2,12,0) )
783 % we cannot call: parser_version_at_least(2,12,0) as parser not yet started
784 % older parsers were printing comments into the binary fastrw output; TODO: perform check later
785 ({get_preference(jvm_parser_fastrw,true)} -> ['-fastprolog'] ; ['-prolog']),
786
787 % -compactpos enables new p3, p4, p5 position terms and disables node numbering
788 ['-prepl', '-compactpos'].
789
790 parser_command_args(native(NativeCmd), NativeCmd) -->
791 parser_jvm_options,
792 parser_cli_options.
793 parser_command_args(java_jar(NativeCmd,JarPath), NativeCmd) -->
794 parser_jvm_options,
795 ['-jar', JarPath],
796 parser_cli_options.
797
798 % ensure that the console Java process is running
799 ensure_console_parser_launched :-
800 catch(parse_formula("1",_), E, (
801 add_internal_error('Cannot launch console parser (probcliparser.jar)',E),
802 fail
803 )).
804
805 parser_is_launched :-
806 java_parser_process(_,_,_,_).
807
808
809 % TO DO: also store output stream for debugging messages
810 get_console_parser(Stream,Out,Err) :-
811 java_parser_process(PID,Stream,Out,Err),
812 % On SICStus, calling is_process seams to be unreliable (if the process has crashed)
813 % and process_wait does not always work either,
814 % so first check if in and output streams are still available.
815 \+ at_end_of_stream(Stream),
816 % On SWI, at_end_of_stream doesn't detect broken pipes,
817 % so additionally also check that the process has not exited/crashed.
818 process_wait(PID,timeout,[timeout(0)]),
819 !,
820 (at_end_of_stream(Err)
821 -> add_error(parsercall,'Java process not running'),
822 read_lines_and_add_as_error(Stream)
823 ; true).
824 get_console_parser(Stream,Out,Err) :-
825 clear_old_console_parsers,
826 get_java_command_for_parser(ParserCmd),
827 phrase(parser_command_args(ParserCmd, NativeCmd), FullArgs),
828 debug_println(19,launching_java_console_parser(NativeCmd,FullArgs)),
829 debug_print_system_call(NativeCmd,FullArgs),
830 system_call_keep_open(NativeCmd,FullArgs,PID,_In,Out,Err,[]), % note: if probcliparser.jar does not exist we will not yet get an error here !
831 debug_println(9,java_console_parser_launched(PID)),
832 % read first line to get port number
833 safe_read_line(Out,Err,1,[],Term),
834 connect_to_console_parser_on_port(Term,PID,Out,Err,Stream),
835 !.
836 get_console_parser(_,_,_) :- missing_parser_diagnostics, fail.
837
838
839
840
841 connect_to_console_parser_on_port(Term,PID,Out,Err,Stream) :-
842 catch(
843 socket_client_open('':Term, Stream, [type(text),encoding(utf8)]),
844 E, %error(system_error,system_error(E)), % SPIO_E_NET_HOST_NOT_FOUND
845 (
846 add_internal_error('Exception while opening parser socket (possibly an error occurred during startup of the parser): ',E),
847 % e.g., not enough memory to start parser and wait for socket connection
848 read_lines_and_add_as_error(Err), % try and read additional info;
849 % if we set the stack size to be very small (-Xss20k) we get: Error: Could not create the Java Virtual Machine
850 fail
851 )),
852 assertz(java_parser_process(PID,Stream,Out,Err)).
853
854 % connect to a separately started parser
855 % allow to connect to a separately started java command line parser (java -jar probcliparser.jar -prepl)
856 connect_to_external_console_parser_on_port(PortNumber) :-
857 clear_old_console_parsers,
858 format('Trying to connect to external parser on port ~w~n',[PortNumber]),
859 Out=user_input, Err=user_input,
860 (connect_to_console_parser_on_port(PortNumber,external_process,Out,Err,_)
861 -> format('Connected to parser on port ~w~n',[PortNumber])
862 ; add_error(parsercall,'Could not connect to parser on port:',PortNumber)
863 ).
864
865 clear_old_console_parsers :-
866 retract(java_parser_process(PID,_,_,_)),
867 parser_process_release(PID),
868 fail.
869 clear_old_console_parsers.
870
871 parser_process_release(external_process) :- !.
872 parser_process_release(PID) :- process_release(PID).
873
874 % useful to try and free up memory
875 % alternatively we could add some JVM options: -XX:GCTimeRatio=19 -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=30
876 % see https://stackoverflow.com/questions/30458195/does-gc-release-back-memory-to-os
877 release_console_parser :-
878 (java_parser_process(PID,Stream,_,_),
879 PID \= external_process
880 -> format('Releasing Java parser id=~w (note: access to DEFINITIONS is lost)~n',[PID]),
881 write(Stream,'halt'), nl(Stream),
882 flush_output(Stream), % handled in Java in runPRepl in CliBParser.java;
883 retract(java_parser_process(PID,_,_,_)),
884 parser_process_release(PID) % or should we call process_kill ?
885 %process_kill(PID)
886 % Note: parser maybe launched again, but then not have access to DEFINITIONS
887 ; true).
888
889 :- use_module(runtime_profiler,[profile_single_call/3]).
890 call_console_parser(Filename, ProB) :-
891 profile_single_call('$parsing',unknown,parsercall:call_console_parser2(Filename, ProB)).
892 call_console_parser2(Filename, ProB) :-
893 update_jvm_parser_preferences,
894 get_console_parser(Stream,Out,Err),
895
896 % path to the .prob file
897 replace_windows_path_backslashes(ProB,ProBW),
898 % the input file
899 replace_windows_path_backslashes(Filename,FilenameW),
900 % call PREPL 'machine' command
901 debug_format(4,'PREPL command: machine~n~w~n~w~n',[FilenameW,ProBW]),
902 format(Stream,'machine~n~w~n~w~n',[FilenameW,ProBW]), % tell parser to write output to ProBW .prob file
903 flush_output(Stream),
904
905 %% WARNING: if the java parser writes too much output on the out stream it will block
906 %% until its output is read and we have a deadlock !
907 %% hence we use safe_polling_read_line
908 safe_polling_read_line(Stream,Out,Err,1,[],Term),
909
910 display_pending_outputs(Out,user_output),
911 handle_console_parser_result(Term,Err).
912
913
914 safe_read_line(Out,Err,LineNr,SoFar,Term) :-
915 safe_read_line_aux(read_line,Out,Err,LineNr,SoFar,Term).
916
917 safe_read_line_if_ready(Out,Err,LineNr,SoFar,Term) :-
918 safe_read_line_aux(read_line_if_ready,Out,Err,LineNr,SoFar,Term).
919
920 % a version that will check every second if there is output to be read
921 safe_polling_read_line(Stream,_Out,Err,LineNr,SoFar,Term) :-
922 stream_ready_to_read(Stream,2),!, % try for two seconds
923 safe_read_line_aux(read_line,Stream,Err,LineNr,SoFar,Term).
924 safe_polling_read_line(Stream,Out,Err,LineNr,SoFar,Term) :-
925 % this should normally not happen unless we have a debugging version of the parser
926 debug_format(19,'Parser not responded yet, trying to read its output~n',[]),
927 display_pending_outputs(Out,user_output),
928 read_lines_and_add_as_error(Err),
929 safe_polling_read_line(Stream,Out,Err,LineNr,SoFar,Term).
930
931 safe_read_line_aux(Pred,Out,Err,LineNr,SoFar,Term) :-
932 catch(call(Pred,Out,Codes),Exception, % read another line
933 (add_error(parsercall,'Exception while reading next line: ',Exception),
934 read_lines_and_add_as_error(Err),
935 display_pending_outputs(user_error,Out),
936 throw(Exception))),
937 Codes \= end_of_file, !, % happens if parser crashed and stream is closed
938 append(SoFar,Codes,NewCodes),
939 catch(my_read_from_codes(NewCodes,Term,Out,Err),Exception,
940 safe_read_exception(Out,Err,LineNr,Exception,NewCodes,Term)).
941 safe_read_line_aux(_Pred,_Out,Err,_LineNr,CodesSoFar,_) :-
942 safe_name(T,CodesSoFar),
943 add_error(parsercall,'Unexpected error while parsing machine: ',T),
944 read_lines_and_add_as_error(Err),fail.
945
946
947 safe_read_exception(Out,_,1,_Exception,CodesSoFar,_Term) :- append("Error",_,CodesSoFar),!,
948 % probably some Java error, we do not obtain a correct Prolog term, but probably something like:
949 % "Error occurred during initialization of VM"
950 safe_name(T,CodesSoFar),
951 add_error(parsercall,'Java error while parsing machine: ',T),
952 read_lines_and_add_as_error(Out),fail.
953 safe_read_exception(Out,Err,LineNr,error(syntax_error(M),_),CodesSoFar,Term) :- LineNr<10, % avoid infinite loop in case unexpected errors occur which cannot be solved by reading more input lines
954 !,
955 %(debug:debug_mode(off) -> true ; format('Syntax error in parser result (line ~w): ~w~nTrying to read another line.~n',[LineNr,M])),
956 add_warning(parsercall,'Syntax error in parser result; trying to read another line. Line:Error = ',LineNr:M),
957 L1 is LineNr+1,
958 safe_read_line_if_ready(Out,Err,L1,CodesSoFar,Term). % try and read another line; we assume the syntax error is because the TERM is not complete yet and transmitted on multiple lines (Windows issue)
959 safe_read_exception(_,_,_LineNr,parse_errors(Errors),_CodesSoFar,_Term) :- !,
960 debug_println(4,read_parse_errors(Errors)),
961 throw(parse_errors(Errors)).
962 safe_read_exception(_,_,_LineNr,Exception,_CodesSoFar,_Term) :-
963 add_internal_error('Unexpected exception occurred during parsing: ',Exception),fail.
964 %safe_name(T,CodesSoFar), add_error_fail(parsercall,'Unexpected error while parsing machine: ',T).
965
966 safe_name(Name,Codes) :- var(Name),Codes == end_of_file,!,
967 %add_internal_error('Parser does not seem to be available',''),
968 Name=end_of_file.
969 safe_name(Name,Codes) :- number(Name),!,
970 safe_number_codes(Name,Codes).
971 safe_name(Name,Codes) :-
972 catch(atom_codes(Name,Codes), E,
973 (print('Exception during atom_codes: '),print(E),nl,nl,throw(E))).
974
975 safe_number_codes(Name,Codes) :-
976 catch(number_codes(Name,Codes), E,
977 (print('Exception during number_codes: '),print(E),nl,nl,throw(E))).
978
979 % Note: As of parser version 2.9.28 commit ee2592d8ca2900b4e339da973920e034dff43658,
980 % all parse errors are reported as terms of the form
981 % parse_exception(Positions,Msg) where Positions is a list of pos/5 terms.
982 % The term formats preparse_exception(Tokens,Msg) (where Tokens is a list of none/0 or pos/3 terms)
983 % and parse_exception(Pos,Msg) (where Pos is a single none/0, pos/3 or pos/5 term)
984 % are no longer generated by the parser.
985 % The code for handling these old term formats can probably be removed soon.
986
987 % see also handle_parser_exception
988 %handle_console_parser_result(E,_) :- nl,print(E),nl,fail.
989 handle_console_parser_result(exit(0),_) :- !.
990 handle_console_parser_result(io_exception(Msg),_) :- !,
991 add_error_fail(parsercall,'Error while opening the B file: ',Msg).
992 handle_console_parser_result(compound_exception(List),ErrStream) :- !,
993 get_compound_exceptions(List,ErrStream,ParseErrors,[]),
994 throw(parse_errors(ParseErrors)).
995 handle_console_parser_result(preparse_exception(Tokens,Msg),_) :- !,
996 remove_msg_posinfo(Msg,SanitizedMsg,MsgPos),
997 findall(error(SanitizedMsg,Pos),member(Pos,Tokens),Errors1),
998 (Errors1 = []
999 -> /* if there are no error tokens: use pos. info from text Msg */
1000 Errors = [error(SanitizedMsg,MsgPos)]
1001 ; Errors = Errors1),
1002 debug_println(4,preparse_exception_parse_errors(Tokens,Msg,Errors)),
1003 throw(parse_errors(Errors)).
1004 handle_console_parser_result(parse_exception([],Msg),_) :-
1005 atom(Msg), atom_codes(Msg,MC),
1006 append("StackOverflowError",_,MC),!,
1007 add_error(parsercall,'Java VM error while running parser: ',Msg),
1008 print_hint('Try increasing stack size for Java using the JVM_PARSER_ARGS preference (e.g.,"-Xss10m").'),
1009 fail.
1010 handle_console_parser_result(parse_exception(Positions,Msg),_) :-
1011 (Positions = [] ; Positions = [_|_]), !,
1012 remove_msg_posinfo(Msg,SanitizedMsg,MsgPos),
1013 findall(error(SanitizedMsg,Pos),member(Pos,Positions),Errors1),
1014 (Errors1 = []
1015 -> /* if there are no error tokens: use pos. info from text Msg */
1016 Errors = [error(SanitizedMsg,MsgPos)]
1017 ; Errors = Errors1),
1018 debug_println(4,parse_exception_parse_errors(Positions,Msg,Errors)),
1019 throw(parse_errors(Errors)).
1020 handle_console_parser_result(parse_exception(Pos,Msg),_) :- !,
1021 remove_msg_posinfo_known(Msg,SanitizedMsg), %print(sanitized(SanitizedMsg)),nl,
1022 debug_println(4,parse_exception(Pos,Msg,SanitizedMsg)),
1023 throw(parse_errors([error(SanitizedMsg,Pos)])).
1024 handle_console_parser_result(exception(Msg),_) :- !,
1025 %add_error(bmachine,'Error in the classical B parser: ', Msg),
1026 remove_msg_posinfo(Msg,SanitizedMsg,Pos),
1027 debug_println(4,exception_in_parser(Msg,SanitizedMsg,Pos)),
1028 throw(parse_errors([error(SanitizedMsg,Pos)])).
1029 handle_console_parser_result(end_of_file,Err) :- !,
1030 % probably NullPointerException or similar in parser; no result on StdOut
1031 debug_println(9,end_of_file_on_parser_output_stream),
1032 format(user_error,'Detected abnormal termination of B Parser~n',[]), % print in case we block
1033 read_line_if_ready(Err,Err1),
1034 (Err1=end_of_file -> Msg = 'Abnormal termination of B Parser: EOF on streams'
1035 ; append("Abnormal termination of B Parser: ",Err1,ErrMsgCodes),
1036 safe_name(Msg,ErrMsgCodes),
1037 read_lines_and_add_as_error(Err) % also show additional error infos
1038 ),
1039 throw(parse_errors([error(Msg,none)])).
1040 handle_console_parser_result(compound_exception(Exs),_) :- !,
1041 maplist(handle_parser_exception_aux,Exs,ExAuxs),
1042 debug_println(4,compound_exception(Exs,ExAuxs)),
1043 throw(parse_errors(ExAuxs)).
1044 handle_console_parser_result(Term,_) :- !,
1045 write_term_to_codes(Term,Text,[]),
1046 safe_name(Msg,Text),
1047 %add_error(bmachine,'Error in the classical B parser: ', Msg),
1048 remove_msg_posinfo(Msg,SanitizedMsg,Pos),
1049 debug_println(4,unknown_error_term(Term,SanitizedMsg,Pos)),
1050 throw(parse_errors([error(SanitizedMsg,Pos)])).
1051
1052 % get all compound exceptions as a single result to be thrown once later:
1053 get_compound_exceptions([],_) --> [].
1054 get_compound_exceptions([Err1|T],ErrStream) -->
1055 {handle_and_catch(Err1,ErrStream,ParseErrors) },
1056 ParseErrors,
1057 get_compound_exceptions(T,ErrStream).
1058
1059 handle_and_catch(Exception,ErrStream,ParseErrors) :-
1060 catch(
1061 (handle_console_parser_result(Exception,ErrStream) -> ParseErrors=[] ; ParseErrors=[]),
1062 parse_errors(Errors1),
1063 ParseErrors=Errors1).
1064
1065 % now replaced by remove_msg_posinfo:
1066 %extract_position_info(Text,pos(Row,Col,Filename)) :-
1067 % atom_codes(Text,Codes),
1068 % epi(Row,Col,Filename,Codes,_),!.
1069 %extract_position_info(_Text,none).
1070
1071 :- assert_must_succeed((parsercall:epi(R,C,F,"[2,3] xxx in file: /usr/lib.c",[]), R==2,C==3,F=='/usr/lib.c')).
1072 :- assert_must_succeed((parsercall:epi(R,C,F,"[22,33] xxx yyy ",_), R==22,C==33,F=='unknown')).
1073
1074 % we expect error messages to start with [Line,Col] information
1075 epi(Row,Col,Filename) --> " ",!,epi(Row,Col,Filename).
1076 epi(Row,Col,Filename) --> "[", epi_number(Row),",",epi_number(Col),"]",
1077 (epi_filename(Filename) -> [] ; {Filename=unknown}).
1078 epi_number(N) --> epi_numbers(Chars),{safe_number_codes(N,Chars)}.
1079 epi_numbers([C|Rest]) --> [C],{is_number(C)},epi_numbers2(Rest).
1080 epi_numbers2([C|Rest]) --> [C],{is_number(C),!},epi_numbers2(Rest).
1081 epi_numbers2("") --> !.
1082 is_number(C) :- member(C,"0123456789"),!.
1083 epi_filename(F) --> " in file: ",!,epi_path2(Chars),{safe_name(F,Chars)}.
1084 epi_filename(F) --> [_], epi_filename(F).
1085 epi_path2([C|Rest]) --> [C],!,epi_path2(Rest). % assume everything until the end is a filename
1086 epi_path2([]) --> [].
1087
1088 :- assert_must_succeed((parsercall:remove_rowcol(R,C,"[22,33] xxx yyy ",_), R==22,C==33)).
1089 % remove parser location info from message to avoid user clutter
1090 remove_rowcol(Row,Col) --> " ",!, remove_rowcol(Row,Col).
1091 remove_rowcol(Row,Col) --> "[", epi_number(Row),",",epi_number(Col),"]".
1092
1093 :- assert_must_succeed((parsercall:remove_filename(F,C,"xxx yyy in file: a.out",R), F == 'a.out', C == "xxx yyy", R==[])).
1094 remove_filename(F,[]) --> " in file: ",!,epi_path2(Chars),{safe_name(F,Chars)}.
1095 remove_filename(F,[C|T]) --> [C], remove_filename(F,T).
1096
1097 :- assert_must_succeed((parsercall:remove_filename_from_codes("xxx yyy in file: a.out",F,C), F == 'a.out', C == "xxx yyy")).
1098 :- assert_must_succeed((parsercall:remove_filename_from_codes("xxx yyy zzz",F,C), F == 'unknown', C == "xxx yyy zzz")).
1099 remove_filename_from_codes(Codes,F,NewCodes) :-
1100 (remove_filename(F,C1,Codes,C2) -> append(C1,C2,NewCodes) ; F=unknown, NewCodes=Codes).
1101
1102 % obtain position information from a message atom and remove the parts from the message at the same time
1103 remove_msg_posinfo_known(Msg,NewMsg) :- remove_msg_posinfo(Msg,NewMsg,_,pos_already_known).
1104 remove_msg_posinfo(Msg,NewMsg,Pos) :- remove_msg_posinfo(Msg,NewMsg,Pos,not_known).
1105 remove_msg_posinfo(Msg,NewMsg,Pos,AlreadyKnown) :- %print(msg(Msg,AlreadyKnown)),nl,
1106 atom_codes(Msg,Codes),
1107 (remove_rowcol(Row,Col,Codes,RestCodes)
1108 -> Pos=pos(Row,Col,Filename),
1109 remove_filename_from_codes(RestCodes,Filename,NewCodes)
1110 ; AlreadyKnown==pos_already_known ->
1111 Pos = none,
1112 remove_filename_from_codes(Codes,_Filename2,NewCodes)
1113 ;
1114 NewCodes=Codes, Pos=none
1115 % do not remove filename: we have not found a position and cannot return the filename
1116 % see, e.g., public_examples/B/Tickets/Hansen27_NestedMchErr/M1.mch
1117 ),
1118 !,
1119 atom_codes(NewMsg,NewCodes).
1120 remove_msg_posinfo(M,M,none,_).
1121
1122
1123 % ----------------------
1124
1125 parser_command_supported(_) :-
1126 prob2_call_back_available(_), % TODO: support at least setoption in parser callback in ProB2
1127 !,
1128 fail.
1129 parser_command_supported(Command) :-
1130 parser_version_at_least(2,12,2),
1131 !,
1132 query_command_supported(Command,true).
1133 parser_command_supported(Command) :-
1134 (Command = fastprolog
1135 ; Command = compactpos
1136 ; Command = verbose
1137 ; Command = checkname
1138 ; Command = lineno
1139 ),
1140 !,
1141 parser_version_at_least(2,12,0).
1142
1143 :- dynamic query_command_supported_cached/2.
1144
1145 query_command_supported(Command,Res) :-
1146 query_command_supported_cached(Command,Res),
1147 !.
1148 query_command_supported(Command,Res) :-
1149 get_console_parser(Stream,_Out,Err),
1150 !,
1151 write(Stream,'commandsupported'), nl(Stream),
1152 write(Stream,Command), nl(Stream),
1153 flush_output(Stream),
1154 read_line(Stream,CodesIn),
1155 my_read_from_codes(CodesIn,Res,Stream,Err),
1156 assertz(query_command_supported_cached(Command,Res)).
1157
1158 % java_parser_version(VersionStr) :- java_parser_version(VersionStr,_,_,_,_,_).
1159
1160 parser_version_at_least(RV1,RV2,RV3) :-
1161 get_parser_version(_,V1,V2,V3,_,_),
1162 lex_leq([RV1,RV2,RV3],[V1,V2,V3]).
1163
1164 lex_leq([V1|_],[VV1|_]) :- V1 < VV1,!.
1165 lex_leq([V1|T1],[V1|T2]) :- lex_leq(T1,T2).
1166 lex_leq([],_).
1167
1168
1169 get_parser_version(VersionStr) :- get_parser_version(VersionStr,_).
1170 get_parser_version(VersionStr,GitSha) :- get_parser_version(VersionStr,_,_,_,_,GitSha).
1171
1172 get_parser_version(VersionStr,V1,V2,V3,SNAPSHOT,GitSha) :- java_parser_version(VersionStr,V1,V2,V3,SNAPSHOT,GitSha),!.
1173 get_parser_version(VersionStr,V1,V2,V3,SNAPSHOT,GitSha) :-
1174 get_version_from_parser(VersionStr),
1175 !,
1176 ? (get_version_numbers(VersionStr,V1,V2,V3,SNAPSHOT,GitSha)
1177 -> assertz(java_parser_version(VersionStr,V1,V2,V3,SNAPSHOT,GitSha)),
1178 debug_format(19,'Parser version recognized ~w.~w.~w (SNAPSHOT=~w)~n',[V1,V2,V3,SNAPSHOT])
1179 ; assertz(java_parser_version(VersionStr,'?','?','?','?','?'))
1180 ).
1181 get_parser_version(_Vers,_V1,_V2,_V3,_SNAPSHOT,_GitSha) :-
1182 (real_error_occurred -> true % already produced error messages in get_version_from_parser
1183 ; add_error(get_parser_version,'Could not get parser version.')),
1184 %missing_parser_diagnostics, % already called if necessary by get_version_from_parser
1185 fail.
1186
1187 :- use_module(probsrc(tools),[split_atom/3,atom_to_number/2]).
1188 % newer parsers generate a version string like 2.9.27-GITSHA or 2.9.27-SNAPSHOT-GITSHA; older ones just GITSHA
1189 get_version_numbers(VersionStr,V1,V2,V3,SNAPSHOT,GitSha) :-
1190 split_atom(VersionStr,['.'],List),
1191 [AV1,AV2,AV3T] = List,
1192 split_atom(AV3T,['-'],List2),
1193 [AV3,Suffix1|T] = List2,
1194 ? atom_to_number(AV1,V1),
1195 ? atom_to_number(AV2,V2),
1196 ? atom_to_number(AV3,V3),
1197 (Suffix1 = 'SNAPSHOT' -> SNAPSHOT=true, [GitSha]=T
1198 ; T=[], SNAPSHOT=false, GitSha=Suffix1).
1199
1200
1201 get_version_from_parser(Version) :-
1202 get_console_parser(Stream,Out,Err),
1203 write(Stream,'version'), nl(Stream),
1204 flush_output(Stream),
1205 read_line(Stream,Text), % if parser cannot be started we receive end_of_file; length will fail
1206 (Text = end_of_file -> add_error(parsercall,'Could not get parser version: '),
1207 read_lines_and_add_as_error(Err),fail
1208 ; length(Text,Length),
1209 ( append("Error",_,Text)
1210 -> atom_codes(ErrMsg,Text),
1211 add_error(parsercall,'Error getting parser version: ',ErrMsg),
1212 read_lines_and_add_as_error(Stream),fail
1213 ; Length < 100 -> atom_codes(Version,Text)
1214 ; prefix_length(Text,Prefix,100),
1215 append(Prefix,"...",Full),
1216 safe_name(Msg,Full),
1217 add_error_fail(parsercall, 'B parser returned unexpected version string: ', Msg))
1218 ),
1219 display_pending_outputs(Out,user_output).
1220
1221 :- use_module(library(sockets),[socket_select/7]).
1222 % check if a stream is ready for reading
1223 stream_ready_to_read(Out) :- stream_ready_to_read(Out,1). % wait 1 second, off: wait forever
1224 stream_ready_to_read(Out,TO) :- var(Out),!,
1225 add_internal_error('Illegal stream: ',stream_ready_to_read(Out,TO)),fail.
1226 stream_ready_to_read(Out,TO) :-
1227 socket_select([],_, [Out],RReady, [],_, TO), % wait TO sec at most %print(ready(Out,RReady)),nl,
1228 RReady == [Out].
1229
1230
1231 read_lines_and_add_as_error(Out) :- read_lines_until_eof(Out,false,ErrMsgCodes,[]),
1232 (ErrMsgCodes=[] -> true
1233 ; ErrMsgCodes=[10] -> true % just a single newline
1234 ; ErrMsgCodes=[13] -> true % ditto
1235 ; safe_name(ErrMsg,ErrMsgCodes),
1236 add_error(parsercall,'Additional information: ',ErrMsg),
1237 analyse_java_error_msg(ErrMsgCodes)
1238 ).
1239
1240 % analyse error messages from the JVM; providing possible solutions:
1241 analyse_java_error_msg(ErrMsgCodes) :-
1242 append([_,"java.lang.OutOfMemoryError",_],ErrMsgCodes),!,
1243 % a typical message contains: Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
1244 % or: Exception in ...: Java heap space
1245 print_hint('Try increasing memory for Java by setting the JVM_PARSER_HEAP_MB preference.'),
1246 print_hint('You can also provide custom JVM arguments using the JVM_PARSER_ARGS preference.').
1247 analyse_java_error_msg(ErrMsgCodes) :-
1248 append([_,"java.lang.StackOverflowError",_],ErrMsgCodes),!,
1249 print_hint('Try increasing stack size for Java using the JVM_PARSER_ARGS preference (e.g.,"-Xss10m").').
1250 analyse_java_error_msg(_).
1251
1252 print_hint(Msg) :- format_with_colour_nl(user_error,[blue],'~w',[Msg]).
1253
1254 read_line_if_ready(Stream,Line) :- stream_ready_to_read(Stream), !, read_line(Stream,Line).
1255 read_line_if_ready(_,end_of_file). % pretend we are at the eof
1256
1257 read_lines_until_eof(Out,NewlineRequired) --> {read_line_if_ready(Out,Text)},
1258 !,
1259 ({Text = end_of_file} -> []
1260 ; ({NewlineRequired==true} -> "\n" ; []),
1261 Text, %{format("read: ~s~n",[Text])},
1262 read_lines_until_eof(Out,true)).
1263 read_lines_until_eof(_Out,_) --> [].
1264
1265 % display pending output lines from a stream
1266 display_pending_outputs(OutStream,ToStream) :- stream_ready_to_read(OutStream,0), !, read_line(OutStream,Line),
1267 (Line=end_of_file -> true
1268 ; format(ToStream,' =parser-output=> ~s~n',[Line]),
1269 display_pending_outputs(OutStream,ToStream)).
1270 display_pending_outputs(_,_).
1271
1272 :- use_module(tools,[get_tail_filename/2]).
1273 get_java_command_for_parser(native(GraalNativeCmd)) :-
1274 (parser_location(GraalNativeCmd)
1275 -> get_tail_filename(GraalNativeCmd,'cliparser'),
1276 (file_exists(GraalNativeCmd) -> true
1277 ; add_warning(parsercall,'Path to parser points to non-existant compiled GraalVM binary: ',GraalNativeCmd)
1278 )
1279 ; fail, % comment out fail to enable auto-detection of cliparser in lib folder
1280 library_abs_name('cliparser',GraalNativeCmd),
1281 file_exists(GraalNativeCmd)
1282 ),
1283 format('Using GRAAL native parser at ~w~n',[GraalNativeCmd]),
1284 !.
1285 get_java_command_for_parser(java_jar(JavaCmdW,JarPathW)) :-
1286 (parser_location(JarPath) -> true ; library_abs_name('probcliparser.jar',JarPath)),
1287 get_java_command_path(JavaCmdW),
1288 replace_windows_path_backslashes(JarPath,JarPathW).
1289
1290 parser_location(PathToParser) :- get_preference(path_to_java_parser,PathToParser), PathToParser \= ''.
1291
1292 % a predicate to check whether we have correct java version number and whether java seems to work ok
1293 % ResultStatus=compatible if everything is ok
1294 check_java_version(VersionCheckResult,ResultStatus) :- get_java_fullversion(Version),!,
1295 check_java_version_aux(Version,VersionCheckResult,ResultStatus).
1296 check_java_version(VersionCheckResult,error) :- get_java_command_for_parser(java_jar(JavaCmdW,JarPathW)),!,
1297 ajoin(['*** Unable to launch java command: ',JavaCmdW,' (classpath: ',JarPathW,')!'],VersionCheckResult).
1298 check_java_version('*** Unable to launch Java or get path to java command!',error). % should not happen
1299
1300 check_java_version_aux(VersionCodes,VersionCheckResult,ResultStatus) :-
1301 get_java_fullversion_numbers(VersionCodes,V1,V2,V3Atom),!,
1302 check_java_version_aux2(V1,V2,V3Atom,VersionCheckResult,ResultStatus).
1303 check_java_version_aux(VersionCodes,VersionCheckResult,error) :-
1304 atom_codes(VersionA,VersionCodes),
1305 ajoin(['*** Unable to identify Java version number: ',VersionA, ' !'],VersionCheckResult).
1306
1307 check_java_version_aux2(V1,V2,V3Atom,VersionCheckResult,compatible) :- java_version_ok_for_parser(V1,V2,V3Atom),
1308 get_java_version(_),!, % if get_java_version fails, this is an indication that Java not fully installed with admin rights
1309 ajoin(['Java is correctly installed and version ',V1,'.',V2,'.',V3Atom,
1310 ' is compatible with ProB requirements (>= 1.7).'],VersionCheckResult).
1311 check_java_version_aux2(V1,V2,V3Atom,VersionCheckResult,error) :- java_version_ok_for_parser(V1,V2,V3Atom),!,
1312 ajoin(['*** Java version ',V1,'.',V2,'.',V3Atom,
1313 ' is compatible with ProB requirements (>= 1.7) ',
1314 'but does not seem to be correctly installed: reinstall Java with admin rights!'],VersionCheckResult).
1315 check_java_version_aux2(V1,V2,V3Atom,VersionCheckResult,incompatible) :-
1316 get_java_version(_),!,
1317 ajoin(['*** Java is correctly installed but version ',V1,'.',V2,'.',V3Atom,
1318 ' is *not* compatible with ProB requirements (>= 1.7)!'],VersionCheckResult).
1319 check_java_version_aux2(V1,V2,V3Atom,VersionCheckResult,error) :-
1320 ajoin(['*** Java is not correctly installed and version ',V1,'.',V2,'.',V3Atom,
1321 ' is *not* compatible with ProB requirements (>= 1.7)!'],VersionCheckResult).
1322
1323
1324 % we need Java 1.7 or higher
1325 java_version_ok_for_parser(V1,_V2,_) :- V1>1.
1326 java_version_ok_for_parser(1,V2,_) :- V2>=7.
1327
1328 get_java_fullversion(V1,V2,V3Atom) :-
1329 get_java_fullversion(VersionCodes),
1330 get_java_fullversion_numbers(VersionCodes,VV1,VV2,VV3),!,
1331 V1=VV1, V2=VV2, V3Atom=VV3.
1332
1333 :- assert_must_succeed((parsercall:get_java_fullversion_numbers("openjdk full version \"1.8.0_312\"",V1,V2,V3),
1334 V1==1, V2==8, V3=='0_312')).
1335 :- assert_must_succeed((parsercall:get_java_fullversion_numbers("openjdk full version \"15+36-1562\"",V1,V2,V3),
1336 V1==1, V2==15, V3=='36-1562')).
1337 :- assert_must_succeed((parsercall:get_java_fullversion_numbers("java full version \"17.0.1+12-LTS-39\"",V1,V2,V3),
1338 V1==1, V2==17, V3=='0.1+12-LTS-39')). % Oracle JDK on macOS
1339 :- use_module(tools,[split_chars/3]).
1340 get_java_fullversion_numbers(VersionCodes,V1,V2,V3Atom) :-
1341 append("java full version """,Tail,VersionCodes), !,
1342 get_java_fullversion_numbers_aux(Tail,V1,V2,V3Atom).
1343 get_java_fullversion_numbers(VersionCodes,V1,V2,V3Atom) :-
1344 append("openjdk full version """,Tail,VersionCodes), !,
1345 get_java_fullversion_numbers_aux(Tail,V1,V2,V3Atom).
1346 get_java_fullversion_numbers_aux(Tail,1,V2,V3Atom) :-
1347 % new version scheme (https://openjdk.java.net/jeps/223), leading 1 dropped, 13+33 stands for 1.13.33
1348 split_chars(Tail,"+",[V2C,V3C]),
1349 !,
1350 (try_number_codes(V2,V2C)
1351 -> get_v3_atom(V3C,V3Atom)
1352 ; append(V2CC,[0'. | FurtherC], V2C) % there is at least one more dot in the version nr (e.g. 17.0.1+12-LTS-39)
1353 ->
1354 try_number_codes(V2,V2CC),
1355 append(FurtherC,[0'+ | V3C], V3C3),
1356 get_v3_atom(V3C3,V3Atom)
1357 ).
1358 get_java_fullversion_numbers_aux(Tail,V1,V2,V3Atom) :-
1359 split_chars(Tail,".",[V1C,V2C|FurtherC]),
1360 append(FurtherC,V3C),
1361 try_number_codes(V1,V1C),
1362 try_number_codes(V2,V2C),!,
1363 get_v3_atom(V3C,V3Atom).
1364
1365 get_v3_atom(V3C,V3Atom) :- strip_closing_quote(V3C,V3C2),
1366 atom_codes(V3Atom,V3C2).
1367
1368 try_number_codes(Name,Codes) :-
1369 catch(number_codes(Name,Codes), _Exc, fail).
1370
1371 strip_closing_quote([],[]).
1372 strip_closing_quote([H|_],R) :- (H=34 ; H=32),!, R=[]. % closing " or newline
1373 strip_closing_quote([H|T],[H|R]) :- strip_closing_quote(T,R).
1374
1375 % get java fullversion as code list; format "java full version "...."\n"
1376 get_java_fullversion(Version) :-
1377 get_java_command_path(JavaCmdW),
1378 (my_system_call(JavaCmdW, ['-fullversion'],java_version,Text) -> Text=Version
1379 ; format(user_error,'Could not execute ~w -fullversion~n',[JavaCmdW]),
1380 fail).
1381
1382 % when java -fullversion works but not java -version it seems that Java was improperly
1383 % installed without admin rights (http://stackoverflow.com/questions/11808829/jre-1-7-returns-java-lang-noclassdeffounderror-java-lang-object)
1384 get_java_version(Version) :-
1385 get_java_command_path(JavaCmdW),
1386 (my_system_call(JavaCmdW, ['-version'],java_version,Text) -> Text=Version
1387 ; format(user_error,'Could not execute ~w -version~n',[JavaCmdW]),
1388 fail).
1389 % should we check for JAVA_PATH first ? would allow user to override java; but default pref would have to be set to ''
1390 get_java_command_path(JavaCmdW) :-
1391 %host_platform(darwin)
1392 get_preference(path_to_java,X),
1393 debug_println(8,path_to_java_preference(X)),
1394 (X=''
1395 ; preference_default_value(path_to_java,Default), debug_println(8,default(Default)),
1396 Default=X
1397 ),
1398 % only try this when the Java path has not been set explicitly
1399 % on Mac: /usr/bin/java exists even when Java not installed ! it is a fake java command that launches a dialog
1400 ? absolute_file_name(path(java),
1401 JavaCmd,
1402 [access(exist),extensions(['.exe','']),solutions(all),file_errors(fail)]),
1403 debug_println(8,obtained_java_cmd_from_path(JavaCmd)),
1404 replace_windows_path_backslashes(JavaCmd,JavaCmdW),
1405 !.
1406 get_java_command_path(JavaCmdW) :- get_preference(path_to_java,JavaCmd),
1407 JavaCmd \= '',
1408 debug_println(8,using_path_to_java_preference(JavaCmd)),
1409 (file_exists(JavaCmd) -> true
1410 ; directory_exists(JavaCmd) -> add_warning(get_java_command_path,'No Java Runtime (7 or newer) found; please correct the JAVA_PATH advanced preference (it has to point to the java tool, *not* the directory enclosing it): ',JavaCmd)
1411 ; add_warning(get_java_command_path,'No Java Runtime (7 or newer) found; please correct the JAVA_PATH advanced preference: ',JavaCmd)),
1412 replace_windows_path_backslashes(JavaCmd,JavaCmdW),
1413 !.
1414 get_java_command_path(_JavaCmd) :-
1415 add_error(get_java_command_path,'Could not get path to the java tool. Make sure a Java Runtime (7 or newer) is installed',''),
1416 fail.
1417
1418 library_abs_name(Lib,Abs) :- absolute_file_name(prob_lib(Lib),Abs).
1419
1420
1421 call_ltl_parser(Formulas, CtlOrLtl, Result) :-
1422 get_ltl_lang_spec(LangSpec),
1423 maplist(parse_temporal_formula(CtlOrLtl,LangSpec),Formulas,Result).
1424
1425 :- use_module(specfile,[b_or_z_mode/0,csp_with_bz_mode/0]).
1426 get_ltl_lang_spec('B,none') :- csp_with_bz_mode,!.
1427 get_ltl_lang_spec('B') :- b_or_z_mode,!.
1428 get_ltl_lang_spec(none).
1429
1430 /* unused code :
1431 generate_formula_codes([F]) -->
1432 !,generate_formula_codes2(F),"\n".
1433 generate_formula_codes([F|Rest]) -->
1434 generate_formula_codes2(F), "###\n",
1435 generate_formula_codes(Rest).
1436 generate_formula_codes2(F) -->
1437 write_to_codes(F).
1438 */
1439
1440
1441 % ---------------------------
1442
1443 % call the TLA2B parser to translate a TLA file into a B machine
1444 call_tla2b_parser(TLAFile) :-
1445 get_java_command_path(JavaCmdW),
1446 replace_windows_path_backslashes(TLAFile,TLAFileW),
1447 phrase(jvm_options, XOpts),
1448 absolute_file_name(prob_lib('TLA2B.jar'),TLA2BJAR),
1449 append(XOpts,['-jar',file(TLA2BJAR),file(TLAFileW)],FullArgs),
1450 % other possible options -verbose -version -config FILE
1451 (my_system_call(JavaCmdW, FullArgs,tla2b_parser)
1452 -> true
1453 ; \+ file_exists(TLA2BJAR),
1454 print_error('Be sure to download TLA2B.jar from https://stups.hhu-hosting.de/downloads/prob/tcltk/jars/,'),
1455 print_error('and put it into the ProB lib/ directory.'),
1456 fail
1457 ).
1458
1459 %call_cspmj_parser(CSPFile,PrologFile) :-
1460 % get_java_command_path(JavaCmdW),
1461 % replace_windows_path_backslashes(CSPFile,CSPFileW),
1462 % phrase(jvm_options, XOpts),
1463 % absolute_file_name(prob_lib('cspmj.jar'),CSPMJAR),
1464 % get_writable_compiled_filename(CSPFile,'.pl',PrologFile),
1465 % tools:string_concatenate('--prologOut=', PrologFile, PrologFileOutArg),
1466 % append(XOpts,['-jar',CSPMJAR,'-parse',CSPFileW,PrologFileOutArg],FullArgs),
1467 % debug_println(9,calling_java(FullArgs)),
1468 % (my_system_call(JavaCmdW, FullArgs,cspmj_parser) -> true
1469 % ; print_error('Be sure that cspmj.jar parser is installed.'),
1470 % fail).
1471
1472
1473 tla2prob_filename(TLAFile,GeneratedProbFile) :-
1474 split_filename(TLAFile,Basename,_Extension),
1475 atom_chars(Basename,BasenameC),
1476 append(BasenameC,['.','p','r','o','b'],TLABC),
1477 atom_chars(GeneratedProbFile,TLABC),!.
1478 tla2prob_filename(Filename, GeneratedProbFile) :-
1479 add_failed_call_error(tla2b_filename(Filename,GeneratedProbFile)),fail.
1480
1481 tla2b_filename(TLAFile,BFile) :-
1482 split_filename(TLAFile,Basename,_Extension),
1483 atom_chars(Basename,BasenameC),
1484 append(BasenameC,['_',t,l,a,'.','m','c','h'],TLABC),
1485 atom_chars(BFile,TLABC),!.
1486 tla2b_filename(Filename, BFile) :-
1487 add_failed_call_error(tla2b_filename(Filename,BFile)),fail.
1488
1489
1490 my_system_call(Command,Args,Origin) :- my_system_call(Command,Args,Origin,_Text).
1491 my_system_call(Command,Args,Origin,ErrText) :-
1492 debug_print_system_call(Command,Args),
1493 system_call(Command,Args,ErrText,Exit), %nl,print(exit(Exit)),nl,nl,
1494 treat_exit_code(Exit,Command,Args,ErrText,Origin).
1495
1496 debug_print_system_call(Command,Args) :-
1497 (debug_mode(off) -> true ; print_system_call(Command,Args)).
1498 print_system_call(Command,Args) :-
1499 ajoin_with_sep(Args,' ',FS), format(user_output,'Executing: ~w ~w~n',[Command,FS]).
1500
1501 my_system_call5(Command,Args,Origin,OutputText,ErrText) :-
1502 system_call(Command,Args,OutputText,ErrText,Exit), %nl,print(exit(Exit)),nl,nl,
1503 treat_exit_code(Exit,Command,Args,ErrText,Origin).
1504
1505 treat_exit_code(exit(0),_,_,_,_) :- !.
1506 treat_exit_code(Exit,Command,Args,ErrText,Origin) :-
1507 debug_println(9,treat_exit_code(Exit,Command,Args,ErrText,Origin)),
1508 catch( my_read_from_codes(ErrText,Term),
1509 _,
1510 (safe_name(T,ErrText),Term = T)), !,
1511 %print_error(Term),
1512 (Term = end_of_file -> ErrMsg = '' ; ErrMsg = Term),
1513 ajoin(['Exit code ',Exit,' for command ',Command, ', error message: '],Msg),
1514 (get_error_position_from_term(Origin,Term,Pos)
1515 -> add_error(Origin,Msg,ErrMsg,Pos)
1516 ; add_error(Origin,Msg,ErrMsg)
1517 ),
1518 fail.
1519
1520
1521 get_error_position_from_term(alloy2b,Term,Pos) :- atom(Term),
1522 atom_codes(Term,Codes), get_error_position_from_codes(Codes,Pos).
1523
1524 % Alloy error message: ! Exception in thread "main" Type error in .../Restrictions.als at line 41 column 23:
1525 get_error_position_from_codes([97,116,32,108,105,110,101,32|Tail],lineCol(Line,Col)) :- % "at line "
1526 epi_number(Line,Tail,RestTail),
1527 (RestTail = [32,99,111,108,117,109,110,32|Tail2], % "column "
1528 epi_number(Col,Tail2,_) -> true
1529 ; Col=0).
1530 get_error_position_from_codes([_|T],Pos) :- get_error_position_from_codes(T,Pos).
1531
1532 % replace backslashes by forward slashes in atoms, only under windows
1533 % TODO(DP,14.8.2008): Check if still needed with SICStus 4.0.4 and if
1534 % everybody uses that version
1535 replace_windows_path_backslashes(Old,New) :-
1536 ( host_platform(windows) ->
1537 safe_name(Old,OldStr),
1538 replace_string_backslashes(OldStr,NewStr),
1539 safe_name(New,NewStr)
1540 ;
1541 Old=New).
1542
1543 replace_string_backslashes([],[]) :- !.
1544 replace_string_backslashes([C|OldRest],[N|NewRest]) :- !,
1545 ( C=92 /* backslash */ ->
1546 N=47 /* forward slash */
1547 ;
1548 N=C ),
1549 replace_string_backslashes(OldRest,NewRest).
1550 replace_string_backslashes(X,Y) :-
1551 add_error(parsercall,'Illegal call: ',replace_string_backslashes(X,Y)),
1552 fail.
1553
1554 % ---------------------------
1555
1556 :- assert_must_succeed((parsercall:parse_string_template("0",Res), Res==[string_codes("0")])).
1557 :- assert_must_succeed((parsercall:parse_string_template("ab${xy}cd",Res),
1558 Res==[string_codes("ab"),template_codes("xy",2,3,[]),string_codes("cd")])).
1559 :- assert_must_succeed((parsercall:parse_string_template("ab${xy}$<<z>>cd$\xab\v\xbb\",Res),
1560 Res==[string_codes("ab"),template_codes("xy",2,3,[]),template_codes("z",3,5,[]),
1561 string_codes("cd"),template_codes("v",2,3,[])])). % \xab\ is double angle
1562
1563 % parse a ProB string template
1564 % each recognised template is an expression.
1565 parse_string_template(Codes,TemplateList) :- %format("parsing: ~s~n",[Codes]),
1566 parse_string_template([],TemplateList,Codes,[]).
1567
1568 parse_string_template(Acc,TemplateList) -->
1569 ? template_inside_string(Str,StartOffset,AdditionalChars,Options),!,
1570 {add_acc_string(Acc,TemplateList,
1571 [template_codes(Str,StartOffset,AdditionalChars,Options)|T])},
1572 parse_string_template([],T).
1573 parse_string_template(Acc,TemplateList) --> [H], !, parse_string_template([H|Acc],TemplateList).
1574 parse_string_template(Acc,TemplateList) --> "", {add_acc_string(Acc,TemplateList,[])}.
1575
1576 % add accumulator as regular string before a detected template ${...}
1577 add_acc_string([],L,R) :- !, L=R.
1578 add_acc_string(Acc,[string_codes(Str)|T],T) :- reverse(Acc,Str).
1579
1580 template_inside_string(Str,StartOffset,AdditionalChars,Options) --> "$",
1581 template_options(Options,OptLen),
1582 ? template_open_paren(ClosingParen,ParenLen),
1583 {StartOffset is OptLen+ParenLen+1,
1584 AdditionalChars is StartOffset+ParenLen}, % add closing parentheses length
1585 template_content(Str,ClosingParen).
1586
1587 template_options(Options,Len) --> "[", template_options2(Options,Len).
1588 template_options([],0) --> [].
1589
1590 template_options2([H|T],Len) --> template_option(H,HLen), !, template_options3(T,HLen,Len).
1591 template_options2([],2) --> "]".
1592 template_options3([H|T],AccLen,Len) --> ",", template_option(H,HLen), !,
1593 {A1 is AccLen+HLen},template_options3(T,A1,Len).
1594 template_options3([],AccLen,Len) --> "]", {Len is AccLen+2}.
1595 template_option(ascii,5) --> "ascii".
1596 template_option(ascii,1) --> "a".
1597 template_option(unicode,7) --> "unicode".
1598 template_option(unicode,1) --> "u".
1599 template_option(template_width(Option,Nr),L1) --> digits(Nr,Len),template_width_option(Option,LO), {L1 is Len+LO}.
1600 % other options could be hex output or padding, ...
1601 % TODO: provide error feedback when option/template parsing fails
1602
1603 template_width_option(float_fixed_point,1) --> "f".
1604 template_width_option(integer_decimal_string,1) --> "d".
1605 template_width_option(pad_string(' '),1) --> "p".
1606
1607 digits(MinusNr,Len) --> "-",!, digit(X), digits2(X,Nr,2,Len), {MinusNr is -Nr}.
1608 digits(Nr,Len) --> digit(X), digits2(X,Nr,1,Len).
1609 digits2(X,Nr,AccLen,Len) --> digit(Y), !, {XY is X*10+Y, A1 is AccLen+1}, digits2(XY,Nr,A1,Len).
1610 digits2(X,X,Len,Len) --> "".
1611
1612 digit(Nr) --> [X], {X >= 48, X=<57, Nr is X-48}.
1613
1614 template_open_paren("}",1) --> "{".
1615 template_open_paren(">>",2) --> "<<".
1616 template_open_paren([187],1) --> [ 171 ]. % Double angle quotation mark "«»"
1617
1618 template_content("",Closing) --> Closing,!.
1619 template_content([H|T],Closing) --> [H], template_content(T,Closing).
1620
1621 % TODO: offset span within template
1622 parse_template_b_expression(Span,Filenumber,template_codes(Codes,_,_,Options),RawStrExpr) :-
1623 \+ get_prob_application_type(rodin), % in Rodin parser callback is not available
1624 !,
1625 catch( parse_at_position_in_file(expression,Codes,RawExpr,Span,Filenumber),
1626 parse_errors(Errors),
1627 (add_all_perrors(Errors,[],parse_template_string), % add_all_perrors_in_context_of_used_filenames?
1628 fail)
1629 ),
1630 create_to_string_conversion(RawExpr,Span,Options,RawStrExpr).
1631 parse_template_b_expression(Span,_,template_codes(Codes,_,_,_),string(Span,Atom)) :-
1632 atom_codes(Atom,Codes).
1633 parse_template_b_expression(Span,_,string_codes(Codes),string(Span,Atom)) :-
1634 atom_codes(Atom,Codes).
1635
1636 :- assert_must_succeed((parsercall:transform_string_template("1+1 = ${1+1}",unknown,Res),
1637 Res= _ )).
1638
1639 :- use_module(error_manager,[extract_file_number_and_name/3]).
1640 % transform a string template into a raw expression that computes a string value
1641 transform_string_template(Codes,Span,RawExpr) :-
1642 parse_string_template(Codes,TemplateList),
1643 TemplateList = [EL1|TL],
1644 (EL1=template_codes(_,_,_,_) -> true ; TL = [_|_]), % at least one template string detected
1645 (extract_file_number_and_name(Span,FileNr,_) -> true ; FileNr = -1),
1646 InitialOffset = 3, % for three starting quotes
1647 ? l_parse_templates(TemplateList,0,InitialOffset,Span,FileNr,BTemplateList),
1648 generate_conc_expr(BTemplateList,Span,RawExpr).
1649
1650 :- use_module(tools_positions, [add_col_offset_to_position/3, add_line_col_offset_to_position/4]).
1651 l_parse_templates([],_,_,_,_,[]).
1652 l_parse_templates([Templ1|T],LineOffset,ColOffset,Span,FileNr,[RawStrExpr|BT]) :-
1653 templ_start_offset(Templ1,StartOffset),
1654 CO2 is ColOffset+StartOffset, % e.g., add two chars for ${ in case Templ1 is a template
1655 add_line_col_offset_to_position(Span,LineOffset,CO2,Span1),
1656 % it is only start position of Span1 that is important
1657 parse_template_b_expression(Span1,FileNr,Templ1,RawStrExpr),
1658 ? count_template_offset(Templ1,LineOffset,ColOffset,LineOffset2,ColOffset2),
1659 ? l_parse_templates(T,LineOffset2,ColOffset2,Span,FileNr,BT).
1660
1661 % accumulate length of template part and add to line/col offset to be added to start of template string
1662 count_template_offset(template_codes(Codes,_StartOffset,AdditionalChars,_),L,C,LineOffset,C3) :-
1663 ? count_offset(Codes,L,C,LineOffset,ColOffset),
1664 C3 is ColOffset+AdditionalChars. % e.g., 3 for ${.}
1665 count_template_offset(string_codes(Codes),L,C,LineOffset,ColOffset) :-
1666 ? count_offset(Codes,L,C,LineOffset,ColOffset).
1667
1668 templ_start_offset(template_codes(_,StartOffset,_,_),StartOffset). % usually 2 for ${
1669 templ_start_offset(string_codes(_),0).
1670
1671 % count line/column offset inside a list of codes:
1672 %count_offset(Codes,LineOffset,ColOffset) :- count_offset(Codes,0,0,LineOffset,ColOffset).
1673 count_offset([],Line,Col,Line,Col).
1674 count_offset(Codes,L,_ColOffset,Line,Col) :- newline(Codes,T),!,
1675 L1 is L+1,
1676 count_offset(T,L1,0,Line,Col).
1677 count_offset([_|T],L,ColOffset,Line,Col) :- C1 is ColOffset+1,
1678 ? count_offset(T,L,C1,Line,Col).
1679
1680 newline([10|T],T).
1681 newline([13|X],T) :- (X=[10|TX] -> T=TX ; T=X). % TO DO: should we check if we are on Windows ?
1682
1683 % generate a raw expression for the concatenation of a list of expressions
1684 generate_conc_expr([],Pos,string(Pos,'')).
1685 generate_conc_expr([OneEl],_,Res) :- !, Res = OneEl. % no need to concatenate
1686 generate_conc_expr(List,Pos,general_concat(Pos,sequence_extension(Pos,List))).
1687
1688 create_to_string_conversion(RawExpr,Pos,Options,
1689 external_function_call_auto(Pos,'STRING_PADLEFT',[RawStrExpr,Len,Char])) :-
1690 select(template_width(pad_string(PadChar),Nr),Options,RestOpts),!,
1691 create_to_string_conversion(RawExpr,Pos,RestOpts,RawStrExpr),
1692 Char = string(Pos,PadChar),
1693 Len = integer(Pos,Nr).
1694 create_to_string_conversion(RawExpr,Pos,Options,RawStrExpr) :-
1695 is_definitely_string(RawExpr),!,
1696 (Options=[] -> true
1697 ; add_warning(string_template,'Ignoring options in string template (already a string): ',Options, Pos)),
1698 RawStrExpr=RawExpr.
1699 create_to_string_conversion(RawExpr,Pos,Options,external_function_call_auto(Pos,'REAL_TO_DEC_STRING',[RawExpr,Prec])) :-
1700 select_option(template_width(float_fixed_point,Nr),Options,Pos),!,
1701 Prec = integer(Pos,Nr).
1702 create_to_string_conversion(RawExpr,Pos,Options,external_function_call_auto(Pos,'INT_TO_DEC_STRING',[RawExpr,Prec])) :-
1703 select_option(template_width(integer_decimal_string,Nr),Options,Pos),!,
1704 Prec = integer(Pos,Nr).
1705 create_to_string_conversion(RawExpr,Pos,Options,external_function_call_auto(Pos,'TO_STRING_UNICODE',[RawExpr])) :-
1706 select_option(unicode,Options,Pos),!. % TODO use a TO_STRING function with options list
1707 create_to_string_conversion(RawExpr,Pos,_Options,external_function_call_auto(Pos,'TO_STRING',[RawExpr])).
1708
1709 select_option(Option,Options,Pos) :-
1710 select(Option,Options,Rest),
1711 (Rest=[] -> true ; add_warning(string_template,'Ignoring additional string template options: ',Rest,Pos)).
1712
1713
1714 is_definitely_string(string(_,_)).
1715
1716 % ---------------------------
1717 :- use_module(pathes,[runtime_application_path/1]).
1718
1719 call_jar_parser(PMLFile,JARFile,Args,ParserName) :-
1720 get_java_command_path(JavaCmdW),
1721 replace_windows_path_backslashes(PMLFile,PMLFileW),
1722 phrase(jvm_options, XOpts),
1723 absolute_file_name(prob_lib(JARFile),FullJAR),
1724 append(XOpts,['-jar',FullJAR,PMLFileW|Args],FullArgs),
1725 %format('Java: ~w, Parser: ~w, Args: ~w~n',[JavaCmdW,ParserName,FullArgs]),
1726 my_system_call(JavaCmdW, FullArgs,ParserName).
1727
1728
1729
1730 call_alloy2pl_parser(AlloyFile,BFile) :-
1731 alloy_pl_filename(AlloyFile,BFile),
1732 call_alloy2pl_parser_aux(AlloyFile,'alloy2b.jar',BFile).
1733
1734 call_alloy2pl_parser_aux(AlloyFile,JARFile,PrologBFile) :-
1735 compilation_not_needed(AlloyFile,PrologBFile,JARFile),!.
1736 call_alloy2pl_parser_aux(AlloyFile,JARFile,PrologBFile) :-
1737 (file_exists(PrologBFile) -> delete_file(PrologBFile) ; true), % TODO: only delete generated files
1738 replace_windows_path_backslashes(PrologBFile,BFileW),
1739 call_jar_parser(AlloyFile,JARFile,['-toProlog',BFileW],alloy2b).
1740
1741 alloy_pl_filename(AlloyFile,BFile) :- atom_codes(AlloyFile,BasenameC),
1742 append(BasenameC,".pl",NewC),
1743 atom_codes(BFile,NewC),!.
1744 alloy_pl_filename(Filename, BFile) :-
1745 add_failed_call_error(alloy_pl_filename(Filename,BFile)),fail.
1746
1747 compilation_not_needed(BaseFile,DerivedFile,LibraryFile) :-
1748 file_exists(BaseFile),
1749 file_property(BaseFile, modify_timestamp, BTime),
1750 %system:datime(BTime,datime(_Year,Month,Day,Hour,Min,Sec)), format('~w modfied on ~w/~w at ~w:~w (~w sec)~n',[BaseFile,Day,Month,Hour,Min,Sec]),
1751 file_exists(DerivedFile),
1752 file_property(DerivedFile, modify_timestamp, DTime),
1753 DTime > BTime,
1754 absolute_file_name(prob_lib(LibraryFile),FullLibFile),
1755 (file_exists(FullLibFile)
1756 -> file_property(FullLibFile, modify_timestamp, LibTime),
1757 DTime > LibTime % check that file was generated by this library (hopefully); ideally we should put version numbers into the DerivedFiles
1758 ; true % Jar/command not available anyway; use the derived file
1759 ).
1760
1761 % ---------------------------
1762
1763 call_fuzz_parser(TexFile,FuzzFile) :-
1764 ? get_command_in_lib(fuzz,FuzzCmd),
1765 absolute_file_name(prob_lib('fuzzlib'),FUZZLIB), % TO DO: windows
1766 fuzz_filename(TexFile,FuzzFile),
1767 % fuzz options:
1768 % -d Allow use before definition
1769 % -l Lisp-style echoing of input
1770 % -p file Use <file> in place of standard prelude
1771 debug_format(19,'Fuzz: running ~w -d -l with library ~w on ~w~n',[FuzzCmd,FUZZLIB,TexFile]),
1772 (file_exists(FUZZLIB)
1773 -> my_system_call5(FuzzCmd,['-d', '-l', '-p', file(FUZZLIB), file(TexFile)],call_fuzz_parser,Text,_ErrText)
1774 ; my_system_call5(FuzzCmd,['-d', '-l', file(TexFile)],call_fuzz_parser,Text,_ErrText)
1775 ),
1776 format('Writing fuzz AST to ~s~n',[FuzzFile]),
1777 open(FuzzFile,write,Stream),
1778 call_cleanup(format(Stream,'~s~n',[Text]), close(Stream)).
1779
1780 get_command_in_lib(Name,Command) :-
1781 get_binary_extensions(Extensions),
1782 ? absolute_file_name(prob_lib(Name),
1783 Command,
1784 [access(exist),extensions(Extensions),solutions(all),file_errors(fail)]).
1785
1786 get_binary_extensions(List) :-
1787 host_platform(windows),!, List=['.exe','']. % will backtrack in get_command_in_lib
1788 get_binary_extensions(['']).
1789
1790 fuzz_filename(TexFile,FuzzFile) :-
1791 split_filename(TexFile,Basename,_Extension),
1792 atom_chars(Basename,BasenameC),
1793 append(BasenameC,['.','f','u','z','z'],CC),
1794 atom_chars(FuzzFile,CC),!.
1795 fuzz_filename(TexFile, FuzzFile) :-
1796 add_failed_call_error(fuzz_filename(TexFile,FuzzFile)),fail.