on master help sign in
Expose Streamly.Unicode.Parser (#1878)
a47303eRanjeet Ranjan
core/src/Streamly/Internal/Unicode/Char/Parser.hs+14−15
@@ -14,11 +14,11 @@ module Streamly.Internal.Unicode.Char.Parser
14 (
14 (
15 -- * Generic
15 -- * Generic
16 char
16 char
17− , charAnyCase
17+ , charIgnoreCase
18
18
19 -- * Sequences
19 -- * Sequences
20 , string
20 , string
21− , stringAnyCase
21+ , stringIgnoreCase
22 , dropSpace
22 , dropSpace
23 , dropSpace1
23 , dropSpace1
24
24
@@ -33,7 +33,7 @@ module Streamly.Internal.Unicode.Char.Parser
33 , lower
33 , lower
34 , upper
34 , upper
35 , mark
35 , mark
36− , print
36+ , printable
37 , punctuation
37 , punctuation
38 , separator
38 , separator
39 , space
39 , space
@@ -43,7 +43,7 @@ module Streamly.Internal.Unicode.Char.Parser
43 , digit
43 , digit
44 , octDigit
44 , octDigit
45 , hexDigit
45 , hexDigit
46− , number
46+ , numeric
47
47
48 -- * Numeric
48 -- * Numeric
49 , signed
49 , signed
@@ -57,7 +57,6 @@ import Control.Applicative (Alternative(..))
57import Control.Monad.Catch (MonadCatch)
57import Control.Monad.Catch (MonadCatch)
58import Data.Bits (Bits, (.|.), shiftL)
58import Data.Bits (Bits, (.|.), shiftL)
59import Data.Char (ord)
59import Data.Char (ord)
60−import Prelude hiding (print)
61import Streamly.Internal.Data.Parser (Parser)
60import Streamly.Internal.Data.Parser (Parser)
62
61
63import qualified Data.Char as Char
62import qualified Data.Char as Char
@@ -77,7 +76,7 @@ import qualified Streamly.Internal.Data.Parser as Parser
77
76
78-- XXX It may be possible to implement faster predicates for ASCII byte stream.
77-- XXX It may be possible to implement faster predicates for ASCII byte stream.
79-- We can measure if there is a signficant difference and if so we can add such
78-- 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.
81--
80--
82#define CHAR_PARSER_SIG(NAME) NAME :: MonadCatch m => Parser m Char Char
81#define CHAR_PARSER_SIG(NAME) NAME :: MonadCatch m => Parser m Char Char
83-- XXX Need to use the predicates from Unicode.Char module/unicode-data package
82-- XXX Need to use the predicates from Unicode.Char module/unicode-data package
@@ -101,8 +100,8 @@ CHAR_PARSER(alpha,isAlpha)
101CHAR_PARSER_SIG(alphaNum)
100CHAR_PARSER_SIG(alphaNum)
102CHAR_PARSER(alphaNum,isAlphaNum)
101CHAR_PARSER(alphaNum,isAlphaNum)
103
102
104−CHAR_PARSER_SIG(print)
103+CHAR_PARSER_SIG(printable)
105−CHAR_PARSER(print,isPrint)
104+CHAR_PARSER(printable,isPrint)
106
105
107CHAR_PARSER_SIG(digit)
106CHAR_PARSER_SIG(digit)
108CHAR_PARSER(digit,isDigit)
107CHAR_PARSER(digit,isDigit)
@@ -119,8 +118,8 @@ CHAR_PARSER(letter,isLetter)
119CHAR_PARSER_SIG(mark)
118CHAR_PARSER_SIG(mark)
120CHAR_PARSER(mark,isMark)
119CHAR_PARSER(mark,isMark)
121
120
122−CHAR_PARSER_SIG(number)
121+CHAR_PARSER_SIG(numeric)
123−CHAR_PARSER(number,isNumber)
122+CHAR_PARSER(numeric,isNumber)
124
123
125CHAR_PARSER_SIG(punctuation)
124CHAR_PARSER_SIG(punctuation)
126CHAR_PARSER(punctuation,isPunctuation)
125CHAR_PARSER(punctuation,isPunctuation)
@@ -154,9 +153,9 @@ char c = Parser.satisfy (== c)
154
153
155-- XXX Case conversion may lead to change in number of chars
154-- XXX Case conversion may lead to change in number of chars
156-- | Match a specific character ignoring case.
155-- | Match a specific character ignoring case.
157−{-# INLINE charAnyCase #-}
156+{-# INLINE charIgnoreCase #-}
158−charAnyCase :: MonadCatch m => Char -> Parser m Char Char
157+charIgnoreCase :: MonadCatch m => Char -> Parser m Char Char
159−charAnyCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
158+charIgnoreCase c = Parser.lmap Char.toLower (Parser.satisfy (== Char.toLower c))
160
159
161--------------------------------------------------------------------------------
160--------------------------------------------------------------------------------
162-- Character sequences
161-- Character sequences
@@ -168,8 +167,8 @@ string = Parser.list
168
167
169-- XXX Not accurate unicode case conversion
168-- XXX Not accurate unicode case conversion
170-- | Match the input with the supplied string and return it if successful.
169-- | Match the input with the supplied string and return it if successful.
171−stringAnyCase :: MonadCatch m => String -> Parser m Char String
170+stringIgnoreCase :: MonadCatch m => String -> Parser m Char String
172−stringAnyCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
171+stringIgnoreCase s = Parser.lmap Char.toLower (Parser.list (map Char.toLower s))
173
172
174-- | Drop /zero/ or more white space characters.
173-- | Drop /zero/ or more white space characters.
175dropSpace :: MonadCatch m => Parser m Char ()
174dropSpace :: 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
357 , Streamly.Data.Unfold
357 , Streamly.Data.Unfold
358 , Streamly.Data.Array.Unboxed
358 , Streamly.Data.Array.Unboxed
359 , Streamly.Data.Array.Unboxed.Mut
359 , Streamly.Data.Array.Unboxed.Mut
360+ , Streamly.Unicode.Parser
360 , Streamly.Unicode.Stream
361 , Streamly.Unicode.Stream
361 , Streamly.Unicode.String
362 , Streamly.Unicode.String
362 , Streamly.FileSystem.Handle
363 , Streamly.FileSystem.Handle
streamly.cabal+1
@@ -229,6 +229,7 @@ extra-source-files:
229 core/src/Streamly/Internal/Unicode/Char/Parser.hs
229 core/src/Streamly/Internal/Unicode/Char/Parser.hs
230 core/src/Streamly/Internal/Unicode/Stream.hs
230 core/src/Streamly/Internal/Unicode/Stream.hs
231 core/src/Streamly/Internal/Unicode/String.hs
231 core/src/Streamly/Internal/Unicode/String.hs
232+ core/src/Streamly/Unicode/Parser.hs
232 core/src/Streamly/Unicode/Stream.hs
233 core/src/Streamly/Unicode/Stream.hs
233 core/src/Streamly/Unicode/String.hs
234 core/src/Streamly/Unicode/String.hs
234 core/src/Streamly/Console/Stdio.hs
235 core/src/Streamly/Console/Stdio.hs