@@ -4,7 +4,8 @@ namespace ZMidi.Internal
4
4
module ParserMonad =
5
5
6
6
open System.IO
7
-
7
+ open FSharpPlus
8
+ open FSharpPlus.Data
8
9
open ZMidi.Internal .Utils
9
10
10
11
/// Status is either OFF or the previous VoiceEvent * Channel.
@@ -87,8 +88,7 @@ module ParserMonad =
87
88
#endif
88
89
)
89
90
90
- type ParserMonad < 'a > =
91
- ParserMonad of ( State -> Result < 'a * State , ParseError > )
91
+ type ParserMonad < 'a > = StateT< State, Result< 'a * State, ParseError>>
92
92
93
93
let nullOut = new StreamWriter( Stream.Null) :> TextWriter
94
94
let mutable debug = false
@@ -101,7 +101,7 @@ module ParserMonad =
101
101
102
102
let inline private apply1 ( parser : ParserMonad < 'a >)
103
103
( state : State ) : Result < 'a * State , ParseError > =
104
- let ( ParserMonad fn) = parser
104
+ let ( StateT fn) = parser
105
105
try
106
106
let result = fn state
107
107
let oldState = state
@@ -131,20 +131,20 @@ module ParserMonad =
131
131
)
132
132
133
133
let inline mreturn ( x : 'a ) : ParserMonad < 'a > =
134
- ParserMonad <| fun st -> Ok ( x, st)
134
+ StateT <| fun st -> Ok ( x, st)
135
135
136
136
let inline private bindM ( parser : ParserMonad < 'a >)
137
137
( next : 'a -> ParserMonad < 'b >) : ParserMonad < 'b > =
138
- ParserMonad <| fun state ->
138
+ StateT <| fun state ->
139
139
match apply1 parser state with
140
140
| Error msg -> Error msg
141
141
| Ok ( ans, st1) -> apply1 ( next ans) st1
142
142
143
143
let mzero () : ParserMonad < 'a > =
144
- ParserMonad <| fun state -> Error ( mkParseError state ( EOF " mzero" ))
144
+ StateT <| fun state -> Error ( mkParseError state ( EOF " mzero" ))
145
145
146
146
let inline mplus ( parser1 : ParserMonad < 'a >) ( parser2 : ParserMonad < 'a >) : ParserMonad < 'a > =
147
- ParserMonad <| fun state ->
147
+ StateT <| fun state ->
148
148
match apply1 parser1 state with
149
149
| Error _ -> apply1 parser2 state
150
150
| Ok res -> Ok res
@@ -158,30 +158,7 @@ module ParserMonad =
158
158
let (>>= ) ( m: ParserMonad< 'a>) ( k: 'a -> ParserMonad< 'b>) : ParserMonad< 'b> =
159
159
bindM m k
160
160
161
- type ParserBuilder () =
162
- member inline self.ReturnFrom ( ma : ParserMonad < 'a >) : ParserMonad < 'a > = ma
163
- member inline self.Return x = mreturn x
164
- member inline self.Bind ( p , f ) = bindM p f
165
- member inline self.Zero a = ParserMonad ( fun state -> Ok( a, state))
166
- //member self.Combine (ma, mb) = ma >>= mb
167
-
168
- // inspired from http://www.fssnip.net/7UJ/title/ResultBuilder-Computational-Expression
169
- // probably broken
170
- member inline self.TryFinally ( m , compensation ) =
171
- try self.ReturnFrom( m)
172
- finally compensation()
173
-
174
- //member self.Delay(f: unit -> ParserMonad<'a>) : ParserMonad<'a> = f ()
175
- //member self.Using(res:#System.IDisposable, body) =
176
- // self.TryFinally(body res, fun () -> if not (isNull res) then res.Dispose())
177
- //member self.While(guard, f) =
178
- // if not (guard()) then self.Zero () else
179
- // do f() |> ignore
180
- // self.While(guard, f)
181
- //member self.For(sequence:seq<_>, body) =
182
- // self.Using(sequence.GetEnumerator(), fun enum -> self.While(enum.MoveNext, fun () -> self.Delay(fun () -> body enum.Current)))
183
-
184
- let ( parseMidi : ParserBuilder ) = new ParserBuilder()
161
+ let parseMidi = monad
185
162
186
163
let runParser ( ma : ParserMonad < 'a >) input initialState =
187
164
apply1 ma { initialState with Input = input}
@@ -194,10 +171,10 @@ module ParserMonad =
194
171
195
172
/// Throw a parse error
196
173
let parseError ( genMessage : Pos -> string ) : ParserMonad < 'a > =
197
- ParserMonad <| fun st -> Error ( mkOtherParseError st genMessage)
174
+ StateT <| fun st -> Error ( mkOtherParseError st genMessage)
198
175
199
176
let fmapM ( modify : 'a -> 'b ) ( parser : ParserMonad < 'a >) : ParserMonad < 'b > =
200
- ParserMonad <| fun state ->
177
+ StateT <| fun state ->
201
178
match apply1 parser state with
202
179
| Error err -> Error err
203
180
| Ok ( a, st2) -> Ok ( modify a, st2)
@@ -208,7 +185,7 @@ module ParserMonad =
208
185
209
186
/// Run the parser, if it fails swap the error message.
210
187
let inline ( <??> ) ( parser : ParserMonad < 'a >) ( genMessage : Pos -> string ) : ParserMonad < 'a > =
211
- ParserMonad <| fun st ->
188
+ StateT <| fun st ->
212
189
match apply1 parser st with
213
190
| Ok result -> Ok result
214
191
| Error e ->
@@ -250,16 +227,16 @@ module ParserMonad =
250
227
251
228
252
229
let fatalError err =
253
- ParserMonad <| fun st -> Error ( mkParseError st err)
230
+ StateT <| fun st -> Error ( mkParseError st err)
254
231
255
232
let getRunningEvent : ParserMonad < VoiceEvent > =
256
- ParserMonad <| fun st -> Ok ( st.RunningStatus , st)
233
+ StateT <| fun st -> Ok ( st.RunningStatus , st)
257
234
258
235
let inline setRunningEvent ( runningStatus : VoiceEvent ) : ParserMonad < unit > =
259
- ParserMonad <| fun st -> Ok ((), { st with RunningStatus = runningStatus })
236
+ StateT <| fun st -> Ok ((), { st with RunningStatus = runningStatus })
260
237
261
238
let getPos : ParserMonad < int > =
262
- ParserMonad <| fun st -> Ok ( st.Position, st)
239
+ StateT <| fun st -> Ok ( st.Position, st)
263
240
264
241
let inline private (| PositionValid | PositionInvalid |) ( input : MidiData , state : State ) =
265
242
if state.Position >= 0 && state.Position < input.Length then
@@ -268,7 +245,7 @@ module ParserMonad =
268
245
PositionInvalid
269
246
270
247
let inline private checkedParseM ( name : string ) ( f : State -> Result <( 'a * State ), ParseError >) =
271
- ParserMonad
248
+ StateT
272
249
( fun state ->
273
250
try
274
251
match state.Input, state with
@@ -296,7 +273,7 @@ module ParserMonad =
296
273
/// Repeats a given <see paramref="parser"/> <see paramref="length"/> times.
297
274
/// Fails with accumulated errors when any encountered.
298
275
let inline count ( length : ^T ) ( parser : ParserMonad < 'a >) : ParserMonad < 'a []> =
299
- ParserMonad <| fun state ->
276
+ StateT <| fun state ->
300
277
let rec work ( i : 'T )
301
278
( st : State )
302
279
( fk : ParseError -> Result < 'a list * State , ParseError >)
@@ -314,7 +291,7 @@ module ParserMonad =
314
291
315
292
/// Run a parser within a bounded section of the input stream.
316
293
let repeatTillPosition ( maxPosition : Pos ) ( parser : ParserMonad < 'a >) : ParserMonad < 'a array > =
317
- ParserMonad <| fun state ->
294
+ StateT <| fun state ->
318
295
let limit = maxPosition
319
296
let rec work ( st : State )
320
297
( fk : ParseError -> Result < 'a list * State , ParseError >)
@@ -402,4 +379,4 @@ module ParserMonad =
402
379
let! b = readByte
403
380
let! c = readByte
404
381
return ( word24be a b c)
405
- }
382
+ }
0 commit comments