Expose Streamly.Unicode.Parser (#1878)

Co-authored-by: Adithya Kumar <adithya@composewell.com>
This commit is contained in:
Ranjeet Ranjan 2022-10-04 16:53:24 +05:30 committed by GitHub
parent cee4d0fe98
commit a47303eb17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 15 deletions

View File

@ -14,11 +14,11 @@ module Streamly.Internal.Unicode.Char.Parser
(
-- * Generic
char
, charAnyCase
, charIgnoreCase
-- * Sequences
, string
, stringAnyCase
, stringIgnoreCase
, dropSpace
, dropSpace1
@ -33,7 +33,7 @@ module Streamly.Internal.Unicode.Char.Parser
, lower
, upper
, mark
, print
, printable
, punctuation
, separator
, space
@ -43,7 +43,7 @@ module Streamly.Internal.Unicode.Char.Parser
, digit
, octDigit
, hexDigit
, number
, numeric
-- * Numeric
, signed
@ -57,7 +57,6 @@ import Control.Applicative (Alternative(..))
import Control.Monad.Catch (MonadCatch)
import Data.Bits (Bits, (.|.), shiftL)
import Data.Char (ord)
import Prelude hiding (print)
import Streamly.Internal.Data.Parser (Parser)
import qualified Data.Char as Char
@ -77,7 +76,7 @@ import qualified Streamly.Internal.Data.Parser as Parser
-- XXX It may be possible to implement faster predicates for ASCII byte stream.
-- We can measure if there is a signficant difference and if so we can add such
-- predicates to Streamly.Unicode.Char.Parser.Latin1.
-- predicates to Streamly.Unicode.Parser.Latin1.
--
#define CHAR_PARSER_SIG(NAME) NAME :: MonadCatch m => Parser m Char Char
-- XXX Need to use the predicates from Unicode.Char module/unicode-data package
@ -101,8 +100,8 @@ CHAR_PARSER(alpha,isAlpha)
CHAR_PARSER_SIG(alphaNum)
CHAR_PARSER(alphaNum,isAlphaNum)
CHAR_PARSER_SIG(print)
CHAR_PARSER(print,isPrint)
CHAR_PARSER_SIG(printable)
CHAR_PARSER(printable,isPrint)
CHAR_PARSER_SIG(digit)
CHAR_PARSER(digit,isDigit)
@ -119,8 +118,8 @@ CHAR_PARSER(letter,isLetter)
CHAR_PARSER_SIG(mark)
CHAR_PARSER(mark,isMark)
CHAR_PARSER_SIG(number)
CHAR_PARSER(number,isNumber)
CHAR_PARSER_SIG(numeric)
CHAR_PARSER(numeric,isNumber)
CHAR_PARSER_SIG(punctuation)
CHAR_PARSER(punctuation,isPunctuation)
@ -154,9 +153,9 @@ char c = Parser.satisfy (== c)
-- XXX Case conversion may lead to change in number of chars
-- | Match a specific character ignoring case.
{-# INLINE charAnyCase #-}
charAnyCase :: MonadCatch m => Char -> Parser m Char Char
charAnyCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
{-# INLINE charIgnoreCase #-}
charIgnoreCase :: MonadCatch m => Char -> Parser m Char Char
charIgnoreCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
--------------------------------------------------------------------------------
-- Character sequences
@ -168,8 +167,8 @@ string = Parser.list
-- XXX Not accurate unicode case conversion
-- | Match the input with the supplied string and return it if successful.
stringAnyCase :: MonadCatch m => String -> Parser m Char String
stringAnyCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
stringIgnoreCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
-- | Drop /zero/ or more white space characters.
dropSpace :: MonadCatch m => Parser m Char ()

View File

@ -0,0 +1,55 @@
-- |
-- Module : Streamly.Unicode.Parser
-- Copyright : (c) 2021 Composewell Technologies
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
-- Stability : released
-- Portability : GHC
--
-- To parse a text input, use the decode routines from
-- "Streamly.Unicode.Stream" module to convert an input byte stream to a
-- Unicode Char stream and then use these parsers on the Char stream.
module Streamly.Unicode.Parser
(
-- * Generic
char
, charIgnoreCase
-- * Sequences
, string
, stringIgnoreCase
, dropSpace
, dropSpace1
-- * Classes
, alpha
, alphaNum
, letter
, ascii
, asciiLower
, asciiUpper
, latin1
, lower
, upper
, mark
, printable
, punctuation
, separator
, space
, symbol
-- * Digits
, digit
, octDigit
, hexDigit
, numeric
-- * Numbers
, signed
, decimal
, hexadecimal
)
where
import Streamly.Internal.Unicode.Char.Parser

View File

@ -357,6 +357,7 @@ library
, Streamly.Data.Unfold
, Streamly.Data.Array.Unboxed
, Streamly.Data.Array.Unboxed.Mut
, Streamly.Unicode.Parser
, Streamly.Unicode.Stream
, Streamly.Unicode.String
, Streamly.FileSystem.Handle

View File

@ -229,6 +229,7 @@ extra-source-files:
core/src/Streamly/Internal/Unicode/Char/Parser.hs
core/src/Streamly/Internal/Unicode/Stream.hs
core/src/Streamly/Internal/Unicode/String.hs
core/src/Streamly/Unicode/Parser.hs
core/src/Streamly/Unicode/Stream.hs
core/src/Streamly/Unicode/String.hs
core/src/Streamly/Console/Stdio.hs