Skip to content

Instantly share code, notes, and snippets.

@klapaucius
Created November 29, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klapaucius/7709883 to your computer and use it in GitHub Desktop.
Save klapaucius/7709883 to your computer and use it in GitHub Desktop.
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