1 ! Copyright (C) 2007, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences io.pathnames kernel regexp.combinators
4 strings splitting system unicode.case peg.ebnf regexp arrays ;
7 : not-path-separator ( -- sep )
8 os windows? R! [^/\\]! R! [^/]! ? ; foldable
12 Character = "\\" .:c => [[ c 1string <literal> ]]
13 | !(","|"}") . => [[ 1string <literal> ]]
15 RangeCharacter = !("]") .
17 Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <char-range> ]]
18 | RangeCharacter => [[ 1string <literal> ]]
20 StartRange = .:a "-" RangeCharacter:b => [[ a b <char-range> ]]
21 | . => [[ 1string <literal> ]]
23 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
25 CharClass = "^"?:n Ranges:e => [[ e <or> n [ <not> ] when ]]
27 AlternationBody = Concatenation:c "," AlternationBody:a => [[ a c prefix ]]
28 | Concatenation => [[ 1array ]]
30 Element = "*" => [[ not-path-separator <zero-or-more> ]]
31 | "?" => [[ not-path-separator ]]
32 | "[" CharClass:c "]" => [[ c ]]
33 | "{" AlternationBody:b "}" => [[ b <or> ]]
36 Concatenation = Element* => [[ <sequence> ]]
40 Main = Concatenation End
44 : glob-matches? ( input glob -- ? )
45 [ >case-fold ] bi@ <glob> matches? ;
47 : glob-pattern? ( string -- ? )
48 [ "\\*?[{" member? ] any? ;
50 : glob-parent-directory ( glob -- parent-directory )
51 path-separator split harvest dup [ glob-pattern? ] find drop head