2011-09-18 20:02:12 +00:00
|
|
|
{- A generic matcher.
|
|
|
|
-
|
|
|
|
- Can be used to check if a user-supplied condition,
|
|
|
|
- like "foo and ( bar or not baz )" matches. The condition must already
|
|
|
|
- be tokenized, and can contain arbitrary operations.
|
|
|
|
-
|
|
|
|
- If operations are not separated by and/or, they are defaulted to being
|
|
|
|
- anded together, so "foo bar baz" all must match.
|
|
|
|
-
|
|
|
|
- Is forgiving about misplaced closing parens, so "foo and (bar or baz"
|
|
|
|
- will be handled, as will "foo and ( bar or baz ) )"
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Matcher (
|
|
|
|
Token(..),
|
2011-09-18 20:32:39 +00:00
|
|
|
generate,
|
2011-09-18 20:02:12 +00:00
|
|
|
match,
|
2011-09-18 20:32:39 +00:00
|
|
|
run
|
2011-09-18 20:02:12 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad
|
|
|
|
|
2011-09-18 20:32:39 +00:00
|
|
|
{- A Token can either be a single word, or an Operation of an arbitrary type. -}
|
|
|
|
data Token op = Token String | Operation op
|
2011-09-18 20:02:12 +00:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2011-09-18 20:32:39 +00:00
|
|
|
data Matcher op = Any
|
|
|
|
| And (Matcher op) (Matcher op)
|
|
|
|
| Or (Matcher op) (Matcher op)
|
|
|
|
| Not (Matcher op)
|
|
|
|
| Op op
|
2011-09-18 20:02:12 +00:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
{- Converts a list of Tokens into a Matcher. -}
|
2011-09-18 20:32:39 +00:00
|
|
|
generate :: [Token op] -> Matcher op
|
|
|
|
generate ts = generate' Any ts
|
|
|
|
generate' :: Matcher op -> [Token op] -> Matcher op
|
|
|
|
generate' m [] = m
|
|
|
|
generate' m ts = generate' m' rest
|
2011-09-18 20:02:12 +00:00
|
|
|
where
|
|
|
|
(m', rest) = consume m ts
|
|
|
|
|
2011-09-18 20:32:39 +00:00
|
|
|
{- Consumes one or more Tokens, constructs a new Matcher,
|
|
|
|
- and returns unconsumed Tokens. -}
|
|
|
|
consume :: Matcher op -> [Token op] -> (Matcher op, [Token op])
|
2011-09-18 20:02:12 +00:00
|
|
|
consume m [] = (m, [])
|
2011-09-18 20:32:39 +00:00
|
|
|
consume m ((Operation o):ts) = (m `And` Op o, ts)
|
2011-09-18 20:02:12 +00:00
|
|
|
consume m ((Token t):ts)
|
|
|
|
| t == "and" = cont $ m `And` next
|
|
|
|
| t == "or" = cont $ m `Or` next
|
|
|
|
| t == "not" = cont $ m `And` (Not next)
|
|
|
|
| t == "(" = let (n, r) = consume next rest in (m `And` n, r)
|
|
|
|
| t == ")" = (m, ts)
|
|
|
|
| otherwise = (m, ts) -- ignore unknown token
|
|
|
|
where
|
|
|
|
(next, rest) = consume Any ts
|
|
|
|
cont v = (v, rest)
|
|
|
|
|
|
|
|
{- Checks if a Matcher matches, using a supplied function to check
|
|
|
|
- the value of Operations. -}
|
2011-09-18 20:32:39 +00:00
|
|
|
match :: (op -> Bool) -> Matcher op -> Bool
|
2011-09-18 20:02:12 +00:00
|
|
|
match a = go
|
|
|
|
where
|
|
|
|
go Any = True
|
|
|
|
go (And m1 m2) = go m1 && go m2
|
|
|
|
go (Or m1 m2) = go m1 || go m2
|
|
|
|
go (Not m1) = not (go m1)
|
|
|
|
go (Op v) = a v
|
|
|
|
|
2011-09-18 20:32:39 +00:00
|
|
|
{- Runs a monadic Matcher, where Operations are actions in the monad. -}
|
|
|
|
run :: Monad m => Matcher (m Bool) -> m Bool
|
|
|
|
run = go
|
2011-09-18 20:02:12 +00:00
|
|
|
where
|
|
|
|
go Any = return True
|
|
|
|
go (And m1 m2) = liftM2 (&&) (go m1) (go m2)
|
|
|
|
go (Or m1 m2) = liftM2 (||) (go m1) (go m2)
|
|
|
|
go (Not m1) = liftM not (go m1)
|
2011-09-18 20:32:39 +00:00
|
|
|
go (Op o) = o -- run o
|