{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Text.Megaparsec.Stream
( Stream (..),
ShareInput (..),
NoShareInput (..),
VisualStream (..),
TraversableStream (..),
)
where
import Data.Bifunctor (second)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BL8
import Data.Char (chr)
import Data.Foldable (toList)
import Data.Kind (Type)
import qualified Data.List
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromMaybe)
import Data.Proxy
import qualified Data.Sequence as S
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Word (Word8)
import Text.Megaparsec.Pos
import Text.Megaparsec.State
import qualified Text.Megaparsec.Unicode as Unicode
class (Ord (Token s), Ord (Tokens s)) => Stream s where
type Token s :: Type
type Tokens s :: Type
tokenToChunk :: Proxy s -> Token s -> Tokens s
tokenToChunk Proxy s
pxy = Proxy s -> [Token s] -> Tokens s
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk Proxy s
pxy ([Token s] -> Tokens s)
-> (Token s -> [Token s]) -> Token s -> Tokens s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Token s -> [Token s]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
tokensToChunk :: Proxy s -> [Token s] -> Tokens s
chunkToTokens :: Proxy s -> Tokens s -> [Token s]
chunkLength :: Proxy s -> Tokens s -> Int
chunkEmpty :: Proxy s -> Tokens s -> Bool
chunkEmpty Proxy s
pxy Tokens s
ts = Proxy s -> Tokens s -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength Proxy s
pxy Tokens s
ts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0
take1_ :: s -> Maybe (Token s, s)
takeN_ :: Int -> s -> Maybe (Tokens s, s)
takeWhile_ :: (Token s -> Bool) -> s -> (Tokens s, s)
instance (Ord a) => Stream [a] where
type Token [a] = a
type Tokens [a] = [a]
tokenToChunk :: Proxy [a] -> Token [a] -> Tokens [a]
tokenToChunk Proxy [a]
Proxy = a -> [a]
Token [a] -> Tokens [a]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
tokensToChunk :: Proxy [a] -> [Token [a]] -> Tokens [a]
tokensToChunk Proxy [a]
Proxy = [a] -> [a]
[Token [a]] -> Tokens [a]
forall a. a -> a
id
chunkToTokens :: Proxy [a] -> Tokens [a] -> [Token [a]]
chunkToTokens Proxy [a]
Proxy = [a] -> [a]
Tokens [a] -> [Token [a]]
forall a. a -> a
id
chunkLength :: Proxy [a] -> Tokens [a] -> Int
chunkLength Proxy [a]
Proxy = [a] -> Int
Tokens [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
chunkEmpty :: Proxy [a] -> Tokens [a] -> Bool
chunkEmpty Proxy [a]
Proxy = [a] -> Bool
Tokens [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
take1_ :: [a] -> Maybe (Token [a], [a])
take1_ [] = Maybe (a, [a])
Maybe (Token [a], [a])
forall a. Maybe a
Nothing
take1_ (a
t : [a]
ts) = (a, [a]) -> Maybe (a, [a])
forall a. a -> Maybe a
Just (a
t, [a]
ts)
takeN_ :: Int -> [a] -> Maybe (Tokens [a], [a])
takeN_ Int
n [a]
s
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just ([], [a]
s)
| [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
s = Maybe ([a], [a])
Maybe (Tokens [a], [a])
forall a. Maybe a
Nothing
| Bool
otherwise = ([a], [a]) -> Maybe ([a], [a])
forall a. a -> Maybe a
Just (Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
s)
takeWhile_ :: (Token [a] -> Bool) -> [a] -> (Tokens [a], [a])
takeWhile_ = (a -> Bool) -> [a] -> ([a], [a])
(Token [a] -> Bool) -> [a] -> (Tokens [a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span
instance (Ord a) => Stream (S.Seq a) where
type Token (S.Seq a) = a
type Tokens (S.Seq a) = S.Seq a
tokenToChunk :: Proxy (Seq a) -> Token (Seq a) -> Tokens (Seq a)
tokenToChunk Proxy (Seq a)
Proxy = a -> Seq a
Token (Seq a) -> Tokens (Seq a)
forall a. a -> Seq a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
tokensToChunk :: Proxy (Seq a) -> [Token (Seq a)] -> Tokens (Seq a)
tokensToChunk Proxy (Seq a)
Proxy = [a] -> Seq a
[Token (Seq a)] -> Tokens (Seq a)
forall a. [a] -> Seq a
S.fromList
chunkToTokens :: Proxy (Seq a) -> Tokens (Seq a) -> [Token (Seq a)]
chunkToTokens Proxy (Seq a)
Proxy = Tokens (Seq a) -> [Token (Seq a)]
Seq a -> [a]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
chunkLength :: Proxy (Seq a) -> Tokens (Seq a) -> Int
chunkLength Proxy (Seq a)
Proxy = Tokens (Seq a) -> Int
Seq a -> Int
forall a. Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length
chunkEmpty :: Proxy (Seq a) -> Tokens (Seq a) -> Bool
chunkEmpty Proxy (Seq a)
Proxy = Tokens (Seq a) -> Bool
Seq a -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null
take1_ :: Seq a -> Maybe (Token (Seq a), Seq a)
take1_ Seq a
S.Empty = Maybe (a, Seq a)
Maybe (Token (Seq a), Seq a)
forall a. Maybe a
Nothing
take1_ (a
t S.:<| Seq a
ts) = (a, Seq a) -> Maybe (a, Seq a)
forall a. a -> Maybe a
Just (a
t, Seq a
ts)
takeN_ :: Int -> Seq a -> Maybe (Tokens (Seq a), Seq a)
takeN_ Int
n Seq a
s
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Seq a, Seq a) -> Maybe (Seq a, Seq a)
forall a. a -> Maybe a
Just (Seq a
forall a. Seq a
S.empty, Seq a
s)
| Seq a -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
s = Maybe (Tokens (Seq a), Seq a)
Maybe (Seq a, Seq a)
forall a. Maybe a
Nothing
| Bool
otherwise = (Seq a, Seq a) -> Maybe (Seq a, Seq a)
forall a. a -> Maybe a
Just (Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
S.splitAt Int
n Seq a
s)
takeWhile_ :: (Token (Seq a) -> Bool) -> Seq a -> (Tokens (Seq a), Seq a)
takeWhile_ = (a -> Bool) -> Seq a -> (Seq a, Seq a)
(Token (Seq a) -> Bool) -> Seq a -> (Tokens (Seq a), Seq a)
forall a. (a -> Bool) -> Seq a -> (Seq a, Seq a)
S.spanl
newtype ShareInput a = ShareInput {forall a. ShareInput a -> a
unShareInput :: a}
instance Stream (ShareInput B.ByteString) where
type Token (ShareInput B.ByteString) = Word8
type Tokens (ShareInput B.ByteString) = B.ByteString
tokenToChunk :: Proxy (ShareInput ByteString)
-> Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
tokenToChunk Proxy (ShareInput ByteString)
Proxy = Word8 -> ByteString
Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
B.singleton
tokensToChunk :: Proxy (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
-> Tokens (ShareInput ByteString)
tokensToChunk Proxy (ShareInput ByteString)
Proxy = [Word8] -> ByteString
[Token (ShareInput ByteString)] -> Tokens (ShareInput ByteString)
B.pack
chunkToTokens :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
chunkToTokens Proxy (ShareInput ByteString)
Proxy = Tokens (ShareInput ByteString) -> [Token (ShareInput ByteString)]
ByteString -> [Word8]
B.unpack
chunkLength :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Int
chunkLength Proxy (ShareInput ByteString)
Proxy = Tokens (ShareInput ByteString) -> Int
ByteString -> Int
B.length
chunkEmpty :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Bool
chunkEmpty Proxy (ShareInput ByteString)
Proxy = Tokens (ShareInput ByteString) -> Bool
ByteString -> Bool
B.null
take1_ :: ShareInput ByteString
-> Maybe (Token (ShareInput ByteString), ShareInput ByteString)
take1_ (ShareInput ByteString
s) = (ByteString -> ShareInput ByteString)
-> (Word8, ByteString) -> (Word8, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((Word8, ByteString) -> (Word8, ShareInput ByteString))
-> Maybe (Word8, ByteString)
-> Maybe (Word8, ShareInput ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe (Word8, ByteString)
B.uncons ByteString
s
takeN_ :: Int
-> ShareInput ByteString
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
takeN_ Int
n (ShareInput ByteString
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (ByteString, ShareInput ByteString)
-> Maybe (ByteString, ShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
B.empty, ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
| ByteString -> Bool
B.null ByteString
s = Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
Maybe (ByteString, ShareInput ByteString)
forall a. Maybe a
Nothing
| Bool
otherwise = (ByteString, ShareInput ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
(ByteString, ShareInput ByteString)
-> Maybe (ByteString, ShareInput ByteString)
forall a. a -> Maybe a
Just ((ByteString, ShareInput ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString))
-> ((ByteString, ByteString)
-> (ByteString, ShareInput ByteString))
-> (ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ShareInput ByteString)
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString))
-> (ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
n ByteString
s
takeWhile_ :: (Token (ShareInput ByteString) -> Bool)
-> ShareInput ByteString
-> (Tokens (ShareInput ByteString), ShareInput ByteString)
takeWhile_ Token (ShareInput ByteString) -> Bool
p (ShareInput ByteString
s) = (ByteString -> ShareInput ByteString)
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((ByteString, ByteString) -> (ByteString, ShareInput ByteString))
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall a b. (a -> b) -> a -> b
$ (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
B.span Word8 -> Bool
Token (ShareInput ByteString) -> Bool
p ByteString
s
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream (ShareInput BL.ByteString) where
type Token (ShareInput BL.ByteString) = Word8
type Tokens (ShareInput BL.ByteString) = BL.ByteString
tokenToChunk :: Proxy (ShareInput ByteString)
-> Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
tokenToChunk Proxy (ShareInput ByteString)
Proxy = Word8 -> ByteString
Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
BL.singleton
tokensToChunk :: Proxy (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
-> Tokens (ShareInput ByteString)
tokensToChunk Proxy (ShareInput ByteString)
Proxy = [Word8] -> ByteString
[Token (ShareInput ByteString)] -> Tokens (ShareInput ByteString)
BL.pack
chunkToTokens :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
chunkToTokens Proxy (ShareInput ByteString)
Proxy = Tokens (ShareInput ByteString) -> [Token (ShareInput ByteString)]
ByteString -> [Word8]
BL.unpack
chunkLength :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Int
chunkLength Proxy (ShareInput ByteString)
Proxy = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (ByteString -> Int64) -> ByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BL.length
chunkEmpty :: Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Bool
chunkEmpty Proxy (ShareInput ByteString)
Proxy = Tokens (ShareInput ByteString) -> Bool
ByteString -> Bool
BL.null
take1_ :: ShareInput ByteString
-> Maybe (Token (ShareInput ByteString), ShareInput ByteString)
take1_ (ShareInput ByteString
s) = (ByteString -> ShareInput ByteString)
-> (Word8, ByteString) -> (Word8, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((Word8, ByteString) -> (Word8, ShareInput ByteString))
-> Maybe (Word8, ByteString)
-> Maybe (Word8, ShareInput ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe (Word8, ByteString)
BL.uncons ByteString
s
takeN_ :: Int
-> ShareInput ByteString
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
takeN_ Int
n (ShareInput ByteString
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (ByteString, ShareInput ByteString)
-> Maybe (ByteString, ShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
BL.empty, ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
| ByteString -> Bool
BL.null ByteString
s = Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
Maybe (ByteString, ShareInput ByteString)
forall a. Maybe a
Nothing
| Bool
otherwise = (ByteString, ShareInput ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
(ByteString, ShareInput ByteString)
-> Maybe (ByteString, ShareInput ByteString)
forall a. a -> Maybe a
Just ((ByteString, ShareInput ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString))
-> ((ByteString, ByteString)
-> (ByteString, ShareInput ByteString))
-> (ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ShareInput ByteString)
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString))
-> (ByteString, ByteString)
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall a b. (a -> b) -> a -> b
$ Int64 -> ByteString -> (ByteString, ByteString)
BL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) ByteString
s
takeWhile_ :: (Token (ShareInput ByteString) -> Bool)
-> ShareInput ByteString
-> (Tokens (ShareInput ByteString), ShareInput ByteString)
takeWhile_ Token (ShareInput ByteString) -> Bool
p (ShareInput ByteString
s) = (ByteString -> ShareInput ByteString)
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ((ByteString, ByteString) -> (ByteString, ShareInput ByteString))
-> (ByteString, ByteString) -> (ByteString, ShareInput ByteString)
forall a b. (a -> b) -> a -> b
$ (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BL.span Word8 -> Bool
Token (ShareInput ByteString) -> Bool
p ByteString
s
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream (ShareInput T.Text) where
type Token (ShareInput T.Text) = Char
type Tokens (ShareInput T.Text) = T.Text
tokenToChunk :: Proxy (ShareInput Text)
-> Token (ShareInput Text) -> Tokens (ShareInput Text)
tokenToChunk Proxy (ShareInput Text)
Proxy = Char -> Text
Token (ShareInput Text) -> Tokens (ShareInput Text)
T.singleton
tokensToChunk :: Proxy (ShareInput Text)
-> [Token (ShareInput Text)] -> Tokens (ShareInput Text)
tokensToChunk Proxy (ShareInput Text)
Proxy = [Char] -> Text
[Token (ShareInput Text)] -> Tokens (ShareInput Text)
T.pack
chunkToTokens :: Proxy (ShareInput Text)
-> Tokens (ShareInput Text) -> [Token (ShareInput Text)]
chunkToTokens Proxy (ShareInput Text)
Proxy = Tokens (ShareInput Text) -> [Token (ShareInput Text)]
Text -> [Char]
T.unpack
chunkLength :: Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Int
chunkLength Proxy (ShareInput Text)
Proxy = Tokens (ShareInput Text) -> Int
Text -> Int
T.length
chunkEmpty :: Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Bool
chunkEmpty Proxy (ShareInput Text)
Proxy = Tokens (ShareInput Text) -> Bool
Text -> Bool
T.null
take1_ :: ShareInput Text -> Maybe (Token (ShareInput Text), ShareInput Text)
take1_ (ShareInput Text
s) = (Text -> ShareInput Text)
-> (Char, Text) -> (Char, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Char, Text) -> (Char, ShareInput Text))
-> Maybe (Char, Text) -> Maybe (Char, ShareInput Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Char, Text)
T.uncons Text
s
takeN_ :: Int
-> ShareInput Text
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
takeN_ Int
n (ShareInput Text
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Text, ShareInput Text) -> Maybe (Text, ShareInput Text)
forall a. a -> Maybe a
Just (Text
T.empty, Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
| Text -> Bool
T.null Text
s = Maybe (Tokens (ShareInput Text), ShareInput Text)
Maybe (Text, ShareInput Text)
forall a. Maybe a
Nothing
| Bool
otherwise = (Text, ShareInput Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
(Text, ShareInput Text) -> Maybe (Text, ShareInput Text)
forall a. a -> Maybe a
Just ((Text, ShareInput Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text))
-> ((Text, Text) -> (Text, ShareInput Text))
-> (Text, Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> ShareInput Text)
-> (Text, Text) -> (Text, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Text, Text) -> Maybe (Tokens (ShareInput Text), ShareInput Text))
-> (Text, Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall a b. (a -> b) -> a -> b
$ Int -> Text -> (Text, Text)
T.splitAt Int
n Text
s
takeWhile_ :: (Token (ShareInput Text) -> Bool)
-> ShareInput Text -> (Tokens (ShareInput Text), ShareInput Text)
takeWhile_ Token (ShareInput Text) -> Bool
p (ShareInput Text
s) = (Text -> ShareInput Text)
-> (Text, Text) -> (Text, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Text, Text) -> (Text, ShareInput Text))
-> (Text, Text) -> (Text, ShareInput Text)
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
Token (ShareInput Text) -> Bool
p Text
s
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream (ShareInput TL.Text) where
type Token (ShareInput TL.Text) = Char
type Tokens (ShareInput TL.Text) = TL.Text
tokenToChunk :: Proxy (ShareInput Text)
-> Token (ShareInput Text) -> Tokens (ShareInput Text)
tokenToChunk Proxy (ShareInput Text)
Proxy = Char -> Text
Token (ShareInput Text) -> Tokens (ShareInput Text)
TL.singleton
tokensToChunk :: Proxy (ShareInput Text)
-> [Token (ShareInput Text)] -> Tokens (ShareInput Text)
tokensToChunk Proxy (ShareInput Text)
Proxy = [Char] -> Text
[Token (ShareInput Text)] -> Tokens (ShareInput Text)
TL.pack
chunkToTokens :: Proxy (ShareInput Text)
-> Tokens (ShareInput Text) -> [Token (ShareInput Text)]
chunkToTokens Proxy (ShareInput Text)
Proxy = Tokens (ShareInput Text) -> [Token (ShareInput Text)]
Text -> [Char]
TL.unpack
chunkLength :: Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Int
chunkLength Proxy (ShareInput Text)
Proxy = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (Text -> Int64) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int64
TL.length
chunkEmpty :: Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Bool
chunkEmpty Proxy (ShareInput Text)
Proxy = Tokens (ShareInput Text) -> Bool
Text -> Bool
TL.null
take1_ :: ShareInput Text -> Maybe (Token (ShareInput Text), ShareInput Text)
take1_ (ShareInput Text
s) = (Text -> ShareInput Text)
-> (Char, Text) -> (Char, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Char, Text) -> (Char, ShareInput Text))
-> Maybe (Char, Text) -> Maybe (Char, ShareInput Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Char, Text)
TL.uncons Text
s
takeN_ :: Int
-> ShareInput Text
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
takeN_ Int
n (ShareInput Text
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Text, ShareInput Text) -> Maybe (Text, ShareInput Text)
forall a. a -> Maybe a
Just (Text
TL.empty, Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
| Text -> Bool
TL.null Text
s = Maybe (Tokens (ShareInput Text), ShareInput Text)
Maybe (Text, ShareInput Text)
forall a. Maybe a
Nothing
| Bool
otherwise = (Text, ShareInput Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
(Text, ShareInput Text) -> Maybe (Text, ShareInput Text)
forall a. a -> Maybe a
Just ((Text, ShareInput Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text))
-> ((Text, Text) -> (Text, ShareInput Text))
-> (Text, Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> ShareInput Text)
-> (Text, Text) -> (Text, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Text, Text) -> Maybe (Tokens (ShareInput Text), ShareInput Text))
-> (Text, Text)
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall a b. (a -> b) -> a -> b
$ Int64 -> Text -> (Text, Text)
TL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) Text
s
takeWhile_ :: (Token (ShareInput Text) -> Bool)
-> ShareInput Text -> (Tokens (ShareInput Text), ShareInput Text)
takeWhile_ Token (ShareInput Text) -> Bool
p (ShareInput Text
s) = (Text -> ShareInput Text)
-> (Text, Text) -> (Text, ShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput ((Text, Text) -> (Text, ShareInput Text))
-> (Text, Text) -> (Text, ShareInput Text)
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> (Text, Text)
TL.span Char -> Bool
Token (ShareInput Text) -> Bool
p Text
s
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
newtype NoShareInput a = NoShareInput {forall a. NoShareInput a -> a
unNoShareInput :: a}
instance Stream (NoShareInput B.ByteString) where
type Token (NoShareInput B.ByteString) = Word8
type Tokens (NoShareInput B.ByteString) = B.ByteString
tokenToChunk :: Proxy (NoShareInput ByteString)
-> Token (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString)
tokenToChunk Proxy (NoShareInput ByteString)
Proxy = Word8 -> ByteString
Token (NoShareInput ByteString) -> Tokens (NoShareInput ByteString)
B.singleton
tokensToChunk :: Proxy (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
-> Tokens (NoShareInput ByteString)
tokensToChunk Proxy (NoShareInput ByteString)
Proxy = [Word8] -> ByteString
[Token (NoShareInput ByteString)]
-> Tokens (NoShareInput ByteString)
B.pack
chunkToTokens :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
chunkToTokens Proxy (NoShareInput ByteString)
Proxy = Tokens (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
ByteString -> [Word8]
B.unpack
chunkLength :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString) -> Int
chunkLength Proxy (NoShareInput ByteString)
Proxy = Tokens (NoShareInput ByteString) -> Int
ByteString -> Int
B.length
chunkEmpty :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString) -> Bool
chunkEmpty Proxy (NoShareInput ByteString)
Proxy = Tokens (NoShareInput ByteString) -> Bool
ByteString -> Bool
B.null
take1_ :: NoShareInput ByteString
-> Maybe (Token (NoShareInput ByteString), NoShareInput ByteString)
take1_ (NoShareInput ByteString
s) = (ByteString -> NoShareInput ByteString)
-> (Word8, ByteString) -> (Word8, NoShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ((Word8, ByteString) -> (Word8, NoShareInput ByteString))
-> Maybe (Word8, ByteString)
-> Maybe (Word8, NoShareInput ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe (Word8, ByteString)
B.uncons ByteString
s
takeN_ :: Int
-> NoShareInput ByteString
-> Maybe
(Tokens (NoShareInput ByteString), NoShareInput ByteString)
takeN_ Int
n (NoShareInput ByteString
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (ByteString, NoShareInput ByteString)
-> Maybe (ByteString, NoShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
B.empty, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
s)
| ByteString -> Bool
B.null ByteString
s = Maybe (Tokens (NoShareInput ByteString), NoShareInput ByteString)
Maybe (ByteString, NoShareInput ByteString)
forall a. Maybe a
Nothing
| Bool
otherwise =
let (ByteString
result, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
n ByteString
s
unSharedResult :: ByteString
unSharedResult = ByteString -> ByteString
B.copy ByteString
result
in (ByteString, NoShareInput ByteString)
-> Maybe (ByteString, NoShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
unSharedResult, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
rest)
takeWhile_ :: (Token (NoShareInput ByteString) -> Bool)
-> NoShareInput ByteString
-> (Tokens (NoShareInput ByteString), NoShareInput ByteString)
takeWhile_ Token (NoShareInput ByteString) -> Bool
p (NoShareInput ByteString
s) =
let (ByteString
result, ByteString
rest) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
B.span Word8 -> Bool
Token (NoShareInput ByteString) -> Bool
p ByteString
s
unSharedResult :: ByteString
unSharedResult = ByteString -> ByteString
B.copy ByteString
result
in (Tokens (NoShareInput ByteString)
ByteString
unSharedResult, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
rest)
instance Stream (NoShareInput BL.ByteString) where
type Token (NoShareInput BL.ByteString) = Word8
type Tokens (NoShareInput BL.ByteString) = BL.ByteString
tokenToChunk :: Proxy (NoShareInput ByteString)
-> Token (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString)
tokenToChunk Proxy (NoShareInput ByteString)
Proxy = Word8 -> ByteString
Token (NoShareInput ByteString) -> Tokens (NoShareInput ByteString)
BL.singleton
tokensToChunk :: Proxy (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
-> Tokens (NoShareInput ByteString)
tokensToChunk Proxy (NoShareInput ByteString)
Proxy = [Word8] -> ByteString
[Token (NoShareInput ByteString)]
-> Tokens (NoShareInput ByteString)
BL.pack
chunkToTokens :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
chunkToTokens Proxy (NoShareInput ByteString)
Proxy = Tokens (NoShareInput ByteString)
-> [Token (NoShareInput ByteString)]
ByteString -> [Word8]
BL.unpack
chunkLength :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString) -> Int
chunkLength Proxy (NoShareInput ByteString)
Proxy = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (ByteString -> Int64) -> ByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int64
BL.length
chunkEmpty :: Proxy (NoShareInput ByteString)
-> Tokens (NoShareInput ByteString) -> Bool
chunkEmpty Proxy (NoShareInput ByteString)
Proxy = Tokens (NoShareInput ByteString) -> Bool
ByteString -> Bool
BL.null
take1_ :: NoShareInput ByteString
-> Maybe (Token (NoShareInput ByteString), NoShareInput ByteString)
take1_ (NoShareInput ByteString
s) = (ByteString -> NoShareInput ByteString)
-> (Word8, ByteString) -> (Word8, NoShareInput ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ((Word8, ByteString) -> (Word8, NoShareInput ByteString))
-> Maybe (Word8, ByteString)
-> Maybe (Word8, NoShareInput ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Maybe (Word8, ByteString)
BL.uncons ByteString
s
takeN_ :: Int
-> NoShareInput ByteString
-> Maybe
(Tokens (NoShareInput ByteString), NoShareInput ByteString)
takeN_ Int
n (NoShareInput ByteString
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (ByteString, NoShareInput ByteString)
-> Maybe (ByteString, NoShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
BL.empty, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
s)
| ByteString -> Bool
BL.null ByteString
s = Maybe (Tokens (NoShareInput ByteString), NoShareInput ByteString)
Maybe (ByteString, NoShareInput ByteString)
forall a. Maybe a
Nothing
| Bool
otherwise =
let (ByteString
result, ByteString
rest) = Int64 -> ByteString -> (ByteString, ByteString)
BL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) ByteString
s
unSharedResult :: ByteString
unSharedResult = ByteString -> ByteString
BL.copy ByteString
result
in (ByteString, NoShareInput ByteString)
-> Maybe (ByteString, NoShareInput ByteString)
forall a. a -> Maybe a
Just (ByteString
unSharedResult, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
rest)
takeWhile_ :: (Token (NoShareInput ByteString) -> Bool)
-> NoShareInput ByteString
-> (Tokens (NoShareInput ByteString), NoShareInput ByteString)
takeWhile_ Token (NoShareInput ByteString) -> Bool
p (NoShareInput ByteString
s) =
let (ByteString
result, ByteString
rest) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
BL.span Word8 -> Bool
Token (NoShareInput ByteString) -> Bool
p ByteString
s
unSharedResult :: ByteString
unSharedResult = ByteString -> ByteString
BL.copy ByteString
result
in (Tokens (NoShareInput ByteString)
ByteString
unSharedResult, ByteString -> NoShareInput ByteString
forall a. a -> NoShareInput a
NoShareInput ByteString
rest)
instance Stream (NoShareInput T.Text) where
type Token (NoShareInput T.Text) = Char
type Tokens (NoShareInput T.Text) = T.Text
tokenToChunk :: Proxy (NoShareInput Text)
-> Token (NoShareInput Text) -> Tokens (NoShareInput Text)
tokenToChunk Proxy (NoShareInput Text)
Proxy = Char -> Text
Token (NoShareInput Text) -> Tokens (NoShareInput Text)
T.singleton
tokensToChunk :: Proxy (NoShareInput Text)
-> [Token (NoShareInput Text)] -> Tokens (NoShareInput Text)
tokensToChunk Proxy (NoShareInput Text)
Proxy = [Char] -> Text
[Token (NoShareInput Text)] -> Tokens (NoShareInput Text)
T.pack
chunkToTokens :: Proxy (NoShareInput Text)
-> Tokens (NoShareInput Text) -> [Token (NoShareInput Text)]
chunkToTokens Proxy (NoShareInput Text)
Proxy = Tokens (NoShareInput Text) -> [Token (NoShareInput Text)]
Text -> [Char]
T.unpack
chunkLength :: Proxy (NoShareInput Text) -> Tokens (NoShareInput Text) -> Int
chunkLength Proxy (NoShareInput Text)
Proxy = Tokens (NoShareInput Text) -> Int
Text -> Int
T.length
chunkEmpty :: Proxy (NoShareInput Text) -> Tokens (NoShareInput Text) -> Bool
chunkEmpty Proxy (NoShareInput Text)
Proxy = Tokens (NoShareInput Text) -> Bool
Text -> Bool
T.null
take1_ :: NoShareInput Text
-> Maybe (Token (NoShareInput Text), NoShareInput Text)
take1_ (NoShareInput Text
s) = (Text -> NoShareInput Text)
-> (Char, Text) -> (Char, NoShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput ((Char, Text) -> (Char, NoShareInput Text))
-> Maybe (Char, Text) -> Maybe (Char, NoShareInput Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Char, Text)
T.uncons Text
s
takeN_ :: Int
-> NoShareInput Text
-> Maybe (Tokens (NoShareInput Text), NoShareInput Text)
takeN_ Int
n (NoShareInput Text
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Text, NoShareInput Text) -> Maybe (Text, NoShareInput Text)
forall a. a -> Maybe a
Just (Text
T.empty, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
s)
| Text -> Bool
T.null Text
s = Maybe (Tokens (NoShareInput Text), NoShareInput Text)
Maybe (Text, NoShareInput Text)
forall a. Maybe a
Nothing
| Bool
otherwise =
let (Text
result, Text
rest) = Int -> Text -> (Text, Text)
T.splitAt Int
n Text
s
unSharedResult :: Text
unSharedResult = Text -> Text
T.copy Text
result
in (Text, NoShareInput Text) -> Maybe (Text, NoShareInput Text)
forall a. a -> Maybe a
Just (Text
unSharedResult, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
rest)
takeWhile_ :: (Token (NoShareInput Text) -> Bool)
-> NoShareInput Text
-> (Tokens (NoShareInput Text), NoShareInput Text)
takeWhile_ Token (NoShareInput Text) -> Bool
p (NoShareInput Text
s) =
let (Text
result, Text
rest) = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
Token (NoShareInput Text) -> Bool
p Text
s
unSharedResult :: Text
unSharedResult = Text -> Text
T.copy Text
result
in (Tokens (NoShareInput Text)
Text
unSharedResult, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
rest)
instance Stream (NoShareInput TL.Text) where
type Token (NoShareInput TL.Text) = Char
type Tokens (NoShareInput TL.Text) = TL.Text
tokenToChunk :: Proxy (NoShareInput Text)
-> Token (NoShareInput Text) -> Tokens (NoShareInput Text)
tokenToChunk Proxy (NoShareInput Text)
Proxy = Char -> Text
Token (NoShareInput Text) -> Tokens (NoShareInput Text)
TL.singleton
tokensToChunk :: Proxy (NoShareInput Text)
-> [Token (NoShareInput Text)] -> Tokens (NoShareInput Text)
tokensToChunk Proxy (NoShareInput Text)
Proxy = [Char] -> Text
[Token (NoShareInput Text)] -> Tokens (NoShareInput Text)
TL.pack
chunkToTokens :: Proxy (NoShareInput Text)
-> Tokens (NoShareInput Text) -> [Token (NoShareInput Text)]
chunkToTokens Proxy (NoShareInput Text)
Proxy = Tokens (NoShareInput Text) -> [Token (NoShareInput Text)]
Text -> [Char]
TL.unpack
chunkLength :: Proxy (NoShareInput Text) -> Tokens (NoShareInput Text) -> Int
chunkLength Proxy (NoShareInput Text)
Proxy = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64 -> Int) -> (Text -> Int64) -> Text -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int64
TL.length
chunkEmpty :: Proxy (NoShareInput Text) -> Tokens (NoShareInput Text) -> Bool
chunkEmpty Proxy (NoShareInput Text)
Proxy = Tokens (NoShareInput Text) -> Bool
Text -> Bool
TL.null
take1_ :: NoShareInput Text
-> Maybe (Token (NoShareInput Text), NoShareInput Text)
take1_ (NoShareInput Text
s) = (Text -> NoShareInput Text)
-> (Char, Text) -> (Char, NoShareInput Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput ((Char, Text) -> (Char, NoShareInput Text))
-> Maybe (Char, Text) -> Maybe (Char, NoShareInput Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe (Char, Text)
TL.uncons Text
s
takeN_ :: Int
-> NoShareInput Text
-> Maybe (Tokens (NoShareInput Text), NoShareInput Text)
takeN_ Int
n (NoShareInput Text
s)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = (Text, NoShareInput Text) -> Maybe (Text, NoShareInput Text)
forall a. a -> Maybe a
Just (Text
TL.empty, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
s)
| Text -> Bool
TL.null Text
s = Maybe (Tokens (NoShareInput Text), NoShareInput Text)
Maybe (Text, NoShareInput Text)
forall a. Maybe a
Nothing
| Bool
otherwise =
let (Text
result, Text
rest) = Int64 -> Text -> (Text, Text)
TL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n) Text
s
unSharedResult :: Text
unSharedResult = Text -> Text
tlCopy Text
result
in (Text, NoShareInput Text) -> Maybe (Text, NoShareInput Text)
forall a. a -> Maybe a
Just (Text
unSharedResult, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
rest)
takeWhile_ :: (Token (NoShareInput Text) -> Bool)
-> NoShareInput Text
-> (Tokens (NoShareInput Text), NoShareInput Text)
takeWhile_ Token (NoShareInput Text) -> Bool
p (NoShareInput Text
s) =
let (Text
result, Text
rest) = (Char -> Bool) -> Text -> (Text, Text)
TL.span Char -> Bool
Token (NoShareInput Text) -> Bool
p Text
s
unSharedResult :: Text
unSharedResult = Text -> Text
tlCopy Text
result
in (Tokens (NoShareInput Text)
Text
unSharedResult, Text -> NoShareInput Text
forall a. a -> NoShareInput a
NoShareInput Text
rest)
tlCopy :: TL.Text -> TL.Text
tlCopy :: Text -> Text
tlCopy = Text -> Text
TL.fromStrict (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.copy (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
TL.toStrict
{-# INLINE tlCopy #-}
instance Stream B.ByteString where
type Token B.ByteString = Token (ShareInput B.ByteString)
type Tokens B.ByteString = Tokens (ShareInput B.ByteString)
tokenToChunk :: Proxy ByteString -> Token ByteString -> Tokens ByteString
tokenToChunk Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput B.ByteString))
tokensToChunk :: Proxy ByteString -> [Token ByteString] -> Tokens ByteString
tokensToChunk Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
-> Tokens (ShareInput ByteString)
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput B.ByteString))
chunkToTokens :: Proxy ByteString -> Tokens ByteString -> [Token ByteString]
chunkToTokens Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput B.ByteString))
chunkLength :: Proxy ByteString -> Tokens ByteString -> Int
chunkLength Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput B.ByteString))
chunkEmpty :: Proxy ByteString -> Tokens ByteString -> Bool
chunkEmpty Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Bool
forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput B.ByteString))
take1_ :: ByteString -> Maybe (Token ByteString, ByteString)
take1_ ByteString
s = (ShareInput ByteString -> ByteString)
-> (Word8, ShareInput ByteString) -> (Word8, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((Word8, ShareInput ByteString) -> (Word8, ByteString))
-> Maybe (Word8, ShareInput ByteString)
-> Maybe (Word8, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShareInput ByteString
-> Maybe (Token (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
takeN_ :: Int -> ByteString -> Maybe (Tokens ByteString, ByteString)
takeN_ Int
n ByteString
s = (ShareInput ByteString -> ByteString)
-> (ByteString, ShareInput ByteString) -> (ByteString, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((ByteString, ShareInput ByteString) -> (ByteString, ByteString))
-> Maybe (ByteString, ShareInput ByteString)
-> Maybe (ByteString, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> ShareInput ByteString
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
n (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
takeWhile_ :: (Token ByteString -> Bool)
-> ByteString -> (Tokens ByteString, ByteString)
takeWhile_ Token ByteString -> Bool
p ByteString
s = (ShareInput ByteString -> ByteString)
-> (Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString))
-> (Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ (Token (ShareInput ByteString) -> Bool)
-> ShareInput ByteString
-> (Tokens (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token (ShareInput ByteString) -> Bool
Token ByteString -> Bool
p (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream BL.ByteString where
type Token BL.ByteString = Token (ShareInput BL.ByteString)
type Tokens BL.ByteString = Tokens (ShareInput BL.ByteString)
tokenToChunk :: Proxy ByteString -> Token ByteString -> Tokens ByteString
tokenToChunk Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Token (ShareInput ByteString) -> Tokens (ShareInput ByteString)
forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput BL.ByteString))
tokensToChunk :: Proxy ByteString -> [Token ByteString] -> Tokens ByteString
tokensToChunk Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
-> Tokens (ShareInput ByteString)
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput BL.ByteString))
chunkToTokens :: Proxy ByteString -> Tokens ByteString -> [Token ByteString]
chunkToTokens Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString)
-> [Token (ShareInput ByteString)]
forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput BL.ByteString))
chunkLength :: Proxy ByteString -> Tokens ByteString -> Int
chunkLength Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput BL.ByteString))
chunkEmpty :: Proxy ByteString -> Tokens ByteString -> Bool
chunkEmpty Proxy ByteString
Proxy = Proxy (ShareInput ByteString)
-> Tokens (ShareInput ByteString) -> Bool
forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty (Proxy (ShareInput ByteString)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput BL.ByteString))
take1_ :: ByteString -> Maybe (Token ByteString, ByteString)
take1_ ByteString
s = (ShareInput ByteString -> ByteString)
-> (Word8, ShareInput ByteString) -> (Word8, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((Word8, ShareInput ByteString) -> (Word8, ByteString))
-> Maybe (Word8, ShareInput ByteString)
-> Maybe (Word8, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShareInput ByteString
-> Maybe (Token (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
takeN_ :: Int -> ByteString -> Maybe (Tokens ByteString, ByteString)
takeN_ Int
n ByteString
s = (ShareInput ByteString -> ByteString)
-> (ByteString, ShareInput ByteString) -> (ByteString, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((ByteString, ShareInput ByteString) -> (ByteString, ByteString))
-> Maybe (ByteString, ShareInput ByteString)
-> Maybe (ByteString, ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> ShareInput ByteString
-> Maybe (Tokens (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
n (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
takeWhile_ :: (Token ByteString -> Bool)
-> ByteString -> (Tokens ByteString, ByteString)
takeWhile_ Token ByteString -> Bool
p ByteString
s = (ShareInput ByteString -> ByteString)
-> (Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput ByteString -> ByteString
forall a. ShareInput a -> a
unShareInput ((Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString))
-> (Tokens ByteString, ShareInput ByteString)
-> (Tokens ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ (Token (ShareInput ByteString) -> Bool)
-> ShareInput ByteString
-> (Tokens (ShareInput ByteString), ShareInput ByteString)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token (ShareInput ByteString) -> Bool
Token ByteString -> Bool
p (ByteString -> ShareInput ByteString
forall a. a -> ShareInput a
ShareInput ByteString
s)
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream T.Text where
type Token T.Text = Token (ShareInput T.Text)
type Tokens T.Text = Tokens (ShareInput T.Text)
tokenToChunk :: Proxy Text -> Token Text -> Tokens Text
tokenToChunk Proxy Text
Proxy = Proxy (ShareInput Text)
-> Token (ShareInput Text) -> Tokens (ShareInput Text)
forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput T.Text))
tokensToChunk :: Proxy Text -> [Token Text] -> Tokens Text
tokensToChunk Proxy Text
Proxy = Proxy (ShareInput Text)
-> [Token (ShareInput Text)] -> Tokens (ShareInput Text)
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput T.Text))
chunkToTokens :: Proxy Text -> Tokens Text -> [Token Text]
chunkToTokens Proxy Text
Proxy = Proxy (ShareInput Text)
-> Tokens (ShareInput Text) -> [Token (ShareInput Text)]
forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput T.Text))
chunkLength :: Proxy Text -> Tokens Text -> Int
chunkLength Proxy Text
Proxy = Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput T.Text))
chunkEmpty :: Proxy Text -> Tokens Text -> Bool
chunkEmpty Proxy Text
Proxy = Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Bool
forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput T.Text))
take1_ :: Text -> Maybe (Token Text, Text)
take1_ Text
s = (ShareInput Text -> Text)
-> (Char, ShareInput Text) -> (Char, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Char, ShareInput Text) -> (Char, Text))
-> Maybe (Char, ShareInput Text) -> Maybe (Char, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShareInput Text -> Maybe (Token (ShareInput Text), ShareInput Text)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
takeN_ :: Int -> Text -> Maybe (Tokens Text, Text)
takeN_ Int
n Text
s = (ShareInput Text -> Text)
-> (Text, ShareInput Text) -> (Text, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Text, ShareInput Text) -> (Text, Text))
-> Maybe (Text, ShareInput Text) -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> ShareInput Text
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
n (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
takeWhile_ :: (Token Text -> Bool) -> Text -> (Tokens Text, Text)
takeWhile_ Token Text -> Bool
p Text
s = (ShareInput Text -> Text)
-> (Tokens Text, ShareInput Text) -> (Tokens Text, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Tokens Text, ShareInput Text) -> (Tokens Text, Text))
-> (Tokens Text, ShareInput Text) -> (Tokens Text, Text)
forall a b. (a -> b) -> a -> b
$ (Token (ShareInput Text) -> Bool)
-> ShareInput Text -> (Tokens (ShareInput Text), ShareInput Text)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token (ShareInput Text) -> Bool
Token Text -> Bool
p (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
instance Stream TL.Text where
type Token TL.Text = Token (ShareInput TL.Text)
type Tokens TL.Text = Tokens (ShareInput TL.Text)
tokenToChunk :: Proxy Text -> Token Text -> Tokens Text
tokenToChunk Proxy Text
Proxy = Proxy (ShareInput Text)
-> Token (ShareInput Text) -> Tokens (ShareInput Text)
forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput TL.Text))
tokensToChunk :: Proxy Text -> [Token Text] -> Tokens Text
tokensToChunk Proxy Text
Proxy = Proxy (ShareInput Text)
-> [Token (ShareInput Text)] -> Tokens (ShareInput Text)
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput TL.Text))
chunkToTokens :: Proxy Text -> Tokens Text -> [Token Text]
chunkToTokens Proxy Text
Proxy = Proxy (ShareInput Text)
-> Tokens (ShareInput Text) -> [Token (ShareInput Text)]
forall s. Stream s => Proxy s -> Tokens s -> [Token s]
chunkToTokens (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput TL.Text))
chunkLength :: Proxy Text -> Tokens Text -> Int
chunkLength Proxy Text
Proxy = Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Int
forall s. Stream s => Proxy s -> Tokens s -> Int
chunkLength (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput TL.Text))
chunkEmpty :: Proxy Text -> Tokens Text -> Bool
chunkEmpty Proxy Text
Proxy = Proxy (ShareInput Text) -> Tokens (ShareInput Text) -> Bool
forall s. Stream s => Proxy s -> Tokens s -> Bool
chunkEmpty (Proxy (ShareInput Text)
forall {k} (t :: k). Proxy t
Proxy :: Proxy (ShareInput TL.Text))
take1_ :: Text -> Maybe (Token Text, Text)
take1_ Text
s = (ShareInput Text -> Text)
-> (Char, ShareInput Text) -> (Char, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Char, ShareInput Text) -> (Char, Text))
-> Maybe (Char, ShareInput Text) -> Maybe (Char, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShareInput Text -> Maybe (Token (ShareInput Text), ShareInput Text)
forall s. Stream s => s -> Maybe (Token s, s)
take1_ (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
takeN_ :: Int -> Text -> Maybe (Tokens Text, Text)
takeN_ Int
n Text
s = (ShareInput Text -> Text)
-> (Text, ShareInput Text) -> (Text, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Text, ShareInput Text) -> (Text, Text))
-> Maybe (Text, ShareInput Text) -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> ShareInput Text
-> Maybe (Tokens (ShareInput Text), ShareInput Text)
forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
takeN_ Int
n (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
takeWhile_ :: (Token Text -> Bool) -> Text -> (Tokens Text, Text)
takeWhile_ Token Text -> Bool
p Text
s = (ShareInput Text -> Text)
-> (Tokens Text, ShareInput Text) -> (Tokens Text, Text)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second ShareInput Text -> Text
forall a. ShareInput a -> a
unShareInput ((Tokens Text, ShareInput Text) -> (Tokens Text, Text))
-> (Tokens Text, ShareInput Text) -> (Tokens Text, Text)
forall a b. (a -> b) -> a -> b
$ (Token (ShareInput Text) -> Bool)
-> ShareInput Text -> (Tokens (ShareInput Text), ShareInput Text)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ Token (ShareInput Text) -> Bool
Token Text -> Bool
p (Text -> ShareInput Text
forall a. a -> ShareInput a
ShareInput Text
s)
{-# INLINE tokenToChunk #-}
{-# INLINE tokensToChunk #-}
{-# INLINE chunkToTokens #-}
{-# INLINE chunkLength #-}
{-# INLINE chunkEmpty #-}
{-# INLINE take1_ #-}
{-# INLINE takeN_ #-}
{-# INLINE takeWhile_ #-}
class (Stream s) => VisualStream s where
showTokens :: Proxy s -> NonEmpty (Token s) -> String
tokensLength :: Proxy s -> NonEmpty (Token s) -> Int
tokensLength Proxy s
Proxy = NonEmpty (Token s) -> Int
forall a. NonEmpty a -> Int
NE.length
instance VisualStream String where
showTokens :: Proxy [Char] -> NonEmpty (Token [Char]) -> [Char]
showTokens Proxy [Char]
Proxy = NonEmpty Char -> [Char]
NonEmpty (Token [Char]) -> [Char]
stringPretty
tokensLength :: Proxy [Char] -> NonEmpty (Token [Char]) -> Int
tokensLength Proxy [Char]
Proxy = NonEmpty Char -> Int
NonEmpty (Token [Char]) -> Int
forall (t :: * -> *). Traversable t => t Char -> Int
Unicode.stringLength
instance VisualStream B.ByteString where
showTokens :: Proxy ByteString -> NonEmpty (Token ByteString) -> [Char]
showTokens Proxy ByteString
Proxy = NonEmpty Char -> [Char]
stringPretty (NonEmpty Char -> [Char])
-> (NonEmpty Word8 -> NonEmpty Char) -> NonEmpty Word8 -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Char) -> NonEmpty Word8 -> NonEmpty Char
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
instance VisualStream BL.ByteString where
showTokens :: Proxy ByteString -> NonEmpty (Token ByteString) -> [Char]
showTokens Proxy ByteString
Proxy = NonEmpty Char -> [Char]
stringPretty (NonEmpty Char -> [Char])
-> (NonEmpty Word8 -> NonEmpty Char) -> NonEmpty Word8 -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Char) -> NonEmpty Word8 -> NonEmpty Char
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
instance VisualStream T.Text where
showTokens :: Proxy Text -> NonEmpty (Token Text) -> [Char]
showTokens Proxy Text
Proxy = NonEmpty Char -> [Char]
NonEmpty (Token Text) -> [Char]
stringPretty
tokensLength :: Proxy Text -> NonEmpty (Token Text) -> Int
tokensLength Proxy Text
Proxy = NonEmpty Char -> Int
NonEmpty (Token Text) -> Int
forall (t :: * -> *). Traversable t => t Char -> Int
Unicode.stringLength
instance VisualStream TL.Text where
showTokens :: Proxy Text -> NonEmpty (Token Text) -> [Char]
showTokens Proxy Text
Proxy = NonEmpty Char -> [Char]
NonEmpty (Token Text) -> [Char]
stringPretty
tokensLength :: Proxy Text -> NonEmpty (Token Text) -> Int
tokensLength Proxy Text
Proxy = NonEmpty Char -> Int
NonEmpty (Token Text) -> Int
forall (t :: * -> *). Traversable t => t Char -> Int
Unicode.stringLength
class (Stream s) => TraversableStream s where
{-# MINIMAL reachOffset | reachOffsetNoLine #-}
reachOffset ::
Int ->
PosState s ->
(Maybe String, PosState s)
reachOffset Int
o PosState s
pst =
(Maybe [Char]
forall a. Maybe a
Nothing, Int -> PosState s -> PosState s
forall s. TraversableStream s => Int -> PosState s -> PosState s
reachOffsetNoLine Int
o PosState s
pst)
reachOffsetNoLine ::
Int ->
PosState s ->
PosState s
reachOffsetNoLine Int
o PosState s
pst =
(Maybe [Char], PosState s) -> PosState s
forall a b. (a, b) -> b
snd (Int -> PosState s -> (Maybe [Char], PosState s)
forall s.
TraversableStream s =>
Int -> PosState s -> (Maybe [Char], PosState s)
reachOffset Int
o PosState s
pst)
instance TraversableStream String where
reachOffset :: Int -> PosState [Char] -> (Maybe [Char], PosState [Char])
reachOffset Int
o PosState [Char]
pst =
(Int -> [Char] -> (Tokens [Char], [Char]))
-> (forall b. (b -> Token [Char] -> b) -> b -> Tokens [Char] -> b)
-> (Tokens [Char] -> [Char])
-> (Token [Char] -> Char)
-> (Token [Char], Token [Char])
-> (Token [Char] -> Pos -> Pos)
-> Int
-> PosState [Char]
-> (Maybe [Char], PosState [Char])
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset' Int -> [Char] -> ([Char], [Char])
Int -> [Char] -> (Tokens [Char], [Char])
forall a. Int -> [a] -> ([a], [a])
splitAt (b -> Char -> b) -> b -> [Char] -> b
(b -> Token [Char] -> b) -> b -> Tokens [Char] -> b
forall b. (b -> Token [Char] -> b) -> b -> Tokens [Char] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.List.foldl' [Char] -> [Char]
Tokens [Char] -> [Char]
forall a. a -> a
id Char -> Char
Token [Char] -> Char
forall a. a -> a
id (Char
Token [Char]
'\n', Char
Token [Char]
'\t') Char -> Pos -> Pos
Token [Char] -> Pos -> Pos
charInc Int
o PosState [Char]
pst
reachOffsetNoLine :: Int -> PosState [Char] -> PosState [Char]
reachOffsetNoLine Int
o PosState [Char]
pst =
(Int -> [Char] -> (Tokens [Char], [Char]))
-> (forall b. (b -> Token [Char] -> b) -> b -> Tokens [Char] -> b)
-> (Token [Char], Token [Char])
-> (Token [Char] -> Pos -> Pos)
-> Int
-> PosState [Char]
-> PosState [Char]
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine' Int -> [Char] -> ([Char], [Char])
Int -> [Char] -> (Tokens [Char], [Char])
forall a. Int -> [a] -> ([a], [a])
splitAt (b -> Char -> b) -> b -> [Char] -> b
(b -> Token [Char] -> b) -> b -> Tokens [Char] -> b
forall b. (b -> Token [Char] -> b) -> b -> Tokens [Char] -> b
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.List.foldl' (Char
Token [Char]
'\n', Char
Token [Char]
'\t') Char -> Pos -> Pos
Token [Char] -> Pos -> Pos
charInc Int
o PosState [Char]
pst
instance TraversableStream B.ByteString where
reachOffset :: Int -> PosState ByteString -> (Maybe [Char], PosState ByteString)
reachOffset Int
o PosState ByteString
pst =
(Int -> ByteString -> (Tokens ByteString, ByteString))
-> (forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b)
-> (Tokens ByteString -> [Char])
-> (Token ByteString -> Char)
-> (Token ByteString, Token ByteString)
-> (Token ByteString -> Pos -> Pos)
-> Int
-> PosState ByteString
-> (Maybe [Char], PosState ByteString)
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset' Int -> ByteString -> (Tokens ByteString, ByteString)
Int -> ByteString -> (ByteString, ByteString)
B.splitAt (b -> Word8 -> b) -> b -> ByteString -> b
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
B.foldl' Tokens ByteString -> [Char]
ByteString -> [Char]
B8.unpack (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (Word8
Token ByteString
10, Word8
Token ByteString
9) Word8 -> Pos -> Pos
Token ByteString -> Pos -> Pos
byteInc Int
o PosState ByteString
pst
reachOffsetNoLine :: Int -> PosState ByteString -> PosState ByteString
reachOffsetNoLine Int
o PosState ByteString
pst =
(Int -> ByteString -> (Tokens ByteString, ByteString))
-> (forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b)
-> (Token ByteString, Token ByteString)
-> (Token ByteString -> Pos -> Pos)
-> Int
-> PosState ByteString
-> PosState ByteString
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine' Int -> ByteString -> (Tokens ByteString, ByteString)
Int -> ByteString -> (ByteString, ByteString)
B.splitAt (b -> Word8 -> b) -> b -> ByteString -> b
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
B.foldl' (Word8
Token ByteString
10, Word8
Token ByteString
9) Word8 -> Pos -> Pos
Token ByteString -> Pos -> Pos
byteInc Int
o PosState ByteString
pst
instance TraversableStream BL.ByteString where
reachOffset :: Int -> PosState ByteString -> (Maybe [Char], PosState ByteString)
reachOffset Int
o PosState ByteString
pst =
(Int -> ByteString -> (Tokens ByteString, ByteString))
-> (forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b)
-> (Tokens ByteString -> [Char])
-> (Token ByteString -> Char)
-> (Token ByteString, Token ByteString)
-> (Token ByteString -> Pos -> Pos)
-> Int
-> PosState ByteString
-> (Maybe [Char], PosState ByteString)
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset' Int -> ByteString -> (Tokens ByteString, ByteString)
Int -> ByteString -> (ByteString, ByteString)
splitAtBL (b -> Word8 -> b) -> b -> ByteString -> b
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
BL.foldl' Tokens ByteString -> [Char]
ByteString -> [Char]
BL8.unpack (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (Word8
Token ByteString
10, Word8
Token ByteString
9) Word8 -> Pos -> Pos
Token ByteString -> Pos -> Pos
byteInc Int
o PosState ByteString
pst
reachOffsetNoLine :: Int -> PosState ByteString -> PosState ByteString
reachOffsetNoLine Int
o PosState ByteString
pst =
(Int -> ByteString -> (Tokens ByteString, ByteString))
-> (forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b)
-> (Token ByteString, Token ByteString)
-> (Token ByteString -> Pos -> Pos)
-> Int
-> PosState ByteString
-> PosState ByteString
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine' Int -> ByteString -> (Tokens ByteString, ByteString)
Int -> ByteString -> (ByteString, ByteString)
splitAtBL (b -> Word8 -> b) -> b -> ByteString -> b
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
forall b.
(b -> Token ByteString -> b) -> b -> Tokens ByteString -> b
BL.foldl' (Word8
Token ByteString
10, Word8
Token ByteString
9) Word8 -> Pos -> Pos
Token ByteString -> Pos -> Pos
byteInc Int
o PosState ByteString
pst
instance TraversableStream T.Text where
reachOffset :: Int -> PosState Text -> (Maybe [Char], PosState Text)
reachOffset Int
o PosState Text
pst =
(Int -> Text -> (Tokens Text, Text))
-> (forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b)
-> (Tokens Text -> [Char])
-> (Token Text -> Char)
-> (Token Text, Token Text)
-> (Token Text -> Pos -> Pos)
-> Int
-> PosState Text
-> (Maybe [Char], PosState Text)
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset' Int -> Text -> (Tokens Text, Text)
Int -> Text -> (Text, Text)
T.splitAt (b -> Char -> b) -> b -> Text -> b
(b -> Token Text -> b) -> b -> Tokens Text -> b
forall a. (a -> Char -> a) -> a -> Text -> a
forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b
T.foldl' Tokens Text -> [Char]
Text -> [Char]
T.unpack Char -> Char
Token Text -> Char
forall a. a -> a
id (Char
Token Text
'\n', Char
Token Text
'\t') Char -> Pos -> Pos
Token Text -> Pos -> Pos
charInc Int
o PosState Text
pst
reachOffsetNoLine :: Int -> PosState Text -> PosState Text
reachOffsetNoLine Int
o PosState Text
pst =
(Int -> Text -> (Tokens Text, Text))
-> (forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b)
-> (Token Text, Token Text)
-> (Token Text -> Pos -> Pos)
-> Int
-> PosState Text
-> PosState Text
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine' Int -> Text -> (Tokens Text, Text)
Int -> Text -> (Text, Text)
T.splitAt (b -> Char -> b) -> b -> Text -> b
(b -> Token Text -> b) -> b -> Tokens Text -> b
forall a. (a -> Char -> a) -> a -> Text -> a
forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b
T.foldl' (Char
Token Text
'\n', Char
Token Text
'\t') Char -> Pos -> Pos
Token Text -> Pos -> Pos
charInc Int
o PosState Text
pst
instance TraversableStream TL.Text where
reachOffset :: Int -> PosState Text -> (Maybe [Char], PosState Text)
reachOffset Int
o PosState Text
pst =
(Int -> Text -> (Tokens Text, Text))
-> (forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b)
-> (Tokens Text -> [Char])
-> (Token Text -> Char)
-> (Token Text, Token Text)
-> (Token Text -> Pos -> Pos)
-> Int
-> PosState Text
-> (Maybe [Char], PosState Text)
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset' Int -> Text -> (Tokens Text, Text)
Int -> Text -> (Text, Text)
splitAtTL (b -> Char -> b) -> b -> Text -> b
(b -> Token Text -> b) -> b -> Tokens Text -> b
forall a. (a -> Char -> a) -> a -> Text -> a
forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b
TL.foldl' Tokens Text -> [Char]
Text -> [Char]
TL.unpack Char -> Char
Token Text -> Char
forall a. a -> a
id (Char
Token Text
'\n', Char
Token Text
'\t') Char -> Pos -> Pos
Token Text -> Pos -> Pos
charInc Int
o PosState Text
pst
reachOffsetNoLine :: Int -> PosState Text -> PosState Text
reachOffsetNoLine Int
o PosState Text
pst =
(Int -> Text -> (Tokens Text, Text))
-> (forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b)
-> (Token Text, Token Text)
-> (Token Text -> Pos -> Pos)
-> Int
-> PosState Text
-> PosState Text
forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine' Int -> Text -> (Tokens Text, Text)
Int -> Text -> (Text, Text)
splitAtTL (b -> Char -> b) -> b -> Text -> b
(b -> Token Text -> b) -> b -> Tokens Text -> b
forall a. (a -> Char -> a) -> a -> Text -> a
forall b. (b -> Token Text -> b) -> b -> Tokens Text -> b
TL.foldl' (Char
Token Text
'\n', Char
Token Text
'\t') Char -> Pos -> Pos
Token Text -> Pos -> Pos
charInc Int
o PosState Text
pst
data St = St {-# UNPACK #-} !SourcePos ShowS
reachOffset' ::
forall s.
(Stream s) =>
(Int -> s -> (Tokens s, s)) ->
(forall b. (b -> Token s -> b) -> b -> Tokens s -> b) ->
(Tokens s -> String) ->
(Token s -> Char) ->
(Token s, Token s) ->
(Token s -> Pos -> Pos) ->
Int ->
PosState s ->
(Maybe String, PosState s)
reachOffset' :: forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Tokens s -> [Char])
-> (Token s -> Char)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> (Maybe [Char], PosState s)
reachOffset'
Int -> s -> (Tokens s, s)
splitAt'
forall b. (b -> Token s -> b) -> b -> Tokens s -> b
foldl''
Tokens s -> [Char]
fromToks
Token s -> Char
fromTok
(Token s
newlineTok, Token s
tabTok)
Token s -> Pos -> Pos
columnIncrement
Int
o
PosState {s
Int
[Char]
Pos
SourcePos
pstateInput :: s
pstateOffset :: Int
pstateSourcePos :: SourcePos
pstateTabWidth :: Pos
pstateLinePrefix :: [Char]
pstateInput :: forall s. PosState s -> s
pstateLinePrefix :: forall s. PosState s -> [Char]
pstateOffset :: forall s. PosState s -> Int
pstateSourcePos :: forall s. PosState s -> SourcePos
pstateTabWidth :: forall s. PosState s -> Pos
..} =
( [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$ case Pos -> [Char] -> [Char]
expandTab Pos
pstateTabWidth
([Char] -> [Char])
-> ((Tokens s, s) -> [Char]) -> (Tokens s, s) -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
addPrefix
([Char] -> [Char])
-> ((Tokens s, s) -> [Char]) -> (Tokens s, s) -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
f
([Char] -> [Char])
-> ((Tokens s, s) -> [Char]) -> (Tokens s, s) -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tokens s -> [Char]
fromToks
(Tokens s -> [Char])
-> ((Tokens s, s) -> Tokens s) -> (Tokens s, s) -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tokens s, s) -> Tokens s
forall a b. (a, b) -> a
fst
((Tokens s, s) -> [Char]) -> (Tokens s, s) -> [Char]
forall a b. (a -> b) -> a -> b
$ (Token s -> Bool) -> s -> (Tokens s, s)
forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
takeWhile_ (Token s -> Token s -> Bool
forall a. Eq a => a -> a -> Bool
/= Token s
newlineTok) s
post of
[Char]
"" -> [Char]
"<empty line>"
[Char]
xs -> [Char]
xs,
PosState
{ pstateInput :: s
pstateInput = s
post,
pstateOffset :: Int
pstateOffset = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
pstateOffset Int
o,
pstateSourcePos :: SourcePos
pstateSourcePos = SourcePos
spos,
pstateTabWidth :: Pos
pstateTabWidth = Pos
pstateTabWidth,
pstateLinePrefix :: [Char]
pstateLinePrefix =
if Bool
sameLine
then
[Char]
pstateLinePrefix [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
f [Char]
""
else [Char] -> [Char]
f [Char]
""
}
)
where
addPrefix :: [Char] -> [Char]
addPrefix [Char]
xs =
if Bool
sameLine
then [Char]
pstateLinePrefix [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
xs
else [Char]
xs
sameLine :: Bool
sameLine = SourcePos -> Pos
sourceLine SourcePos
spos Pos -> Pos -> Bool
forall a. Eq a => a -> a -> Bool
== SourcePos -> Pos
sourceLine SourcePos
pstateSourcePos
(Tokens s
pre, s
post) = Int -> s -> (Tokens s, s)
splitAt' (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pstateOffset) s
pstateInput
St SourcePos
spos [Char] -> [Char]
f = (St -> Token s -> St) -> St -> Tokens s -> St
forall b. (b -> Token s -> b) -> b -> Tokens s -> b
foldl'' St -> Token s -> St
go (SourcePos -> ([Char] -> [Char]) -> St
St SourcePos
pstateSourcePos [Char] -> [Char]
forall a. a -> a
id) Tokens s
pre
go :: St -> Token s -> St
go (St SourcePos
apos [Char] -> [Char]
g) Token s
ch =
let SourcePos [Char]
n Pos
l Pos
c = SourcePos
apos
c' :: Int
c' = Pos -> Int
unPos Pos
c
w :: Int
w = Pos -> Int
unPos Pos
pstateTabWidth
in if
| Token s
ch Token s -> Token s -> Bool
forall a. Eq a => a -> a -> Bool
== Token s
newlineTok ->
SourcePos -> ([Char] -> [Char]) -> St
St
([Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n (Pos
l Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1) Pos
pos1)
[Char] -> [Char]
forall a. a -> a
id
| Token s
ch Token s -> Token s -> Bool
forall a. Eq a => a -> a -> Bool
== Token s
tabTok ->
SourcePos -> ([Char] -> [Char]) -> St
St
([Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n Pos
l (Int -> Pos
mkPos (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ Int
c' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- ((Int
c' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
w)))
([Char] -> [Char]
g ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Token s -> Char
fromTok Token s
ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:))
| Bool
otherwise ->
SourcePos -> ([Char] -> [Char]) -> St
St
([Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n Pos
l (Token s -> Pos -> Pos
columnIncrement Token s
ch Pos
c))
([Char] -> [Char]
g ([Char] -> [Char]) -> ([Char] -> [Char]) -> [Char] -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Token s -> Char
fromTok Token s
ch Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
:))
{-# INLINE reachOffset' #-}
reachOffsetNoLine' ::
forall s.
(Stream s) =>
(Int -> s -> (Tokens s, s)) ->
(forall b. (b -> Token s -> b) -> b -> Tokens s -> b) ->
(Token s, Token s) ->
(Token s -> Pos -> Pos) ->
Int ->
PosState s ->
PosState s
reachOffsetNoLine' :: forall s.
Stream s =>
(Int -> s -> (Tokens s, s))
-> (forall b. (b -> Token s -> b) -> b -> Tokens s -> b)
-> (Token s, Token s)
-> (Token s -> Pos -> Pos)
-> Int
-> PosState s
-> PosState s
reachOffsetNoLine'
Int -> s -> (Tokens s, s)
splitAt'
forall b. (b -> Token s -> b) -> b -> Tokens s -> b
foldl''
(Token s
newlineTok, Token s
tabTok)
Token s -> Pos -> Pos
columnIncrement
Int
o
PosState {s
Int
[Char]
Pos
SourcePos
pstateInput :: forall s. PosState s -> s
pstateLinePrefix :: forall s. PosState s -> [Char]
pstateOffset :: forall s. PosState s -> Int
pstateSourcePos :: forall s. PosState s -> SourcePos
pstateTabWidth :: forall s. PosState s -> Pos
pstateInput :: s
pstateOffset :: Int
pstateSourcePos :: SourcePos
pstateTabWidth :: Pos
pstateLinePrefix :: [Char]
..} =
( PosState
{ pstateInput :: s
pstateInput = s
post,
pstateOffset :: Int
pstateOffset = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
pstateOffset Int
o,
pstateSourcePos :: SourcePos
pstateSourcePos = SourcePos
spos,
pstateTabWidth :: Pos
pstateTabWidth = Pos
pstateTabWidth,
pstateLinePrefix :: [Char]
pstateLinePrefix = [Char]
pstateLinePrefix
}
)
where
spos :: SourcePos
spos = (SourcePos -> Token s -> SourcePos)
-> SourcePos -> Tokens s -> SourcePos
forall b. (b -> Token s -> b) -> b -> Tokens s -> b
foldl'' SourcePos -> Token s -> SourcePos
go SourcePos
pstateSourcePos Tokens s
pre
(Tokens s
pre, s
post) = Int -> s -> (Tokens s, s)
splitAt' (Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pstateOffset) s
pstateInput
go :: SourcePos -> Token s -> SourcePos
go (SourcePos [Char]
n Pos
l Pos
c) Token s
ch =
let c' :: Int
c' = Pos -> Int
unPos Pos
c
w :: Int
w = Pos -> Int
unPos Pos
pstateTabWidth
in if
| Token s
ch Token s -> Token s -> Bool
forall a. Eq a => a -> a -> Bool
== Token s
newlineTok ->
[Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n (Pos
l Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1) Pos
pos1
| Token s
ch Token s -> Token s -> Bool
forall a. Eq a => a -> a -> Bool
== Token s
tabTok ->
[Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n Pos
l (Int -> Pos
mkPos (Int -> Pos) -> Int -> Pos
forall a b. (a -> b) -> a -> b
$ Int
c' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- ((Int
c' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
w))
| Bool
otherwise ->
[Char] -> Pos -> Pos -> SourcePos
SourcePos [Char]
n Pos
l (Token s -> Pos -> Pos
columnIncrement Token s
ch Pos
c)
{-# INLINE reachOffsetNoLine' #-}
splitAtBL :: Int -> BL.ByteString -> (BL.ByteString, BL.ByteString)
splitAtBL :: Int -> ByteString -> (ByteString, ByteString)
splitAtBL Int
n = Int64 -> ByteString -> (ByteString, ByteString)
BL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
{-# INLINE splitAtBL #-}
splitAtTL :: Int -> TL.Text -> (TL.Text, TL.Text)
splitAtTL :: Int -> Text -> (Text, Text)
splitAtTL Int
n = Int64 -> Text -> (Text, Text)
TL.splitAt (Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
{-# INLINE splitAtTL #-}
stringPretty :: NonEmpty Char -> String
stringPretty :: NonEmpty Char -> [Char]
stringPretty (Char
x :| []) = Char -> [Char]
charPretty Char
x
stringPretty (Char
'\r' :| [Char]
"\n") = [Char]
"crlf newline"
stringPretty NonEmpty Char
xs = [Char]
"\"" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> (Char -> [Char]) -> [Char] -> [Char]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> [Char]
f (NonEmpty Char -> [Char]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty Char
xs) [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"\""
where
f :: Char -> [Char]
f Char
ch =
case Char -> Maybe [Char]
charPretty' Char
ch of
Maybe [Char]
Nothing -> [Char
ch]
Just [Char]
pretty -> [Char]
"<" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
pretty [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
">"
charPretty :: Char -> String
charPretty :: Char -> [Char]
charPretty Char
' ' = [Char]
"space"
charPretty Char
ch = [Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe ([Char]
"'" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char
ch] [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"'") (Char -> Maybe [Char]
charPretty' Char
ch)
charPretty' :: Char -> Maybe String
charPretty' :: Char -> Maybe [Char]
charPretty' = \case
Char
'\NUL' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"null"
Char
'\SOH' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"start of heading"
Char
'\STX' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"start of text"
Char
'\ETX' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"end of text"
Char
'\EOT' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"end of transmission"
Char
'\ENQ' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"enquiry"
Char
'\ACK' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"acknowledge"
Char
'\BEL' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"bell"
Char
'\BS' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"backspace"
Char
'\t' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"tab"
Char
'\n' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"newline"
Char
'\v' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"vertical tab"
Char
'\f' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"form feed"
Char
'\r' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"carriage return"
Char
'\SO' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"shift out"
Char
'\SI' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"shift in"
Char
'\DLE' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"data link escape"
Char
'\DC1' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"device control one"
Char
'\DC2' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"device control two"
Char
'\DC3' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"device control three"
Char
'\DC4' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"device control four"
Char
'\NAK' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"negative acknowledge"
Char
'\SYN' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"synchronous idle"
Char
'\ETB' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"end of transmission block"
Char
'\CAN' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"cancel"
Char
'\EM' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"end of medium"
Char
'\SUB' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"substitute"
Char
'\ESC' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"escape"
Char
'\FS' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"file separator"
Char
'\GS' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"group separator"
Char
'\RS' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"record separator"
Char
'\US' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"unit separator"
Char
'\DEL' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"delete"
Char
'\160' -> [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"non-breaking space"
Char
_ -> Maybe [Char]
forall a. Maybe a
Nothing
expandTab ::
Pos ->
String ->
String
expandTab :: Pos -> [Char] -> [Char]
expandTab Pos
w' = Int -> Int -> [Char] -> [Char]
go Int
0 Int
0
where
go :: Int -> Int -> [Char] -> [Char]
go Int
_ Int
0 [] = []
go !Int
i Int
0 (Char
'\t' : [Char]
xs) = Int -> Int -> [Char] -> [Char]
go Int
i (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
i Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
w)) [Char]
xs
go !Int
i Int
0 (Char
x : [Char]
xs) = Char
x Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> Int -> [Char] -> [Char]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
Unicode.charLength Char
x) Int
0 [Char]
xs
go !Int
i Int
n [Char]
xs = Char
' ' Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Int -> Int -> [Char] -> [Char]
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Char]
xs
w :: Int
w = Pos -> Int
unPos Pos
w'
charInc :: Char -> Pos -> Pos
charInc :: Char -> Pos -> Pos
charInc Char
ch Pos
c =
case Char -> Int
Unicode.charLength Char
ch of
Int
0 -> Pos
c
Int
2 -> Pos
c Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1 Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1
Int
_ -> Pos
c Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1
byteInc :: Word8 -> Pos -> Pos
byteInc :: Word8 -> Pos -> Pos
byteInc Word8
w Pos
c
| Word8
w Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
0x20
Bool -> Bool -> Bool
|| (Word8
w Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
0x7f Bool -> Bool -> Bool
&& Word8
w Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
0xa0)
Bool -> Bool -> Bool
|| Word8
w Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0xad =
Pos
c
| Bool
otherwise = Pos
c Pos -> Pos -> Pos
forall a. Semigroup a => a -> a -> a
<> Pos
pos1