| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Text.Parsec.Class
Contents
Description
HasParser can be considered a dual to Pretty like Read is to Show.
The class provides Data.Parsec parsers for its instances that construct
the type from its textual representation. Combined with the parseM and
parse convenience functions, this class makes parsing simple. Unlike
Read, Parsec parsers return reasonable error messages in case of failure.
Also, there is a rich set of combinators and additional libraries available
for re-use.
- type CharParser st input m a = Stream st m Char => ParsecT st input m a
- class HasParser a where
- type ErrorContext = String
- parseM :: (MonadFail m, Stream input m Char, HasParser a) => ErrorContext -> input -> m a
- parse :: (Stream input Identity Char, HasParser a) => ErrorContext -> input -> a
- module Text.Parsec
Documentation
type CharParser st input m a = Stream st m Char => ParsecT st input m a #
A simplified ParsecT parser that consumes some kind of character stream
without requiring any particular state state.
Types that are instances of this class can be parsed and constructed from some character based text representation.
Minimal complete definition
Methods
parser :: CharParser st input m a #
type ErrorContext = String #
Parsers functions like parse or parseM use this type to provide a
helpful context in case the parser failes. Parsec uses the synonym
SourceName for the same purpose, but in fact this type doesn't necessarily
have to be a file name. It can be any name or identifier. Oftentimes, it
it's useful to pass the name of the type that the parser attempted to parse.
parseM :: (MonadFail m, Stream input m Char, HasParser a) => ErrorContext -> input -> m a #
Convenience wrapper around runParserT that uses the HasParser class to
determine the desired parser for the given result type. The function reports
syntax errors via fail.
>>>parseM "Natural" "987654321" :: IO Natural987654321>>>parseM "Natural" "123456789" :: Maybe NaturalJust 123456789
Please note that parsers run this way do not ignore any white space:
>>>parseM "Natural" " 1" :: Maybe NaturalNothing>>>parseM "Natural" "1 " :: Maybe NaturalNothing
parse :: (Stream input Identity Char, HasParser a) => ErrorContext -> input -> a #
Convenience wrapper around runParser that uses the HasParser class to
determine the desired parser for the given result type. The function reports
syntax errors by throwing ParseError. This approach is inherently impure
and complicates error handling greatly. Use this function only on occasions
where parser errors are fatal errors that your code cannot recover from. In
almost all cases, parseM is the better choice.
>>>parse "Natural" "12345" :: Natural12345
Like parseM, this function does not skip over any white space. Use
Parsec's primitive runParser or runParserT functions if you don't like
this behavior:
>>>runParser (spaces >> parser) () "Natural" " 1 " :: Either ParseError NaturalRight 1
Re-exports from Text.Parsec
module Text.Parsec