feat: consistent whitespace + non-empty sepBy
This commit is contained in:
parent
3aafe819fb
commit
76e8cea454
2 changed files with 9 additions and 4 deletions
|
@ -227,6 +227,7 @@ keyValue = do
|
||||||
val <- value
|
val <- value
|
||||||
|
|
||||||
-- result
|
-- result
|
||||||
|
whitespace
|
||||||
pure (name, val)
|
pure (name, val)
|
||||||
|
|
||||||
object :: Parser (Map Text Value)
|
object :: Parser (Map Text Value)
|
||||||
|
@ -234,9 +235,10 @@ object = do
|
||||||
_ <- Parser.char '{'
|
_ <- Parser.char '{'
|
||||||
whitespace
|
whitespace
|
||||||
|
|
||||||
members <- (keyValue <* whitespace) `Parser.sepBy` comma
|
members <- keyValue `Parser.sepBy` comma
|
||||||
|
|
||||||
_ <- Parser.char '}'
|
_ <- Parser.char '}'
|
||||||
|
whitespace
|
||||||
pure $ Map.fromList members
|
pure $ Map.fromList members
|
||||||
|
|
||||||
whitespace :: Parser ()
|
whitespace :: Parser ()
|
||||||
|
@ -250,5 +252,8 @@ array = do
|
||||||
elements <- value `Parser.sepBy` comma
|
elements <- value `Parser.sepBy` comma
|
||||||
|
|
||||||
_ <- Parser.char ']'
|
_ <- Parser.char ']'
|
||||||
pure $ Array.listArray (0, fromIntegral $ length elements - 1) elements
|
whitespace
|
||||||
|
pure $ case elements of
|
||||||
|
[] -> Array.listArray (1, 0) elements
|
||||||
|
_ -> Array.listArray (0, fromIntegral $ length elements - 1) elements
|
||||||
|
|
||||||
|
|
|
@ -88,10 +88,10 @@ times n body = go n
|
||||||
|
|
||||||
sepBy :: Parser a -> Parser b -> Parser [a]
|
sepBy :: Parser a -> Parser b -> Parser [a]
|
||||||
sepBy p sep = do
|
sepBy p sep = do
|
||||||
first <- p
|
first <- optional p
|
||||||
rest <- sep *> sepBy p sep <|> pure []
|
rest <- sep *> sepBy p sep <|> pure []
|
||||||
|
|
||||||
pure $ first : rest
|
pure $ maybe id (:) first rest
|
||||||
|
|
||||||
eof :: Parser Bool
|
eof :: Parser Bool
|
||||||
eof = anyChar *> empty <|> pure True
|
eof = anyChar *> empty <|> pure True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue