Debian Codegraph @

src/Lambdabot/Plugin/Core/Compose.hs

raw
1 {-# LANGUAGE PatternGuards #-}
2 -- Copyright (c) 2005 Don Stewart - http://www.cse.unsw.edu.au/~dons
3 -- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
4
5 -- Another progressive plugin. Compose two (for now) plugins transparently
6 -- A sort of mini interpreter. Could do with some more thinking.
7 module Lambdabot.Plugin.Core.Compose (composePlugin) where
8
9 import Lambdabot.Command
10 import Lambdabot.Module
11 import Lambdabot.Monad
12 import Lambdabot.Plugin
13
14 import Control.Arrow (first)
15 import Control.Monad
16 import Control.Monad.Reader
17 import Data.Char
18 import Data.List
19 import Data.List.Split
20
21 type Compose = ModuleT () LB
22
23 composePlugin :: Module ()
24 composePlugin = newModule
25 { moduleCmds = return
26 [ (command "@")
27 { aliases = ["?"]
28 , help = do
29 c <- getCmdName
30 let cc = c++c
31 mapM_ say
32 [ cc++" [args]."
33 , cc++" executes plugin invocations in its arguments, parentheses can be used."
34 , " The commands are right associative."
35 , " For example: "++cc++" "++c++"pl "++c++"undo code"
36 , " is the same as: "++cc++" ("++c++"pl ("++c++"undo code))"
37 ]
38 , process = evalBracket
39 }
40 , (command ".")
41 { aliases = ["compose"]
42 , help = mapM_ say
43 [ ". <cmd1> <cmd2> [args]."
44 , ". [or compose] is the composition of two plugins"
45 , " The following semantics are used: . f g xs == g xs >>= f"
46 ]
47 , process = \args -> case splitOn " " args of
48 (f:g:xs) -> do
49 f' <- lookupP f
50 g' <- lookupP g
51 lb (compose f' g' (concat $ intersperse " " xs)) >>= mapM_ say
52 _ -> say "Not enough arguments to @."
53 }
54 ]
55 }
56
57 -- | Compose two plugin functions
58 compose :: (String -> LB [String]) -> (String -> LB [String]) -> (String -> LB [String])
59 compose f g xs = g xs >>= f . unlines
60
61 ------------------------------------------------------------------------
62 -- | Lookup the `process' method we're after, and apply it to the dummy args
63 --
64 lookupP :: String -> Cmd Compose (String -> LB [String])
65 lookupP cmd = withMsg $ \a -> do
66 b <- getTarget
67 lb $ withCommand cmd
68 (fail $ "Unknown command: " ++ show cmd)
69 (\theCmd -> do
70 when (privileged theCmd) $ fail "Privileged commands cannot be composed"
71 mTag <- asks moduleID
72 return (inModuleWithID mTag (return []) . runCommand theCmd a b cmd))
73
74 ------------------------------------------------------------------------
75
76 -- | More interesting composition/evaluation
77 -- @@ @f x y (@g y z)
78 evalBracket :: String -> Cmd Compose ()
79 evalBracket args = do
80 cmdPrefixes <- getConfig commandPrefixes
81
82 let conf = cmdPrefixes
83 xs <- mapM evalExpr (fst (parseBracket 0 True args conf))
84 mapM_ (say . addSpace) (concat' xs)
85 where concat' ([x]:[y]:xs) = concat' ([x++y]:xs)
86 concat' xs = concat xs
87
88 addSpace :: String -> String
89 addSpace (' ':xs) = ' ':xs
90 addSpace xs = ' ':xs
91
92 evalExpr :: Expr -> Cmd Compose [String]
93 evalExpr (Arg s) = return [s]
94 evalExpr (Cmd c args) = do
95 args' <- mapM evalExpr args
96 let arg = concat $ concat $ map (intersperse " ") args'
97 cmd <- lookupP c
98 lift (lift (cmd arg))
99
100 ------------------------------------------------------------------------
101
102 data Expr = Cmd String [Expr]
103 | Arg String
104 deriving Show
105
106 -- TODO: rewrite this using parsec or something
107 -- | Parse a command invocation that can contain parentheses
108 -- The Int indicates how many brackets must be closed to end the current argument, or 0
109 -- The Bool indicates if this is a valid location for a character constant
110 parseBracket :: Int -> Bool -> String -> [String] -> ([Expr],String)
111 parseBracket 0 _ [] _ = ([],[])
112 parseBracket _ _ [] _ = error "Missing ')' in nested command"
113 parseBracket 1 _ (')':xs) _ = ([],xs)
114 parseBracket n _ (')':xs) c | n > 0
115 = first (addArg ")") $ parseBracket (n-1) True xs c
116 parseBracket n _ ('(':xs) c | Just ys <- isCommand xs c -- (@cmd arg arg)
117 = parseCommand n ys c
118 parseBracket n _ ('(':xs) c | n > 0
119 = first (addArg "(") $ parseBracket (n+1) True xs c
120 parseBracket n _ xs c | Just ('(':ys) <- isCommand xs c -- @(cmd arg arg)
121 = parseCommand n ys c
122 parseBracket n _ xs c | Just ys <- isCommand xs c -- @cmd arg arg
123 = parseInlineCommand n ys c
124 parseBracket n c (x:xs) cfg | x `elem` "\"'" && (c || x /= '\'')
125 = let (str, ys) = parseString x xs
126 (rest,zs) = parseBracket n True ys cfg
127 in (addArg (x:str) rest, zs)
128 parseBracket n c (x:xs) cfg = first (addArg [x])
129 $ parseBracket n (not (isAlphaNum x) && (c || x /= '\'')) xs cfg
130
131 parseCommand, parseInlineCommand :: Int -> String -> [String] -> ([Expr],String)
132 parseCommand n xs conf = (Cmd cmd args:rest, ws)
133 where
134 (cmd, ys) = break (`elem` " )") xs
135 (args,zs) = parseBracket 1 True (dropWhile (==' ') ys) conf
136 (rest,ws) = parseBracket n True zs conf
137
138 parseInlineCommand n xs conf = (Cmd cmd rest:[], zs)
139 where
140 (cmd, ys) = break (`elem` " )") xs
141 (rest,zs) = parseBracket n True (dropWhile (==' ') ys) conf
142
143 parseString :: Char -> String -> (String, String)
144 parseString _ [] = ([],[])
145 parseString delim ('\\':x:xs) = first (\ys -> '\\':x:ys) (parseString delim xs)
146 parseString delim (x:xs)
147 | delim == x = ([x],xs)
148 | otherwise = first (x:) (parseString delim xs)
149
150
151 -- | Does xs start with a command prefix?
152 isCommand :: String -> [String] -> Maybe String
153 isCommand xs = msum . map dropPrefix
154 where dropPrefix p
155 | p `isPrefixOf` xs = Just $ drop (length p) xs
156 | otherwise = Nothing
157
158 addArg :: String -> [Expr] -> [Expr]
159 addArg s (Arg a:es) = Arg (s++a):es
160 addArg s es = Arg s :es