on master help sign in
Removed bad x tsnok (#34)

* Removed bad x tsnok * simplified specific * Removed extra comment

commit4130d50cdca41afae2f1bdaf90eb46e995cdc873 authoriko <ilyakooo0@gmail.com> dateSat 15 Feb 2020, 17:31 (7 years ago) parent1ee8769
5 files changed+20−25
src/Control/SemiIso.hs+8 src/Data/Isoparsec.hs+3−4 src/Data/Isoparsec/Char.hs+4−4 src/Data/Isoparsec/Internal.hs+4−16 test/Spec/Ssh.hs+1−1
src/Control/SemiIso.hs+8
@@ -7,6 +7,8 @@ module Control.SemiIso
77 turn,
88 withSemiIso,
99 AlternativeMonad,
10+ maskr,
11+ maskl,
1012 )
1113where
1214
@@ -82,3 +84,9 @@ withSemiIso ::
8284 ((a -> f b) -> (b -> f a) -> c) ->
8385 c
8486withSemiIso (SemiIso a b) f = f a b
87+
88+maskr :: SemiIso a () -> SemiIso a ()
89+maskr (SemiIso e p) = SemiIso (\a -> e a <|> pure ()) p
90+
91+maskl :: SemiIso () b -> SemiIso () b
92+maskl (SemiIso e p) = SemiIso e (\b -> p b <|> pure ())
src/Data/Isoparsec.hs+3−4
@@ -2,7 +2,6 @@
22
33module Data.Isoparsec
44 ( module X,
5− -- Isoparsec,
65 (%>>),
76 (%>%),
87 (>>%),
@@ -40,7 +39,7 @@ opt :: (ArrowPlus m, PolyArrow m SemiIso) => m () () -> m () ()
4039opt m = m <+^ konst ()
4140
4241opt' :: (ArrowPlus m, PolyArrow m SemiIso, Eq a) => a -> m () a -> m () ()
43−opt' a m = (m >>^ tsnok a) <+^ konst ()
42+opt' a m = (m >>^ turn (konst a)) <+^ konst ()
4443
4544repeating :: (PolyArrow m SemiIso, ArrowPlus m, Eq b) => m () b -> m () [b]
4645repeating m = (m &&& (repeating m <+^ konst [])) >>^ siCons
@@ -135,8 +134,8 @@ mapIso m = arr $ siMaybe (`M.lookup` n) (`M.lookup` u)
135134auto :: forall x s m. (ToIsoparsec x s, Isoparsec m s) => m () x
136135auto = toIsoparsec
137136
138−specific :: forall x s m. (ToIsoparsec x s, Isoparsec m s, Eq x, Show x) => x -> m () ()
139−specific x = auto @x >>> check (== x) ^>^ tsnok x
137+specific :: forall x s m. (ToIsoparsec x s, Isoparsec m s, Eq x) => x -> m () ()
138+specific x = auto @x >>^ turn (konst x)
140139
141140throughIntegral ::
142141 (Integral a, Integral b, Num a, Num b, PolyArrow m SemiIso) =>
src/Data/Isoparsec/Char.hs+4−4
@@ -14,11 +14,11 @@ import Prelude hiding ((.), id)
1414space :: (Isoparsec m s, Element s ~ Char) => m () ()
1515space = token ' '
1616
17−unsafeWhiteSpace :: (Isoparsec m s, Element s ~ Char) => m () ()
18−unsafeWhiteSpace = tokensWhile isSpace >>^ badTsnok mempty
17+unsafeWhiteSpace :: (Isoparsec m s, Element s ~ Char, Eq s) => m () ()
18+unsafeWhiteSpace = tokensWhile isSpace >>^ (maskr . turn . konst $ mempty)
1919
20−unsafeWhiteSpace1 :: (Isoparsec m s, Element s ~ Char) => m () ()
21−unsafeWhiteSpace1 = tokensWhile1 isSpace >>^ badTsnok (singleton ' ')
20+unsafeWhiteSpace1 :: (Isoparsec m s, Element s ~ Char, Eq s) => m () ()
21+unsafeWhiteSpace1 = tokensWhile1 isSpace >>^ (maskr . turn . konst $ singleton ' ')
2222
2323number :: (Isoparsec m s, Element s ~ Char) => m () Integer
2424number =
src/Data/Isoparsec/Internal.hs+4−16
@@ -3,13 +3,10 @@ module Data.Isoparsec.Internal
33 IsoparsecFail (..),
44 Isoparsec (..),
55 konst,
6− tsnok,
76 siCons,
87 isoCheck,
98 siCheck,
109 check,
11− badKonst,
12− badTsnok,
1310 unroll,
1411 arrowsWhile,
1512 )
@@ -33,7 +30,7 @@ class
3330
3431 token :: Element s -> m () ()
3532 default token :: Eq (Element s) => Element s -> m () ()
36− token x = anyToken >>^ tsnok x
33+ token x = anyToken >>^ turn (konst x)
3734
3835 tokens :: [Element s] -> m () ()
3936 default tokens :: [Element s] -> m () ()
@@ -115,17 +112,8 @@ isoCheck f a b = siCheck f (pure . a) (pure . b)
115112isoConst :: s -> a -> SemiIso s a
116113isoConst s a = SI (const $ pure a) (const $ pure s)
117114
118−konst :: Eq x => x -> SemiIso () x
119−konst x = badKonst x >>> check (== x)
120−
121−badKonst :: x -> SemiIso () x
122−badKonst x = SI (const $ pure x) (const $ pure ())
123−
124−tsnok :: Eq x => x -> SemiIso x ()
125−tsnok x = check (== x) >>> badTsnok x
126−
127−badTsnok :: x -> SemiIso x ()
128−badTsnok x = SI (const $ pure ()) (const $ pure x)
129−
130115check :: (s -> Bool) -> SemiIso s s
131116check f = isoCheck f id id
117+
118+konst :: Eq x => x -> SemiIso () x
119+konst x = SI (const $ pure x) (const $ pure ()) >>> check (== x)
test/Spec/Ssh.hs+1−1
@@ -100,7 +100,7 @@ instance ToIsoparsec Payload ByteString where
100100 )
101101 <+> ( _VersionPayload <.> chunk "SSH-2.0-"
102102 &&& (tokensWhile (`BS.notElem` " \n\r") >>> utf8)
103− &&& ( (chunk " " >>> takeUntil "\r\n" >>^ turn (badKonst ""))
103+ &&& ( (chunk " " >>> takeUntil "\r\n" >>^ (maskr . turn . konst $ ""))
104104 <+> chunk "\r\n"
105105 )
106106 )