Skip to content

Commit 1aec97f

Browse files
trying to catch few snags in ParserMonad.fs
1 parent 88d11b8 commit 1aec97f

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

demo/MidiCopy.fsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#r "../build/Debug/AnyCPU/net45/zmidi-fs-core.dll"
2+
3+
//let process path =
4+
5+
// readMidi path
6+
// | Error e -> printfn "%A" e
7+
// | Ok result ->
8+
// let outfile = filename + ".001"

src/ZMidi/Internal/ParserMonad.fs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ module ParserMonad =
182182
ParserMonad <| fun input st ->
183183
match apply1 parser input st with
184184
| Ok result -> Ok result
185-
| Error _ -> Error(mkOtherParseError st genMessage)
185+
| Error e ->
186+
logf "oops <??>: e:%A" e
187+
Error(mkOtherParseError st genMessage)
186188

187189
///
188190
let fmap (f: 'a -> 'b) (p: ParserMonad<'a>) : ParserMonad<'b> =
@@ -244,7 +246,7 @@ module ParserMonad =
244246
| PositionValid -> f input state
245247
| PositionInvalid -> Error (mkParseError state (EOF name))
246248
with
247-
| e -> Error (mkParseError state (Other (sprintf "%A" e)))
249+
| e -> Error (mkParseError state (Other (sprintf "%s %A" name e)))
248250
)
249251

250252
let peek : ParserMonad<byte> =
@@ -265,7 +267,7 @@ module ParserMonad =
265267
/// Repeats a given <see paramref="parser"/> <see paramref="length"/> times.
266268
/// Fails with accumulated errors when any encountered.
267269
let inline count (length : ^T) (parser : ParserMonad<'a>) : ParserMonad<'a []> =
268-
ParserMonad <| fun input state ->
270+
ParserMonad <| fun input state ->
269271
let rec work (i : 'T)
270272
(st : State)
271273
(fk : ParseError -> Result<'a list * State, ParseError>)
@@ -284,23 +286,24 @@ module ParserMonad =
284286

285287
/// Run a parser within a bounded section of the input stream.
286288
let inline boundRepeat (n: ^T) (p: ParserMonad<'a>) : ParserMonad<'a array> =
287-
288289
ParserMonad(fun data state ->
289290
let result = Array.zeroCreate (int n)
290291
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)))
292+
// revisit with a fold?
293+
let mutable error = Ok (Unchecked.defaultof<_>,lastState)
294+
let mutable i = LanguagePrimitives.GenericZero
295+
let mutable errorOccured = false
296+
while i < n && not errorOccured do
297+
i <- i + LanguagePrimitives.GenericOne
298+
match apply1 p data lastState with
299+
| Ok (item,state) ->
300+
lastState <- state
301+
result.[int i] <- item
302+
| (Error e) ->
303+
error <- Error e
304+
errorOccured <- true
305+
if errorOccured then
306+
error
304307
else
305308
Ok (result, lastState)
306309
)
@@ -310,6 +313,7 @@ module ParserMonad =
310313
let inline gencount (plen: ParserMonad<'T>) (p: ParserMonad<'a>) (constr: ^T -> 'a array -> 'answer) : ParserMonad<'answer> =
311314
parseMidi {
312315
let! l = plen
316+
printfn "gen count: l: %i" l
313317
let! items = boundRepeat l p
314318
return constr l items
315319
}

0 commit comments

Comments
 (0)