1
1
-- | Functions for working with streams of tokens.
2
+ -- |
3
+ -- | This module is a port of the Haskell
4
+ -- | [__Text.Parsec.Token__](https://hackage.haskell.org/package/docs/Text-Parsec-Token.html)
5
+ -- | module.
2
6
3
7
module Text.Parsing.Parser.Token
4
8
( token
@@ -11,14 +15,7 @@ module Text.Parsing.Parser.Token
11
15
, TokenParser
12
16
, GenTokenParser
13
17
, makeTokenParser
14
- -- should these be exported? Maybe they should go in a different module?
15
- , digit
16
- , hexDigit
17
- , octDigit
18
- , upper
19
- , space
20
- , letter
21
- , alphaNum
18
+ , module Text.Parsing.Parser.String.Basic
22
19
) where
23
20
24
21
import Prelude hiding (between , when )
@@ -28,7 +25,7 @@ import Control.Monad.State (get, gets, modify_)
28
25
import Control.MonadPlus (guard , (<|>))
29
26
import Data.Array as Array
30
27
import Data.Char (fromCharCode , toCharCode )
31
- import Data.CodePoint.Unicode (hexDigitToInt , isAlpha , isAlphaNum , isDecDigit , isHexDigit , isOctDigit , isSpace , isUpper )
28
+ import Data.CodePoint.Unicode (hexDigitToInt , isAlpha , isSpace )
32
29
import Data.Either (Either (..))
33
30
import Data.Foldable (foldl , foldr )
34
31
import Data.Identity (Identity )
@@ -37,8 +34,7 @@ import Data.List (List(..))
37
34
import Data.List as List
38
35
import Data.List.NonEmpty (NonEmptyList )
39
36
import Data.Maybe (Maybe (..), maybe )
40
- import Data.Number (fromString )
41
- import Data.String (CodePoint , null , toLower )
37
+ import Data.String (null , toLower )
42
38
import Data.String.CodePoints (codePointFromChar )
43
39
import Data.String.CodeUnits (singleton , toChar ) as CodeUnits
44
40
import Data.String.CodeUnits as SCU
@@ -49,7 +45,8 @@ import Text.Parsing.Parser (ParseState(..), ParserT, consume, fail)
49
45
import Text.Parsing.Parser.Combinators (between , choice , notFollowedBy , option , sepBy , sepBy1 , skipMany , skipMany1 , try , tryRethrow , (<?>), (<??>))
50
46
import Text.Parsing.Parser.Pos (Position )
51
47
import Text.Parsing.Parser.String (char , noneOf , oneOf , satisfy , satisfyCodePoint , string )
52
- import Text.Parsing.Parser.String as Parser.String
48
+ import Text.Parsing.Parser.String.Basic as Basic
49
+ import Text.Parsing.Parser.String.Basic (digit , hexDigit , octDigit , upper , space , letter , alphaNum )
53
50
54
51
-- | A parser which returns the first token in the stream.
55
52
token :: forall m a . Monad m => (a -> Position ) -> ParserT (List a ) m a
@@ -477,7 +474,7 @@ makeTokenParser (LanguageDef languageDef) =
477
474
escapeEmpty = char ' &'
478
475
479
476
escapeGap :: ParserT String m Char
480
- escapeGap = Array .some space *> char ' \\ ' <?> " end of string gap"
477
+ escapeGap = Array .some Basic . space *> char ' \\ ' <?> " end of string gap"
481
478
482
479
-- -- escape codes
483
480
escapeCode :: ParserT String m Char
@@ -487,16 +484,16 @@ makeTokenParser (LanguageDef languageDef) =
487
484
charControl :: ParserT String m Char
488
485
charControl = do
489
486
_ <- char ' ^'
490
- code <- upper
487
+ code <- Basic . upper
491
488
case fromCharCode (toCharCode code - toCharCode ' A' + 1 ) of
492
489
Just c -> pure c
493
490
Nothing -> fail " invalid character code (should not happen)"
494
491
495
492
charNum :: ParserT String m Char
496
493
charNum = do
497
494
code <- decimal
498
- <|> (char ' o' *> number 8 octDigit)
499
- <|> (char ' x' *> number 16 hexDigit)
495
+ <|> (char ' o' *> number 8 Basic . octDigit)
496
+ <|> (char ' x' *> number 16 Basic . hexDigit)
500
497
if code > 0x10FFFF then fail " invalid escape sequence"
501
498
else case fromCharCode code of
502
499
Just c -> pure c
@@ -610,14 +607,8 @@ makeTokenParser (LanguageDef languageDef) =
610
607
natural = lexeme nat <?> " natural"
611
608
612
609
-- floats
613
-
614
610
floating :: ParserT String m Number
615
- -- floating = decimal >>= fractExponent
616
- floating = do
617
- Tuple section _ <- Parser.String .match $ skipMany1 $ oneOf [ ' -' , ' .' , ' e' , ' 0' , ' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' ]
618
- case fromString section of
619
- Nothing -> fail $ " Could not parse Number " <> section
620
- Just x -> pure x
611
+ floating = decimal >>= fractExponent
621
612
622
613
natFloat :: ParserT String m (Either Int Number )
623
614
natFloat = char ' 0' *> zeroNumFloat
@@ -654,7 +645,7 @@ makeTokenParser (LanguageDef languageDef) =
654
645
fraction :: ParserT String m Number
655
646
fraction = " fraction" <??> do
656
647
_ <- char ' .'
657
- digits <- Array .some digit <?> " fraction"
648
+ digits <- Array .some Basic . digit <?> " fraction"
658
649
maybe (fail " not digit" ) pure $ foldr op (Just 0.0 ) digits
659
650
where
660
651
op :: Char -> Maybe Number -> Maybe Number
@@ -696,13 +687,13 @@ makeTokenParser (LanguageDef languageDef) =
696
687
(hexadecimal <|> octal <|> decimal <|> pure 0 ) <?> " "
697
688
698
689
decimal :: ParserT String m Int
699
- decimal = number 10 digit
690
+ decimal = number 10 Basic . digit
700
691
701
692
hexadecimal :: ParserT String m Int
702
- hexadecimal = oneOf [ ' x' , ' X' ] *> number 16 hexDigit
693
+ hexadecimal = oneOf [ ' x' , ' X' ] *> number 16 Basic . hexDigit
703
694
704
695
octal :: ParserT String m Int
705
- octal = oneOf [ ' o' , ' O' ] *> number 8 octDigit
696
+ octal = oneOf [ ' o' , ' O' ] *> number 8 Basic . octDigit
706
697
707
698
number :: Int -> ParserT String m Char -> ParserT String m Int
708
699
number base baseDigit = do
@@ -886,38 +877,3 @@ inCommentSingle (LanguageDef languageDef) =
886
877
startEnd :: Array Char
887
878
startEnd = SCU .toCharArray languageDef.commentEnd <> SCU .toCharArray languageDef.commentStart
888
879
889
- -- -----------------------------------------------------------------------
890
- -- Helper functions that should maybe go in Text.Parsing.Parser.String --
891
- -- -----------------------------------------------------------------------
892
-
893
- satisfyCP :: forall m . Monad m => (CodePoint -> Boolean ) -> ParserT String m Char
894
- satisfyCP p = satisfy (p <<< codePointFromChar)
895
-
896
- -- | Parse a digit. Matches any char that satisfies `Data.CodePoint.Unicode.isDecDigit`.
897
- digit :: forall m . Monad m => ParserT String m Char
898
- digit = satisfyCP isDecDigit <?> " digit"
899
-
900
- -- | Parse a hex digit. Matches any char that satisfies `Data.CodePoint.Unicode.isHexDigit`.
901
- hexDigit :: forall m . Monad m => ParserT String m Char
902
- hexDigit = satisfyCP isHexDigit <?> " hex digit"
903
-
904
- -- | Parse an octal digit. Matches any char that satisfies `Data.CodePoint.Unicode.isOctDigit`.
905
- octDigit :: forall m . Monad m => ParserT String m Char
906
- octDigit = satisfyCP isOctDigit <?> " oct digit"
907
-
908
- -- | Parse an uppercase letter. Matches any char that satisfies `Data.CodePoint.Unicode.isUpper`.
909
- upper :: forall m . Monad m => ParserT String m Char
910
- upper = satisfyCP isUpper <?> " uppercase letter"
911
-
912
- -- | Parse a space character. Matches any char that satisfies `Data.CodePoint.Unicode.isSpace`.
913
- space :: forall m . Monad m => ParserT String m Char
914
- space = satisfyCP isSpace <?> " space"
915
-
916
- -- | Parse an alphabetical character. Matches any char that satisfies `Data.CodePoint.Unicode.isAlpha`.
917
- letter :: forall m . Monad m => ParserT String m Char
918
- letter = satisfyCP isAlpha <?> " letter"
919
-
920
- -- | Parse an alphabetical or numerical character.
921
- -- | Matches any char that satisfies `Data.CodePoint.Unicode.isAlphaNum`.
922
- alphaNum :: forall m . Monad m => ParserT String m Char
923
- alphaNum = satisfyCP isAlphaNum <?> " letter or digit"
0 commit comments