@@ -1,16 +1,15 @@
2-----------------------------------------------------------------------------
1-----------------------------------------------------------------------------
4-- Module : Data.Attoparsec.Char8
3-- Module : Data.Attoparsec.Char8
5−-- Copyright : Daan Leijen 1999-2001, Jeremy Shaw 2006, Bryan O'Sullivan 2007-2009
4+-- Copyright : Bryan O'Sullivan 2007-2010
6-- License : BSD3
5-- License : BSD3
8-- Maintainer : bos@serpentine.com
7-- Maintainer : bos@serpentine.com
9-- Stability : experimental
8-- Stability : experimental
10-- Portability : unknown
9-- Portability : unknown
12−-- Simple, efficient, character-oriented parser combinators for lazy
11+-- Simple, efficient, character-oriented parser combinators for
13−-- 'LB.ByteString' strings, loosely based on the Parsec library.
12+-- 'B.ByteString' strings, loosely based on the Parsec library.
15-- /Note/: This module is intended for parsing text that is
14-- /Note/: This module is intended for parsing text that is
16-- represented using an 8-bit character set, e.g. ASCII or
15-- represented using an 8-bit character set, e.g. ASCII or
@@ -21,17 +20,17 @@
21module Data.Attoparsec.Char8
20module Data.Attoparsec.Char8
23 -- * Parser types
22 -- * Parser types
25− , Parser
24+ , A.Result(..)
27 -- * Running parsers
26 -- * Running parsers
30− , parseTest
28+ , A.parseTest
32 -- * Combinators
31 -- * Combinators
36 -- * Parsing individual characters
35 -- * Parsing individual characters
@@ -48,57 +47,129 @@ module Data.Attoparsec.Char8
48 , notInClass
47 , notInClass
50 -- * Efficient string handling
49 -- * Efficient string handling
51− , string
50+ , I.string
52 , stringCI
51 , stringCI
53 , skipSpace
52 , skipSpace
54 , skipWhile
53 , skipWhile
57 , takeTill
55 , takeTill
58 , takeWhile
56 , takeWhile
59 , takeWhile1
57 , takeWhile1
65 -- * Text parsing
59 -- * Text parsing
66− , endOfLine
60+ , I.endOfLine
68 -- * Numeric parsers
62 -- * Numeric parsers
70− , integer
64+ --, integer
71− , double
65+ --, double
73 -- * State observation functions
67 -- * State observation functions
74− , endOfInput
68+ , I.endOfInput
79 -- * Combinators
70 -- * Combinators
80 , module Data.Attoparsec.Combinator
71 , module Data.Attoparsec.Combinator
74+import Data.Attoparsec.Combinator
75+import Data.Attoparsec.FastSet (charClass, memberChar)
76+import Data.Attoparsec.Internal (Parser, (<?>))
83import Data.ByteString.Internal (c2w, w2c)
77import Data.ByteString.Internal (c2w, w2c)
78+import Data.ByteString.Lex.Double (readDouble)
84import Data.Char (isDigit, isLetter, isSpace, toLower)
79import Data.Char (isDigit, isLetter, isSpace, toLower)
85−import Data.Attoparsec.FastSet (charClass, memberChar)
86import Data.Word (Word8)
80import Data.Word (Word8)
81+import Prelude hiding (takeWhile)
82+import qualified Data.Attoparsec as A
87import qualified Data.Attoparsec.Internal as I
83import qualified Data.Attoparsec.Internal as I
88−import Data.Attoparsec.Combinator
84+import qualified Data.ByteString.Char8 as B
89−import Data.Attoparsec.Internal
90−import Data.ByteString.Lex.Double (readDouble)
92-- | Satisfy a literal string, ignoring case.
86-- | Satisfy a literal string, ignoring case.
93−stringCI :: LB.ByteString -> Parser LB.ByteString
87+stringCI :: B.ByteString -> Parser B.ByteString
94−stringCI = I.stringTransform (LB.map toLower)
88+stringCI = I.stringTransform (B.map toLower)
95{-# INLINE stringCI #-}
89{-# INLINE stringCI #-}
97−takeWhile1 :: (Char -> Bool) -> Parser LB.ByteString
91+takeWhile1 :: (Char -> Bool) -> Parser B.ByteString
98takeWhile1 p = I.takeWhile1 (p . w2c)
92takeWhile1 p = I.takeWhile1 (p . w2c)
99{-# INLINE takeWhile1 #-}
93{-# INLINE takeWhile1 #-}
95+-- | Character parser.
96+satisfy :: (Char -> Bool) -> Parser Char
97+satisfy p = w2c `fmap` I.satisfy (p . w2c)
98+{-# INLINE satisfy #-}
100+letter :: Parser Char
101+letter = satisfy isLetter <?> "letter"
102+{-# INLINE letter #-}
105+digit = satisfy isDigit <?> "digit"
108+anyChar :: Parser Char
109+anyChar = satisfy $ const True
110+{-# INLINE anyChar #-}
113+space = satisfy isSpace <?> "space"
116+-- | Match a specific character.
117+char :: Char -> Parser Char
118+char c = satisfy (== c) <?> [c]
121+-- | Match a specific character.
122+char8 :: Char -> Parser Word8
123+char8 c = I.satisfy (== c2w c) <?> [c]
126+-- | Match any character except the given one.
127+notChar :: Char -> Parser Char
128+notChar c = satisfy (/= c) <?> "not " ++ [c]
129+{-# INLINE notChar #-}
131+-- | Match any character in a set.
133+-- > vowel = inClass "aeiou"
135+-- Range notation is supported.
137+-- > halfAlphabet = inClass "a-nA-N"
139+-- To add a literal \'-\' to a set, place it at the beginning or end
141+inClass :: String -> Char -> Bool
142+inClass s = (`memberChar` mySet)
143+ where mySet = charClass s
144+{-# INLINE inClass #-}
146+-- | Match any character not in a set.
147+notInClass :: String -> Char -> Bool
148+notInClass s = not . inClass s
149+{-# INLINE notInClass #-}
151+-- | Consume characters while the predicate succeeds.
152+takeWhile :: (Char -> Bool) -> Parser B.ByteString
153+takeWhile p = I.takeWhile (p . w2c)
154+{-# INLINE takeWhile #-}
156+-- | Consume characters while the predicate fails.
157+takeTill :: (Char -> Bool) -> Parser B.ByteString
158+takeTill p = I.takeTill (p . w2c)
159+{-# INLINE takeTill #-}
161+-- | Skip over characters while the predicate succeeds.
162+skipWhile :: (Char -> Bool) -> Parser ()
163+skipWhile p = I.skipWhile (p . w2c)
164+{-# INLINE skipWhile #-}
166+-- | Skip over white space.
167+skipSpace :: Parser ()
168+skipSpace = skipWhile isSpace >> return ()
169+{-# INLINE skipSpace #-}
101−numeric :: String -> (LB.ByteString -> Maybe (a,LB.ByteString)) -> Parser a
172+numeric :: String -> (B.ByteString -> Maybe (a,B.ByteString)) -> Parser a
102numeric desc f = do
173numeric desc f = do
103 s <- getInput
174 s <- getInput
104 case f s of
175 case f s of
@@ -107,15 +178,13 @@ numeric desc f = do
108-- | Parse an integer. The position counter is not updated.
179-- | Parse an integer. The position counter is not updated.
109int :: Parser Int
180int :: Parser Int
110−int = numeric "Int" LB.readInt
181+int = numeric "Int" B.readInt
112-- | Parse an integer. The position counter is not updated.
183-- | Parse an integer. The position counter is not updated.
113integer :: Parser Integer
184integer :: Parser Integer
114−integer = numeric "Integer" LB.readInteger
185+integer = numeric "Integer" B.readInteger
116-- | Parse a Double. The position counter is not updated.
187-- | Parse a Double. The position counter is not updated.
117double :: Parser Double
188double :: Parser Double
118double = numeric "Double" readDouble
189double = numeric "Double" readDouble
120−#define PARSER Parser
121−#include "Char8Boilerplate.h"