Created
November 29, 2013 18:21
-
-
Save klapaucius/7709883 to your computer and use it in GitHub Desktop.
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
module Parsers = | |
let inline konst a b = a //const зарезервировано | |
type Parser<'s, 't> = 's -> ('t * 's) option | |
let inline uncons l = match l with x::xs -> Some(x,xs) | [] -> None | |
let inline unit a xs = Some (a, xs) | |
let inline (<*>) pf px inp = | |
match pf inp with | |
| Some (f,inp2) -> (match px inp2 with Some(x,left) -> Some(f x, left) | None -> None) | |
| None -> None | |
let inline fail () = konst None | |
let inline (<|>) p1 p2 inp = match p1 inp with Some _ as r -> r | None -> p2 inp | |
let inline choice ps = List.fold (<|>) (fail ()) ps | |
let inline (<<|>) f p = unit f <*> p | |
let inline (<<| ) f p = konst <<|> unit f <*> p | |
let inline (<* ) p q = konst <<|> p <*> q | |
let inline ( *>) p q = id <<| p <*> q | |
let inline opt p v = p <|> unit v | |
let inline optional p = opt (Some <<|> p) None | |
let inline uncrazy f x = match f x with Some(res,state) -> Some((res,state),state) | None -> None | |
let inline many' v p s = | |
match Seq.toList <| Seq.unfold (uncrazy p) s with | |
| [] -> v | |
| xs -> Some (List.map fst xs, snd <| Seq.last xs) | |
let inline many p s = many' (Some ([], s)) p s | |
let inline some p s = many' None p s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment