on master help sign in
Mouse: factor control sequence parser out into its own module
19be5beJonathan Daugherty
src/Graphics/Vty/Input/Classify/Parse.hsnew file+57
@@ -0,0 +1,57 @@
1+-- | This module provides a simple parser for parsing input event
2+-- control sequences.
3+module Graphics.Vty.Input.Classify.Parse
4+ ( Parser
5+ , runParser
6+ , failParse
7+ , readInt
8+ , readChar
9+ , expectChar
10+ ) where
11+
12+import Graphics.Vty.Input.Events
13+import Graphics.Vty.Input.Classify.Types
14+
15+import Control.Monad.Trans.Maybe
16+import Control.Monad.State
17+
18+type Parser a = MaybeT (State String) a
19+
20+-- | Run a parser on a given input string. If the parser fails, return
21+-- 'Invalid'. Otherwise return the valid event ('Valid') and the
22+-- remaining unparsed characters.
23+runParser :: String -> Parser Event -> KClass
24+runParser s parser =
25+ case runState (runMaybeT parser) s of
26+ (Nothing, _) -> Invalid
27+ (Just e, remaining) -> Valid e remaining
28+
29+-- | Fail a parsing operation.
30+failParse :: Parser a
31+failParse = fail "invalid parse"
32+
33+-- | Read an integer from the input stream. If an integer cannot be
34+-- read, fail parsing. E.g. calling readInt on an input of "123abc" will
35+-- return '123' and consume those characters.
36+readInt :: Parser Int
37+readInt = do
38+ s <- get
39+ case (reads :: ReadS Int) s of
40+ [(i, rest)] -> put rest >> return i
41+ _ -> failParse
42+
43+-- | Read a character from the input stream. If one cannot be read (e.g.
44+-- we are out of characters), fail parsing.
45+readChar :: Parser Char
46+readChar = do
47+ s <- get
48+ case s of
49+ c:rest -> put rest >> return c
50+ _ -> failParse
51+
52+-- | Read a character from the input stream and fail parsing if it is
53+-- not the specified character.
54+expectChar :: Char -> Parser ()
55+expectChar c = do
56+ c' <- readChar
57+ if c' == c then return () else failParse
src/Graphics/Vty/Input/Mouse.hs+1−30
@@ -11,6 +11,7 @@ module Graphics.Vty.Input.Mouse
11
11
12import Graphics.Vty.Input.Events
12import Graphics.Vty.Input.Events
13import Graphics.Vty.Input.Classify.Types
13import Graphics.Vty.Input.Classify.Types
14+import Graphics.Vty.Input.Classify.Parse
14
15
15import Control.Monad.Trans.Maybe
16import Control.Monad.Trans.Maybe
16import Control.Monad.State
17import Control.Monad.State
@@ -110,36 +111,6 @@ getModifiers mods =
110 , if mods `hasBitSet` ctrlBit then Just MCtrl else Nothing
111 , if mods `hasBitSet` ctrlBit then Just MCtrl else Nothing
111 ]
112 ]
112
113
113−type Parser a = MaybeT (State String) a
114−
115−runParser :: String -> Parser Event -> KClass
116−runParser s parser =
117− case runState (runMaybeT parser) s of
118− (Nothing, _) -> Invalid
119− (Just e, remaining) -> Valid e remaining
120−
121−failParse :: Parser a
122−failParse = fail "invalid parse"
123−
124−readInt :: Parser Int
125−readInt = do
126− s <- get
127− case (reads :: ReadS Int) s of
128− [(i, rest)] -> put rest >> return i
129− _ -> failParse
130−
131−readChar :: Parser Char
132−readChar = do
133− s <- get
134− case s of
135− c:rest -> put rest >> return c
136− _ -> failParse
137−
138−expectChar :: Char -> Parser ()
139−expectChar c = do
140− c' <- readChar
141− if c' == c then return () else failParse
142−
143-- Attempt to classify a control sequence as a "normal" mouse event. To
114-- Attempt to classify a control sequence as a "normal" mouse event. To
144-- get here we should have already read "\ESC[M" so that will not be
115-- get here we should have already read "\ESC[M" so that will not be
145-- included in the string to be parsed.
116-- included in the string to be parsed.
vty.cabal+1
@@ -95,6 +95,7 @@ library
95 Graphics.Vty.Image.Internal
95 Graphics.Vty.Image.Internal
96 Graphics.Vty.Input.Classify
96 Graphics.Vty.Input.Classify
97 Graphics.Vty.Input.Classify.Types
97 Graphics.Vty.Input.Classify.Types
98+ Graphics.Vty.Input.Classify.Parse
98 Graphics.Vty.Input.Loop
99 Graphics.Vty.Input.Loop
99 Graphics.Vty.Input.Mouse
100 Graphics.Vty.Input.Mouse
100 Graphics.Vty.Input.Terminfo
101 Graphics.Vty.Input.Terminfo