@@ -1,9 +1,6 @@
1−{-# LANGUAGE BangPatterns, FlexibleInstances #-}
2−{-# OPTIONS_GHC -fno-warn-orphans #-}
3−
41-- |
52-- Module : Data.Attoparsec.Char8
6−-- Copyright : Bryan O'Sullivan 2007-2010
3+-- Copyright : Bryan O'Sullivan 2007-2011
74-- License : BSD3
85--
96-- Maintainer : bos@serpentine.com
@@ -11,492 +8,11 @@
118-- Portability : unknown
129--
1310-- Simple, efficient, character-oriented combinator parsing for
14−-- 'B.ByteString' strings, loosely based on the Parsec library.
11+-- 'ByteString' strings, loosely based on the Parsec library.
1512
1613module Data.Attoparsec.Char8
1714 (
18− -- * Character encodings
19− -- $encodings
20−
21− -- * Parser types
22− Parser
23− , A.Result(..)
24−
25− -- * Running parsers
26− , A.parse
27− , A.feed
28− , A.parseOnly
29− , A.parseTest
30− , A.parseWith
31−
32− -- ** Result conversion
33− , A.maybeResult
34− , A.eitherResult
35−
36− -- * Combinators
37− , (I.<?>)
38− , I.try
39− , module Data.Attoparsec.Combinator
40−
41− -- * Parsing individual characters
42− , char
43− , char8
44− , anyChar
45− , notChar
46− , satisfy
47−
48− -- ** Special character parsers
49− , digit
50− , letter_iso8859_15
51− , letter_ascii
52− , space
53−
54− -- ** Fast predicates
55− , isDigit
56− , isDigit_w8
57− , isAlpha_iso8859_15
58− , isAlpha_ascii
59− , isSpace
60− , isSpace_w8
61−
62− -- *** Character classes
63− , inClass
64− , notInClass
65−
66− -- * Efficient string handling
67− , I.string
68− , stringCI
69− , skipSpace
70− , skipWhile
71− , I.take
72− , scan
73− , takeWhile
74− , takeWhile1
75− , takeTill
76−
77− -- ** Consume all remaining input
78− , I.takeByteString
79− , I.takeLazyByteString
80−
81− -- * Text parsing
82− , I.endOfLine
83− , isEndOfLine
84− , isHorizontalSpace
85−
86− -- * Numeric parsers
87− , decimal
88− , hexadecimal
89− , signed
90− , double
91− , Number(..)
92− , number
93− , rational
94−
95− -- * State observation and manipulation functions
96− , I.endOfInput
97− , I.atEnd
15+ module Data.Attoparsec.ByteString.Char8
9816 ) where
9917
100−import Control.Applicative ((*>), (<$>), (<|>))
101−import Data.Attoparsec.Combinator
102−import Data.Attoparsec.FastSet (charClass, memberChar)
103−import Data.Attoparsec.Internal (Parser, (<?>))
104−import Data.Attoparsec.Number (Number(..))
105−import Data.Bits (Bits, (.|.), shiftL)
106−import Data.ByteString.Internal (c2w, w2c)
107−import Data.Int (Int8, Int16, Int32, Int64)
108−import Data.Ratio ((%))
109−import Data.String (IsString(..))
110−import Data.Word (Word8, Word16, Word32, Word64, Word)
111−import Prelude hiding (takeWhile)
112−import qualified Data.Attoparsec as A
113−import qualified Data.Attoparsec.Internal as I
114−import qualified Data.ByteString as B8
115−import qualified Data.ByteString.Char8 as B
116−
117−instance IsString (Parser B.ByteString) where
118− fromString = I.string . B.pack
119−
120−-- $encodings
121−--
122−-- This module is intended for parsing text that is
123−-- represented using an 8-bit character set, e.g. ASCII or
124−-- ISO-8859-15. It /does not/ make any attempt to deal with character
125−-- encodings, multibyte characters, or wide characters. In
126−-- particular, all attempts to use characters above code point U+00FF
127−-- will give wrong answers.
128−--
129−-- Code points below U+0100 are simply translated to and from their
130−-- numeric values, so e.g. the code point U+00A4 becomes the byte
131−-- @0xA4@ (which is the Euro symbol in ISO-8859-15, but the generic
132−-- currency sign in ISO-8859-1). Haskell 'Char' values above U+00FF
133−-- are truncated, so e.g. U+1D6B7 is truncated to the byte @0xB7@.
134−
135−-- ASCII-specific but fast, oh yes.
136−toLower :: Word8 -> Word8
137−toLower w | w >= 65 && w <= 90 = w + 32
138− | otherwise = w
139−
140−-- | Satisfy a literal string, ignoring case.
141−stringCI :: B.ByteString -> Parser B.ByteString
142−stringCI = I.stringTransform (B8.map toLower)
143−{-# INLINE stringCI #-}
144−
145−-- | Consume input as long as the predicate returns 'True', and return
146−-- the consumed input.
147−--
148−-- This parser requires the predicate to succeed on at least one byte
149−-- of input: it will fail if the predicate never returns 'True' or if
150−-- there is no input left.
151−takeWhile1 :: (Char -> Bool) -> Parser B.ByteString
152−takeWhile1 p = I.takeWhile1 (p . w2c)
153−{-# INLINE takeWhile1 #-}
154−
155−-- | The parser @satisfy p@ succeeds for any byte for which the
156−-- predicate @p@ returns 'True'. Returns the byte that is actually
157−-- parsed.
158−--
159−-- >digit = satisfy isDigit
160−-- > where isDigit c = c >= '0' && c <= '9'
161−satisfy :: (Char -> Bool) -> Parser Char
162−satisfy = I.satisfyWith w2c
163−{-# INLINE satisfy #-}
164−
165−-- | Match a letter, in the ISO-8859-15 encoding.
166−letter_iso8859_15 :: Parser Char
167−letter_iso8859_15 = satisfy isAlpha_iso8859_15 <?> "letter_iso8859_15"
168−{-# INLINE letter_iso8859_15 #-}
169−
170−-- | Match a letter, in the ASCII encoding.
171−letter_ascii :: Parser Char
172−letter_ascii = satisfy isAlpha_ascii <?> "letter_ascii"
173−{-# INLINE letter_ascii #-}
174−
175−-- | A fast alphabetic predicate for the ISO-8859-15 encoding
176−--
177−-- /Note/: For all character encodings other than ISO-8859-15, and
178−-- almost all Unicode code points above U+00A3, this predicate gives
179−-- /wrong answers/.
180−isAlpha_iso8859_15 :: Char -> Bool
181−isAlpha_iso8859_15 c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
182− (c >= '\166' && moby c)
183− where moby = notInClass "\167\169\171-\179\182\183\185\187\191\215\247"
184− {-# NOINLINE moby #-}
185−{-# INLINE isAlpha_iso8859_15 #-}
186−
187−-- | A fast alphabetic predicate for the ASCII encoding
188−--
189−-- /Note/: For all character encodings other than ASCII, and
190−-- almost all Unicode code points above U+007F, this predicate gives
191−-- /wrong answers/.
192−isAlpha_ascii :: Char -> Bool
193−isAlpha_ascii c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
194−{-# INLINE isAlpha_ascii #-}
195−
196−-- | Parse a single digit.
197−digit :: Parser Char
198−digit = satisfy isDigit <?> "digit"
199−{-# INLINE digit #-}
200−
201−-- | A fast digit predicate.
202−isDigit :: Char -> Bool
203−isDigit c = c >= '0' && c <= '9'
204−{-# INLINE isDigit #-}
205−
206−-- | A fast digit predicate.
207−isDigit_w8 :: Word8 -> Bool
208−isDigit_w8 w = w >= 48 && w <= 57
209−{-# INLINE isDigit_w8 #-}
210−
211−-- | Match any character.
212−anyChar :: Parser Char
213−anyChar = satisfy $ const True
214−{-# INLINE anyChar #-}
215−
216−-- | Fast predicate for matching ASCII space characters.
217−--
218−-- /Note/: This predicate only gives correct answers for the ASCII
219−-- encoding. For instance, it does not recognise U+00A0 (non-breaking
220−-- space) as a space character, even though it is a valid ISO-8859-15
221−-- byte. For a Unicode-aware and only slightly slower predicate,
222−-- use 'Data.Char.isSpace'
223−isSpace :: Char -> Bool
224−isSpace c = (c == ' ') || ('\t' <= c && c <= '\r')
225−{-# INLINE isSpace #-}
226−
227−-- | Fast 'Word8' predicate for matching ASCII space characters.
228−isSpace_w8 :: Word8 -> Bool
229−isSpace_w8 w = (w == 32) || (9 <= w && w <= 13)
230−{-# INLINE isSpace_w8 #-}
231−
232−
233−-- | Parse a space character.
234−--
235−-- /Note/: This parser only gives correct answers for the ASCII
236−-- encoding. For instance, it does not recognise U+00A0 (non-breaking
237−-- space) as a space character, even though it is a valid ISO-8859-15
238−-- byte.
239−space :: Parser Char
240−space = satisfy isSpace <?> "space"
241−{-# INLINE space #-}
242−
243−-- | Match a specific character.
244−char :: Char -> Parser Char
245−char c = satisfy (== c) <?> [c]
246−{-# INLINE char #-}
247−
248−-- | Match a specific character, but return its 'Word8' value.
249−char8 :: Char -> Parser Word8
250−char8 c = I.satisfy (== c2w c) <?> [c]
251−{-# INLINE char8 #-}
252−
253−-- | Match any character except the given one.
254−notChar :: Char -> Parser Char
255−notChar c = satisfy (/= c) <?> "not " ++ [c]
256−{-# INLINE notChar #-}
257−
258−-- | Match any character in a set.
259−--
260−-- >vowel = inClass "aeiou"
261−--
262−-- Range notation is supported.
263−--
264−-- >halfAlphabet = inClass "a-nA-N"
265−--
266−-- To add a literal \'-\' to a set, place it at the beginning or end
267−-- of the string.
268−inClass :: String -> Char -> Bool
269−inClass s = (`memberChar` mySet)
270− where mySet = charClass s
271−{-# INLINE inClass #-}
272−
273−-- | Match any character not in a set.
274−notInClass :: String -> Char -> Bool
275−notInClass s = not . inClass s
276−{-# INLINE notInClass #-}
277−
278−-- | Consume input as long as the predicate returns 'True', and return
279−-- the consumed input.
280−--
281−-- This parser does not fail. It will return an empty string if the
282−-- predicate returns 'False' on the first byte of input.
283−--
284−-- /Note/: Because this parser does not fail, do not use it with
285−-- combinators such as 'many', because such parsers loop until a
286−-- failure occurs. Careless use will thus result in an infinite loop.
287−takeWhile :: (Char -> Bool) -> Parser B.ByteString
288−takeWhile p = I.takeWhile (p . w2c)
289−{-# INLINE takeWhile #-}
290−
291−-- | A stateful scanner. The predicate consumes and transforms a
292−-- state argument, and each transformed state is passed to successive
293−-- invocations of the predicate on each byte of the input until one
294−-- returns 'Nothing' or the input ends.
295−--
296−-- This parser does not fail. It will return an empty string if the
297−-- predicate returns 'Nothing' on the first byte of input.
298−--
299−-- /Note/: Because this parser does not fail, do not use it with
300−-- combinators such as 'many', because such parsers loop until a
301−-- failure occurs. Careless use will thus result in an infinite loop.
302−scan :: s -> (s -> Char -> Maybe s) -> Parser B.ByteString
303−scan s0 p = I.scan s0 (\s -> p s . w2c)
304−{-# INLINE scan #-}
305−
306−-- | Consume input as long as the predicate returns 'False'
307−-- (i.e. until it returns 'True'), and return the consumed input.
308−--
309−-- This parser does not fail. It will return an empty string if the
310−-- predicate returns 'True' on the first byte of input.
311−--
312−-- /Note/: Because this parser does not fail, do not use it with
313−-- combinators such as 'many', because such parsers loop until a
314−-- failure occurs. Careless use will thus result in an infinite loop.
315−takeTill :: (Char -> Bool) -> Parser B.ByteString
316−takeTill p = I.takeTill (p . w2c)
317−{-# INLINE takeTill #-}
318−
319−-- | Skip past input for as long as the predicate returns 'True'.
320−skipWhile :: (Char -> Bool) -> Parser ()
321−skipWhile p = I.skipWhile (p . w2c)
322−{-# INLINE skipWhile #-}
323−
324−-- | Skip over white space.
325−skipSpace :: Parser ()
326−skipSpace = I.skipWhile isSpace_w8
327−{-# INLINE skipSpace #-}
328−
329−-- | A predicate that matches either a carriage return @\'\\r\'@ or
330−-- newline @\'\\n\'@ character.
331−isEndOfLine :: Word8 -> Bool
332−isEndOfLine w = w == 13 || w == 10
333−{-# INLINE isEndOfLine #-}
334−
335−-- | A predicate that matches either a space @\' \'@ or horizontal tab
336−-- @\'\\t\'@ character.
337−isHorizontalSpace :: Word8 -> Bool
338−isHorizontalSpace w = w == 32 || w == 9
339−{-# INLINE isHorizontalSpace #-}
340−
341−-- | Parse and decode an unsigned hexadecimal number. The hex digits
342−-- @\'a\'@ through @\'f\'@ may be upper or lower case.
343−--
344−-- This parser does not accept a leading @\"0x\"@ string.
345−hexadecimal :: (Integral a, Bits a) => Parser a
346−hexadecimal = B8.foldl' step 0 `fmap` I.takeWhile1 isHexDigit
347− where
348− isHexDigit w = (w >= 48 && w <= 57) ||
349− (w >= 97 && w <= 102) ||
350− (w >= 65 && w <= 90)
351− step a w | w >= 48 && w <= 57 = (a `shiftL` 4) .|. fromIntegral (w - 48)
352− | w >= 97 = (a `shiftL` 4) .|. fromIntegral (w - 87)
353− | otherwise = (a `shiftL` 4) .|. fromIntegral (w - 55)
354−{-# SPECIALISE hexadecimal :: Parser Int #-}
355−{-# SPECIALISE hexadecimal :: Parser Int8 #-}
356−{-# SPECIALISE hexadecimal :: Parser Int16 #-}
357−{-# SPECIALISE hexadecimal :: Parser Int32 #-}
358−{-# SPECIALISE hexadecimal :: Parser Int64 #-}
359−{-# SPECIALISE hexadecimal :: Parser Integer #-}
360−{-# SPECIALISE hexadecimal :: Parser Word #-}
361−{-# SPECIALISE hexadecimal :: Parser Word8 #-}
362−{-# SPECIALISE hexadecimal :: Parser Word16 #-}
363−{-# SPECIALISE hexadecimal :: Parser Word32 #-}
364−{-# SPECIALISE hexadecimal :: Parser Word64 #-}
365−
366−-- | Parse and decode an unsigned decimal number.
367−decimal :: Integral a => Parser a
368−decimal = B8.foldl' step 0 `fmap` I.takeWhile1 isDig
369− where isDig w = w >= 48 && w <= 57
370− step a w = a * 10 + fromIntegral (w - 48)
371−{-# SPECIALISE decimal :: Parser Int #-}
372−{-# SPECIALISE decimal :: Parser Int8 #-}
373−{-# SPECIALISE decimal :: Parser Int16 #-}
374−{-# SPECIALISE decimal :: Parser Int32 #-}
375−{-# SPECIALISE decimal :: Parser Int64 #-}
376−{-# SPECIALISE decimal :: Parser Integer #-}
377−{-# SPECIALISE decimal :: Parser Word #-}
378−{-# SPECIALISE decimal :: Parser Word8 #-}
379−{-# SPECIALISE decimal :: Parser Word16 #-}
380−{-# SPECIALISE decimal :: Parser Word32 #-}
381−{-# SPECIALISE decimal :: Parser Word64 #-}
382−
383−-- | Parse a number with an optional leading @\'+\'@ or @\'-\'@ sign
384−-- character.
385−signed :: Num a => Parser a -> Parser a
386−{-# SPECIALISE signed :: Parser Int -> Parser Int #-}
387−{-# SPECIALISE signed :: Parser Int8 -> Parser Int8 #-}
388−{-# SPECIALISE signed :: Parser Int16 -> Parser Int16 #-}
389−{-# SPECIALISE signed :: Parser Int32 -> Parser Int32 #-}
390−{-# SPECIALISE signed :: Parser Int64 -> Parser Int64 #-}
391−{-# SPECIALISE signed :: Parser Integer -> Parser Integer #-}
392−signed p = (negate <$> (char8 '-' *> p))
393− <|> (char8 '+' *> p)
394− <|> p
395−
396−-- | Parse a rational number.
397−--
398−-- This parser accepts an optional leading sign character, followed by
399−-- at least one decimal digit. The syntax similar to that accepted by
400−-- the 'read' function, with the exception that a trailing @\'.\'@ or
401−-- @\'e\'@ /not/ followed by a number is not consumed.
402−--
403−-- Examples with behaviour identical to 'read', if you feed an empty
404−-- continuation to the first result:
405−--
406−-- >rational "3" == Done 3.0 ""
407−-- >rational "3.1" == Done 3.1 ""
408−-- >rational "3e4" == Done 30000.0 ""
409−-- >rational "3.1e4" == Done 31000.0, ""
410−
411−-- Examples with behaviour identical to 'read':
412−--
413−-- >rational ".3" == Fail "input does not start with a digit"
414−-- >rational "e3" == Fail "input does not start with a digit"
415−--
416−-- Examples of differences from 'read':
417−--
418−-- >rational "3.foo" == Done 3.0 ".foo"
419−-- >rational "3e" == Done 3.0 "e"
420−--
421−-- This function does not accept string representations of \"NaN\" or
422−-- \"Infinity\".
423−rational :: Fractional a => Parser a
424−{-# SPECIALIZE rational :: Parser Double #-}
425−{-# SPECIALIZE rational :: Parser Float #-}
426−{-# SPECIALIZE rational :: Parser Rational #-}
427−rational = floaty $ \real frac fracDenom -> fromRational $
428− real % 1 + frac % fracDenom
429−
430−-- | Parse a rational number.
431−--
432−-- The syntax accepted by this parser is the same as for 'rational'.
433−--
434−-- /Note/: This function is almost ten times faster than 'rational',
435−-- but is slightly less accurate.
436−--
437−-- The 'Double' type supports about 16 decimal places of accuracy.
438−-- For 94.2% of numbers, this function and 'rational' give identical
439−-- results, but for the remaining 5.8%, this function loses precision
440−-- around the 15th decimal place. For 0.001% of numbers, this
441−-- function will lose precision at the 13th or 14th decimal place.
442−--
443−-- This function does not accept string representations of \"NaN\" or
444−-- \"Infinity\".
445−double :: Parser Double
446−double = floaty asDouble
447−
448−asDouble :: Integer -> Integer -> Integer -> Double
449−asDouble real frac fracDenom =
450− fromIntegral real + fromIntegral frac / fromIntegral fracDenom
451−{-# INLINE asDouble #-}
452−
453−-- | Parse a number, attempting to preserve both speed and precision.
454−--
455−-- The syntax accepted by this parser is the same as for 'rational'.
456−--
457−-- /Note/: This function is almost ten times faster than 'rational'.
458−-- On integral inputs, it gives perfectly accurate answers, and on
459−-- floating point inputs, it is slightly less accurate than
460−-- 'rational'.
461−--
462−-- This function does not accept string representations of \"NaN\" or
463−-- \"Infinity\".
464−number :: Parser Number
465−number = floaty $ \real frac fracDenom ->
466− if frac == 0 && fracDenom == 0
467− then I real
468− else D (asDouble real frac fracDenom)
469−{-# INLINE number #-}
470−
471−data T = T !Integer !Int
472−
473−floaty :: Fractional a => (Integer -> Integer -> Integer -> a) -> Parser a
474−{-# INLINE floaty #-}
475−floaty f = do
476− let minus = 45
477− plus = 43
478− !positive <- ((== plus) <$> I.satisfy (\c -> c == minus || c == plus)) <|>
479− return True
480− real <- decimal
481− let tryFraction = do
482− let dot = 46
483− _ <- I.satisfy (==dot)
484− ds <- I.takeWhile isDigit_w8
485− case I.parseOnly decimal ds of
486− Right n -> return $ T n (B.length ds)
487− _ -> fail "no digits after decimal"
488− T fraction fracDigits <- tryFraction <|> return (T 0 0)
489− let littleE = 101
490− bigE = 69
491− e w = w == littleE || w == bigE
492− power <- (I.satisfy e *> signed decimal) <|> return (0::Int)
493− let n = if fracDigits == 0
494− then if power == 0
495− then fromIntegral real
496− else fromIntegral real * (10 ^^ power)
497− else if power == 0
498− then f real fraction (10 ^ fracDigits)
499− else f real fraction (10 ^ fracDigits) * (10 ^^ power)
500− return $ if positive
501− then n
502− else -n
18+import Data.Attoparsec.ByteString.Char8