module Hydrogen.Parsing.Char (
module Hydrogen.Parsing
, oneOf
, noneOf
, spaces
, space
, newline
, tab
, upper
, lower
, alphaNum
, letter
, digit
, hexDigit
, char
, anyChar
, satisfy
, string
, number
, positiveNumber
, negativeNumber
, decimal
, name
, name_
, keyword
, keyword_
, between'
) where
import Hydrogen.Prelude hiding ((<|>), many, optional)
import Hydrogen.Parsing
import Text.Parsec.Char hiding (newline)
name :: (Monad m, Stream s m Char) => ParsecT s u m String
name = liftA2 (:) letter (many alphaNum)
name_ :: (Monad m, Stream s m Char) => ParsecT s u m String
name_ = liftA2 (:) (letter <|> char '_') (many (alphaNum <|> char '_'))
keyword :: (Monad m, Stream s m Char) => String -> ParsecT s u m String
keyword w = string w <* notFollowedBy alphaNum
keyword_ :: (Monad m, Stream s m Char) => String -> ParsecT s u m ()
keyword_ w = const () <$> (string w <* notFollowedBy alphaNum)
between' :: (Monad m, Stream s m Char) => Char -> Char -> ParsecT s u m t -> ParsecT s u m t
between' a b = between (char a) (char b)
number, positiveNumber, negativeNumber
:: (Monad m, Stream s m Char, Read a, Num a, Integral a) => ParsecT s u m a
number = negativeNumber <|> positiveNumber
positiveNumber = (read <$> many1 digit)
negativeNumber = negate . read <$> (char '-' >> many1 digit)
decimal, positiveDecimal, negativeDecimal
:: (Monad m, Stream s m Char, Read a, Num a, RealFrac a) => ParsecT s u m a
decimal = negativeDecimal <|> positiveDecimal
positiveDecimal = fst . head . readFloat <$> liftM2 (++) (many1 digit) (option "" (exp_ <|> digits))
where
digits = liftA2 (:) (char '.') (many1 digit)
exp_ = concat <$> sequence [return <$> char 'e', option "" (string "-"), many1 digit]
negativeDecimal = negate <$> (char '-' >> positiveDecimal)
newline :: (Monad m, Stream s m Char) => ParsecT s u m Char
newline = char '\n' <|> (char '\r' <* optional (char '\n'))