11-- | 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.
26
37module Text.Parsing.Parser.Token
48 ( token
@@ -11,14 +15,7 @@ module Text.Parsing.Parser.Token
1115 , TokenParser
1216 , GenTokenParser
1317 , 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
2219 ) where
2320
2421import Prelude hiding (between , when )
@@ -28,7 +25,7 @@ import Control.Monad.State (get, gets, modify_)
2825import Control.MonadPlus (guard , (<|>))
2926import Data.Array as Array
3027import 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 )
3229import Data.Either (Either (..))
3330import Data.Foldable (foldl , foldr )
3431import Data.Identity (Identity )
@@ -37,8 +34,7 @@ import Data.List (List(..))
3734import Data.List as List
3835import Data.List.NonEmpty (NonEmptyList )
3936import Data.Maybe (Maybe (..), maybe )
40- import Data.Number (fromString )
41- import Data.String (CodePoint , null , toLower )
37+ import Data.String (null , toLower )
4238import Data.String.CodePoints (codePointFromChar )
4339import Data.String.CodeUnits (singleton , toChar ) as CodeUnits
4440import Data.String.CodeUnits as SCU
@@ -49,7 +45,8 @@ import Text.Parsing.Parser (ParseState(..), ParserT, consume, fail)
4945import Text.Parsing.Parser.Combinators (between , choice , notFollowedBy , option , sepBy , sepBy1 , skipMany , skipMany1 , try , tryRethrow , (<?>), (<??>))
5046import Text.Parsing.Parser.Pos (Position )
5147import 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 )
5350
5451-- | A parser which returns the first token in the stream.
5552token :: forall m a . Monad m => (a -> Position ) -> ParserT (List a ) m a
@@ -477,7 +474,7 @@ makeTokenParser (LanguageDef languageDef) =
477474 escapeEmpty = char ' &'
478475
479476 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"
481478
482479 -- -- escape codes
483480 escapeCode :: ParserT String m Char
@@ -487,16 +484,16 @@ makeTokenParser (LanguageDef languageDef) =
487484 charControl :: ParserT String m Char
488485 charControl = do
489486 _ <- char ' ^'
490- code <- upper
487+ code <- Basic . upper
491488 case fromCharCode (toCharCode code - toCharCode ' A' + 1 ) of
492489 Just c -> pure c
493490 Nothing -> fail " invalid character code (should not happen)"
494491
495492 charNum :: ParserT String m Char
496493 charNum = do
497494 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)
500497 if code > 0x10FFFF then fail " invalid escape sequence"
501498 else case fromCharCode code of
502499 Just c -> pure c
@@ -610,14 +607,8 @@ makeTokenParser (LanguageDef languageDef) =
610607 natural = lexeme nat <?> " natural"
611608
612609 -- floats
613-
614610 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
621612
622613 natFloat :: ParserT String m (Either Int Number )
623614 natFloat = char ' 0' *> zeroNumFloat
@@ -654,7 +645,7 @@ makeTokenParser (LanguageDef languageDef) =
654645 fraction :: ParserT String m Number
655646 fraction = " fraction" <??> do
656647 _ <- char ' .'
657- digits <- Array .some digit <?> " fraction"
648+ digits <- Array .some Basic . digit <?> " fraction"
658649 maybe (fail " not digit" ) pure $ foldr op (Just 0.0 ) digits
659650 where
660651 op :: Char -> Maybe Number -> Maybe Number
@@ -696,13 +687,13 @@ makeTokenParser (LanguageDef languageDef) =
696687 (hexadecimal <|> octal <|> decimal <|> pure 0 ) <?> " "
697688
698689 decimal :: ParserT String m Int
699- decimal = number 10 digit
690+ decimal = number 10 Basic . digit
700691
701692 hexadecimal :: ParserT String m Int
702- hexadecimal = oneOf [ ' x' , ' X' ] *> number 16 hexDigit
693+ hexadecimal = oneOf [ ' x' , ' X' ] *> number 16 Basic . hexDigit
703694
704695 octal :: ParserT String m Int
705- octal = oneOf [ ' o' , ' O' ] *> number 8 octDigit
696+ octal = oneOf [ ' o' , ' O' ] *> number 8 Basic . octDigit
706697
707698 number :: Int -> ParserT String m Char -> ParserT String m Int
708699 number base baseDigit = do
@@ -886,38 +877,3 @@ inCommentSingle (LanguageDef languageDef) =
886877 startEnd :: Array Char
887878 startEnd = SCU .toCharArray languageDef.commentEnd <> SCU .toCharArray languageDef.commentStart
888879
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