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

top level '|' #1128

Merged
merged 5 commits into from
Jan 31, 2025
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
22 changes: 22 additions & 0 deletions src/Sound/Tidal/ParseBP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ pSequence f = do
try $ symbol ".."
b <- pPart f
return $ TPat_EnumFromTo a b
<|> try
( do
lookAhead
( char '|'
<|> do
pElongate a <|> pRepeat a
char '|'
)
pChoice f a
)
<|> pElongate a
<|> pRepeat a
<|> return a
Expand All @@ -425,6 +435,18 @@ pSequence f = do
takeFoot (TPat_Foot : pats'') = ([], pats'')
takeFoot (pat : pats'') = first (pat :) $ takeFoot pats''

pChoice :: (Parseable a) => MyParser (TPat a) -> TPat a -> MyParser (TPat a)
pChoice f a = do
eor <- option (TPat_Seq []) (pElongate a <|> pRepeat a)
cs <- many1 $
do
char '|'
b <- pPart f
pElongate b <|> pRepeat b <|> return b
seed <- newSeed
rest <- option (TPat_Seq []) (pSequence f)
return $ TPat_Seq [TPat_CycleChoose seed (eor : (a : cs)), rest]

pRepeat :: TPat a -> MyParser (TPat a)
pRepeat a = do
es <- many1 $ do
Expand Down
9 changes: 8 additions & 1 deletion test/Sound/Tidal/ExceptionsTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ action `shouldThrow` p = prop "shouldThrow" $
Left e ->
-- "threw exception that did not meet expectation")
Test.Microspec.assert $ p e
where

shouldNotThrow :: (Exception e) => IO a -> Selector e -> Microspec ()
action `shouldNotThrow` p = prop "shouldNotThrow" $
monadicIO $ do
r <- Test.Microspec.run $ try action
case r of
Right _ -> Test.Microspec.assert True
Left e -> Test.Microspec.assert $ p e

-- a string repsentation of the expected exception's type
{-
Expand Down
16 changes: 15 additions & 1 deletion test/Sound/Tidal/ParseTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Sound.Tidal.ParseTest where

import Control.Exception
import Sound.Tidal.Core
import Sound.Tidal.ExceptionsTest (anyException, shouldThrow)
import Sound.Tidal.ExceptionsTest (anyException, shouldThrow, shouldNotThrow)
import Sound.Tidal.Pattern
import Sound.Tidal.UI (_degradeBy)
import Test.Microspec
Expand Down Expand Up @@ -336,5 +336,19 @@ run =
(Arc 0 1)
("<-- 2 -- - 8>" :: Pattern String)
("<~~ 2 ~~ ~ 8>" :: Pattern String)
it "toplevel '|' is the same as in list" $ do
compareP
(Arc 0 1)
("[a a|b b b|c c c c]" :: Pattern String)
("a a |b b b|c c c c" :: Pattern String)
it "'|' in list first" $ do
evaluate ("1 2@3|4|-|5!6|[7!8 9] 10 . 11 12*2 13!2 . 1@1" :: Pattern String)
`shouldNotThrow` anyException
it "'|' in list last" $ do
evaluate ("12@1|23@1|12|[1 3!21]" :: Pattern String)
`shouldNotThrow` anyException
it "toplevel '|'" $ do
evaluate ("121|23@1|12|[1 321]" :: Pattern String)
`shouldNotThrow` anyException
where
degradeByDefault = _degradeBy 0.5
Loading