parsers 0.2 → 0.3
raw patch · 7 files changed
+83/−88 lines, 7 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
- Text.Parser.Char: instance (CharParsing m, Monoid w) => CharParsing (RWST r w s m)
- Text.Parser.Char: instance (CharParsing m, Monoid w) => CharParsing (WriterT w m)
- Text.Parser.Char: instance CharParsing m => CharParsing (IdentityT m)
- Text.Parser.Char: instance CharParsing m => CharParsing (ReaderT e m)
- Text.Parser.Char: instance CharParsing m => CharParsing (StateT s m)
- Text.Parser.Combinators: instance (Parsing m, Monoid w) => Parsing (RWST r w s m)
- Text.Parser.Combinators: instance (Parsing m, Monoid w) => Parsing (WriterT w m)
- Text.Parser.Combinators: instance Parsing m => Parsing (IdentityT m)
- Text.Parser.Combinators: instance Parsing m => Parsing (ReaderT e m)
- Text.Parser.Combinators: instance Parsing m => Parsing (StateT s m)
- Text.Parser.Token: instance (TokenParsing m, Monoid w) => TokenParsing (RWST r w s m)
- Text.Parser.Token: instance (TokenParsing m, Monoid w) => TokenParsing (WriterT w m)
- Text.Parser.Token: instance TokenParsing m => TokenParsing (IdentityT m)
- Text.Parser.Token: instance TokenParsing m => TokenParsing (ReaderT e m)
- Text.Parser.Token: instance TokenParsing m => TokenParsing (StateT s m)
- Text.Parser.Token: instance TokenParsing m => TokenParsing (Unhighlighted m)
- Text.Parser.Token: instance TokenParsing m => TokenParsing (Unspaced m)
- Text.Parser.Token.Style: emptyCommentStyle, haskellCommentStyle, javaCommentStyle :: CommentStyle
- Text.Parser.Token.Style: emptyIdents, haskellIdents, haskell98Idents :: TokenParsing m => IdentifierStyle m
- Text.Parser.Token.Style: emptyOps, haskellOps, haskell98Ops :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Char: instance (CharParsing m, MonadPlus m) => CharParsing (IdentityT m)
+ Text.Parser.Char: instance (CharParsing m, MonadPlus m) => CharParsing (ReaderT e m)
+ Text.Parser.Char: instance (CharParsing m, MonadPlus m) => CharParsing (StateT s m)
+ Text.Parser.Char: instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (RWST r w s m)
+ Text.Parser.Char: instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (WriterT w m)
+ Text.Parser.Combinators: instance (Parsing m, Monad m) => Parsing (IdentityT m)
+ Text.Parser.Combinators: instance (Parsing m, MonadPlus m) => Parsing (ReaderT e m)
+ Text.Parser.Combinators: instance (Parsing m, MonadPlus m) => Parsing (StateT s m)
+ Text.Parser.Combinators: instance (Parsing m, MonadPlus m, Monoid w) => Parsing (RWST r w s m)
+ Text.Parser.Combinators: instance (Parsing m, MonadPlus m, Monoid w) => Parsing (WriterT w m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m) => TokenParsing (IdentityT m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m) => TokenParsing (ReaderT e m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m) => TokenParsing (StateT s m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m) => TokenParsing (Unhighlighted m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m) => TokenParsing (Unspaced m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (RWST r w s m)
+ Text.Parser.Token: instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (WriterT w m)
+ Text.Parser.Token.Style: emptyCommentStyle :: CommentStyle
+ Text.Parser.Token.Style: emptyIdents :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Token.Style: emptyOps :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Token.Style: haskell98Idents :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Token.Style: haskell98Ops, haskellOps :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Token.Style: haskellCommentStyle :: CommentStyle
+ Text.Parser.Token.Style: haskellIdents :: TokenParsing m => IdentifierStyle m
+ Text.Parser.Token.Style: javaCommentStyle :: CommentStyle
- Text.Parser.Combinators: class (Alternative m, MonadPlus m) => Parsing m where skipMany p = () <$ many p skipSome p = p >> skipMany p unexpected = lift . unexpected eof = lift eof notFollowedBy p = try ((try p >>= unexpected . show) <|> pure ())
+ Text.Parser.Combinators: class Alternative m => Parsing m where skipMany p = () <$ many p skipSome p = p *> skipMany p unexpected = lift . unexpected eof = lift eof notFollowedBy p = try ((try p >>= unexpected . show) <|> pure ())
- Text.Parser.Combinators: notFollowedBy :: (Parsing m, Show a) => m a -> m ()
+ Text.Parser.Combinators: notFollowedBy :: (Parsing m, Monad m, Show a) => m a -> m ()
- Text.Parser.Expression: buildExpressionParser :: Parsing m => OperatorTable m a -> m a -> m a
+ Text.Parser.Expression: buildExpressionParser :: (Parsing m, Monad m) => OperatorTable m a -> m a -> m a
- Text.Parser.Token: double :: TokenParsing m => m Double
+ Text.Parser.Token: double :: (TokenParsing m, Monad m) => m Double
- Text.Parser.Token: ident :: TokenParsing m => IdentifierStyle m -> m String
+ Text.Parser.Token: ident :: (TokenParsing m, Monad m) => IdentifierStyle m -> m String
- Text.Parser.Token: integer :: TokenParsing m => m Integer
+ Text.Parser.Token: integer :: (TokenParsing m, Monad m) => m Integer
- Text.Parser.Token: integer' :: TokenParsing m => m Integer
+ Text.Parser.Token: integer' :: (TokenParsing m, Monad m) => m Integer
- Text.Parser.Token: natural :: TokenParsing m => m Integer
+ Text.Parser.Token: natural :: (TokenParsing m, Monad m) => m Integer
- Text.Parser.Token: naturalOrDouble :: TokenParsing m => m (Either Integer Double)
+ Text.Parser.Token: naturalOrDouble :: (TokenParsing m, Monad m) => m (Either Integer Double)
- Text.Parser.Token: reserve :: TokenParsing m => IdentifierStyle m -> String -> m ()
+ Text.Parser.Token: reserve :: (TokenParsing m, Monad m) => IdentifierStyle m -> String -> m ()
Files
- Text/Parser/Char.hs +11/−9
- Text/Parser/Combinators.hs +14/−13
- Text/Parser/Expression.hs +1/−1
- Text/Parser/Token.hs +28/−54
- Text/Parser/Token/Highlight.hs +1/−0
- Text/Parser/Token/Style.hs +25/−8
- parsers.cabal +3/−3
Text/Parser/Char.hs view
@@ -53,6 +53,7 @@ import Control.Monad.Trans.RWS.Strict as Strict import Control.Monad.Trans.Reader import Control.Monad.Trans.Identity+import Control.Monad (MonadPlus(..)) import Data.Char import Data.CharSet (CharSet(..)) import qualified Data.CharSet as CharSet@@ -160,11 +161,12 @@ octDigit = satisfy isOctDigit <?> "octal digit" {-# INLINE octDigit #-} +-- | Additional functionality needed to parse character streams. class Parsing m => CharParsing m where -- | Parse a single character of the input, with UTF-8 decoding satisfy :: (Char -> Bool) -> m Char #ifdef USE_DEFAULT_SIGNATURES- default satisfy :: (MonadTrans t, CharParsing n, m ~ t n) =>+ default satisfy :: (MonadTrans t, CharParsing n, Monad n, m ~ t n) => (Char -> Bool) -> t n Char satisfy = lift . satisfy@@ -196,56 +198,56 @@ string s = s <$ try (traverse_ char s) <?> show s -instance CharParsing m => CharParsing (Lazy.StateT s m) where+instance (CharParsing m, MonadPlus m) => CharParsing (Lazy.StateT s m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance CharParsing m => CharParsing (Strict.StateT s m) where+instance (CharParsing m, MonadPlus m) => CharParsing (Strict.StateT s m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance CharParsing m => CharParsing (ReaderT e m) where+instance (CharParsing m, MonadPlus m) => CharParsing (ReaderT e m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance (CharParsing m, Monoid w) => CharParsing (Strict.WriterT w m) where+instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (Strict.WriterT w m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance (CharParsing m, Monoid w) => CharParsing (Lazy.WriterT w m) where+instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (Lazy.WriterT w m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance (CharParsing m, Monoid w) => CharParsing (Lazy.RWST r w s m) where+instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (Lazy.RWST r w s m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance (CharParsing m, Monoid w) => CharParsing (Strict.RWST r w s m) where+instance (CharParsing m, MonadPlus m, Monoid w) => CharParsing (Strict.RWST r w s m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar anyChar = lift anyChar string = lift . string -instance CharParsing m => CharParsing (IdentityT m) where+instance (CharParsing m, MonadPlus m) => CharParsing (IdentityT m) where satisfy = lift . satisfy char = lift . char notChar = lift . notChar
Text/Parser/Combinators.hs view
@@ -208,7 +208,8 @@ infixr 0 <?> -class (Alternative m, MonadPlus m) => Parsing m where+-- | Additional functionality needed to describe parsers independent of input type.+class Alternative m => Parsing m where -- | Take a parser that may consume input, and on failure, go back to -- where we started and fail as if we didn't consume input. try :: m a -> m a@@ -224,7 +225,7 @@ -- | @skipSome p@ applies the parser @p@ /one/ or more times, skipping -- its result. (aka skipMany1 in parsec) skipSome :: m a -> m ()- skipSome p = p >> skipMany p+ skipSome p = p *> skipMany p -- | @lookAhead p@ parses @p@ without consuming any input. lookAhead :: m a -> m a@@ -232,7 +233,7 @@ -- | Used to emit an error on an unexpected token unexpected :: String -> m a #ifdef USE_DEFAULT_SIGNATURES- default unexpected :: (MonadTrans t, Parsing n, m ~ t n) =>+ default unexpected :: (MonadTrans t, Monad n, Parsing n, m ~ t n) => String -> t n a unexpected = lift . unexpected #endif@@ -243,7 +244,7 @@ -- > eof = notFollowedBy anyChar <?> "end of input" eof :: m () #ifdef USE_DEFAULT_SIGNATURES- default eof :: (MonadTrans t, Parsing n, m ~ t n) => t n ()+ default eof :: (MonadTrans t, Monad n, Parsing n, m ~ t n) => t n () eof = lift eof #endif @@ -256,24 +257,24 @@ -- behaviour as follows: -- -- > keywordLet = try $ string "let" <* notFollowedBy alphaNum- notFollowedBy :: Show a => m a -> m ()+ notFollowedBy :: (Monad m, Show a) => m a -> m () notFollowedBy p = try ((try p >>= unexpected . show) <|> pure ()) -instance Parsing m => Parsing (Lazy.StateT s m) where+instance (Parsing m, MonadPlus m) => Parsing (Lazy.StateT s m) where try (Lazy.StateT m) = Lazy.StateT $ try . m Lazy.StateT m <?> l = Lazy.StateT $ \s -> m s <?> l lookAhead (Lazy.StateT m) = Lazy.StateT $ lookAhead . m unexpected = lift . unexpected eof = lift eof -instance Parsing m => Parsing (Strict.StateT s m) where+instance (Parsing m, MonadPlus m) => Parsing (Strict.StateT s m) where try (Strict.StateT m) = Strict.StateT $ try . m Strict.StateT m <?> l = Strict.StateT $ \s -> m s <?> l lookAhead (Strict.StateT m) = Strict.StateT $ lookAhead . m unexpected = lift . unexpected eof = lift eof -instance Parsing m => Parsing (ReaderT e m) where+instance (Parsing m, MonadPlus m) => Parsing (ReaderT e m) where try (ReaderT m) = ReaderT $ try . m ReaderT m <?> l = ReaderT $ \e -> m e <?> l skipMany (ReaderT m) = ReaderT $ skipMany . m@@ -281,35 +282,35 @@ unexpected = lift . unexpected eof = lift eof -instance (Parsing m, Monoid w) => Parsing (Strict.WriterT w m) where+instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Strict.WriterT w m) where try (Strict.WriterT m) = Strict.WriterT $ try m Strict.WriterT m <?> l = Strict.WriterT (m <?> l) lookAhead (Strict.WriterT m) = Strict.WriterT $ lookAhead m unexpected = lift . unexpected eof = lift eof -instance (Parsing m, Monoid w) => Parsing (Lazy.WriterT w m) where+instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Lazy.WriterT w m) where try (Lazy.WriterT m) = Lazy.WriterT $ try m Lazy.WriterT m <?> l = Lazy.WriterT (m <?> l) lookAhead (Lazy.WriterT m) = Lazy.WriterT $ lookAhead m unexpected = lift . unexpected eof = lift eof -instance (Parsing m, Monoid w) => Parsing (Lazy.RWST r w s m) where+instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Lazy.RWST r w s m) where try (Lazy.RWST m) = Lazy.RWST $ \r s -> try (m r s) Lazy.RWST m <?> l = Lazy.RWST $ \r s -> m r s <?> l lookAhead (Lazy.RWST m) = Lazy.RWST $ \r s -> lookAhead (m r s) unexpected = lift . unexpected eof = lift eof -instance (Parsing m, Monoid w) => Parsing (Strict.RWST r w s m) where+instance (Parsing m, MonadPlus m, Monoid w) => Parsing (Strict.RWST r w s m) where try (Strict.RWST m) = Strict.RWST $ \r s -> try (m r s) Strict.RWST m <?> l = Strict.RWST $ \r s -> m r s <?> l lookAhead (Strict.RWST m) = Strict.RWST $ \r s -> lookAhead (m r s) unexpected = lift . unexpected eof = lift eof -instance Parsing m => Parsing (IdentityT m) where+instance (Parsing m, Monad m) => Parsing (IdentityT m) where try = IdentityT . try . runIdentityT IdentityT m <?> l = IdentityT (m <?> l) skipMany = IdentityT . skipMany . runIdentityT
Text/Parser/Expression.hs view
@@ -86,7 +86,7 @@ -- > prefix name fun = Prefix (fun <* reservedOp name) -- > postfix name fun = Postfix (fun <* reservedOp name) -buildExpressionParser :: Parsing m+buildExpressionParser :: (Parsing m, Monad m) => OperatorTable m a -> m a -> m a
Text/Parser/Token.hs view
@@ -84,7 +84,7 @@ -- | Skip zero or more bytes worth of white space. More complex parsers are -- free to consider comments as white space. whiteSpace :: TokenParsing m => m ()-whiteSpace = someSpace <|> return ()+whiteSpace = someSpace <|> pure () {-# INLINE whiteSpace #-} -- | @token p@ first applies parser @p@ and then the 'whiteSpace'@@ -118,7 +118,6 @@ -- gaps. The literal string is parsed according to the grammar rules -- defined in the Haskell report (which matches most programming -- languages quite closely).- stringLiteral :: TokenParsing m => m String stringLiteral = token (highlight StringLiteral lit) where lit = Prelude.foldr (maybe id (:)) ""@@ -134,8 +133,7 @@ <|> Nothing <$ escapeEmpty <|> Just <$> escapeCode escapeEmpty = char '&'- escapeGap = do skipSome space- char '\\' <?> "end of string gap"+ escapeGap = skipSome space *> (char '\\' <?> "end of string gap") {-# INLINE stringLiteral #-} -- | This token parser parses a natural number (a positive whole@@ -143,8 +141,7 @@ -- specified in 'decimal', 'hexadecimal' or -- 'octal'. The number is parsed according to the grammar -- rules in the Haskell report.--natural :: TokenParsing m => m Integer+natural :: (TokenParsing m, Monad m) => m Integer natural = token natural' {-# INLINE natural #-} @@ -154,8 +151,7 @@ -- number can be specified in 'decimal', 'hexadecimal' -- or 'octal'. The number is parsed according -- to the grammar rules in the Haskell report.--integer :: TokenParsing m => m Integer+integer :: (TokenParsing m, Monad m) => m Integer integer = token (token (highlight Operator sgn <*> natural')) <?> "integer" where sgn = negate <$ char '-'@@ -166,8 +162,7 @@ -- | This token parser parses a floating point value. Returns the value -- of the number. The number is parsed according to the grammar rules -- defined in the Haskell report.--double :: TokenParsing m => m Double+double :: (TokenParsing m, Monad m) => m Double double = token (highlight Number floating <?> "double") {-# INLINE double #-} @@ -175,85 +170,72 @@ -- Returns the value of the number. This parsers deals with -- any overlap in the grammar rules for naturals and floats. The number -- is parsed according to the grammar rules defined in the Haskell report.--naturalOrDouble :: TokenParsing m => m (Either Integer Double)+naturalOrDouble :: (TokenParsing m, Monad m) => m (Either Integer Double) naturalOrDouble = token (highlight Number natDouble <?> "number") {-# INLINE naturalOrDouble #-} -- | Token parser @symbol s@ parses 'string' @s@ and skips -- trailing white space.- symbol :: TokenParsing m => String -> m String symbol name = token (highlight Symbol (string name)) {-# INLINE symbol #-} -- | Token parser @symbolic s@ parses 'char' @s@ and skips -- trailing white space.- symbolic :: TokenParsing m => Char -> m Char symbolic name = token (highlight Symbol (char name)) {-# INLINE symbolic #-} -- | Token parser @parens p@ parses @p@ enclosed in parenthesis, -- returning the value of @p@.- parens :: TokenParsing m => m a -> m a parens = nesting . between (symbolic '(') (symbolic ')') {-# INLINE parens #-} -- | Token parser @braces p@ parses @p@ enclosed in braces (\'{\' and -- \'}\'), returning the value of @p@.- braces :: TokenParsing m => m a -> m a braces = nesting . between (symbolic '{') (symbolic '}') {-# INLINE braces #-} -- | Token parser @angles p@ parses @p@ enclosed in angle brackets (\'\<\' -- and \'>\'), returning the value of @p@.- angles :: TokenParsing m => m a -> m a angles = nesting . between (symbolic '<') (symbolic '>') {-# INLINE angles #-} -- | Token parser @brackets p@ parses @p@ enclosed in brackets (\'[\' -- and \']\'), returning the value of @p@.- brackets :: TokenParsing m => m a -> m a brackets = nesting . between (symbolic '[') (symbolic ']') {-# INLINE brackets #-} -- | Token parser @comma@ parses the character \',\' and skips any -- trailing white space. Returns the string \",\".- comma :: TokenParsing m => m Char comma = symbolic ',' {-# INLINE comma #-} -- | Token parser @colon@ parses the character \':\' and skips any -- trailing white space. Returns the string \":\".- colon :: TokenParsing m => m Char colon = symbolic ':' {-# INLINE colon #-} -- | Token parser @dot@ parses the character \'.\' and skips any -- trailing white space. Returns the string \".\".- dot :: TokenParsing m => m Char dot = symbolic '.' {-# INLINE dot #-} -- | Token parser @semiSep p@ parses /zero/ or more occurrences of @p@--- separated by 'semi'. Returns a list of values returned by--- @p@.-+-- separated by 'semi'. Returns a list of values returned by @p@. semiSep :: TokenParsing m => m a -> m [a] semiSep p = sepBy p semi {-# INLINE semiSep #-} -- | Token parser @semiSep1 p@ parses /one/ or more occurrences of @p@ -- separated by 'semi'. Returns a list of values returned by @p@.- semiSep1 :: TokenParsing m => m a -> m [a] semiSep1 p = sepBy1 p semi {-# INLINE semiSep1 #-}@@ -261,7 +243,6 @@ -- | Token parser @commaSep p@ parses /zero/ or more occurrences of -- @p@ separated by 'comma'. Returns a list of values returned -- by @p@.- commaSep :: TokenParsing m => m a -> m [a] commaSep p = sepBy p comma {-# INLINE commaSep #-}@@ -269,11 +250,11 @@ -- | Token parser @commaSep1 p@ parses /one/ or more occurrences of -- @p@ separated by 'comma'. Returns a list of values returned -- by @p@.- commaSep1 :: TokenParsing m => m a -> m [a] commaSep1 p = sepBy p comma {-# INLINE commaSep1 #-} +-- | Additional functionality that is needed to tokenize input while ignoring whitespace. class CharParsing m => TokenParsing m where -- | Usually, someSpace consists of /one/ or more occurrences of a 'space'. -- Some parsers may choose to recognize line comments or block (multi line)@@ -297,54 +278,55 @@ highlight :: Highlight -> m a -> m a highlight _ a = a -instance TokenParsing m => TokenParsing (Lazy.StateT s m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (Lazy.StateT s m) where nesting (Lazy.StateT m) = Lazy.StateT $ nesting . m someSpace = lift someSpace semi = lift semi highlight h (Lazy.StateT m) = Lazy.StateT $ highlight h . m -instance TokenParsing m => TokenParsing (Strict.StateT s m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (Strict.StateT s m) where nesting (Strict.StateT m) = Strict.StateT $ nesting . m someSpace = lift someSpace semi = lift semi highlight h (Strict.StateT m) = Strict.StateT $ highlight h . m -instance TokenParsing m => TokenParsing (ReaderT e m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (ReaderT e m) where nesting (ReaderT m) = ReaderT $ nesting . m someSpace = lift someSpace semi = lift semi highlight h (ReaderT m) = ReaderT $ highlight h . m -instance (TokenParsing m, Monoid w) => TokenParsing (Strict.WriterT w m) where+instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (Strict.WriterT w m) where nesting (Strict.WriterT m) = Strict.WriterT $ nesting m someSpace = lift someSpace semi = lift semi highlight h (Strict.WriterT m) = Strict.WriterT $ highlight h m -instance (TokenParsing m, Monoid w) => TokenParsing (Lazy.WriterT w m) where+instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (Lazy.WriterT w m) where nesting (Lazy.WriterT m) = Lazy.WriterT $ nesting m someSpace = lift someSpace semi = lift semi highlight h (Lazy.WriterT m) = Lazy.WriterT $ highlight h m -instance (TokenParsing m, Monoid w) => TokenParsing (Lazy.RWST r w s m) where+instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (Lazy.RWST r w s m) where nesting (Lazy.RWST m) = Lazy.RWST $ \r s -> nesting (m r s) someSpace = lift someSpace semi = lift semi highlight h (Lazy.RWST m) = Lazy.RWST $ \r s -> highlight h (m r s) -instance (TokenParsing m, Monoid w) => TokenParsing (Strict.RWST r w s m) where+instance (TokenParsing m, MonadPlus m, Monoid w) => TokenParsing (Strict.RWST r w s m) where nesting (Strict.RWST m) = Strict.RWST $ \r s -> nesting (m r s) someSpace = lift someSpace semi = lift semi highlight h (Strict.RWST m) = Strict.RWST $ \r s -> highlight h (m r s) -instance TokenParsing m => TokenParsing (IdentityT m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (IdentityT m) where nesting = IdentityT . nesting . runIdentityT someSpace = lift someSpace semi = lift semi highlight h = IdentityT . highlight h . runIdentityT +-- | Used to describe an input style for constructors, values, operators, etc. data IdentifierStyle m = IdentifierStyle { styleName :: String , styleStart :: m Char@@ -363,14 +345,14 @@ {-# INLINE liftIdentifierStyle #-} -- | parse a reserved operator or identifier using a given style-reserve :: TokenParsing m => IdentifierStyle m -> String -> m ()+reserve :: (TokenParsing m, Monad m) => IdentifierStyle m -> String -> m () reserve s name = token $ try $ do _ <- highlight (styleReservedHighlight s) $ string name notFollowedBy (styleLetter s) <?> "end of " ++ show name {-# INLINE reserve #-} -- | parse an non-reserved identifier or symbol-ident :: TokenParsing m => IdentifierStyle m -> m String+ident :: (TokenParsing m, Monad m) => IdentifierStyle m -> m String ident s = token $ try $ do name <- highlight (styleHighlight s) ((:) <$> styleStart s <*> many (styleLetter s) <?> styleName s)@@ -439,7 +421,7 @@ -- rules in the Haskell report. -- -- This parser does NOT swallow trailing whitespace.-natural' :: TokenParsing m => m Integer+natural' :: (TokenParsing m, Monad m) => m Integer natural' = highlight Number nat <?> "natural" number :: TokenParsing m => Integer -> m Char -> m Integer@@ -458,7 +440,7 @@ -- Also, unlike the 'integer' parser, this parser does not admit spaces -- between the sign and the number. -integer' :: TokenParsing m => m Integer+integer' :: (TokenParsing m, Monad m) => m Integer integer' = int <?> "integer" {-# INLINE integer' #-} @@ -468,13 +450,13 @@ <|> id <$ char '+' <|> pure id -int :: TokenParsing m => m Integer+int :: (TokenParsing m, Monad m) => m Integer int = {-token-} sign <*> highlight Number nat-nat, zeroNumber :: TokenParsing m => m Integer+nat, zeroNumber :: (TokenParsing m, Monad m) => m Integer nat = zeroNumber <|> decimal zeroNumber = char '0' *> (hexadecimal <|> octal <|> decimal <|> return 0) <?> "" -floating :: TokenParsing m => m Double+floating :: (TokenParsing m, Monad m) => m Double floating = decimal >>= fractExponent {-# INLINE floating #-} @@ -483,17 +465,12 @@ <|> (fromInteger n *) <$> exponent' where fraction = Prelude.foldr op 0.0 <$> (char '.' *> (some digit <?> "fraction")) op d f = (f + fromIntegral (digitToInt d))/10.0- exponent' = do- _ <- oneOf "eE"- f <- sign- e <- decimal <?> "exponent"- return (power (f e))- <?> "exponent"+ exponent' = ((\f e -> power (f e)) <$ oneOf "eE" <*> sign <*> (decimal <?> "exponent")) <?> "exponent" power e | e < 0 = 1.0/power(-e) | otherwise = fromInteger (10^e) -natDouble, zeroNumFloat, decimalFloat :: TokenParsing m => m (Either Integer Double)+natDouble, zeroNumFloat, decimalFloat :: (TokenParsing m, Monad m) => m (Either Integer Double) natDouble = char '0' *> zeroNumFloat <|> decimalFloat@@ -514,7 +491,6 @@ -- value of the number. -- -- This parser does NOT swallow trailing whitespace- decimal :: TokenParsing m => m Integer decimal = number 10 digit {-# INLINE decimal #-}@@ -524,7 +500,6 @@ -- number. -- -- This parser does NOT swallow trailing whitespace- hexadecimal :: TokenParsing m => m Integer hexadecimal = oneOf "xX" *> number 16 hexDigit {-# INLINE hexadecimal #-}@@ -534,7 +509,6 @@ -- number. -- -- This parser does NOT swallow trailing whitespace- octal :: TokenParsing m => m Integer octal = oneOf "oO" *> number 8 octDigit {-# INLINE octal #-}@@ -547,7 +521,7 @@ instance MonadTrans Unhighlighted where lift = Unhighlighted -instance TokenParsing m => TokenParsing (Unhighlighted m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (Unhighlighted m) where nesting (Unhighlighted m) = Unhighlighted (nesting m) someSpace = lift someSpace semi = lift semi@@ -561,7 +535,7 @@ instance MonadTrans Unspaced where lift = Unspaced -instance TokenParsing m => TokenParsing (Unspaced m) where+instance (TokenParsing m, MonadPlus m) => TokenParsing (Unspaced m) where nesting (Unspaced m) = Unspaced (nesting m) someSpace = empty semi = lift semi
Text/Parser/Token/Highlight.hs view
@@ -18,6 +18,7 @@ ( Highlight(..) ) where +-- | Tags used by the 'Text.Parser.Token.TokenParsing' 'Text.Parser.Token.highlight' combinator. data Highlight = EscapeCode | Number
Text/Parser/Token/Style.hs view
@@ -37,16 +37,24 @@ import Text.Parser.Token.Highlight import Data.List (nub) +-- | How to deal with comments. data CommentStyle = CommentStyle- { commentStart :: String- , commentEnd :: String- , commentLine :: String- , commentNesting :: Bool+ { commentStart :: String -- ^ String that starts a multiline comment+ , commentEnd :: String -- ^ String that ends a multiline comment+ , commentLine :: String -- ^ String that starts a single line comment+ , commentNesting :: Bool -- ^ Can we nest multiline comments? } -emptyCommentStyle, javaCommentStyle, haskellCommentStyle :: CommentStyle+-- | No comments at all+emptyCommentStyle :: CommentStyle emptyCommentStyle = CommentStyle "" "" "" True-javaCommentStyle = CommentStyle "/*" "*/" "//" True++-- | Use java-style comments+javaCommentStyle :: CommentStyle+javaCommentStyle = CommentStyle "/*" "*/" "//" True++-- | Use haskell-style comments+haskellCommentStyle :: CommentStyle haskellCommentStyle = CommentStyle "{-" "-}" "--" True -- | Use this to easily build the definition of whiteSpace for your MonadParser@@ -79,7 +87,8 @@ set :: [String] -> HashSet String set = HashSet.fromList -emptyOps, haskell98Ops, haskellOps :: TokenParsing m => IdentifierStyle m+-- | A simple operator style based on haskell with no reserved operators+emptyOps :: TokenParsing m => IdentifierStyle m emptyOps = IdentifierStyle { styleName = "operator" , styleStart = styleLetter emptyOps@@ -88,12 +97,15 @@ , styleHighlight = Operator , styleReservedHighlight = ReservedOperator }+-- | A simple operator style based on haskell with the operators from Haskell 98.+haskell98Ops, haskellOps :: TokenParsing m => IdentifierStyle m haskell98Ops = emptyOps { styleReserved = set ["::","..","=","\\","|","<-","->","@","~","=>"] } haskellOps = haskell98Ops -emptyIdents, haskell98Idents, haskellIdents :: TokenParsing m => IdentifierStyle m+-- | A simple identifier style based on haskell with no reserve words+emptyIdents :: TokenParsing m => IdentifierStyle m emptyIdents = IdentifierStyle { styleName = "identifier" , styleStart = letter <|> char '_'@@ -103,8 +115,13 @@ , styleReservedHighlight = ReservedIdentifier } +-- | A simple identifier style based on haskell with only the reserved words from Haskell 98.+haskell98Idents :: TokenParsing m => IdentifierStyle m haskell98Idents = emptyIdents { styleReserved = set haskell98ReservedIdents }++-- | A simple identifier style based on haskell with the reserved words from Haskell 98 and some common extensions.+haskellIdents :: TokenParsing m => IdentifierStyle m haskellIdents = haskell98Idents { styleLetter = styleLetter haskell98Idents <|> char '#' , styleReserved = set $ haskell98ReservedIdents ++
parsers.cabal view
@@ -1,6 +1,6 @@ name: parsers category: Text, Parsing-version: 0.2+version: 0.3 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -10,8 +10,8 @@ homepage: http://github.com/ekmett/parsers/ bug-reports: http://github.com/ekmett/parsers/issues copyright: Copyright (C) 2010-2012 Edward A. Kmett-synopsis: Simple parsing combinators-description: Simple parsing combinators+synopsis: Parsing combinators+description: Parsing combinators build-type: Simple extra-source-files: .travis.yml