on master help sign in
Switch the value' parser away from backtracking
commit2fcf5085f58cc2fcc4d8b709d28c6da14bb62e6c authorBryan O'Sullivan <bos@serpentine.com> dateTue 26 Nov 2013, 23:41 (13 years ago) parentdcd86df
1 file changed+18−20
Data/Aeson/Parser/Internal.hs+18−20
Data/Aeson/Parser/Internal.hs+18−20
@@ -41,9 +41,9 @@ import Data.ByteString.Lazy.Builder
4141 (Builder, byteString, toLazyByteString, charUtf8, word8)
4242#endif
4343
44−import Control.Applicative ((*>), (<$>), (<*), (<|>), liftA2, pure)
44+import Control.Applicative ((*>), (<$>), (<*), liftA2, pure)
4545import Data.Aeson.Types (Result(..), Value(..))
46−import Data.Attoparsec.Char8 (Parser, char, endOfInput, rational, satisfy,
46+import Data.Attoparsec.Char8 (Parser, char, endOfInput, rational,
4747 skipSpace, string)
4848import Data.Bits ((.|.), shiftL)
4949import Data.ByteString (ByteString)
@@ -57,7 +57,6 @@ import qualified Data.Attoparsec as A
5757import qualified Data.Attoparsec.Lazy as L
5858import qualified Data.Attoparsec.Zepto as Z
5959import qualified Data.ByteString as B
60−import qualified Data.ByteString.Char8 as B8
6160import qualified Data.ByteString.Lazy as L
6261import qualified Data.ByteString.Unsafe as B
6362import qualified Data.HashMap.Strict as H
@@ -179,23 +178,22 @@ value = do
179178
180179-- | Strict version of 'value'. See also 'json''.
181180value' :: Parser Value
182−value' = most <|> num
183− where
184− most = do
185− c <- satisfy (`B8.elem` "{[\"ftn")
186− case c of
187− '{' -> object_'
188− '[' -> array_'
189− '"' -> do
190− !s <- jstring_
191− return (String s)
192− 'f' -> string "alse" *> pure (Bool False)
193− 't' -> string "rue" *> pure (Bool True)
194− 'n' -> string "ull" *> pure Null
195− _ -> error "attoparsec panic! the impossible happened!"
196− num = do
197− !n <- rational
198− return (Number n)
181+value' = do
182+ w <- A.peekWord8'
183+ case w of
184+ DOUBLE_QUOTE -> do
185+ !s <- A.anyWord8 *> jstring_
186+ return (String s)
187+ OPEN_CURLY -> A.anyWord8 *> object_'
188+ OPEN_SQUARE -> A.anyWord8 *> array_'
189+ C_f -> string "false" *> pure (Bool False)
190+ C_t -> string "true" *> pure (Bool True)
191+ C_n -> string "null" *> pure Null
192+ _ | w >= 48 && w <= 57 || w == 45
193+ -> do
194+ !n <- rational
195+ return (Number n)
196+ | otherwise -> fail "not a valid json value"
199197
200198-- | Parse a quoted JSON string.
201199jstring :: Parser Text