1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (C): 2008-2016, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(pure_input, 37 [ phrase_from_file/2, % :Grammar, +File 38 phrase_from_file/3, % :Grammar, +File, +Options 39 phrase_from_stream/2, % :Grammar, +Stream 40 stream_to_lazy_list/2, % :Stream -List 41 42 syntax_error//1, % +ErrorTerm 43 % Low level interface 44 lazy_list_location//1, % -Location 45 lazy_list_character_count//1 % -CharacterCount 46 ]). 47:- use_module(library(error)). 48:- set_prolog_flag(generate_debug_info, false).
71:- predicate_options(phrase_from_file/3, 3,
72 [ pass_to(system:open/4, 4)
73 ]).
:- use_module(library(dcg/basics)). file_contains(File, Pattern) :- phrase_from_file(match(Pattern), File). match(Pattern) --> string(_), string(Pattern), remainder(_). match_count(File, Pattern, Count) :- aggregate_all(count, file_contains(File, Pattern), Count).
This can be called as (note that the pattern must be a string (code list)):
?- match_count('pure_input.pl', `file`, Count).
109:- meta_predicate 110 phrase_from_file( , ), 111 phrase_from_file( , , ), 112 phrase_from_stream( , ). 113 114phrase_from_file(Grammar, File) :- 115 phrase_from_file( , File, []).
122phrase_from_file(Grammar, File, Options) :-
123 setup_call_cleanup(
124 open(File, read, In, Options),
125 phrase_from_stream( , In),
126 close(In)).
133phrase_from_stream(Grammar, In) :-
134 stream_to_lazy_list(In, List),
135 phrase( , List).
145syntax_error(Error) -->
146 lazy_list_location(Location),
147 { throw(error(syntax_error(Error), Location))
148 }.
164lazy_list_location(Location, Here, Here) :- 165 lazy_list_location(Here, Location). 166 167lazy_list_location(Here, Location) :- 168 '$skip_list'(Skipped, Here, Tail), 169 ( attvar(Tail) 170 -> get_attr(Tail, pure_input, State), 171 State = lazy_input(Stream, PrevPos, Pos, _), 172 Details = [Line, LinePos, CharNo], 173 ( stream_property(Stream, file_name(File)) 174 -> PosParts = [file, File|Details] 175 ; PosParts = [stream, Stream|Details] 176 ), 177 Location =.. PosParts, 178 ( PrevPos == (-) % nothing is read. 179 -> Line = 1, LinePos = 0, CharNo = 0 180 ; stream_position_data(char_count, Pos, EndRecordCharNo), 181 CharNo is EndRecordCharNo - Skipped, 182 set_stream_position(Stream, PrevPos), 183 stream_position_data(char_count, PrevPos, StartRecordCharNo), 184 Skip is CharNo-StartRecordCharNo, 185 forall(between(1, Skip, _), get_code(Stream, _)), 186 stream_property(Stream, position(ErrorPos)), 187 stream_position_data(line_count, ErrorPos, Line), 188 stream_position_data(line_position, ErrorPos, LinePos) 189 ) 190 ; Tail == [] 191 -> Location = end_of_file-Skipped 192 ; type_error(lazy_list, Here) 193 ).
208lazy_list_character_count(Location, Here, Here) :- 209 lazy_list_character_count(Here, Location). 210 211lazy_list_character_count(Here, CharNo) :- 212 '$skip_list'(Skipped, Here, Tail), 213 ( attvar(Tail) 214 -> get_attr(Tail, pure_input, State), 215 arg(3, State, Pos), 216 stream_position_data(char_count, Pos, EndRecordCharNo), 217 CharNo is EndRecordCharNo - Skipped 218 ; Tail == [] 219 -> CharNo = end_of_file-Skipped 220 ; type_error(lazy_list, Here) 221 ).
237stream_to_lazy_list(Stream, List) :- 238 ( stream_property(Stream, buffer(false)) 239 -> permission_error(create, lazy_list, Stream) 240 ; true 241 ), 242 stream_to_lazy_list(Stream, -, List). 243 244stream_to_lazy_list(Stream, PrevPos, List) :- 245 stream_property(Stream, position(Pos)), 246 put_attr(List, pure_input, lazy_input(Stream, PrevPos, Pos, _)). 247 248attr_unify_hook(State, Value) :- 249 notrace(attr_unify_hook_ndebug(State, Value)). 250 251attr_unify_hook_ndebug(State, Value) :- 252 State = lazy_input(Stream, _PrevPos, Pos, Read), 253 ( var(Read) 254 -> fill_buffer(Stream), 255 read_pending_codes(Stream, NewList, Tail), 256 ( Tail == [] 257 -> nb_setarg(4, State, []), 258 Value = [] 259 ; stream_to_lazy_list(Stream, Pos, Tail), 260 nb_linkarg(4, State, NewList), 261 Value = NewList 262 ) 263 ; Value = Read 264 )
Pure Input from files and streams
This module is part of
pio.pl
, dealing with pure input: processing input streams from the outside world using pure predicates, notably grammar rules (DCG). Using pure predicates makes non-deterministic processing of input much simpler.Pure input uses attributed variables to read input from the external source into a list on demand. The overhead of lazy reading is more than compensated for by using block reads based on read_pending_codes/3.
Ulrich Neumerkel came up with the idea to use coroutining for creating a lazy list. His implementation repositioned the file to deal with re-reading that can be necessary on backtracking. The current implementation uses destructive assignment together with more low-level attribute handling to realise pure input on any (buffered) stream.