on master help sign in
Renamed some things (#26)

* Renamed some things * unroll and arrowWhile * fixed ssh version payload * removed extra dependency

commit54a5b3b50e5d22382ac74e0644918e40caaf5ca3 authoriko <ilyakooo0@gmail.com> dateMon 10 Feb 2020, 00:10 (7 years ago) parentb145d0e
6 files changed+51−51
package.yaml−1 src/Data/Isoparsec.hs+3−3 src/Data/Isoparsec/ByteString.hs+7−11 src/Data/Isoparsec/Internal.hs+30−24 test/Spec/JSON.hs+2−2 test/Spec/Ssh.hs+9−10
package.yaml−1
@@ -77,4 +77,3 @@ tests:
7777 - QuickCheck
7878 - megaparsec
7979 - bytestring
80− - word8
src/Data/Isoparsec.hs+3−3
@@ -44,7 +44,7 @@ opt' :: (ArrowPlus m, PolyArrow m SemiIso, Eq a) => a -> m () a -> m () ()
4444opt' a m = (m >>> tsnok a) <+> konst ()
4545
4646repeating :: (PolyArrow m SemiIso, ArrowPlus m, Eq b) => m () b -> m () [b]
47−repeating m = (m &&& (repeating m <+> konst [])) >>^ cons'
47+repeating m = (m &&& (repeating m <+> konst [])) >>^ siCons
4848
4949infixr 1 %>>
5050
@@ -110,7 +110,7 @@ infixr 0 <^>
110110b <^> p = b >>> morphed %>% p
111111
112112coercing :: forall b a m. (Coercible a b, PolyArrow m SemiIso) => m a b
113−coercing = arr $ siJust coerce coerce
113+coercing = arr $ siPure coerce coerce
114114
115115morphed ::
116116 (TupleMorphable a c, TupleMorphable b c) => SemiIso a b
@@ -142,4 +142,4 @@ specific x = auto @x >>> check (== x) >>> tsnok x
142142throughIntegral ::
143143 (Integral a, Integral b, Num a, Num b, PolyArrow m SemiIso) =>
144144 m a b
145−throughIntegral = arr $ siJust fromIntegral fromIntegral
145+throughIntegral = arr $ siPure fromIntegral fromIntegral
src/Data/Isoparsec/ByteString.hs+7−11
@@ -2,7 +2,6 @@
22
33module Data.Isoparsec.ByteString
44 ( utf8,
5− ftu8,
65 ByteString,
76 Endianness (..),
87 Byte8,
@@ -21,11 +20,8 @@ import Data.Proxy
2120import Data.Word
2221import Prelude as P hiding ((.))
2322
24−utf8 :: Isoparsec m ByteString => String -> m () ()
25−utf8 = chunk . C.pack
26−
27−ftu8 :: PolyArrow m SemiIso => m ByteString String
28−ftu8 = arr $ siJust C.unpack C.pack
23+utf8 :: PolyArrow m SemiIso => m ByteString String
24+utf8 = arr $ siPure C.unpack C.pack
2925
3026data Endianness = BE | LE
3127
@@ -35,7 +31,7 @@ class BytesToIsoparsec b (e :: Endianness) where
3531instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'BE where
3632 bytesToIsoparsec _ =
3733 konst word8s >>> manyTokens
38− >>^ siJust
34+ >>^ siPure
3935 (fromInteger . BS.foldl (\i w -> shiftL i 8 .|. toInteger w) 0)
4036 ( liftTokens . snd . P.head
4137 . P.drop (fromIntegral word8s)
@@ -49,7 +45,7 @@ instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'BE where
4945instance (FiniteBits b, Integral b) => BytesToIsoparsec b 'LE where
5046 bytesToIsoparsec _ =
5147 konst word8s >>> manyTokens
52− >>^ siJust
48+ >>^ siPure
5349 (fromInteger . BS.foldr (\w i -> shiftL i 8 .|. toInteger w) 0)
5450 ( liftTokens . P.reverse . snd . P.head
5551 . P.drop (fromIntegral word8s)
@@ -89,9 +85,9 @@ newtype SSHString = SSHString {unSSHString :: String}
8985instance ToIsoparsec SSHString ByteString where
9086 toIsoparsec =
9187 auto @(Byte32 'BE) >>> coercing @Word32
92− >>> siJust fromIntegral fromIntegral ^>> manyTokens
93− >>> ftu8
94− >>^ siJust SSHString unSSHString
88+ >>> siPure fromIntegral fromIntegral ^>> manyTokens
89+ >>> utf8
90+ >>^ siPure SSHString unSSHString
9591
9692instance Tokenable ByteString where
9793 type Token ByteString = Word8
src/Data/Isoparsec/Internal.hs+30−24
@@ -4,14 +4,15 @@ module Data.Isoparsec.Internal
44 Isoparsec (..),
55 konst,
66 tsnok,
7− cons',
8− siJust,
7+ siCons,
8+ isoCheck,
99 siCheck,
10− siCheck',
1110 check,
1211 levitate,
1312 badKonst,
1413 badTsnok,
14+ unroll,
15+ arrowsWhile,
1516 )
1617where
1718
@@ -36,8 +37,8 @@ class
3637
3738 tokens :: [Token s] -> m () ()
3839 default tokens :: [Token s] -> m () ()
39− tokens [] = arr $ isoConst' () ()
40− tokens (t : ts) = token t &&& tokens ts >>> arr (isoConst' ((), ()) ())
40+ tokens [] = arr $ isoConst () ()
41+ tokens (t : ts) = token t &&& tokens ts >>> arr (isoConst ((), ()) ())
4142
4243 chunk :: s -> m () ()
4344 chunk = tokens . lowerTokens
@@ -58,15 +59,15 @@ class
5859 default takeUntil :: Eq (Token s) => s -> m () s
5960 takeUntil s = takeUntil' s >>^ levitate
6061 where
61− takeUntil' s' = (chunk s' >>> konst []) <+> ((anyToken &&& takeUntil' s') >>^ cons')
62+ takeUntil' s' = (chunk s' >>> konst []) <+> ((anyToken &&& takeUntil' s') >>^ siCons)
6263
6364 default tokensWhile :: (Token s -> Bool) -> m () s
6465 tokensWhile f =
6566 tokensWhile' f >>> check (P.all f) >>^ levitate
6667 where
6768 tokensWhile' g =
68− (tokenWhere g &&& tokensWhile' g >>^ cons')
69− <+^ isoConst' () []
69+ (tokenWhere g &&& tokensWhile' g >>^ siCons)
70+ <+^ isoConst () []
7071
7172 tokensWhile1 :: (Token s -> Bool) -> m () s
7273 tokensWhile1 f =
@@ -90,13 +91,21 @@ class
9091 -- > └─────┘ └─────┘
9192 tuck :: m () a -> m s a
9293
94+arrowsWhile :: (PolyArrow m SemiIso, ArrowPlus m) => m () a -> m () [a]
95+arrowsWhile f = ((f &&& arrowsWhile f) >>^ siCons) <+^ isoConst () []
96+
97+unroll :: (PolyArrow m SemiIso, ArrowPlus m, Eq a) => a -> m a (b, a) -> m () [b]
98+unroll a f = (konst a >>> unroll' f) <+^ isoConst () []
99+ where
100+ unroll' g = (g >>> second (unroll' g)) >>^ siCons
101+
93102levitateHead :: Alternative f => Tokenable s => s -> f (Token s, s)
94103levitateHead s = case lowerTokens s of
95104 (t : tt) -> pure (t, liftTokens tt)
96105 [] -> empty
97106
98−cons' :: SemiIso (t, [t]) [t]
99−cons' =
107+siCons :: SemiIso (t, [t]) [t]
108+siCons =
100109 SI
101110 (pure . uncurry (:))
102111 ( \case
@@ -105,35 +114,32 @@ cons' =
105114 )
106115
107116levitate :: Tokenable s => SemiIso [Token s] s
108−levitate = siJust liftTokens lowerTokens
117+levitate = siPure liftTokens lowerTokens
109118
110119class IsoparsecFail m e where
111120 failure :: e -> m a b
112121
113−siCheck' ::
122+siCheck ::
114123 (s -> Bool) ->
115124 (forall f. AlternativeMonad f => s -> f a) ->
116125 (forall f. AlternativeMonad f => a -> f s) ->
117126 SemiIso s a
118−siCheck' f a b =
127+siCheck f a b =
119128 SI
120129 (\c -> guard (f c) >> a c)
121130 (b >=> (\c -> guard (f c) >> pure c))
122131
123−siCheck :: (s -> Bool) -> (s -> a) -> (a -> s) -> SemiIso s a
124−siCheck f a b = siCheck' f (pure . a) (pure . b)
125−
126−siJust :: (s -> a) -> (a -> s) -> SemiIso s a
127−siJust a b = SI (pure . a) (pure . b)
132+isoCheck :: (s -> Bool) -> (s -> a) -> (a -> s) -> SemiIso s a
133+isoCheck f a b = siCheck f (pure . a) (pure . b)
128134
129−isoConst' :: s -> a -> SemiIso s a
130−isoConst' s a = SI (const $ pure a) (const $ pure s)
135+isoConst :: s -> a -> SemiIso s a
136+isoConst s a = SI (const $ pure a) (const $ pure s)
131137
132138konst :: (PolyArrow a SemiIso, Eq x) => x -> a () x
133−konst x = badKonst x >>> check (== x)
139+konst x = badKonst x ^>> check (== x)
134140
135−badKonst :: (PolyArrow a SemiIso) => x -> a () x
136−badKonst x = arr $ SI (const $ pure x) (const $ pure ())
141+badKonst :: x -> SemiIso () x
142+badKonst x = SI (const $ pure x) (const $ pure ())
137143
138144tsnok :: (PolyArrow a SemiIso, Eq x) => x -> a x ()
139145tsnok x = check (== x) >>> badTsnok x
@@ -142,4 +148,4 @@ badTsnok :: (PolyArrow a SemiIso) => x -> a x ()
142148badTsnok x = arr $ SI (const $ pure ()) (const $ pure x)
143149
144150check :: PolyArrow a SemiIso => (s -> Bool) -> a s s
145−check f = arr $ siCheck f id id
151+check f = arr $ isoCheck f id id
test/Spec/JSON.hs+2−2
@@ -66,7 +66,7 @@ json = SI pure pure ^<< (string <+> array <+> integer <+> object)
6666 )
6767 >>% morphed
6868 )
69− >>^ cons'
69+ >>^ siCons
7070 )
7171 <+> konst []
7272 )
@@ -76,7 +76,7 @@ json = SI pure pure ^<< (string <+> array <+> integer <+> object)
7676 object =
7777 _Object <.> token '{' &&& unsafeWhiteSpace
7878 &&& ( ( let pair = (unsafeWhiteSpace &&& string' &&& unsafeWhiteSpace &&& token ':' &&& unsafeWhiteSpace &&& json &&& unsafeWhiteSpace) >>^ morphed
79− in (pair &&& (repeating ((token ',' &&& unsafeWhiteSpace &&& pair) >>^ morphed) <+> konst [])) >>^ cons'
79+ in (pair &&& (repeating ((token ',' &&& unsafeWhiteSpace &&& pair) >>^ morphed) <+> konst [])) >>^ siCons
8080 )
8181 <+> konst []
8282 )
test/Spec/Ssh.hs+9−10
@@ -11,7 +11,6 @@ import Data.ByteString as BS
1111import qualified Data.Char as C
1212import Data.Isoparsec
1313import Data.Isoparsec.ByteString
14−import qualified Data.Word8 as W8
1514import GHC.Generics
1615import Spec.Helper
1716import Spec.Orphans ()
@@ -93,16 +92,16 @@ instance ToIsoparsec Payload ByteString where
9392 ( _DisconnectPayload
9493 <.> specific DisconnectMsg
9594 &&& auto @DisconnectReasonCode
96− &&& (tokensWhile (const True) >>> ftu8)
95+ &&& (tokensWhile (const True) >>> utf8)
9796 )
9897 <+> ( _ServiceRequest <.> specific ServiceRequestMsg
9998 &&& auto @SSHString
10099 )
101− <+> ( _VersionPayload <.> utf8 "SSH-2.0-"
102− &&& ( ( (takeUntil "\r\n" >>> check (BS.all (not . W8.isSpace)))
103− <+> ((takeUntil " " &&& (takeUntil "\r\n" >>> badTsnok "")) >>% morphed)
104− )
105− >>> ftu8
100+ <+> ( _VersionPayload <.> chunk "SSH-2.0-"
101+ &&& ( (tokensWhile (not . flip BS.elem " \n\r") >>> utf8)
102+ &&& ( (chunk " " >>> takeUntil "\r\n" >>^ turn (badKonst ""))
103+ <+> chunk "\r\n"
104+ )
106105 )
107106 )
108107 <+> (_IgnorePayload <.> specific IgnoreMsg &&& tokensWhile (const True))
@@ -127,7 +126,7 @@ badZeroPadding :: Isoparsec m ByteString => m Byte8 ZeroPadding
127126badZeroPadding =
128127 throughIntegral
129128 >>> manyTokens
130− >>^ siJust
129+ >>^ siPure
131130 (ZeroPadding . fromIntegral . BS.length)
132131 (flip BS.replicate 0 . fromIntegral . zeroPaddingLength)
133132
@@ -156,7 +155,7 @@ instance ToIsoparsec mac ByteString => ToIsoparsec (Packet mac) ByteString where
156155 toIsoparsec =
157156 ( ( (auto @(Byte32 'BE) &&& auto @Byte8)
158157 >>> (throughIntegral *** throughIntegral)
159− >>> siJust
158+ >>> siPure
160159 (\(packetL, paddingL) -> (packetL - paddingL - 1, paddingL))
161160 (\(payloadL, paddingL) -> (payloadL + paddingL + 1, paddingL))
162161 ^>> (manyTokens *** throughIntegral)
@@ -164,7 +163,7 @@ instance ToIsoparsec mac ByteString => ToIsoparsec (Packet mac) ByteString where
164163 )
165164 &&& auto @mac
166165 )
167− >>^ siJust (\((a, b), c) -> Packet a b c) (\(Packet a b c) -> ((a, b), c))
166+ >>^ siPure (\((a, b), c) -> Packet a b c) (\(Packet a b c) -> ((a, b), c))
168167
169168spec :: Spec
170169spec = do