Created
June 14, 2009 23:45
-
-
Save mzp/129868 to your computer and use it in GitHub Desktop.
parserMonad for pa_monad.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* ocamlc -pp 'camlp4orf pa_monad.cmo' parserMonad.ml *) | |
type ('a,'b) either = Left of 'a | Right of 'b | |
module Make | |
(T : sig | |
type ts | |
type error | |
end) : sig | |
type ts = T.ts | |
type error = T.error | |
type 'a m = ts -> ('a * ts, error) either | |
val return : 'a -> 'a m | |
val bind : 'a m -> ('a -> 'b m) -> 'b m | |
val (<|>) : 'a m -> 'a m -> 'a m | |
val many : 'a m -> 'a list m | |
val opt : 'a m -> 'a option m | |
end = | |
struct | |
type ts = T.ts | |
type error = T.error | |
type 'a m = ts -> ('a * ts, error) either | |
let return x = fun code -> Left (x, code) | |
let bind p f = fun code -> | |
match p code with | |
| Left (x, ts) -> f x ts | |
| Right err -> Right err | |
let (<|>) p1 p2 = fun code -> | |
match p1 code, p2 code with | |
| (Left _ as x) , _ -> x | |
| _ , (Left _ as x) -> x | |
| Right _ , Right err -> Right err | |
let rec many p = | |
perform | |
x <-- p; | |
xs <-- many p; | |
return (x::xs) | |
<|> return [] | |
let opt p = | |
perform | |
x <-- p; | |
return (Some x) | |
<|> return None | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment