ADDED: phrase_from_file/3 in library(pio)

This allows us to specify type(binary), and read from binary files
with DCGs.
This commit is contained in:
Markus Triska
2020-05-19 18:44:28 +02:00
parent 1c0c660c32
commit 0bd9831eec
3 changed files with 56 additions and 23 deletions

View File

@@ -1,12 +1,23 @@
:- module(pio, [phrase_from_file/2]).
:- module(pio, [phrase_from_file/2,
phrase_from_file/3]).
:- use_module(library(dcgs)).
:- use_module(library(error)).
phrase_from_file(NT, File) :-
( var(File) -> instantiation_error(phrase_from_file/2)
phrase_from_file(NT, File, []).
phrase_from_file(NT, File, Options) :-
( var(File) -> instantiation_error(phrase_from_file/3)
; (\+ atom(File) ; File = []) ->
domain_error(source_sink, File, phrase_from_file/2)
; '$file_to_chars'(File, Chars),
domain_error(source_sink, File, phrase_from_file/3)
; must_be(list, Options),
( member(Var, Options), var(Var) -> instantiation_error(phrase_from_file/3)
; member(type(Type), Options) ->
must_be(atom, Type),
member(Type, [text,binary])
; Type = text
),
'$file_to_chars'(File, Chars, Type),
phrase(NT, Chars)
).