@@ -1,16 +1,15 @@
1−{-# LANGUAGE CPP #-}
21-----------------------------------------------------------------------------
32-- |
43-- 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
65-- License : BSD3
76--
87-- Maintainer : bos@serpentine.com
98-- Stability : experimental
109-- Portability : unknown
1110--
12−-- Simple, efficient, character-oriented parser combinators for lazy
13−-- 'LB.ByteString' strings, loosely based on the Parsec library.
11+-- Simple, efficient, character-oriented parser combinators for
12+-- 'B.ByteString' strings, loosely based on the Parsec library.
1413--
1514-- /Note/: This module is intended for parsing text that is
1615-- represented using an 8-bit character set, e.g. ASCII or
@@ -21,17 +20,17 @@
2120module Data.Attoparsec.Char8
2221 (
2322 -- * Parser types
24− ParseError
25− , Parser
23+ Parser
24+ , A.Result(..)
2625
2726 -- * Running parsers
28− , parse
29− , parseAt
30− , parseTest
27+ , A.parse
28+ , A.parseTest
29+ , A.feed
3130
3231 -- * Combinators
33− , (<?>)
34− , try
32+ , (I.<?>)
33+ , I.try
3534
3635 -- * Parsing individual characters
3736 , anyChar
@@ -48,57 +47,129 @@ module Data.Attoparsec.Char8
4847 , notInClass
4948
5049 -- * Efficient string handling
51− , string
50+ , I.string
5251 , stringCI
5352 , skipSpace
5453 , skipWhile
55− , takeAll
56− , takeCount
54+ , take
5755 , takeTill
5856 , takeWhile
5957 , takeWhile1
6058
61− -- ** Combinators
62− , match
63− , notEmpty
64−
6559 -- * Text parsing
66− , endOfLine
60+ , I.endOfLine
6761
6862 -- * Numeric parsers
69− , int
70− , integer
71− , double
63+ --, int
64+ --, integer
65+ --, double
7266
7367 -- * State observation functions
74− , endOfInput
75− , getConsumed
76− , getInput
77− , lookAhead
68+ , I.endOfInput
7869
7970 -- * Combinators
8071 , module Data.Attoparsec.Combinator
8172 ) where
8273
74+import Data.Attoparsec.Combinator
75+import Data.Attoparsec.FastSet (charClass, memberChar)
76+import Data.Attoparsec.Internal (Parser, (<?>))
8377import Data.ByteString.Internal (c2w, w2c)
78+import Data.ByteString.Lex.Double (readDouble)
8479import Data.Char (isDigit, isLetter, isSpace, toLower)
85−import Data.Attoparsec.FastSet (charClass, memberChar)
8680import Data.Word (Word8)
81+import Prelude hiding (takeWhile)
82+import qualified Data.Attoparsec as A
8783import qualified Data.Attoparsec.Internal as I
88−import Data.Attoparsec.Combinator
89−import Data.Attoparsec.Internal
90−import Data.ByteString.Lex.Double (readDouble)
84+import qualified Data.ByteString.Char8 as B
9185
9286-- | Satisfy a literal string, ignoring case.
93−stringCI :: LB.ByteString -> Parser LB.ByteString
94−stringCI = I.stringTransform (LB.map toLower)
87+stringCI :: B.ByteString -> Parser B.ByteString
88+stringCI = I.stringTransform (B.map toLower)
9589{-# INLINE stringCI #-}
9690
97−takeWhile1 :: (Char -> Bool) -> Parser LB.ByteString
91+takeWhile1 :: (Char -> Bool) -> Parser B.ByteString
9892takeWhile1 p = I.takeWhile1 (p . w2c)
9993{-# INLINE takeWhile1 #-}
10094
101−numeric :: String -> (LB.ByteString -> Maybe (a,LB.ByteString)) -> Parser a
95+-- | Character parser.
96+satisfy :: (Char -> Bool) -> Parser Char
97+satisfy p = w2c `fmap` I.satisfy (p . w2c)
98+{-# INLINE satisfy #-}
99+
100+letter :: Parser Char
101+letter = satisfy isLetter <?> "letter"
102+{-# INLINE letter #-}
103+
104+digit :: Parser Char
105+digit = satisfy isDigit <?> "digit"
106+{-# INLINE digit #-}
107+
108+anyChar :: Parser Char
109+anyChar = satisfy $ const True
110+{-# INLINE anyChar #-}
111+
112+space :: Parser Char
113+space = satisfy isSpace <?> "space"
114+{-# INLINE space #-}
115+
116+-- | Match a specific character.
117+char :: Char -> Parser Char
118+char c = satisfy (== c) <?> [c]
119+{-# INLINE char #-}
120+
121+-- | Match a specific character.
122+char8 :: Char -> Parser Word8
123+char8 c = I.satisfy (== c2w c) <?> [c]
124+{-# INLINE char8 #-}
125+
126+-- | Match any character except the given one.
127+notChar :: Char -> Parser Char
128+notChar c = satisfy (/= c) <?> "not " ++ [c]
129+{-# INLINE notChar #-}
130+
131+-- | Match any character in a set.
132+--
133+-- > vowel = inClass "aeiou"
134+--
135+-- Range notation is supported.
136+--
137+-- > halfAlphabet = inClass "a-nA-N"
138+--
139+-- To add a literal \'-\' to a set, place it at the beginning or end
140+-- of the string.
141+inClass :: String -> Char -> Bool
142+inClass s = (`memberChar` mySet)
143+ where mySet = charClass s
144+{-# INLINE inClass #-}
145+
146+-- | Match any character not in a set.
147+notInClass :: String -> Char -> Bool
148+notInClass s = not . inClass s
149+{-# INLINE notInClass #-}
150+
151+-- | Consume characters while the predicate succeeds.
152+takeWhile :: (Char -> Bool) -> Parser B.ByteString
153+takeWhile p = I.takeWhile (p . w2c)
154+{-# INLINE takeWhile #-}
155+
156+-- | Consume characters while the predicate fails.
157+takeTill :: (Char -> Bool) -> Parser B.ByteString
158+takeTill p = I.takeTill (p . w2c)
159+{-# INLINE takeTill #-}
160+
161+-- | Skip over characters while the predicate succeeds.
162+skipWhile :: (Char -> Bool) -> Parser ()
163+skipWhile p = I.skipWhile (p . w2c)
164+{-# INLINE skipWhile #-}
165+
166+-- | Skip over white space.
167+skipSpace :: Parser ()
168+skipSpace = skipWhile isSpace >> return ()
169+{-# INLINE skipSpace #-}
170+
171+{-
172+numeric :: String -> (B.ByteString -> Maybe (a,B.ByteString)) -> Parser a
102173numeric desc f = do
103174 s <- getInput
104175 case f s of
@@ -107,15 +178,13 @@ numeric desc f = do
107178
108179-- | Parse an integer. The position counter is not updated.
109180int :: Parser Int
110−int = numeric "Int" LB.readInt
181+int = numeric "Int" B.readInt
111182
112183-- | Parse an integer. The position counter is not updated.
113184integer :: Parser Integer
114−integer = numeric "Integer" LB.readInteger
185+integer = numeric "Integer" B.readInteger
115186
116187-- | Parse a Double. The position counter is not updated.
117188double :: Parser Double
118189double = numeric "Double" readDouble
119−
120−#define PARSER Parser
121−#include "Char8Boilerplate.h"
190+-}