[project @ 2002-05-31 12:22:33 by panne]
[packages/containers.git] / Text / ParserCombinators / Parsec / Char.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Text.ParserCombinators.Parsec.Char
4 -- Copyright : (c) Daan Leijen 1999-2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer : daan@cs.uu.nl
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- Commonly used character parsers.
12 --
13 -----------------------------------------------------------------------------
14
15 module Text.ParserCombinators.Parsec.Char
16 ( CharParser
17 , spaces, space
18 , newline, tab
19 , upper, lower, alphaNum, letter
20 , digit, hexDigit, octDigit
21 , char, string
22 , anyChar, oneOf, noneOf
23 , satisfy
24 ) where
25
26 import Data.Char
27 import Text.ParserCombinators.Parsec.Pos( updatePosChar, updatePosString )
28 import Text.ParserCombinators.Parsec.Prim
29
30 -----------------------------------------------------------
31 -- Type of character parsers
32 -----------------------------------------------------------
33 type CharParser st a = GenParser Char st a
34
35 -----------------------------------------------------------
36 -- Character parsers
37 -----------------------------------------------------------
38 oneOf cs = satisfy (\c -> elem c cs)
39 noneOf cs = satisfy (\c -> not (elem c cs))
40
41 spaces = skipMany space <?> "white space"
42 space = satisfy (isSpace) <?> "space"
43
44 newline = char '\n' <?> "new-line"
45 tab = char '\t' <?> "tab"
46
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"
54
55 char c = satisfy (==c) <?> show [c]
56 anyChar = satisfy (const True)
57
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)
65
66 string :: String -> CharParser st String
67 string s = tokens show updatePosString s