Source

lambda-translator / parser.ml

(*
type term =
  | Var of var
  | Abs of var * term
  | App of term * term
*)

open Lexer
open ParserUtil
open Term

type 'a parser = ('a, token list) ParserUtil.parser

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

  
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.