3a_asm: adding some libraries, in order to be compatible with the tilab environment
[calu.git] / 3a_asm / Text / Parsec / Char.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Text.Parsec.Char
4 -- Copyright   :  (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
5 -- License     :  BSD-style (see the LICENSE file)
6 -- 
7 -- Maintainer  :  derek.a.elkins@gmail.com
8 -- Stability   :  provisional
9 -- Portability :  portable
10 -- 
11 -- Commonly used character parsers.
12 -- 
13 -----------------------------------------------------------------------------
14
15 {-# LANGUAGE FlexibleContexts #-}
16
17 module Text.Parsec.Char where
18
19 import Data.Char
20 import Text.Parsec.Pos
21 import Text.Parsec.Prim
22
23 -- | @oneOf cs@ succeeds if the current character is in the supplied
24 -- list of characters @cs@. Returns the parsed character. See also
25 -- 'satisfy'.
26 -- 
27 -- >   vowel  = oneOf "aeiou"
28
29 oneOf :: (Stream s m Char) => [Char] -> ParsecT s u m Char
30 oneOf cs            = satisfy (\c -> elem c cs)
31
32 -- | As the dual of 'oneOf', @noneOf cs@ succeeds if the current
33 -- character /not/ in the supplied list of characters @cs@. Returns the
34 -- parsed character.
35 --
36 -- >  consonant = noneOf "aeiou"
37
38 noneOf :: (Stream s m Char) => [Char] -> ParsecT s u m Char
39 noneOf cs           = satisfy (\c -> not (elem c cs))
40
41 -- | Skips /zero/ or more white space characters. See also 'skipMany'.
42
43 spaces :: (Stream s m Char) => ParsecT s u m ()
44 spaces              = skipMany space        <?> "white space"
45
46 -- | Parses a white space character (any character which satisfies 'isSpace')
47 -- Returns the parsed character. 
48
49 space :: (Stream s m Char) => ParsecT s u m Char
50 space               = satisfy isSpace       <?> "space"
51
52 -- | Parses a newline character (\'\\n\'). Returns a newline character. 
53
54 newline :: (Stream s m Char) => ParsecT s u m Char
55 newline             = char '\n'             <?> "new-line"
56
57 -- | Parses a tab character (\'\\t\'). Returns a tab character. 
58
59 tab :: (Stream s m Char) => ParsecT s u m Char
60 tab                 = char '\t'             <?> "tab"
61
62 -- | Parses an upper case letter (a character between \'A\' and \'Z\').
63 -- Returns the parsed character. 
64
65 upper :: (Stream s m Char) => ParsecT s u m Char
66 upper               = satisfy isUpper       <?> "uppercase letter"
67
68 -- | Parses a lower case character (a character between \'a\' and \'z\').
69 -- Returns the parsed character. 
70
71 lower :: (Stream s m Char) => ParsecT s u m Char
72 lower               = satisfy isLower       <?> "lowercase letter"
73
74 -- | Parses a letter or digit (a character between \'0\' and \'9\').
75 -- Returns the parsed character. 
76
77 alphaNum :: (Stream s m Char => ParsecT s u m Char)
78 alphaNum            = satisfy isAlphaNum    <?> "letter or digit"
79
80 -- | Parses a letter (an upper case or lower case character). Returns the
81 -- parsed character. 
82
83 letter :: (Stream s m Char) => ParsecT s u m Char
84 letter              = satisfy isAlpha       <?> "letter"
85
86 -- | Parses a digit. Returns the parsed character. 
87
88 digit :: (Stream s m Char) => ParsecT s u m Char
89 digit               = satisfy isDigit       <?> "digit"
90
91 -- | Parses a hexadecimal digit (a digit or a letter between \'a\' and
92 -- \'f\' or \'A\' and \'F\'). Returns the parsed character. 
93
94 hexDigit :: (Stream s m Char) => ParsecT s u m Char
95 hexDigit            = satisfy isHexDigit    <?> "hexadecimal digit"
96
97 -- | Parses an octal digit (a character between \'0\' and \'7\'). Returns
98 -- the parsed character. 
99
100 octDigit :: (Stream s m Char) => ParsecT s u m Char
101 octDigit            = satisfy isOctDigit    <?> "octal digit"
102
103 -- | @char c@ parses a single character @c@. Returns the parsed
104 -- character (i.e. @c@).
105 --
106 -- >  semiColon  = char ';'
107
108 char :: (Stream s m Char) => Char -> ParsecT s u m Char
109 char c              = satisfy (==c)  <?> show [c]
110
111 -- | This parser succeeds for any character. Returns the parsed character. 
112
113 anyChar :: (Stream s m Char) => ParsecT s u m Char
114 anyChar             = satisfy (const True)
115
116 -- | The parser @satisfy f@ succeeds for any character for which the
117 -- supplied function @f@ returns 'True'. Returns the character that is
118 -- actually parsed.
119
120 -- >  digit     = satisfy isDigit
121 -- >  oneOf cs  = satisfy (\c -> c `elem` cs)
122
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)
127
128 -- | @string s@ parses a sequence of characters given by @s@. Returns
129 -- the parsed string (i.e. @s@).
130 --
131 -- >  divOrMod    =   string "div" 
132 -- >              <|> string "mod"
133
134 string :: (Stream s m Char) => String -> ParsecT s u m String
135 string s            = tokens show updatePosString s