on master help sign in
Moving construction of terminfo CapExpression to MonadIO

Ignore-this: 5c78cc78ce2ad3f394484f76cb840272 darcs-hash:20100114013836-f0a0d-52bfc693e29d2e2bc36931b0e944d1cc3da7a94f.gz

commit04c79d2e8c1eedfed1eb492727805954eee95df7 authorcoreyoconnor <coreyoconnor@gmail.com> dateWed 13 Jan 2010, 17:38 (17 years ago) parent3e8999d
5 files changed+41−28
src/Data/Terminfo/Parse.hs+7−4 src/Graphics/Vty/Terminal/TerminfoBased.hs+15−9 test/Verify/Data/Terminfo/Parse.hs+2−2 test/verify_eval_terminfo_caps.hs+1−1 test/verify_parse_terminfo_caps.hs+16−12
src/Data/Terminfo/Parse.hs+7−4
@@ -10,6 +10,7 @@ module Data.Terminfo.Parse ( module Data.Terminfo.Parse
1010 where
1111
1212import Control.Monad ( liftM )
13+import Control.Monad.Trans
1314import Control.Parallel.Strategies
1415
1516import Data.Array.Unboxed
@@ -66,16 +67,18 @@ data ParamOp =
6667 IncFirstTwo
6768 deriving ( Show )
6869
69−parse_cap_expression :: String -> Either ParseError CapExpression
70+parse_cap_expression :: MonadIO m => String -> m ( Either ParseError CapExpression )
7071parse_cap_expression cap_string =
7172 let v = runParser cap_expression_parser
7273 initial_build_state
7374 "terminfo cap"
7475 cap_string
7576 in case v of
76− Left e -> Left e
77+ Left e -> return $ Left e
7778 Right build_results ->
78− Right $! ( CapExpression
79+ return
80+ $! Right
81+ $! ( CapExpression
7982 { cap_ops = out_cap_ops build_results
8083 -- The cap bytes are the lower 8 bits of the input string's characters.
8184 -- \todo Verify the input string actually contains an 8bit byte per character.
@@ -86,7 +89,7 @@ parse_cap_expression cap_string =
8689 , param_ops = out_param_ops build_results
8790 }
8891 `using` rdeepseq
89− )
92+ )
9093
9194type CapParser a = GenParser Char BuildState a
9295
src/Graphics/Vty/Terminal/TerminfoBased.hs+15−9
@@ -75,15 +75,19 @@ terminal_instance in_ID = do
7575 let require_cap str
7676 = case Terminfo.getCapability ti (Terminfo.tiGetStr str) of
7777 Nothing -> fail $ "Terminal does not define required capability \"" ++ str ++ "\""
78− Just cap_str -> case parse_cap_expression cap_str of
79− Left e -> fail $ show e
80− Right cap -> return cap
78+ Just cap_str -> do
79+ parse_result <- parse_cap_expression cap_str
80+ case parse_result of
81+ Left e -> fail $ show e
82+ Right cap -> return cap
8183 probe_cap cap_name
8284 = case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
8385 Nothing -> return Nothing
84− Just cap_str -> case parse_cap_expression cap_str of
85− Left e -> fail $ show e
86− Right cap -> return $ Just cap
86+ Just cap_str -> do
87+ parse_result <- parse_cap_expression cap_str
88+ case parse_result of
89+ Left e -> fail $ show e
90+ Right cap -> return $ Just cap
8791 pure Term
8892 <*> pure in_ID
8993 <*> pure ti
@@ -114,9 +118,11 @@ current_display_attr_caps ti
114118 where probe_cap cap_name
115119 = case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
116120 Nothing -> return Nothing
117− Just cap_str -> case parse_cap_expression cap_str of
118− Left e -> fail $ show e
119− Right cap -> return $ Just cap
121+ Just cap_str -> do
122+ parse_result <- parse_cap_expression cap_str
123+ case parse_result of
124+ Left e -> fail $ show e
125+ Right cap -> return $ Just cap
120126
121127instance Terminal Term where
122128 terminal_ID t = term_info_ID t ++ " :: TerminfoBased"
test/Verify/Data/Terminfo/Parse.hs+2−2
@@ -103,8 +103,8 @@ collect_bytes e = concat [ bytes
103103
104104verify_bytes_equal out_bytes expected_bytes
105105 = if out_bytes == expected_bytes
106− then liftResult succeeded
107− else liftResult $ failed
106+ then succeeded
107+ else failed
108108 { reason = "out_bytes ["
109109 ++ hex_dump ( listArray (0, toEnum $ length out_bytes - 1) out_bytes )
110110 ++ "] /= expected_bytes ["
test/verify_eval_terminfo_caps.hs+1−1
@@ -101,7 +101,7 @@ main = do
101101 liftIO $ putStrLn $ "\tevaluating cap: " ++ cap_name
102102 case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
103103 Just cap_def -> do
104− let parse_result = parse_cap_expression cap_def
104+ parse_result <- parse_cap_expression cap_def
105105 let test_name = "\teval cap " ++ cap_name ++ " -> " ++ show cap_def
106106 _ <- case parse_result of
107107 Left error -> verify test_name ( liftResult $ failed { reason = "prase error " ++ show error } )
test/verify_parse_terminfo_caps.hs+16−12
@@ -98,7 +98,7 @@ main = do
9898 case Terminfo.getCapability ti (Terminfo.tiGetStr cap_name) of
9999 Just cap_def -> do
100100 verify ( "\tparse cap " ++ cap_name ++ " -> " ++ show cap_def )
101− ( verify_parse_cap cap_def $ const (liftResult succeeded) )
101+ ( verify_parse_cap cap_def $ const ( return succeeded ) )
102102 return ()
103103 Nothing -> do
104104 return ()
@@ -110,9 +110,10 @@ main = do
110110 return ()
111111 return ()
112112
113−verify_parse_cap cap_string on_parse = do
114− case parse_cap_expression cap_string of
115− Left error -> liftResult $ failed { reason = "parse error " ++ show error }
113+verify_parse_cap cap_string on_parse = liftIOResult $ do
114+ parse_result <- parse_cap_expression cap_string
115+ case parse_result of
116+ Left error -> return $ failed { reason = "parse error " ++ show error }
116117 Right e -> on_parse e
117118
118119non_paramaterized_caps (NonParamCapString cap) = do
@@ -120,39 +121,42 @@ non_paramaterized_caps (NonParamCapString cap) = do
120121 let expected_count :: Word8 = toEnum $ length cap
121122 expected_bytes = map (toEnum . fromEnum) cap
122123 out_bytes = bytes_for_range e 0 expected_count
123− in verify_bytes_equal out_bytes expected_bytes
124+ in return $ verify_bytes_equal out_bytes expected_bytes
124125
125126literal_percent_caps (LiteralPercentCap cap_string expected_bytes) = do
126127 verify_parse_cap cap_string $ \e ->
127128 let expected_count :: Word8 = toEnum $ length expected_bytes
128129 out_bytes = collect_bytes e
129− in verify_bytes_equal out_bytes expected_bytes
130+ in return $ verify_bytes_equal out_bytes expected_bytes
130131
131132inc_first_two_caps (IncFirstTwoCap cap_string expected_bytes) = do
132133 verify_parse_cap cap_string $ \e ->
133134 let expected_count :: Word8 = toEnum $ length expected_bytes
134135 out_bytes = collect_bytes e
135− in verify_bytes_equal out_bytes expected_bytes
136+ in return $ verify_bytes_equal out_bytes expected_bytes
136137
137138push_param_caps (PushParamCap cap_string expected_param_count expected_bytes) = do
138139 verify_parse_cap cap_string $ \e ->
139140 let expected_count :: Word8 = toEnum $ length expected_bytes
140141 out_bytes = collect_bytes e
141142 out_param_count = param_count e
142− in verify_bytes_equal out_bytes expected_bytes
143− .&. out_param_count == expected_param_count
143+ in return $ if out_param_count == expected_param_count
144+ then verify_bytes_equal out_bytes expected_bytes
145+ else failed { reason = "out param count /= expected param count" }
144146
145147dec_print_param_caps (DecPrintCap cap_string expected_param_count expected_bytes) = do
146148 verify_parse_cap cap_string $ \e ->
147149 let expected_count :: Word8 = toEnum $ length expected_bytes
148150 out_bytes = collect_bytes e
149151 out_param_count = param_count e
150− in verify_bytes_equal out_bytes expected_bytes
151− .&. out_param_count == expected_param_count
152+ in return $ if out_param_count == expected_param_count
153+ then verify_bytes_equal out_bytes expected_bytes
154+ else failed { reason = "out param count /= expected param count" }
152155
153156print_cap ti cap_name = do
154157 putStrLn $ cap_name ++ ": " ++ show (from_capname ti cap_name)
155158
156159print_expression ti cap_name = do
157− putStrLn $ cap_name ++ ": " ++ show (parse_cap_expression $ from_capname ti cap_name)
160+ parse_result <- parse_cap_expression $ from_capname ti cap_name
161+ putStrLn $ cap_name ++ ": " ++ show parse_result
158162