@@ -11,11 +11,15 @@
12module Streamly.Internal.Unicode.Char.Parser where
12module Streamly.Internal.Unicode.Char.Parser where
14+import Control.Applicative (Alternative(..))
14import Control.Monad.Catch (MonadCatch)
15import Control.Monad.Catch (MonadCatch)
16+import Data.Bits (Bits, (.|.), shiftL)
17+import Data.Char (ord)
15import Streamly.Internal.Data.Parser (Parser)
18import Streamly.Internal.Data.Parser (Parser)
17import qualified Data.Char as Char
20import qualified Data.Char as Char
18import qualified Streamly.Internal.Data.Parser as Parser
21import qualified Streamly.Internal.Data.Parser as Parser
22+import qualified Streamly.Internal.Data.Fold as Fold
20--------------------------------------------------------------------------------
24--------------------------------------------------------------------------------
21-- Character classification
25-- Character classification
@@ -80,3 +84,88 @@ CHAR_PARSER(asciiUpper,isAsciiUpper)
81CHAR_PARSER_SIG(asciiLower)
85CHAR_PARSER_SIG(asciiLower)
82CHAR_PARSER(asciiLower,isAsciiLower)
86CHAR_PARSER(asciiLower,isAsciiLower)
88+--------------------------------------------------------------------------------
90+--------------------------------------------------------------------------------
92+-- | Match a specific character.
93+char :: MonadCatch m => Char -> Parser m Char Char
94+char c = Parser.satisfy (== c)
96+--------------------------------------------------------------------------------
98+--------------------------------------------------------------------------------
100+-- | Parse and decode an unsigned decimal number.
101+{-# INLINE decimal #-}
102+decimal :: (MonadCatch m, Integral a) => Parser m Char a
103+decimal = Parser.takeWhile1 Char.isDigit (Fold.foldl' step 0)
107+ step a c = a * 10 + fromIntegral (ord c - 48)
110+-- | Parse and decode an unsigned hexadecimal number. The hex digits
111+-- @\'a\'@ through @\'f\'@ may be upper or lower case.
113+-- This parser does not accept a leading @\"0x\"@ string.
114+{-# INLINE hexadecimal #-}
115+hexadecimal :: (MonadCatch m, Integral a, Bits a) => Parser m Char a
116+hexadecimal = Parser.takeWhile1 isHexDigit (Fold.foldl' step 0)
121+ (c >= '0' && c <= '9')
122+ || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')
125+ | w >= 48 && w <= 57 = (a `shiftL` 4) .|. fromIntegral (w - 48)
126+ | w >= 97 = (a `shiftL` 4) .|. fromIntegral (w - 87)
127+ | otherwise = (a `shiftL` 4) .|. fromIntegral (w - 55)
133+-- | Parse a number with an optional leading @\'+\'@ or @\'-\'@ sign
135+{-# INLINE signed #-}
136+signed :: (Num a, MonadCatch m) => Parser m Char a -> Parser m Char a
137+signed p = (negate <$> (char '-' *> p)) <|> (char '+' *> p) <|> p
139+-- | Parse a 'Double'.
141+-- This parser accepts an optional leading sign character, followed by
142+-- at most one decimal digit. The syntax is similar to that accepted by
143+-- the 'read' function, with the exception that a trailing @\'.\'@ is
148+-- These examples use this helper:
150+-- Examples with behaviour identical to 'read', if you feed an empty
151+-- continuation to the first result:
153+-- > IS.parse double (IS.fromList "3") == 3.0
154+-- > IS.parse double (IS.fromList "3.1") == 3.1
155+-- > IS.parse double (IS.fromList "3e4") == 30000.0
156+-- > IS.parse double (IS.fromList "3.1e4") == 31000.0
157+-- > IS.parse double (IS.fromList "3e") == 30
159+-- Examples with behaviour identical to 'read':
161+-- > IS.parse (IS.fromList ".3") == error "Parse failed"
162+-- > IS.parse (IS.fromList "e3") == error "Parse failed"
164+-- Example of difference from 'read':
166+-- > IS.parse double (IS.fromList "3.foo") == 3.0
168+-- This function does not accept string representations of \"NaN\" or
170+double :: Parser m Char Double