1 -- Copyright (C) 2003 David Roundy 2 -- 3 -- This program is free software; you can redistribute it and/or modify 4 -- it under the terms of the GNU General Public License as published by 5 -- the Free Software Foundation; either version 2, or (at your option) 6 -- any later version. 7 -- 8 -- This program is distributed in the hope that it will be useful, 9 -- but WITHOUT ANY WARRANTY; without even the implied warranty of 10 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 -- GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License 14 -- along with this program; see the file COPYING. If not, write to 15 -- the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 16 -- Boston, MA 02110-1301, USA. 17 18 19 module RegChars ( regChars, 20 ) where 21 22 (&&&) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) 23 (&&&) a b c = a c && b c 24 25 (|||) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) 26 (|||) a b c = a c || b c 27 28 {-# INLINE regChars #-} 29 30 -- | 'regChars' returns a filter function that tells if a char is a member 31 -- of the regChar expression or not. The regChar expression is basically a 32 -- set of chars, but it can contain ranges with use of the '-' (dash), and 33 -- it can also be specified as a complement set by prefixing with '^' 34 -- (caret). The dash and caret, as well as the backslash, can all be 35 -- escaped with a backslash to suppress their special meaning. 36 -- 37 -- NOTE: The '.' (dot) is allowed to be escaped. It has no special meaning 38 -- if it is not escaped, but the default 'filename_toks' in 39 -- Darcs.Commands.Replace uses an escaped dot (WHY?). 40 41 regChars :: String -> (Char -> Bool) 42 regChars ('^':cs) = not . normalRegChars (unescapeChars cs) 43 regChars ('\\':'^':cs) = normalRegChars $ unescapeChars $ '^':cs 44 regChars cs = normalRegChars $ unescapeChars cs 45 46 {-# INLINE unescapeChars #-} 47 48 -- | 'unescapeChars' unescapes whitespace, which is escaped in the replace 49 -- patch file format. It will also unescape escaped carets, which is useful 50 -- for escaping a leading caret that should not invert the regChars. All 51 -- other escapes are left for the unescaping in 'normalRegChars'. 52 53 unescapeChars :: String -> String 54 unescapeChars ('\\':'n':cs) = '\n' : unescapeChars cs 55 unescapeChars ('\\':'t':cs) = '\t' : unescapeChars cs 56 unescapeChars ('\\':'^':cs) = '^' : unescapeChars cs 57 unescapeChars (c:cs) = c : unescapeChars cs 58 unescapeChars [] = [] 59 60 {-# INLINE normalRegChars #-} 61 62 -- | 'normalRegChars' assembles the filter function. It handles special 63 -- chars, and also unescaping of escaped special chars. If a non-special 64 -- char is still escaped by now we get a failure. 65 66 normalRegChars :: String -> (Char -> Bool) 67 normalRegChars ('\\':'.':cs) = (=='.') ||| normalRegChars cs 68 normalRegChars ('\\':'-':cs) = (=='-') ||| normalRegChars cs 69 normalRegChars ('\\':'\\':cs) = (=='\\') ||| normalRegChars cs 70 normalRegChars ('\\':c:_) = error $ "'\\"++[c]++"' not supported." 71 normalRegChars (c1:'-':c2:cs) = ((>= c1) &&& (<= c2)) ||| normalRegChars cs 72 normalRegChars (c:cs) = (== c) ||| normalRegChars cs 73 normalRegChars [] = \_ -> False 74 75