|
25 | 25 | :- use_module(library(lists)).
|
26 | 26 | :- use_module(library(between)).
|
27 | 27 | :- use_module(library(iso_ext), [partial_string/1,partial_string/3]).
|
| 28 | +:- use_module(library(charsio/memory_stream_utils)). |
28 | 29 |
|
29 | 30 | fabricate_var_name(VarType, VarName, N) :-
|
30 | 31 | char_code('A', AC),
|
|
424 | 425 | % ```
|
425 | 426 |
|
426 | 427 | chars_to_stream(Chars, Stream, StreamOpts) :-
|
427 |
| - parse_stream_options(StreamOpts, [Alias, EOFAction, Reposition, Type]), |
| 428 | + parse_stream_options_list(StreamOpts, [Alias, EOFAction, Reposition, Type]), |
428 | 429 | validate_chars(Chars, Type),
|
429 | 430 | '$memory_stream'(Stream),
|
430 | 431 | '$set_stream_options'(Stream, Alias ,EOFAction, Reposition, Type),
|
|
433 | 434 | ; maplist(put_char(Stream), Chars)
|
434 | 435 | ).
|
435 | 436 |
|
436 |
| -%% ugly ripoff of parser from library(builtins). |
437 |
| - |
438 |
| -:- meta_predicate parse_options_list(?, 2, ?, ?, ?). |
439 |
| - |
440 |
| -parse_options_list(Options, Selector, DefaultPairs, OptionValues, Stub) :- |
441 |
| - '$skip_max_list'(_, _, Options, Tail), |
442 |
| - ( Tail == [] -> |
443 |
| - true |
444 |
| - ; var(Tail) -> |
445 |
| - throw(error(instantiation_error, Stub)) % 8.11.5.3c) |
446 |
| - ; Tail \== [] -> |
447 |
| - throw(error(type_error(list, Options), Stub)) % 8.11.5.3e) |
448 |
| - ), |
449 |
| - ( lists:maplist('$call'(nonvar), Options), % need '$call' because |
450 |
| - % maplist isn't |
451 |
| - % declared as a |
452 |
| - % meta-predicate yet |
453 |
| - catch(lists:maplist(Selector, Options, OptionPairs0), |
454 |
| - error(E, _), |
455 |
| - builtins:throw(error(E, Stub))) -> |
456 |
| - lists:append(DefaultPairs, OptionPairs0, OptionPairs1), |
457 |
| - keysort(OptionPairs1, OptionPairs), |
458 |
| - select_rightmost_options(OptionPairs, OptionValues) |
459 |
| - ; |
460 |
| - throw(error(instantiation_error, Stub)) % 8.11.5.3c) |
461 |
| - ). |
462 |
| - |
463 |
| -parse_stream_options_(type(Type), type-Type) :- |
464 |
| - ( var(Type) -> |
465 |
| - throw(error(instantiation_error, open/4)) % 8.1.3 7) |
466 |
| - ; |
467 |
| - lists:member(Type, [text, binary]), ! |
468 |
| - ; |
469 |
| - throw(error(domain_error(stream_option, type(Type)), _)) |
470 |
| - ). |
471 |
| -parse_stream_options_(reposition(Bool), reposition-Bool) :- |
472 |
| - ( nonvar(Bool), lists:member(Bool, [true, false]), ! |
473 |
| - ; |
474 |
| - throw(error(domain_error(stream_option, reposition(Bool)), _)) |
475 |
| - ). |
476 |
| - |
477 |
| -parse_stream_options_(alias(A), alias-A) :- |
478 |
| - |
479 |
| - ( var(A) -> |
480 |
| - throw(error(instantiation_error, open/4)) % 8.1.3 7) |
481 |
| - ; |
482 |
| - atom(A), A \== [] -> true |
483 |
| - ; |
484 |
| - throw(error(domain_error(stream_option, alias(A)), _)) |
485 |
| - ). |
486 |
| -parse_stream_options_(eof_action(Action), eof_action-Action) :- |
487 |
| - ( nonvar(Action), lists:member(Action, [eof_code, error, reset]), ! |
488 |
| - ; |
489 |
| - throw(error(domain_error(stream_option, eof_action(Action)), _)) |
490 |
| - ). |
491 |
| -parse_stream_options_(E, _) :- |
492 |
| - throw(error(domain_error(stream_option, E), _)). % 8.11.5.3i) % |
493 |
| - |
494 |
| -parse_stream_options(Options, OptionValues) :- |
495 |
| - DefaultOptions = [alias-[], eof_action-eof_code, reposition-false, type-text], |
496 |
| - parse_options_list(Options, parse_stream_options_, DefaultOptions, OptionValues, chars_to_stream/3). |
497 |
| - |
498 |
| - |
499 |
| -select_rightmost_options([Option-Value | OptionPairs], OptionValues) :- |
500 |
| - ( pairs:same_key(Option, OptionPairs, OtherValues, _), |
501 |
| - OtherValues == [] -> |
502 |
| - OptionValues = [Value | OptionValues0], |
503 |
| - select_rightmost_options(OptionPairs, OptionValues0) |
504 |
| - ; |
505 |
| - select_rightmost_options(OptionPairs, OptionValues) |
506 |
| - ). |
507 |
| - |
508 |
| -select_rightmost_options([], []). |
509 |
| - |
510 |
| - |
511 |
| -valid_byte_(Byte) :- |
512 |
| - must_be(integer, Byte), |
513 |
| - Byte >= 0, |
514 |
| - Byte < 256. |
515 |
| - |
516 |
| -validate_chars_(Chars, binary) :- |
517 |
| - maplist(valid_byte, Chars). |
518 |
| - |
519 |
| -validate_chars_(Chars, text) :- |
520 |
| - must_be(chars, Chars). |
0 commit comments