From 0ab355eadacb981dec9f63957f7be3fa28e59e47 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 14 Aug 2022 08:23:39 +0200 Subject: [PATCH] use a DCG to describe the path --- src/lib/files.pl | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib/files.pl b/src/lib/files.pl index aa18ea78..ef73e851 100644 --- a/src/lib/files.pl +++ b/src/lib/files.pl @@ -1,5 +1,5 @@ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Written June 2020 by Markus Triska (triska@metalevel.at) + Written 2020, 2022 by Markus Triska (triska@metalevel.at) Part of Scryer Prolog. Predicates for reasoning about files and directories. @@ -65,6 +65,7 @@ :- use_module(library(error)). :- use_module(library(lists)). :- use_module(library(charsio)). +:- use_module(library(dcgs)). directory_files(Directory, Files) :- must_be(chars, Directory), @@ -198,19 +199,19 @@ path_segments(Path, Segments) :- ( var(Path) -> must_be(list, Segments), maplist(must_be(chars), Segments), - append_with_separator(Segments, Sep, Path) + phrase(append_with_separator(Segments, Sep), Path) ; must_be(chars, Path), path_to_segments(Path, Sep, Segments) ). -append_with_separator([], _, []). -append_with_separator([Segment|Segments], Sep, Path) :- - append_with_separator_(Segments, Segment, Sep, Path). +append_with_separator([], _) --> []. +append_with_separator([Segment|Segments], Sep) --> + append_with_separator_(Segments, Segment, Sep). -append_with_separator_([], Segment, _, Segment). -append_with_separator_([Segment|Segments], Prev, Sep, Path) :- - append(Prev, [Sep|Rest], Path), - append_with_separator_(Segments, Segment, Sep, Rest). +append_with_separator_([], Segment, _) --> seq(Segment). +append_with_separator_([Segment|Segments], Prev, Sep) --> + seq(Prev), [Sep], + append_with_separator_(Segments, Segment, Sep). path_to_segments(Path, Sep, Segments) :- ( append(Front, [Sep|Ps], Path) -> -- 2.54.0