]> gitweb.factorcode.org Git - factor.git/blob - basis/globs/globs.factor
Merge branch 'master' of git://factorcode.org/git/factor into s3
[factor.git] / basis / globs / globs.factor
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 ;
5 IN: globs
6
7 : not-path-separator ( -- sep )
8     os windows? R! [^/\\]! R! [^/]! ? ; foldable
9
10 EBNF: <glob>
11
12 Character = "\\" .:c => [[ c 1string <literal> ]]
13           | !(","|"}") . => [[ 1string <literal> ]]
14
15 RangeCharacter = !("]") .
16
17 Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <char-range> ]]
18       | RangeCharacter => [[ 1string <literal> ]]
19
20 StartRange = .:a "-" RangeCharacter:b => [[ a b <char-range> ]]
21            | . => [[ 1string <literal> ]]
22
23 Ranges = StartRange:s Range*:r => [[ r s prefix ]]
24
25 CharClass = "^"?:n Ranges:e => [[ e <or> n [ <not> ] when ]]
26
27 AlternationBody = Concatenation:c "," AlternationBody:a => [[ a c prefix ]]
28                 | Concatenation => [[ 1array ]]
29
30 Element = "*" => [[ not-path-separator <zero-or-more> ]]
31         | "?" => [[ not-path-separator ]]
32         | "[" CharClass:c "]" => [[ c ]]
33         | "{" AlternationBody:b "}" => [[ b <or> ]]
34         | Character
35
36 Concatenation = Element* => [[ <sequence> ]]
37
38 End = !(.)
39
40 Main = Concatenation End
41
42 ;EBNF
43
44 : glob-matches? ( input glob -- ? )
45     [ >case-fold ] bi@ <glob> matches? ;
46
47 : glob-pattern? ( string -- ? )
48     [ "\\*?[{" member? ] any? ;
49
50 : glob-parent-directory ( glob -- parent-directory )
51     path-separator split harvest dup [ glob-pattern? ] find drop head
52     path-separator join ;