@@ -112,7 +112,7 @@ module ParserMonad =
112
112
match apply1 parser input st with
113
113
| Ok result -> Ok result
114
114
| Error _ -> Error ( ParseError( st.Position, Other ( genMessage st.Position)))
115
-
115
+
116
116
let fatalError err =
117
117
ParserMonad <| fun _ st -> Error ( ParseError( st.Position, err))
118
118
@@ -159,27 +159,23 @@ module ParserMonad =
159
159
160
160
/// Repeats a given <see paramref="parser"/> <see paramref="length"/> times.
161
161
/// Fails with accumulated errors when any encountered.
162
- let count ( length : int ) ( parser : ParserMonad < 'a >) : ParserMonad < 'a []> =
162
+ let inline count ( length : ^T ) ( parser : ParserMonad < 'a >) : ParserMonad < 'a []> =
163
163
ParserMonad <| fun input state ->
164
- let rec work ( i : int )
164
+ let rec work ( i : 'T )
165
165
( st : State )
166
166
( fk : ParseError -> Result < 'a list * State , ParseError >)
167
167
( sk : State -> 'a list -> Result < 'a list * State , ParseError >) =
168
- if i <= 0 then
168
+ if i <= LanguagePrimitives.GenericZero then
169
169
sk st []
170
170
else
171
171
match apply1 parser input st with
172
172
| Error msg -> fk msg
173
173
| Ok ( a1, st1) ->
174
- work ( i-1 ) st1 fk ( fun st2 ac ->
174
+ work ( i - LanguagePrimitives.GenericOne ) st1 fk ( fun st2 ac ->
175
175
sk st2 ( a1 :: ac))
176
176
work length state ( fun msg -> Error msg) ( fun st ac -> Ok ( ac, st))
177
177
|> Result.map ( fun ( ans , st ) -> ( List.toArray ans, st))
178
178
179
- /// Apply the parser for /count/ times, derive the final answer
180
- /// from the intermediate list with the supplied function.
181
- let inline gencount ( plen : ParserMonad < 'T >) ( p : ParserMonad < 'a >) ( constr : ^T -> 'a array -> 'answer ) : ParserMonad < 'answer > =
182
- failwith " gencount: not implemented"
183
179
184
180
/// Run a parser within a bounded section of the input stream.
185
181
let inline boundRepeat ( n : ^T ) ( p : ParserMonad < 'a >) : ParserMonad < 'a array > =
@@ -190,6 +186,14 @@ module ParserMonad =
190
186
l.[ i] <- r
191
187
return l
192
188
}
189
+ /// Apply the parser for /count/ times, derive the final answer
190
+ /// from the intermediate list with the supplied function.
191
+ let inline gencount plen p constr = //(plen: ParserMonad<'T>) (p: ParserMonad<'a>) (constr: ^T -> 'a array -> 'answer) : ParserMonad<'answer> =
192
+ parseMidi {
193
+ let! l = plen
194
+ let! items = boundRepeat l p
195
+ return constr l items
196
+ }
193
197
194
198
/// Drop a byte (word8).
195
199
let dropByte : ParserMonad < unit > =
0 commit comments