summaryrefslogtreecommitdiff
path: root/Utility/Matcher.hs
blob: 342775d68c8e0ff4678f1b695050fedc81825d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{- 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 Utility.Matcher (
	Token(..),
	Matcher,
	generate,
	match,
	matchM,
	matchesAny
) where

import Control.Monad

{- A Token can either be a single word, or an Operation of an arbitrary type. -}
data Token op = Token String | Operation op
	deriving (Show, Eq)

data Matcher op = Any
	| And (Matcher op) (Matcher op)
	| Or (Matcher op) (Matcher op)
	| Not (Matcher op)
	| Op op
	deriving (Show, Eq)

{- Converts a list of Tokens into a Matcher. -}
generate :: [Token op] -> Matcher op
generate ts = generate' Any ts
generate' :: Matcher op -> [Token op] -> Matcher op
generate' m [] = m
generate' m ts = uncurry generate' $ consume m ts

{- Consumes one or more Tokens, constructs a new Matcher,
 - and returns unconsumed Tokens. -}
consume :: Matcher op -> [Token op] -> (Matcher op, [Token op])
consume m [] = (m, [])
consume m ((Operation o):ts) = (m `And` Op o, ts)
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 = error $ "unknown token " ++ t
	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. -}
match :: (op -> v -> Bool) -> Matcher op -> v -> Bool
match a m v = go m
	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 o) = a o v

{- Runs a monadic Matcher, where Operations are actions in the monad. -}
matchM :: Monad m => Matcher (v -> m Bool) -> v -> m Bool
matchM m v = go m
	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)
		go (Op o) = o v

{- Checks is a matcher contains no limits, and so (presumably) matches
 - anything. Note that this only checks the trivial case; it is possible
 - to construct matchers that match anything but are more complicated. -}
matchesAny :: Matcher a -> Bool
matchesAny Any = True
matchesAny _ = False