Source

lambda-translator / parser.ml

open Lexer
open ParserUtil
open Term

type 'a parser = ('a, token list) ParserUtil.parser
exception Error of ParserUtil.error_msg

let ident : var parser = function
  | Ident s :: ts -> Inl (s, ts)
  | _  -> Inr "ident"

let token t : token parser = function
  | t' :: ts when t = t' -> Inl (t, ts)
  | _ -> Inr "token"

let rec term1 () : term parser =
    (ident >>= fun s -> return (Var s))
	<|> (token Lambda >> ident >>= fun s -> token Dot >> term >>= fun t -> return (Abs (s, t)))
	<|> (token Open >> term >>= fun t -> token Close >> return t)
and term_app t1 : term parser =
    (term1() >>= fun t -> term_app (App (t1, t)))
	<|> return t1
and term = fun code -> (term1() >>= fun t1 -> term_app t1) code

let parse ts =
  match term ts with
  | Inl (t, []) -> t
  | Inl (t, _) -> t
  | Inr err -> failwith err