1 -----------------------------------------------------------------------------
3 -- Module : Text.Parsec.Char
4 -- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
5 -- License : BSD-style (see the LICENSE file)
7 -- Maintainer : derek.a.elkins@gmail.com
8 -- Stability : provisional
9 -- Portability : portable
11 -- Commonly used character parsers.
13 -----------------------------------------------------------------------------
15 {-# LANGUAGE FlexibleContexts #-}
17 module Text.Parsec.Char where
20 import Text.Parsec.Pos
21 import Text.Parsec.Prim
23 -- | @oneOf cs@ succeeds if the current character is in the supplied
24 -- list of characters @cs@. Returns the parsed character. See also
27 -- > vowel = oneOf "aeiou"
29 oneOf :: (Stream s m Char) => [Char] -> ParsecT s u m Char
30 oneOf cs = satisfy (\c -> elem c cs)
32 -- | As the dual of 'oneOf', @noneOf cs@ succeeds if the current
33 -- character /not/ in the supplied list of characters @cs@. Returns the
36 -- > consonant = noneOf "aeiou"
38 noneOf :: (Stream s m Char) => [Char] -> ParsecT s u m Char
39 noneOf cs = satisfy (\c -> not (elem c cs))
41 -- | Skips /zero/ or more white space characters. See also 'skipMany'.
43 spaces :: (Stream s m Char) => ParsecT s u m ()
44 spaces = skipMany space <?> "white space"
46 -- | Parses a white space character (any character which satisfies 'isSpace')
47 -- Returns the parsed character.
49 space :: (Stream s m Char) => ParsecT s u m Char
50 space = satisfy isSpace <?> "space"
52 -- | Parses a newline character (\'\\n\'). Returns a newline character.
54 newline :: (Stream s m Char) => ParsecT s u m Char
55 newline = char '\n' <?> "new-line"
57 -- | Parses a tab character (\'\\t\'). Returns a tab character.
59 tab :: (Stream s m Char) => ParsecT s u m Char
60 tab = char '\t' <?> "tab"
62 -- | Parses an upper case letter (a character between \'A\' and \'Z\').
63 -- Returns the parsed character.
65 upper :: (Stream s m Char) => ParsecT s u m Char
66 upper = satisfy isUpper <?> "uppercase letter"
68 -- | Parses a lower case character (a character between \'a\' and \'z\').
69 -- Returns the parsed character.
71 lower :: (Stream s m Char) => ParsecT s u m Char
72 lower = satisfy isLower <?> "lowercase letter"
74 -- | Parses a letter or digit (a character between \'0\' and \'9\').
75 -- Returns the parsed character.
77 alphaNum :: (Stream s m Char => ParsecT s u m Char)
78 alphaNum = satisfy isAlphaNum <?> "letter or digit"
80 -- | Parses a letter (an upper case or lower case character). Returns the
83 letter :: (Stream s m Char) => ParsecT s u m Char
84 letter = satisfy isAlpha <?> "letter"
86 -- | Parses a digit. Returns the parsed character.
88 digit :: (Stream s m Char) => ParsecT s u m Char
89 digit = satisfy isDigit <?> "digit"
91 -- | Parses a hexadecimal digit (a digit or a letter between \'a\' and
92 -- \'f\' or \'A\' and \'F\'). Returns the parsed character.
94 hexDigit :: (Stream s m Char) => ParsecT s u m Char
95 hexDigit = satisfy isHexDigit <?> "hexadecimal digit"
97 -- | Parses an octal digit (a character between \'0\' and \'7\'). Returns
98 -- the parsed character.
100 octDigit :: (Stream s m Char) => ParsecT s u m Char
101 octDigit = satisfy isOctDigit <?> "octal digit"
103 -- | @char c@ parses a single character @c@. Returns the parsed
104 -- character (i.e. @c@).
106 -- > semiColon = char ';'
108 char :: (Stream s m Char) => Char -> ParsecT s u m Char
109 char c = satisfy (==c) <?> show [c]
111 -- | This parser succeeds for any character. Returns the parsed character.
113 anyChar :: (Stream s m Char) => ParsecT s u m Char
114 anyChar = satisfy (const True)
116 -- | The parser @satisfy f@ succeeds for any character for which the
117 -- supplied function @f@ returns 'True'. Returns the character that is
120 -- > digit = satisfy isDigit
121 -- > oneOf cs = satisfy (\c -> c `elem` cs)
123 satisfy :: (Stream s m Char) => (Char -> Bool) -> ParsecT s u m Char
124 satisfy f = tokenPrim (\c -> show [c])
125 (\pos c _cs -> updatePosChar pos c)
126 (\c -> if f c then Just c else Nothing)
128 -- | @string s@ parses a sequence of characters given by @s@. Returns
129 -- the parsed string (i.e. @s@).
131 -- > divOrMod = string "div"
132 -- > <|> string "mod"
134 string :: (Stream s m Char) => String -> ParsecT s u m String
135 string s = tokens show updatePosString s