Skip to content

Commit

Permalink
Add support for zero and single tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Apr 15, 2024
1 parent d9c960e commit d4d731d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions data/tuples.settings
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[org/gnome/tuples]
t0=()
t1=(5,)
t1-sp=(5, )
t2=(true, 'woman')
t3=(false, 'man', -2)
t4=('hello', 22, 'world', true)
Expand Down
3 changes: 3 additions & 0 deletions output/tuples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ with lib.hm.gvariant;
"org/gnome/tuples" = {
n1 = mkTuple [ false (mkTuple [ "xkb" "us" ]) "nested" ];
n2 = mkTuple [ "hi" (mkTuple [ true "us" (mkTuple [ (-787) "lvl" ]) ]) "super-nested" ];
t0 = mkTuple [];
t1 = mkTuple [ 5 ];
t1-sp = mkTuple [ 5 ];
t2 = mkTuple [ true "woman" ];
t3 = mkTuple [ false "man" (-2) ];
t4 = mkTuple [ "hello" 22 "world" true ];
Expand Down
22 changes: 16 additions & 6 deletions src/DConf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,22 @@ bracket s1 s2 pa = do
_ <- string s2
return a

comma :: Parsec Text () ()
comma = string "," >> spaces

commaSeparated :: Parsec Text () a -> Parsec Text () [a]
commaSeparated pa = sepBy pa $ string "," >> spaces
commaSeparated pa = sepBy pa comma

-- Like 'sepEndBy' but the separator can only appear at the end when there is just a single item.
sepOneEndBy :: Show a => Parsec Text () a -> Parsec Text () sep -> Parsec Text () [a]
sepOneEndBy p sep =
do
{ x <- p
; _ <- sep
; xs <- sepBy p sep
; return (x:xs)
}
<|> return []

vBool :: Parsec Text () Value
vBool = B False <$ string "false" <|> B True <$ string "true"
Expand Down Expand Up @@ -58,11 +72,7 @@ vInt64 = try $ do
I64 . read <$> many1 digit

vTuple :: Parsec Text () Value
vTuple = do
rs <- bracket "(" ")" $ commaSeparated $ value
case rs of
xs@(_ : _ : _) -> pure $ T xs
_ -> fail "Not a tuple"
vTuple = T <$> (bracket "(" ")" $ sepOneEndBy value comma)

vString :: Parsec Text () Text
vString = T.pack <$> (single <|> double)
Expand Down

0 comments on commit d4d731d

Please sign in to comment.