feat: clearer whitespace parsing
This commit is contained in:
parent
01d47850f6
commit
3aafe819fb
1 changed files with 17 additions and 8 deletions
|
@ -70,6 +70,7 @@ instance Arbitrary PrintableValue where
|
||||||
null :: Parser Value
|
null :: Parser Value
|
||||||
null = do
|
null = do
|
||||||
Parser.exact "null"
|
Parser.exact "null"
|
||||||
|
whitespace
|
||||||
pure Null
|
pure Null
|
||||||
|
|
||||||
true :: Parser Bool
|
true :: Parser Bool
|
||||||
|
@ -83,7 +84,7 @@ false = do
|
||||||
pure False
|
pure False
|
||||||
|
|
||||||
boolean :: Parser Bool
|
boolean :: Parser Bool
|
||||||
boolean = true <|> false
|
boolean = (true <|> false) <* whitespace
|
||||||
|
|
||||||
digit :: Parser Int
|
digit :: Parser Int
|
||||||
digit = Char.digitToInt <$> Parser.oneOf (Parser.char <$> ['0'..'9'])
|
digit = Char.digitToInt <$> Parser.oneOf (Parser.char <$> ['0'..'9'])
|
||||||
|
@ -138,6 +139,7 @@ number = do
|
||||||
decimalPartLength = length . show $ part
|
decimalPartLength = length . show $ part
|
||||||
in fromIntegral (integerPart * 10 ^ decimalPartLength + part) % 10 ^ decimalPartLength
|
in fromIntegral (integerPart * 10 ^ decimalPartLength + part) % 10 ^ decimalPartLength
|
||||||
|
|
||||||
|
whitespace
|
||||||
pure $ concatRational * fromIntegral @Integer (applySign factor (10 ^ Maybe.fromMaybe 0 power))
|
pure $ concatRational * fromIntegral @Integer (applySign factor (10 ^ Maybe.fromMaybe 0 power))
|
||||||
|
|
||||||
-- >>> import Language.Json.Parser
|
-- >>> import Language.Json.Parser
|
||||||
|
@ -153,6 +155,7 @@ string = do
|
||||||
_ <- Parser.exact "\""
|
_ <- Parser.exact "\""
|
||||||
str <- many stringChar
|
str <- many stringChar
|
||||||
_ <- Parser.exact "\""
|
_ <- Parser.exact "\""
|
||||||
|
whitespace
|
||||||
|
|
||||||
pure $ Text.pack str
|
pure $ Text.pack str
|
||||||
|
|
||||||
|
@ -209,15 +212,21 @@ value = Parser.oneOf
|
||||||
document :: Parser Value
|
document :: Parser Value
|
||||||
document = whitespace *> value <* whitespace <* Parser.eof
|
document = whitespace *> value <* whitespace <* Parser.eof
|
||||||
|
|
||||||
keyvalue :: Parser (Text, Value)
|
comma :: Parser Char
|
||||||
keyvalue = do
|
comma = Parser.char ',' <* whitespace
|
||||||
|
|
||||||
|
keyValue :: Parser (Text, Value)
|
||||||
|
keyValue = do
|
||||||
|
-- name
|
||||||
name <- string
|
name <- string
|
||||||
|
|
||||||
whitespace
|
-- separator
|
||||||
_ <- Parser.char ':'
|
Parser.char ':' *> whitespace
|
||||||
whitespace
|
|
||||||
|
|
||||||
|
-- value
|
||||||
val <- value
|
val <- value
|
||||||
|
|
||||||
|
-- result
|
||||||
pure (name, val)
|
pure (name, val)
|
||||||
|
|
||||||
object :: Parser (Map Text Value)
|
object :: Parser (Map Text Value)
|
||||||
|
@ -225,7 +234,7 @@ object = do
|
||||||
_ <- Parser.char '{'
|
_ <- Parser.char '{'
|
||||||
whitespace
|
whitespace
|
||||||
|
|
||||||
members <- (keyvalue <* whitespace) `Parser.sepBy` (Parser.char ',' <* whitespace)
|
members <- (keyValue <* whitespace) `Parser.sepBy` comma
|
||||||
|
|
||||||
_ <- Parser.char '}'
|
_ <- Parser.char '}'
|
||||||
pure $ Map.fromList members
|
pure $ Map.fromList members
|
||||||
|
@ -238,7 +247,7 @@ array = do
|
||||||
_ <- Parser.char '['
|
_ <- Parser.char '['
|
||||||
|
|
||||||
whitespace
|
whitespace
|
||||||
elements <- (value <* whitespace) `Parser.sepBy` (Parser.char ',' <* whitespace)
|
elements <- value `Parser.sepBy` comma
|
||||||
|
|
||||||
_ <- Parser.char ']'
|
_ <- Parser.char ']'
|
||||||
pure $ Array.listArray (0, fromIntegral $ length elements - 1) elements
|
pure $ Array.listArray (0, fromIntegral $ length elements - 1) elements
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue