|
| 1 | +{-# LANGUAGE BangPatterns #-} |
| 2 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 3 | +module Data.Aeson.Internal.Scientific ( |
| 4 | + scanScientific, |
| 5 | +) where |
| 6 | + |
| 7 | +import Data.Integer.Conversion (textToInteger) |
| 8 | +import Data.Scientific (Scientific) |
| 9 | +import Data.Text (Text) |
| 10 | + |
| 11 | +import qualified Data.Scientific as Sci |
| 12 | +import qualified Data.Text as T |
| 13 | + |
| 14 | +-- | Parse 'Scientific' number from 'Text'. |
| 15 | +-- |
| 16 | +-- This is different from how JSON numbers are parsed: arbitrary leading zeroes are accepted. |
| 17 | +-- |
| 18 | +scanScientific |
| 19 | + :: forall r. (Scientific -> Text -> r) |
| 20 | + -> (String -> r) |
| 21 | + -> Text |
| 22 | + -> r |
| 23 | +scanScientific kont err input0 = case T.uncons input0 of |
| 24 | + Nothing -> errEnd |
| 25 | + Just (c, text') |
| 26 | + | c == '+' -> scanScientific' kont err text' |
| 27 | + | c == '-' -> scanScientific' (\sci -> kont (negate sci)) err text' |
| 28 | + | otherwise -> scanScientific' kont err input0 |
| 29 | + where |
| 30 | + errEnd = err "Unexpected end-of-input while parsing number literal" |
| 31 | + |
| 32 | +scanScientific' |
| 33 | + :: forall r. (Scientific -> Text -> r) |
| 34 | + -> (String -> r) |
| 35 | + -> Text |
| 36 | + -> r |
| 37 | +scanScientific' kont err input0 = state_start input0 where |
| 38 | + state_start :: Text -> r |
| 39 | + state_start !text = case T.uncons text of |
| 40 | + Nothing -> errEnd |
| 41 | + Just (c, text') |
| 42 | + | '0' <= c, c <= '9' -> state_i 1 text' |
| 43 | + | otherwise -> err $ "Unexpected " ++ show c ++ " while parsing number literal" |
| 44 | + |
| 45 | + state_i :: Int -> Text -> r |
| 46 | + state_i !n !text = case T.uncons text of |
| 47 | + Nothing -> kont (fromInteger int) text |
| 48 | + Just (c, text') |
| 49 | + | '0' <= c, c <= '9' -> state_i (n + 1) text' |
| 50 | + | '.' == c -> go_dec int text' |
| 51 | + | 'e' == c || 'E' == c -> go_sci int 0 text' |
| 52 | + | otherwise -> kont (fromInteger int) text |
| 53 | + where |
| 54 | + int = textToInteger (T.take n input0) |
| 55 | + |
| 56 | + go_dec :: Integer -> Text -> r |
| 57 | + go_dec !int !text1 = case T.uncons text1 of |
| 58 | + Nothing -> errEnd |
| 59 | + Just (c, text') |
| 60 | + | '0' <= c, c <= '9' -> state_dec 1 text' |
| 61 | + | otherwise -> err $ "Unexpected " ++ show c ++ " while parsing number literal" |
| 62 | + where |
| 63 | + state_dec :: Int -> Text -> r |
| 64 | + state_dec !n !text = case T.uncons text of |
| 65 | + Nothing -> kont dec text |
| 66 | + Just (c, text') |
| 67 | + | '0' <= c, c <= '9' -> state_dec (n + 1) text' |
| 68 | + | 'e' == c || 'E' == c -> go_sci coef (negate n) text' |
| 69 | + | otherwise -> kont dec text |
| 70 | + where |
| 71 | + frac = textToInteger (T.take n text1) |
| 72 | + coef = int * 10 ^ n + frac |
| 73 | + dec = Sci.scientific coef (negate n) |
| 74 | + |
| 75 | + go_sci :: Integer -> Int -> Text -> r |
| 76 | + go_sci !coef !exp10 !text2 = case T.uncons text2 of |
| 77 | + Nothing -> errEnd |
| 78 | + Just (c, text') |
| 79 | + | '0' <= c, c <= '9' -> go_sci_pos coef exp10 text2 1 text' |
| 80 | + | '+' == c -> case T.uncons text' of |
| 81 | + Nothing -> errEnd |
| 82 | + Just (c', text'') |
| 83 | + | '0' <= c', c' <= '9' -> go_sci_pos coef exp10 text' 1 text'' |
| 84 | + | otherwise -> errUnx c' |
| 85 | + | '-' == c -> case T.uncons text' of |
| 86 | + Nothing -> errEnd |
| 87 | + Just (c', text'') |
| 88 | + | '0' <= c', c' <= '9' -> go_sci_neg coef exp10 text' 1 text'' |
| 89 | + | otherwise -> errUnx c' |
| 90 | + | otherwise -> errUnx c |
| 91 | + |
| 92 | + go_sci_pos :: Integer -> Int -> Text -> Int -> Text -> r |
| 93 | + go_sci_pos !coef !exp10 !text2 !n !text = case T.uncons text of |
| 94 | + Nothing -> kont sci text |
| 95 | + Just (c, text') |
| 96 | + | '0' <= c, c <= '9' -> go_sci_pos coef exp10 text2 (n + 1) text' |
| 97 | + | otherwise -> kont sci text |
| 98 | + where |
| 99 | + exp10' = fromInteger (textToInteger (T.take n text2)) |
| 100 | + sci = Sci.scientific coef (exp10 + exp10') |
| 101 | + |
| 102 | + go_sci_neg :: Integer -> Int -> Text -> Int -> Text -> r |
| 103 | + go_sci_neg !coef !exp10 !text2 !n !text = case T.uncons text of |
| 104 | + Nothing -> kont sci text |
| 105 | + Just (c, text') |
| 106 | + | '0' <= c, c <= '9' -> go_sci_neg coef exp10 text2 (n + 1) text' |
| 107 | + | otherwise -> kont sci text |
| 108 | + where |
| 109 | + exp10' = fromInteger (textToInteger (T.take n text2)) |
| 110 | + sci = Sci.scientific coef (exp10 - exp10') |
| 111 | + |
| 112 | + errEnd = err "Unexpected end-of-input while parsing number literal" |
| 113 | + errUnx c = err $ "Unexpected " ++ show c ++ " while parsing number literal" |
0 commit comments