on master help sign in
Mouse: factor control sequence parser out into its own module
commit19be5be5846cbee12c7d05afa3cd4c3ac48652a3 authorJonathan Daugherty <cygnus@foobox.com> dateFri 24 Jun 2016, 16:54 (10 years ago) parent64257de
3 files changed+59−30
src/Graphics/Vty/Input/Classify/Parse.hs+57 src/Graphics/Vty/Input/Mouse.hs+1−30 vty.cabal+1
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
1111
1212import Graphics.Vty.Input.Events
1313import Graphics.Vty.Input.Classify.Types
14+import Graphics.Vty.Input.Classify.Parse
1415
1516import Control.Monad.Trans.Maybe
1617import Control.Monad.State
@@ -110,36 +111,6 @@ getModifiers mods =
110111 , if mods `hasBitSet` ctrlBit then Just MCtrl else Nothing
111112 ]
112113
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−
143114-- Attempt to classify a control sequence as a "normal" mouse event. To
144115-- get here we should have already read "\ESC[M" so that will not be
145116-- included in the string to be parsed.
vty.cabal+1
@@ -95,6 +95,7 @@ library
9595 Graphics.Vty.Image.Internal
9696 Graphics.Vty.Input.Classify
9797 Graphics.Vty.Input.Classify.Types
98+ Graphics.Vty.Input.Classify.Parse
9899 Graphics.Vty.Input.Loop
99100 Graphics.Vty.Input.Mouse
100101 Graphics.Vty.Input.Terminfo