1995-11-09 03:03:37 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-11-09 03:03:37 -08:00
|
|
|
(* *)
|
|
|
|
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-11-09 03:03:37 -08:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
open Asttypes
|
|
|
|
open Parsetree
|
|
|
|
open Longident
|
|
|
|
open Location
|
|
|
|
|
|
|
|
type stream_pattern_component =
|
1995-11-10 07:14:14 -08:00
|
|
|
Spat_term of pattern * expression option
|
1995-11-09 03:03:37 -08:00
|
|
|
| Spat_nterm of pattern * expression
|
|
|
|
| Spat_sterm of pattern
|
|
|
|
type stream_expr_component =
|
|
|
|
Sexp_term of expression
|
|
|
|
| Sexp_nterm of expression
|
|
|
|
|
|
|
|
let mkpat d = { ppat_desc = d; ppat_loc = symbol_loc() }
|
|
|
|
let mkexp d = { pexp_desc = d; pexp_loc = symbol_loc() }
|
|
|
|
let eloc loc e = { pexp_desc = e; pexp_loc = loc }
|
|
|
|
let ploc loc p = { ppat_desc = p; ppat_loc = loc }
|
|
|
|
|
|
|
|
let spat = Ppat_var "%strm"
|
|
|
|
let sexp = Pexp_ident (Lident "%strm")
|
|
|
|
let eval x = mkexp (Pexp_ident (Ldot (Lident "Stream", x)))
|
1997-06-16 11:10:35 -07:00
|
|
|
let econ c x = mkexp (Pexp_construct (Ldot (Lident "Stream", c), x, false))
|
|
|
|
let pcon c x = mkpat (Ppat_construct (Ldot (Lident "Stream", c), x, false))
|
1995-11-09 03:03:37 -08:00
|
|
|
let afun f x =
|
|
|
|
mkexp (Pexp_apply (mkexp (Pexp_ident (Ldot (Lident "Stream", f))), x))
|
|
|
|
let araise c x =
|
|
|
|
mkexp (Pexp_apply (mkexp (Pexp_ident (Lident "raise")), [econ c x]))
|
1997-06-16 11:10:35 -07:00
|
|
|
let esome x = mkexp (Pexp_construct (Lident "Some", Some x, false))
|
1995-11-09 03:03:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
(* parsers *)
|
|
|
|
|
|
|
|
let stream_pattern_component skont =
|
|
|
|
function
|
1995-11-10 07:14:14 -08:00
|
|
|
Spat_term (p, None) ->
|
1995-11-09 03:03:37 -08:00
|
|
|
(afun "peek" [mkexp sexp],
|
|
|
|
p, mkexp (Pexp_sequence (afun "junk" [mkexp sexp], skont)))
|
1995-11-10 07:14:14 -08:00
|
|
|
| Spat_term (p, Some e) ->
|
|
|
|
(afun "peek" [mkexp sexp],
|
|
|
|
p,
|
|
|
|
mkexp
|
|
|
|
(Pexp_when
|
|
|
|
(e, mkexp(Pexp_sequence (afun "junk" [mkexp sexp], skont)))))
|
1995-11-09 03:03:37 -08:00
|
|
|
| Spat_nterm (p, e) ->
|
|
|
|
(mkexp
|
|
|
|
(Pexp_try
|
|
|
|
(esome (mkexp (Pexp_apply (e, [mkexp sexp]))),
|
1997-06-16 08:33:44 -07:00
|
|
|
[(pcon "Failure" None,
|
1997-06-16 11:10:35 -07:00
|
|
|
mkexp (Pexp_construct (Lident "None", None, false)))])),
|
1995-11-09 03:03:37 -08:00
|
|
|
p, skont)
|
|
|
|
| Spat_sterm p ->
|
|
|
|
(esome (mkexp sexp), p, skont)
|
|
|
|
|
1995-11-13 05:28:09 -08:00
|
|
|
let rec stream_pattern epo e ekont =
|
1995-11-09 03:03:37 -08:00
|
|
|
function
|
|
|
|
[] ->
|
|
|
|
begin match epo with
|
|
|
|
Some ep ->
|
|
|
|
mkexp (Pexp_let (Nonrecursive, [(ep, afun "count" [mkexp sexp])], e))
|
|
|
|
| _ -> e
|
|
|
|
end
|
|
|
|
| (spc, err) :: spcl ->
|
1995-11-13 05:28:09 -08:00
|
|
|
let skont =
|
|
|
|
let ekont err =
|
1995-11-09 03:03:37 -08:00
|
|
|
let str =
|
|
|
|
match err with
|
|
|
|
Some estr -> estr
|
|
|
|
| _ -> mkexp (Pexp_constant (Const_string ""))
|
|
|
|
in
|
1997-06-16 08:33:44 -07:00
|
|
|
araise "Error" (Some str)
|
1995-11-13 05:28:09 -08:00
|
|
|
in
|
|
|
|
stream_pattern epo e ekont spcl
|
1995-11-09 03:03:37 -08:00
|
|
|
in
|
1995-11-13 05:28:09 -08:00
|
|
|
let (tst, p, e) = stream_pattern_component skont spc in
|
|
|
|
let ckont = ekont err in
|
1995-11-09 03:03:37 -08:00
|
|
|
mkexp
|
|
|
|
(Pexp_match
|
|
|
|
(tst,
|
1997-06-16 11:10:35 -07:00
|
|
|
[(ploc p.ppat_loc (Ppat_construct (Lident "Some", Some p, false)),
|
|
|
|
e);
|
1995-11-09 03:03:37 -08:00
|
|
|
(mkpat Ppat_any, ckont)]))
|
|
|
|
|
1995-11-13 05:28:09 -08:00
|
|
|
let rec parser_cases =
|
|
|
|
function
|
1997-06-16 08:33:44 -07:00
|
|
|
[] -> araise "Failure" None
|
1995-11-13 05:28:09 -08:00
|
|
|
| (spcl, epo, e)::cl -> stream_pattern epo e (fun _ -> parser_cases cl) spcl
|
1995-11-09 03:03:37 -08:00
|
|
|
|
1995-11-13 05:28:09 -08:00
|
|
|
let cparser (bpo, pc) =
|
1995-11-09 03:03:37 -08:00
|
|
|
let pc = parser_cases pc in
|
|
|
|
let e =
|
|
|
|
match bpo with
|
|
|
|
Some bp ->
|
|
|
|
mkexp (Pexp_let (Nonrecursive, [(bp, afun "count" [mkexp sexp])], pc))
|
|
|
|
| None -> pc
|
|
|
|
in
|
|
|
|
mkexp (Pexp_function [(mkpat spat, e)])
|
|
|
|
|
|
|
|
|
|
|
|
(* streams *)
|
|
|
|
|
|
|
|
let lazy e = mkexp (Pexp_function [(mkpat Ppat_any, e)])
|
|
|
|
|
|
|
|
let rec cstream =
|
|
|
|
function
|
|
|
|
[] -> eval "sempty"
|
|
|
|
| Sexp_term e :: secl -> afun "scons" [lazy e; cstream secl]
|
|
|
|
| Sexp_nterm e :: secl -> afun "sapp" [lazy e; cstream secl]
|