1 -----------------------------------------------------------------------------
3 -- Module : Text.ParserCombinators.Parsec.Char
4 -- Copyright : (c) Daan Leijen 1999-2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : daan@cs.uu.nl
8 -- Stability : provisional
9 -- Portability : portable
11 -- Commonly used character parsers.
13 -----------------------------------------------------------------------------
15 module Text.ParserCombinators.Parsec.Char
19 , upper, lower, alphaNum, letter
20 , digit, hexDigit, octDigit
22 , anyChar, oneOf, noneOf
27 import Text.ParserCombinators.Parsec.Pos( updatePosChar, updatePosString )
28 import Text.ParserCombinators.Parsec.Prim
30 -----------------------------------------------------------
31 -- Type of character parsers
32 -----------------------------------------------------------
33 type CharParser st a = GenParser Char st a
35 -----------------------------------------------------------
37 -----------------------------------------------------------
38 oneOf cs = satisfy (\c -> elem c cs)
39 noneOf cs = satisfy (\c -> not (elem c cs))
41 spaces = skipMany space <?> "white space"
42 space = satisfy (isSpace) <?> "space"
44 newline = char '\n' <?> "new-line"
45 tab = char '\t' <?> "tab"
47 upper = satisfy (isUpper) <?> "uppercase letter"
48 lower = satisfy (isLower) <?> "lowercase letter"
49 alphaNum = satisfy (isAlphaNum) <?> "letter or digit"
50 letter = satisfy (isAlpha) <?> "letter"
51 digit = satisfy (isDigit) <?> "digit"
52 hexDigit = satisfy (isHexDigit) <?> "hexadecimal digit"
53 octDigit = satisfy (isOctDigit) <?> "octal digit"
55 char c = satisfy (==c) <?> show [c]
56 anyChar = satisfy (const True)
58 -----------------------------------------------------------
59 -- Primitive character parsers
60 -----------------------------------------------------------
61 satisfy :: (Char -> Bool) -> CharParser st Char
62 satisfy f = tokenPrim (\c -> show [c])
63 (\pos c cs -> updatePosChar pos c)
64 (\c -> if f c then Just c else Nothing)
66 string :: String -> CharParser st String
67 string s = tokens show updatePosString s