| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | ! Copyright (C) 2007, 2009 Slava Pestov, Daniel Ehrenberg. | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-02-13 16:35:04 -05:00
										 |  |  | USING: sequences io.pathnames kernel regexp.combinators | 
					
						
							| 
									
										
										
										
											2010-02-16 13:33:21 -05:00
										 |  |  | strings splitting system unicode.case peg.ebnf regexp arrays ;
 | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | IN: globs | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 16:35:04 -05:00
										 |  |  | : not-path-separator ( -- sep )
 | 
					
						
							| 
									
										
										
										
											2010-02-16 13:33:21 -05:00
										 |  |  |     os windows? R! [^/\\]! R! [^/]! ? ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-13 16:35:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | EBNF: <glob> | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | Character = "\\" .:c => [[ c 1string <literal> ]] | 
					
						
							|  |  |  |           | !(","|"}") . => [[ 1string <literal> ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | RangeCharacter = !("]") .
 | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | Range = RangeCharacter:a "-" RangeCharacter:b => [[ a b <char-range> ]] | 
					
						
							|  |  |  |       | RangeCharacter => [[ 1string <literal> ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | StartRange = .:a "-" RangeCharacter:b => [[ a b <char-range> ]] | 
					
						
							|  |  |  |            | . => [[ 1string <literal> ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | Ranges = StartRange:s Range*:r => [[ r s prefix ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | CharClass = "^"?:n Ranges:e => [[ e <or> n [ <not> ] when ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | AlternationBody = Concatenation:c "," AlternationBody:a => [[ a c prefix ]] | 
					
						
							|  |  |  |                 | Concatenation => [[ 1array ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 16:35:04 -05:00
										 |  |  | Element = "*" => [[ not-path-separator <zero-or-more> ]] | 
					
						
							|  |  |  |         | "?" => [[ not-path-separator ]] | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  |         | "[" CharClass:c "]" => [[ c ]] | 
					
						
							|  |  |  |         | "{" AlternationBody:b "}" => [[ b <or> ]] | 
					
						
							|  |  |  |         | Character | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | Concatenation = Element* => [[ <sequence> ]] | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  | End = !(.) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Main = Concatenation End | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ;EBNF | 
					
						
							| 
									
										
										
										
											2007-11-28 02:12:42 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : glob-matches? ( input glob -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-03-02 16:31:28 -05:00
										 |  |  |     [ >case-fold ] bi@ <glob> matches? ;
 | 
					
						
							| 
									
										
										
										
											2010-02-14 14:49:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : glob-pattern? ( string -- ? )
 | 
					
						
							|  |  |  |     [ "\\*?[{" member? ] any? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : glob-parent-directory ( glob -- parent-directory )
 | 
					
						
							| 
									
										
										
										
											2010-02-16 13:33:21 -05:00
										 |  |  |     path-separator split harvest dup [ glob-pattern? ] find drop head
 | 
					
						
							| 
									
										
										
										
											2010-02-14 14:49:33 -05:00
										 |  |  |     path-separator join ;
 |