Skip to content

Commit 9b763fc

Browse files
Improve parsing of octave indicators
i.e. high octave with the apostrophe, low with the comma.
1 parent 0d5b6db commit 9b763fc

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

src/Data/Abc/Parser.purs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module Data.Abc.Parser
33
( parse
44
, parseKeySignature
5+
, module StringParser
56
) where
67

78
import Data.Abc
@@ -21,13 +22,14 @@ import Data.Maybe (Maybe(..), fromMaybe)
2122
import Data.Rational (Rational, fromInt, (%))
2223
import Data.String (drop, toUpper)
2324
import Data.String.CodePoints (length)
24-
import Data.String.CodeUnits (charAt, fromCharArray, toCharArray)
25+
import Data.String.CodeUnits (charAt, fromCharArray, toCharArray, singleton)
2526
import Data.String.Utils (startsWith, includes)
2627
import Data.Tuple (Tuple(..))
2728
import Data.Unfoldable1 (replicate1A)
28-
import Prelude (bind, flip, join, max, pure, ($), (*>), (+), (-), (<$), (<$>), (<*), (<*>), (<<<), (<>), (==))
29+
import Prelude (bind, flip, join, max, negate, pure, ($), (*>), (+), (-), (<$), (<$>), (<*), (<*>), (<<<), (<>), (==), (||), (&&), (>=), (<=))
2930
import StringParser (Parser, ParseError, runParser, try)
30-
import StringParser.CodePoints (satisfy, string, alphaNum, char, eof, regex)
31+
import StringParser (ParseError()) as StringParser
32+
import StringParser.CodePoints (anyDigit, satisfy, string, alphaNum, char, eof, regex)
3133
import StringParser.Combinators (between, choice, many, many1, manyTill, option, optional, optionMaybe, sepBy, sepBy1, (<?>))
3234

3335
{- transient data type just used for parsing the awkward Tempo syntax
@@ -357,20 +359,39 @@ pitch :: Parser String
357359
pitch =
358360
regex "[A-Ga-g]"
359361

362+
{-
360363
moveOctave :: Parser Int
361364
moveOctave =
362365
octaveShift <$> regex "[',]*"
366+
-}
363367

364368
{- count the number of apostrophe (up) or comma (down) characters in the string
365369
and give the result a value of (up-down)
366370
-}
371+
{-}
367372
octaveShift :: String -> Int
368373
octaveShift s =
369374
let
370375
up = Array.length $ Array.filter ((==) '\'') (toCharArray s)
371376
down = Array.length $ Array.filter ((==) ',') (toCharArray s)
372377
in
373378
up - down
379+
-}
380+
381+
moveOctave :: Parser Int
382+
moveOctave =
383+
option 0 $ highOctave <|> lowOctave
384+
385+
where
386+
highOctave :: Parser Int
387+
highOctave =
388+
Nel.length <$>
389+
many1 (char ''')
390+
391+
lowOctave :: Parser Int
392+
lowOctave =
393+
negate <$> Nel.length <$>
394+
many1 (char ',')
374395

375396
{- the duration of a note in the body
376397
order of choices here is important to remove ambiguity
@@ -595,7 +616,7 @@ longDecoration =
595616
<?> "long decoration"
596617

597618
-- | our whiteSpace differs from that of the string parser we do NOT want to
598-
-- |consume carriage returns or newlines
619+
-- | consume carriage returns or newlines
599620
whiteSpace :: Parser String
600621
whiteSpace =
601622
(fromCharArray <<< Array.fromFoldable)
@@ -629,7 +650,7 @@ scoreSpace =
629650
-- tab <|> space
630651
(char '\t') <|> space
631652

632-
space :: Parser Char --
653+
space :: Parser Char
633654
space = char ' '
634655

635656
{- characters to ignore
@@ -1061,7 +1082,7 @@ suffixedTempoDesignation =
10611082
<$> tempoDesignation
10621083
<*> spacedQuotedString
10631084

1064-
{-\ we have for example 120 -}
1085+
{- we have for example 120 -}
10651086
degenerateTempo :: Parser TempoSignature
10661087
degenerateTempo =
10671088
buildTempoSignature3 <$>
@@ -1303,12 +1324,12 @@ anyInt :: Parser String
13031324
anyInt =
13041325
regex "(0|[1-9][0-9]*)"
13051326

1306-
anyDigit :: Parser String
1307-
anyDigit =
1308-
regex "([0-9])"
1309-
13101327
-- low level
13111328

1329+
anyDigitAsString :: Parser String
1330+
anyDigitAsString =
1331+
singleton <$> anyDigit
1332+
13121333
{- an alphnumeric string with added +,- and _ -}
13131334
alphaNumPlusString :: Parser String
13141335
alphaNumPlusString =
@@ -1370,7 +1391,7 @@ digit =
13701391
fromMaybe 1
13711392
<$> fromString
13721393
<$>
1373-
anyDigit
1394+
anyDigitAsString
13741395
<?> "expected a digit"
13751396

13761397
-- | literal quoted String. Optionally retain the quotes surrounding the returned String

0 commit comments

Comments
 (0)