on master help sign in
Expose Streamly.Unicode.Parser (#1878)

Co-authored-by: Adithya Kumar <adithya@composewell.com>

commita47303eb17db4ce47a974a1e93a5d221c7b7b394 authorRanjeet Ranjan <ranjeetr@composewell.com> dateTue 4 Oct 2022, 16:53 (4 years ago) parentcee4d0f
4 files changed+71−15
core/src/Streamly/Internal/Unicode/Char/Parser.hs+14−15 core/src/Streamly/Unicode/Parser.hs+55 core/streamly-core.cabal+1 streamly.cabal+1
core/src/Streamly/Internal/Unicode/Char/Parser.hs+14−15
@@ -14,11 +14,11 @@ module Streamly.Internal.Unicode.Char.Parser
1414 (
1515 -- * Generic
1616 char
17− , charAnyCase
17+ , charIgnoreCase
1818
1919 -- * Sequences
2020 , string
21− , stringAnyCase
21+ , stringIgnoreCase
2222 , dropSpace
2323 , dropSpace1
2424
@@ -33,7 +33,7 @@ module Streamly.Internal.Unicode.Char.Parser
3333 , lower
3434 , upper
3535 , mark
36− , print
36+ , printable
3737 , punctuation
3838 , separator
3939 , space
@@ -43,7 +43,7 @@ module Streamly.Internal.Unicode.Char.Parser
4343 , digit
4444 , octDigit
4545 , hexDigit
46− , number
46+ , numeric
4747
4848 -- * Numeric
4949 , signed
@@ -57,7 +57,6 @@ import Control.Applicative (Alternative(..))
5757import Control.Monad.Catch (MonadCatch)
5858import Data.Bits (Bits, (.|.), shiftL)
5959import Data.Char (ord)
60−import Prelude hiding (print)
6160import Streamly.Internal.Data.Parser (Parser)
6261
6362import qualified Data.Char as Char
@@ -77,7 +76,7 @@ import qualified Streamly.Internal.Data.Parser as Parser
7776
7877-- XXX It may be possible to implement faster predicates for ASCII byte stream.
7978-- We can measure if there is a signficant difference and if so we can add such
80−-- predicates to Streamly.Unicode.Char.Parser.Latin1.
79+-- predicates to Streamly.Unicode.Parser.Latin1.
8180--
8281#define CHAR_PARSER_SIG(NAME) NAME :: MonadCatch m => Parser m Char Char
8382-- XXX Need to use the predicates from Unicode.Char module/unicode-data package
@@ -101,8 +100,8 @@ CHAR_PARSER(alpha,isAlpha)
101100CHAR_PARSER_SIG(alphaNum)
102101CHAR_PARSER(alphaNum,isAlphaNum)
103102
104−CHAR_PARSER_SIG(print)
105−CHAR_PARSER(print,isPrint)
103+CHAR_PARSER_SIG(printable)
104+CHAR_PARSER(printable,isPrint)
106105
107106CHAR_PARSER_SIG(digit)
108107CHAR_PARSER(digit,isDigit)
@@ -119,8 +118,8 @@ CHAR_PARSER(letter,isLetter)
119118CHAR_PARSER_SIG(mark)
120119CHAR_PARSER(mark,isMark)
121120
122−CHAR_PARSER_SIG(number)
123−CHAR_PARSER(number,isNumber)
121+CHAR_PARSER_SIG(numeric)
122+CHAR_PARSER(numeric,isNumber)
124123
125124CHAR_PARSER_SIG(punctuation)
126125CHAR_PARSER(punctuation,isPunctuation)
@@ -154,9 +153,9 @@ char c = Parser.satisfy (== c)
154153
155154-- XXX Case conversion may lead to change in number of chars
156155-- | Match a specific character ignoring case.
157−{-# INLINE charAnyCase #-}
158−charAnyCase :: MonadCatch m => Char -> Parser m Char Char
159−charAnyCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
156+{-# INLINE charIgnoreCase #-}
157+charIgnoreCase :: MonadCatch m => Char -> Parser m Char Char
158+charIgnoreCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
160159
161160--------------------------------------------------------------------------------
162161-- Character sequences
@@ -168,8 +167,8 @@ string = Parser.list
168167
169168-- XXX Not accurate unicode case conversion
170169-- | Match the input with the supplied string and return it if successful.
171−stringAnyCase :: MonadCatch m => String -> Parser m Char String
172−stringAnyCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
170+stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
171+stringIgnoreCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
173172
174173-- | Drop /zero/ or more white space characters.
175174dropSpace :: MonadCatch m => Parser m Char ()
core/src/Streamly/Unicode/Parser.hsnew file+55
@@ -0,0 +1,55 @@
1+-- |
2+-- Module : Streamly.Unicode.Parser
3+-- Copyright : (c) 2021 Composewell Technologies
4+-- License : BSD-3-Clause
5+-- Maintainer : streamly@composewell.com
6+-- Stability : released
7+-- Portability : GHC
8+--
9+-- To parse a text input, use the decode routines from
10+-- "Streamly.Unicode.Stream" module to convert an input byte stream to a
11+-- Unicode Char stream and then use these parsers on the Char stream.
12+
13+module Streamly.Unicode.Parser
14+ (
15+ -- * Generic
16+ char
17+ , charIgnoreCase
18+
19+ -- * Sequences
20+ , string
21+ , stringIgnoreCase
22+ , dropSpace
23+ , dropSpace1
24+
25+ -- * Classes
26+ , alpha
27+ , alphaNum
28+ , letter
29+ , ascii
30+ , asciiLower
31+ , asciiUpper
32+ , latin1
33+ , lower
34+ , upper
35+ , mark
36+ , printable
37+ , punctuation
38+ , separator
39+ , space
40+ , symbol
41+
42+ -- * Digits
43+ , digit
44+ , octDigit
45+ , hexDigit
46+ , numeric
47+
48+ -- * Numbers
49+ , signed
50+ , decimal
51+ , hexadecimal
52+ )
53+where
54+
55+import Streamly.Internal.Unicode.Char.Parser
core/streamly-core.cabal+1
@@ -357,6 +357,7 @@ library
357357 , Streamly.Data.Unfold
358358 , Streamly.Data.Array.Unboxed
359359 , Streamly.Data.Array.Unboxed.Mut
360+ , Streamly.Unicode.Parser
360361 , Streamly.Unicode.Stream
361362 , Streamly.Unicode.String
362363 , Streamly.FileSystem.Handle
streamly.cabal+1
@@ -229,6 +229,7 @@ extra-source-files:
229229 core/src/Streamly/Internal/Unicode/Char/Parser.hs
230230 core/src/Streamly/Internal/Unicode/Stream.hs
231231 core/src/Streamly/Internal/Unicode/String.hs
232+ core/src/Streamly/Unicode/Parser.hs
232233 core/src/Streamly/Unicode/Stream.hs
233234 core/src/Streamly/Unicode/String.hs
234235 core/src/Streamly/Console/Stdio.hs