Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for zero and single tuple #93

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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