{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Text.Megaparsec.Char
(
newline,
crlf,
eol,
tab,
space,
hspace,
space1,
hspace1,
controlChar,
spaceChar,
upperChar,
lowerChar,
letterChar,
alphaNumChar,
printChar,
digitChar,
binDigitChar,
octDigitChar,
hexDigitChar,
markChar,
numberChar,
punctuationChar,
symbolChar,
separatorChar,
asciiChar,
latin1Char,
charCategory,
categoryName,
char,
char',
string,
string',
)
where
import Control.Applicative
import Data.Char
import Data.Functor (void)
import Data.Proxy
import Text.Megaparsec
import Text.Megaparsec.Common
newline :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
newline :: m (Token s)
newline = Token s -> m (Token s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char Char
Token s
'\n'
{-# INLINE newline #-}
crlf :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m (Tokens s)
crlf :: m (Tokens s)
crlf = Tokens s -> m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Tokens s -> m (Tokens s)
string (Proxy s -> [Token s] -> Tokens s
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s) [Char]
[Token s]
"\r\n")
{-# INLINE crlf #-}
eol :: forall e s m. (MonadParsec e s m, Token s ~ Char) => m (Tokens s)
eol :: m (Tokens s)
eol =
(Proxy s -> Token s -> Tokens s
forall s. Stream s => Proxy s -> Token s -> Tokens s
tokenToChunk (Proxy s
forall k (t :: k). Proxy t
Proxy :: Proxy s) (Char -> Tokens s) -> m Char -> m (Tokens s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Char
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Token s)
newline)
m (Tokens s) -> m (Tokens s) -> m (Tokens s)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> m (Tokens s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
m (Tokens s)
crlf
m (Tokens s) -> [Char] -> m (Tokens s)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"end of line"
{-# INLINE eol #-}
tab :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
tab :: m (Token s)
tab = Token s -> m (Token s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char Char
Token s
'\t'
{-# INLINE tab #-}
space :: (MonadParsec e s m, Token s ~ Char) => m ()
space :: m ()
space = m (Tokens s) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Tokens s) -> m ()) -> m (Tokens s) -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
takeWhileP ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"white space") Char -> Bool
Token s -> Bool
isSpace
{-# INLINE space #-}
hspace :: (MonadParsec e s m, Token s ~ Char) => m ()
hspace :: m ()
hspace = m (Tokens s) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Tokens s) -> m ()) -> m (Tokens s) -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
takeWhileP ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"white space") Char -> Bool
Token s -> Bool
isHSpace
{-# INLINE hspace #-}
space1 :: (MonadParsec e s m, Token s ~ Char) => m ()
space1 :: m ()
space1 = m (Tokens s) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Tokens s) -> m ()) -> m (Tokens s) -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
takeWhile1P ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"white space") Char -> Bool
Token s -> Bool
isSpace
{-# INLINE space1 #-}
hspace1 :: (MonadParsec e s m, Token s ~ Char) => m ()
hspace1 :: m ()
hspace1 = m (Tokens s) -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m (Tokens s) -> m ()) -> m (Tokens s) -> m ()
forall a b. (a -> b) -> a -> b
$ Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Maybe [Char] -> (Token s -> Bool) -> m (Tokens s)
takeWhile1P ([Char] -> Maybe [Char]
forall a. a -> Maybe a
Just [Char]
"white space") Char -> Bool
Token s -> Bool
isHSpace
{-# INLINE hspace1 #-}
controlChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
controlChar :: m (Token s)
controlChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isControl m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"control character"
{-# INLINE controlChar #-}
spaceChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
spaceChar :: m (Token s)
spaceChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isSpace m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"white space"
{-# INLINE spaceChar #-}
upperChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
upperChar :: m (Token s)
upperChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isUpper m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"uppercase letter"
{-# INLINE upperChar #-}
lowerChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
lowerChar :: m (Token s)
lowerChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isLower m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"lowercase letter"
{-# INLINE lowerChar #-}
letterChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
letterChar :: m (Token s)
letterChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isLetter m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"letter"
{-# INLINE letterChar #-}
alphaNumChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
alphaNumChar :: m (Token s)
alphaNumChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isAlphaNum m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"alphanumeric character"
{-# INLINE alphaNumChar #-}
printChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
printChar :: m (Token s)
printChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isPrint m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"printable character"
{-# INLINE printChar #-}
digitChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
digitChar :: m (Token s)
digitChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isDigit m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"digit"
{-# INLINE digitChar #-}
binDigitChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
binDigitChar :: m (Token s)
binDigitChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isBinDigit m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"binary digit"
where
isBinDigit :: Char -> Bool
isBinDigit Char
x = Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'1'
{-# INLINE binDigitChar #-}
octDigitChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
octDigitChar :: m (Token s)
octDigitChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isOctDigit m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"octal digit"
{-# INLINE octDigitChar #-}
hexDigitChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
hexDigitChar :: m (Token s)
hexDigitChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isHexDigit m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"hexadecimal digit"
{-# INLINE hexDigitChar #-}
markChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
markChar :: m (Token s)
markChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isMark m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"mark character"
{-# INLINE markChar #-}
numberChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
numberChar :: m (Token s)
numberChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isNumber m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"numeric character"
{-# INLINE numberChar #-}
punctuationChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
punctuationChar :: m (Token s)
punctuationChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isPunctuation m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"punctuation"
{-# INLINE punctuationChar #-}
symbolChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
symbolChar :: m (Token s)
symbolChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isSymbol m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"symbol"
{-# INLINE symbolChar #-}
separatorChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
separatorChar :: m (Token s)
separatorChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isSeparator m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"separator"
{-# INLINE separatorChar #-}
asciiChar :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
asciiChar :: m (Token s)
asciiChar = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isAscii m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"ASCII character"
{-# INLINE asciiChar #-}
latin1Char :: (MonadParsec e s m, Token s ~ Char) => m (Token s)
latin1Char :: m (Token s)
latin1Char = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy Char -> Bool
Token s -> Bool
isLatin1 m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> [Char]
"Latin-1 character"
{-# INLINE latin1Char #-}
charCategory ::
(MonadParsec e s m, Token s ~ Char) =>
GeneralCategory ->
m (Token s)
charCategory :: GeneralCategory -> m (Token s)
charCategory GeneralCategory
cat = (Token s -> Bool) -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
(Token s -> Bool) -> m (Token s)
satisfy ((GeneralCategory -> GeneralCategory -> Bool
forall a. Eq a => a -> a -> Bool
== GeneralCategory
cat) (GeneralCategory -> Bool)
-> (Char -> GeneralCategory) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> GeneralCategory
generalCategory) m Char -> [Char] -> m Char
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> [Char] -> m a
<?> GeneralCategory -> [Char]
categoryName GeneralCategory
cat
{-# INLINE charCategory #-}
categoryName :: GeneralCategory -> String
categoryName :: GeneralCategory -> [Char]
categoryName = \case
GeneralCategory
UppercaseLetter -> [Char]
"uppercase letter"
GeneralCategory
LowercaseLetter -> [Char]
"lowercase letter"
GeneralCategory
TitlecaseLetter -> [Char]
"titlecase letter"
GeneralCategory
ModifierLetter -> [Char]
"modifier letter"
GeneralCategory
OtherLetter -> [Char]
"other letter"
GeneralCategory
NonSpacingMark -> [Char]
"non-spacing mark"
GeneralCategory
SpacingCombiningMark -> [Char]
"spacing combining mark"
GeneralCategory
EnclosingMark -> [Char]
"enclosing mark"
GeneralCategory
DecimalNumber -> [Char]
"decimal number character"
GeneralCategory
LetterNumber -> [Char]
"letter number character"
GeneralCategory
OtherNumber -> [Char]
"other number character"
GeneralCategory
ConnectorPunctuation -> [Char]
"connector punctuation"
GeneralCategory
DashPunctuation -> [Char]
"dash punctuation"
GeneralCategory
OpenPunctuation -> [Char]
"open punctuation"
GeneralCategory
ClosePunctuation -> [Char]
"close punctuation"
GeneralCategory
InitialQuote -> [Char]
"initial quote"
GeneralCategory
FinalQuote -> [Char]
"final quote"
GeneralCategory
OtherPunctuation -> [Char]
"other punctuation"
GeneralCategory
MathSymbol -> [Char]
"math symbol"
GeneralCategory
CurrencySymbol -> [Char]
"currency symbol"
GeneralCategory
ModifierSymbol -> [Char]
"modifier symbol"
GeneralCategory
OtherSymbol -> [Char]
"other symbol"
GeneralCategory
Space -> [Char]
"white space"
GeneralCategory
LineSeparator -> [Char]
"line separator"
GeneralCategory
ParagraphSeparator -> [Char]
"paragraph separator"
GeneralCategory
Control -> [Char]
"control character"
GeneralCategory
Format -> [Char]
"format character"
GeneralCategory
Surrogate -> [Char]
"surrogate character"
GeneralCategory
PrivateUse -> [Char]
"private-use Unicode character"
GeneralCategory
NotAssigned -> [Char]
"non-assigned Unicode character"
char :: (MonadParsec e s m, Token s ~ Char) => Token s -> m (Token s)
char :: Token s -> m (Token s)
char = Token s -> m (Token s)
forall e s (m :: * -> *).
MonadParsec e s m =>
Token s -> m (Token s)
single
{-# INLINE char #-}
char' :: (MonadParsec e s m, Token s ~ Char) => Token s -> m (Token s)
char' :: Token s -> m (Token s)
char' Token s
c =
[m Char] -> m Char
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Alternative m) =>
f (m a) -> m a
choice
[ Token s -> m (Token s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char (Char -> Char
toLower Char
Token s
c),
Token s -> m (Token s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char (Char -> Char
toUpper Char
Token s
c),
Token s -> m (Token s)
forall e s (m :: * -> *).
(MonadParsec e s m, Token s ~ Char) =>
Token s -> m (Token s)
char (Char -> Char
toTitle Char
Token s
c)
]
{-# INLINE char' #-}
isHSpace :: Char -> Bool
isHSpace :: Char -> Bool
isHSpace Char
x = Char -> Bool
isSpace Char
x Bool -> Bool -> Bool
&& Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\n' Bool -> Bool -> Bool
&& Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r'