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