]> Repositorios git - scryer-prolog.git/commitdiff
use a DCG to describe the path
authorMarkus Triska <[email protected]>
Sun, 14 Aug 2022 06:23:39 +0000 (08:23 +0200)
committerMark Thom <[email protected]>
Thu, 27 Oct 2022 05:36:07 +0000 (23:36 -0600)
src/lib/files.pl

index aa18ea78383000a7f89fec8babaaf923ccd581e1..ef73e851f0f3a59549cc354db68cb8953ff4556e 100644 (file)
@@ -1,5 +1,5 @@
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-   Written June 2020 by Markus Triska ([email protected])
+   Written 2020, 2022 by Markus Triska ([email protected])
    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) ->