{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Text.Megaparsec.Byte
(
newline,
crlf,
eol,
tab,
space,
hspace,
space1,
hspace1,
controlChar,
spaceChar,
upperChar,
lowerChar,
letterChar,
alphaNumChar,
printChar,
digitChar,
binDigitChar,
octDigitChar,
hexDigitChar,
asciiChar,
char,
char',
string,
string',
)
where
import Control.Applicative
import Data.Char hiding (isSpace, toLower, toUpper)
import Data.Functor (void)
import Data.Proxy
import Data.Word (Word8)
import Text.Megaparsec
import Text.Megaparsec.Common
newline :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
newline :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
newline = forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char Token s
10
{-# INLINE newline #-}
crlf :: forall e s m. (MonadParsec e s m, Token s ~ Word8) => m (Tokens s)
crlf :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Tokens s)
crlf = forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string (forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (forall {k} (t :: k). Proxy t
Proxy :: Proxy s) [Word8
13, Word8
10])
{-# INLINE crlf #-}
eol :: forall e s m. (MonadParsec e s m, Token s ~ Word8) => m (Tokens s)
eol :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Tokens s)
eol =
(forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (forall {k} (t :: k). Proxy t
Proxy :: Proxy s) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
newline)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Tokens s)
crlf
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"end of line"
{-# INLINE eol #-}
tab :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
tab :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
tab = forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char Token s
9
{-# INLINE tab #-}
space :: (MonadParsec e s m, Token s ~ Word8) => m ()
space :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m ()
space = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe String -> (Token s -> Bool) -> m (Tokens s)
takeWhileP (forall a. a -> Maybe a
Just String
"white space") Word8 -> Bool
isSpace
{-# INLINE space #-}
hspace :: (MonadParsec e s m, Token s ~ Word8) => m ()
hspace :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m ()
hspace = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe String -> (Token s -> Bool) -> m (Tokens s)
takeWhileP (forall a. a -> Maybe a
Just String
"white space") Word8 -> Bool
isHSpace
{-# INLINE hspace #-}
space1 :: (MonadParsec e s m, Token s ~ Word8) => m ()
space1 :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m ()
space1 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe String -> (Token s -> Bool) -> m (Tokens s)
takeWhile1P (forall a. a -> Maybe a
Just String
"white space") Word8 -> Bool
isSpace
{-# INLINE space1 #-}
hspace1 :: (MonadParsec e s m, Token s ~ Word8) => m ()
hspace1 :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m ()
hspace1 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe String -> (Token s -> Bool) -> m (Tokens s)
takeWhile1P (forall a. a -> Maybe a
Just String
"white space") Word8 -> Bool
isHSpace
{-# INLINE hspace1 #-}
controlChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
controlChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
controlChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isControl forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"control character"
{-# INLINE controlChar #-}
spaceChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
spaceChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
spaceChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Word8 -> Bool
isSpace forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"white space"
{-# INLINE spaceChar #-}
upperChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
upperChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
upperChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isUpper forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"uppercase letter"
{-# INLINE upperChar #-}
lowerChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
lowerChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
lowerChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isLower forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"lowercase letter"
{-# INLINE lowerChar #-}
letterChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
letterChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
letterChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isLetter forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"letter"
{-# INLINE letterChar #-}
alphaNumChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
alphaNumChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
alphaNumChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isAlphaNum forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"alphanumeric character"
{-# INLINE alphaNumChar #-}
printChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
printChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
printChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isPrint forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"printable character"
{-# INLINE printChar #-}
digitChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
digitChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
digitChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy forall {a}. (Ord a, Num a) => a -> Bool
isDigit' forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"digit"
where
isDigit' :: a -> Bool
isDigit' a
x = a
x forall a. Ord a => a -> a -> Bool
>= a
48 Bool -> Bool -> Bool
&& a
x forall a. Ord a => a -> a -> Bool
<= a
57
{-# INLINE digitChar #-}
binDigitChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
binDigitChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
binDigitChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy forall {a}. (Eq a, Num a) => a -> Bool
isBinDigit forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"binary digit"
where
isBinDigit :: a -> Bool
isBinDigit a
x = a
x forall a. Eq a => a -> a -> Bool
== a
48 Bool -> Bool -> Bool
|| a
x forall a. Eq a => a -> a -> Bool
== a
49
{-# INLINE binDigitChar #-}
octDigitChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
octDigitChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
octDigitChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy forall {a}. (Ord a, Num a) => a -> Bool
isOctDigit' forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"octal digit"
where
isOctDigit' :: a -> Bool
isOctDigit' a
x = a
x forall a. Ord a => a -> a -> Bool
>= a
48 Bool -> Bool -> Bool
&& a
x forall a. Ord a => a -> a -> Bool
<= a
55
{-# INLINE octDigitChar #-}
hexDigitChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
hexDigitChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
hexDigitChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (Char -> Bool
isHexDigit forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
toChar) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"hexadecimal digit"
{-# INLINE hexDigitChar #-}
asciiChar :: (MonadParsec e s m, Token s ~ Word8) => m (Token s)
asciiChar :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
m (Token s)
asciiChar = forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy (forall a. Ord a => a -> a -> Bool
< Token s
128) forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> String -> m a
<?> String
"ASCII character"
{-# INLINE asciiChar #-}
char :: (MonadParsec e s m, Token s ~ Word8) => Token s -> m (Token s)
char :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char = forall e s (m :: * -> *).
MonadParsec e s m =>
Token s -> m (Token s)
single
{-# INLINE char #-}
char' :: (MonadParsec e s m, Token s ~ Word8) => Token s -> m (Token s)
char' :: forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char' Token s
c =
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
[ forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char (Word8 -> Word8
toLower Token s
c),
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Word8) =>
Token s -> m (Token s)
char (Word8 -> Word8
toUpper Token s
c)
]
{-# INLINE char' #-}
isSpace :: Word8 -> Bool
isSpace :: Word8 -> Bool
isSpace Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
>= Word8
9 Bool -> Bool -> Bool
&& Word8
x forall a. Ord a => a -> a -> Bool
<= Word8
13 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
32 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
160 = Bool
True
| Bool
otherwise = Bool
False
{-# INLINE isSpace #-}
isHSpace :: Word8 -> Bool
isHSpace :: Word8 -> Bool
isHSpace Word8
x
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
9 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
11 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
12 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
32 = Bool
True
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
160 = Bool
True
| Bool
otherwise = Bool
False
{-# INLINE isHSpace #-}
toChar :: Word8 -> Char
toChar :: Word8 -> Char
toChar = Int -> Char
chr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE toChar #-}
toUpper :: Word8 -> Word8
toUpper :: Word8 -> Word8
toUpper Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
>= Word8
97 Bool -> Bool -> Bool
&& Word8
x forall a. Ord a => a -> a -> Bool
<= Word8
122 = Word8
x forall a. Num a => a -> a -> a
- Word8
32
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
247 = Word8
x
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
255 = Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
>= Word8
224 = Word8
x forall a. Num a => a -> a -> a
- Word8
32
| Bool
otherwise = Word8
x
{-# INLINE toUpper #-}
toLower :: Word8 -> Word8
toLower :: Word8 -> Word8
toLower Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
>= Word8
65 Bool -> Bool -> Bool
&& Word8
x forall a. Ord a => a -> a -> Bool
<= Word8
90 = Word8
x forall a. Num a => a -> a -> a
+ Word8
32
| Word8
x forall a. Eq a => a -> a -> Bool
== Word8
215 = Word8
x
| Word8
x forall a. Ord a => a -> a -> Bool
>= Word8
192 Bool -> Bool -> Bool
&& Word8
x forall a. Ord a => a -> a -> Bool
<= Word8
222 = Word8
x forall a. Num a => a -> a -> a
+ Word8
32
| Bool
otherwise = Word8
x
{-# INLINE toLower #-}