Skip to content

Commit f0fcdf0

Browse files
Implement boundRepeat as a loop (can't figure out the way to make it work as a CE, using ParserMonad + apply1 directly instead)
fix a bug in `textEvent`
1 parent 329cf5c commit f0fcdf0

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/ZMidi/Internal/ParserMonad.fs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,25 @@ module ParserMonad =
285285
/// Run a parser within a bounded section of the input stream.
286286
let inline boundRepeat (n: ^T) (p: ParserMonad<'a>) : ParserMonad<'a array> =
287287

288-
289-
let rec loop i (data: 'a array) =
290-
parseMidi {
291-
if i < n then
292-
let! r = p
293-
data.[int i] <- r
294-
return! (loop (i + LanguagePrimitives.GenericOne) data)
295-
}
296-
parseMidi {
297-
let data = Array.zeroCreate (int n) // can't use array expression inside a CE (at least as is)
298-
do! loop LanguagePrimitives.GenericZero data
299-
return data
300-
}
288+
ParserMonad(fun data state ->
289+
let result = Array.zeroCreate (int n)
290+
let mutable lastState = state
291+
let errors = ResizeArray()
292+
for i in LanguagePrimitives.GenericZero .. n do
293+
match apply1 p data lastState with
294+
| Ok (item,state) ->
295+
lastState <- state
296+
result.[int i] <- item
297+
| Error e ->
298+
errors.Add (i, e)
299+
300+
if Seq.isEmpty errors then
301+
let message =
302+
errors |> Seq.map (sprintf "%A") |> String.concat System.Environment.NewLine
303+
Error (ParseError(lastState.Position , ErrMsg.Other(message)))
304+
else
305+
Ok (result, lastState)
306+
)
301307

302308
/// Apply the parser for /count/ times, derive the final answer
303309
/// from the intermediate list with the supplied function.

src/ZMidi/Read.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ module ReadFile =
9393

9494
let textEvent textType =
9595
parseMidi {
96-
let! a = assertWord8 2uy
97-
let! b = peek
9896
let! text = getVarlenText
9997
return TextEvent(textType, text)
10098
}

0 commit comments

Comments
 (0)