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

fixed: Parser errors for new rest symbol #1091 #1113

Merged
merged 5 commits into from
Jan 24, 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
27 changes: 19 additions & 8 deletions src/Sound/Tidal/ParseBP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,21 @@ parseBP_E s = toE parsed
toE (Right tp) = toPat tp

parseTPat :: Parseable a => String -> Either ParseError (TPat a)
parseTPat = runParser (pSequence f' Prelude.<* eof) (0 :: Int) ""
where f' = do tPatParser
<|> do oneOf "~-" <?> "rest"
return TPat_Silence
parseTPat = runParser (pSequence parseRest Prelude.<* eof) (0 :: Int) ""

-- | a '-' is a negative sign if followed anything but another dash
-- otherwise, it's treated as rest
parseRest :: Parseable a => MyParser (TPat a)
parseRest =
try (do
lookAhead $ do
char '-'
spaces
noneOf "-"
tPatParser)
<|> char '-' Prelude.*> pure TPat_Silence
<|> tPatParser
<|> char '~' Prelude.*> pure TPat_Silence

cP :: (Enumerable a, Parseable a) => String -> Pattern a
cP s = innerJoin $ parseBP_E <$> _cX_ getS s
Expand Down Expand Up @@ -335,10 +346,10 @@ lexer :: P.GenTokenParser String u Data.Functor.Identity.Identity
lexer = P.makeTokenParser haskellDef

braces, brackets, parens, angles:: MyParser a -> MyParser a
braces = P.braces lexer
brackets = P.brackets lexer
parens = P.parens lexer
angles = P.angles lexer
braces p = char '{' Prelude.*> p Prelude.<* char '}'
brackets p = char '[' Prelude.*> p Prelude.<* char ']'
parens p = char '(' Prelude.*> p Prelude.<* char ')'
angles p = char '<' Prelude.*> p Prelude.<* char '>'

symbol :: String -> MyParser String
symbol = P.symbol lexer
Expand Down
24 changes: 24 additions & 0 deletions test/Sound/Tidal/ParseTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,28 @@ run =
compareP (Arc 0 1)
("t*2t t" :: Pattern Bool)
("1*2%3 1" :: Pattern Bool)
it "does the same for '-' and '~' in simple patterns" $ do
compareP (Arc 0 1)
("- 2" :: Pattern String)
("~ 2" :: Pattern String)
it "does the same for '-' and '~' in complex patterns parsed as Rational" $ do
compareP (Arc 0 1)
("[-- 2 <-- 2@7 3> 4%2 3? 4 9|8 -- [-- <2 9q> -]] 2!4" :: Pattern Rational)
("[~~ 2 <~~ 2@7 3> 4%2 3? 4 9|8 ~~ [~~ <2 9q> ~]] 2!4" :: Pattern Rational)
it "does the same for '-' and '~' in complex patterns" $ do
compareP (Arc 0 1)
("[-- 2 <-- 2@7 3> 1*4%2 3? 4 9|8 -- [-- <2 9q> -]] 2!4" :: Pattern String)
("[~~ 2 <~~ 2@7 3> 1*4%2 3? 4 9|8 ~~ [~~ <2 9q> ~]] 2!4" :: Pattern String)
it "does the same for '-' and '~' using rational numbers" $ do
compareP (Arc 0 1)
("- 2q -3.999-9" :: Pattern String)
("~ 2q -3.999-9" :: Pattern String)
it "does the same for '-' and '~' in list patterns" $ do
compareP (Arc 0 1)
("[-- 2 -- -]" :: Pattern String)
("[~~ 2 ~~ ~]" :: Pattern String)
it "does the same for '-' and '~' alternating patterns" $ do
compareP (Arc 0 1)
("<-- 2 -- - 8>" :: Pattern String)
("<~~ 2 ~~ ~ 8>" :: Pattern String)
where degradeByDefault = _degradeBy 0.5
Loading