Skip to content

Conversation

pranaysashank
Copy link
Member

No description provided.

@pranaysashank pranaysashank changed the title Json Json parser Jul 17, 2020
@pranaysashank pranaysashank force-pushed the json branch 2 times, most recently from 9ffbe66 to ec4f205 Compare July 24, 2020 09:47

{-# INLINE match #-}
match :: MonadCatch m => Word8 -> Parser m Word8 ()
match w = P.eqBy (==) [w]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be P.satisfy (== w).

where
initial = return begin

step s a = return $ s * 10 + fromIntegral (a - 48)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are assuming that the input is already validated to be digits.

n <- P.takeWhile1 (\w -> w - 48 <= 9) (foldToInteger 0)
when (z == zero && n /= 0) $ do
P.die $ " Leading zero in a number is not accepted in JSON."
return n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not use a monad here, I am not sure if it fuses, you can check. We can write it in other ways e.g.:

  1. write it directly as a primitive parser
  2. use a tee

let positive = sign == plus || sign /= minus
pr = P.takeWhile1 (\w -> w - 48 <= 9) (foldToInteger 0)
when (sign == plus || sign == minus) (skip 1)
if positive then pr else negate <$> pr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using monad.

{-# INLINE parseJsonNumber #-}
parseJsonNumber :: MonadCatch m => Parser m Word8 Scientific
parseJsonNumber = do
sign <- P.peek
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid monad.


{-# INLINE parseJsonString #-}
parseJsonString :: MonadCatch m => Parser m Word8 JsonString
parseJsonString = do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid monad.

parseJson :: MonadCatch m => PR.Parser m Word8 Value
parseJson = K.toParserK $ parseJsonValue

{-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this?

escapeFoldUtf8With :: Monad m => Fold m Char container -> Fold m Word8 container
escapeFoldUtf8With = Uni.escapeFoldUtf8With 92 jsonEscapes

type JsonString = Array Char
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we have this as Array Word8 using UTF-8 encoding? When needed it can be converted to Chars.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using Data.Array for this? It is very inefficient. We can use the Storable array instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants