megaparsec-5.3.1: Monadic parser combinators

Copyright | © 2015–2017 Megaparsec contributors © 2007 Paolo Martini © 1999–2001 Daan Leijen
License | FreeBSD
Maintainer | Mark Karpov <markkarpov92@gmail.com>
Stability | experimental
Portability | non-portable
Safe Haskell | None
Language | Haskell2010

Text.Megaparsec.Char

Contents

- Simple parsers
- Categories of characters
- More general parsers
- Sequence of characters

Description

Commonly used character parsers.

Synopsis

- newline :: (MonadParsec e s m, Token s ~ Char) => m Char
- crlf :: (MonadParsec e s m, Token s ~ Char) => m String
- eol :: (MonadParsec e s m, Token s ~ Char) => m String
- tab :: (MonadParsec e s m, Token s ~ Char) => m Char
- space :: (MonadParsec e s m, Token s ~ Char) => m ()
- controlChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- spaceChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- upperChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- lowerChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- letterChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- alphaNumChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- printChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- digitChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- octDigitChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- hexDigitChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- markChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- numberChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- punctuationChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- symbolChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- separatorChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- asciiChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- latin1Char :: (MonadParsec e s m, Token s ~ Char) => m Char
- charCategory :: (MonadParsec e s m, Token s ~ Char) => GeneralCategory -> m Char
- categoryName :: GeneralCategory -> String
- char :: (MonadParsec e s m, Token s ~ Char) => Char -> m Char
- char' :: (MonadParsec e s m, Token s ~ Char) => Char -> m Char
- anyChar :: (MonadParsec e s m, Token s ~ Char) => m Char
- oneOf :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char
- oneOf' :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char
- noneOf :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char
- noneOf' :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char
- satisfy :: (MonadParsec e s m, Token s ~ Char) => (Char -> Bool) -> m Char
- string :: (MonadParsec e s m, Token s ~ Char) => String -> m String
- string' :: (MonadParsec e s m, Token s ~ Char) => String -> m String

# Simple parsers

newline :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a newline character.

crlf :: (MonadParsec e s m, Token s ~ Char) => m String #

Parse a carriage return character followed by a newline character. Return the sequence of characters parsed.

eol :: (MonadParsec e s m, Token s ~ Char) => m String #

Parse a CRLF (see `crlf`) or LF (see `newline`) end of line. Return the sequence of characters parsed.

    eol = (pure <$> newline) <|> crlf <?> "end of line"

tab :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a tab character.

space :: (MonadParsec e s m, Token s ~ Char) => m () #

Skip zero or more white space characters.

See also: `skipMany` and `spaceChar`.

# Categories of characters

controlChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a control character (a non-printing character of the Latin-1 subset of Unicode).

spaceChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode space character, and the control characters: tab, newline, carriage return, form feed, and vertical tab.

upperChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse an upper-case or title-case alphabetic Unicode character. Title case is used by a small number of letter ligatures like the single-character form of Lj.

lowerChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a lower-case alphabetic Unicode character.

letterChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse an alphabetic Unicode character: lower-case, upper-case, or title-case letter, or a letter of case-less scripts/modifier letter.

alphaNumChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse an alphabetic or numeric digit Unicode characters.

Note that the numeric digits outside the ASCII range are parsed by this parser but not by `digitChar`. Such digits may be part of identifiers but are not used by the printer and reader to represent numbers.

printChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a printable Unicode character: letter, number, mark, punctuation, symbol or space.

digitChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse an ASCII digit, i.e between “0” and “9”.

octDigitChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse an octal digit, i.e. between “0” and “7”.

hexDigitChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a hexadecimal digit, i.e. between “0” and “9”, or “a” and “f”, or “A” and “F”.

markChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode mark character (accents and the like), which combines with preceding characters.

numberChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode numeric character, including digits from various scripts, Roman numerals, etc.

punctuationChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode punctuation character, including various kinds of connectors, brackets and quotes.

symbolChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode symbol characters, including mathematical and currency symbols.

separatorChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a Unicode space and separator characters.

asciiChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a character from the first 128 characters of the Unicode character set, corresponding to the ASCII character set.

latin1Char :: (MonadParsec e s m, Token s ~ Char) => m Char #

Parse a character from the first 256 characters of the Unicode character set, corresponding to the ISO 8859-1 (Latin-1) character set.

charCategory :: (MonadParsec e s m, Token s ~ Char) => GeneralCategory -> m Char #

`charCategory cat` parses character in Unicode General Category `cat`, see `GeneralCategory`.

categoryName :: GeneralCategory -> String #

Return the human-readable name of Unicode General Category.

# More general parsers

char :: (MonadParsec e s m, Token s ~ Char) => Char -> m Char #

`char c` parses a single character `c`.

    semicolon = char ';'

char' :: (MonadParsec e s m, Token s ~ Char) => Char -> m Char #

The same as `char` but case-insensitive. This parser returns the actually parsed character preserving its case.

    >>> parseTest (char' 'e') "E"
    'E'
    >>> parseTest (char' 'e') "G"
    1:1:
    unexpected 'G'
    expecting 'E' or 'e'

anyChar :: (MonadParsec e s m, Token s ~ Char) => m Char #

This parser succeeds for any character. Returns the parsed character.

oneOf :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char #

`oneOf cs` succeeds if the current character is in the supplied collection of characters `cs`. Returns the parsed character. Note that this parser cannot automatically generate the “expected” component of error message, so usually you should label it manually with `label` or (`<?>`).

See also: `satisfy`.

    digit = oneOf ['0'..'9'] <?> "digit"

oneOf' :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char #

The same as `oneOf`, but case-insensitive. Returns the parsed character preserving its case.

    vowel = oneOf' "aeiou" <?> "vowel"

noneOf :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char #

As the dual of `oneOf`, `noneOf cs` succeeds if the current character not in the supplied list of characters `cs`. Returns the parsed character.

noneOf' :: (Foldable f, MonadParsec e s m, Token s ~ Char) => f Char -> m Char #

The same as `noneOf`, but case-insensitive.

    consonant = noneOf' "aeiou" <?> "consonant"

satisfy :: (MonadParsec e s m, Token s ~ Char) => (Char -> Bool) -> m Char #

The parser `satisfy f` succeeds for any character for which the supplied function `f` returns `True`. Returns the character that is actually parsed.

    digitChar = satisfy isDigit <?> "digit"
    oneOf cs  = satisfy (`elem` cs)

# Sequence of characters

string :: (MonadParsec e s m, Token s ~ Char) => String -> m String #

`string s` parses a sequence of characters given by `s`. Returns the parsed string (i.e. `s`).

    divOrMod = string "div" <|> string "mod"

string' :: (MonadParsec e s m, Token s ~ Char) => String -> m String #

The same as `string`, but case-insensitive. On success returns string cased as actually parsed input.

    >>> parseTest (string' "foobar") "foObAr"
    "foObAr"