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 *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-11-09 03:03:37 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $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
|
|
|
|
|
1999-09-08 10:42:36 -07:00
|
|
|
(* Important: see parser.mly for the difference between "mk" and "gh". *)
|
|
|
|
|
|
|
|
let mktyp d = { ptyp_desc = d; ptyp_loc = symbol_rloc() }
|
|
|
|
let mkpat d = { ppat_desc = d; ppat_loc = symbol_rloc() }
|
|
|
|
let mkexp d = { pexp_desc = d; pexp_loc = symbol_rloc() }
|
|
|
|
|
|
|
|
let ghtyp d = { ptyp_desc = d; ptyp_loc = symbol_gloc() }
|
|
|
|
let ghpat d = { ppat_desc = d; ppat_loc = symbol_gloc() }
|
|
|
|
let ghexp d = { pexp_desc = d; pexp_loc = symbol_gloc() }
|
1995-11-09 03:03:37 -08:00
|
|
|
|
|
|
|
let spat = Ppat_var "%strm"
|
|
|
|
let sexp = Pexp_ident (Lident "%strm")
|
1999-09-08 10:42:36 -07:00
|
|
|
let econ c x = ghexp (Pexp_construct (Ldot (Lident "Stream", c), x, false))
|
|
|
|
let pcon c x = ghpat (Ppat_construct (Ldot (Lident "Stream", c), x, false))
|
1995-11-09 03:03:37 -08:00
|
|
|
let afun f x =
|
1999-11-30 08:07:38 -08:00
|
|
|
ghexp (Pexp_apply (ghexp (Pexp_ident (Ldot (Lident "Stream", f))),
|
2000-01-07 08:35:15 -08:00
|
|
|
List.map (fun a -> "", a) x))
|
1995-11-09 03:03:37 -08:00
|
|
|
let araise c x =
|
1999-11-30 08:07:38 -08:00
|
|
|
ghexp (Pexp_apply (ghexp (Pexp_ident (Lident "raise")), ["", econ c x]))
|
1999-09-08 10:42:36 -07:00
|
|
|
let esome x = ghexp (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) ->
|
1999-09-08 10:42:36 -07:00
|
|
|
(afun "peek" [ghexp sexp],
|
|
|
|
p, ghexp (Pexp_sequence (afun "junk" [ghexp sexp], skont)))
|
1995-11-10 07:14:14 -08:00
|
|
|
| Spat_term (p, Some e) ->
|
1999-09-08 10:42:36 -07:00
|
|
|
(afun "peek" [ghexp sexp],
|
1995-11-10 07:14:14 -08:00
|
|
|
p,
|
1999-09-08 10:42:36 -07:00
|
|
|
ghexp
|
1995-11-10 07:14:14 -08:00
|
|
|
(Pexp_when
|
1999-09-08 10:42:36 -07:00
|
|
|
(e, ghexp(Pexp_sequence (afun "junk" [ghexp sexp], skont)))))
|
1995-11-09 03:03:37 -08:00
|
|
|
| Spat_nterm (p, e) ->
|
1999-09-08 10:42:36 -07:00
|
|
|
(ghexp
|
1995-11-09 03:03:37 -08:00
|
|
|
(Pexp_try
|
1999-11-30 08:07:38 -08:00
|
|
|
(esome (ghexp (Pexp_apply (e, ["", ghexp sexp]))),
|
1997-06-16 08:33:44 -07:00
|
|
|
[(pcon "Failure" None,
|
1999-09-08 10:42:36 -07:00
|
|
|
ghexp (Pexp_construct (Lident "None", None, false)))])),
|
1995-11-09 03:03:37 -08:00
|
|
|
p, skont)
|
|
|
|
| Spat_sterm p ->
|
1999-09-08 10:42:36 -07:00
|
|
|
(esome (ghexp sexp), p, skont)
|
1995-11-09 03:03:37 -08:00
|
|
|
|
1997-06-18 08:02:04 -07:00
|
|
|
(* error continuation for 2nd to last component of a stream pattern *)
|
|
|
|
let ekont1 = function
|
|
|
|
| Some _ as estr -> araise "Error" estr
|
1999-09-08 10:42:36 -07:00
|
|
|
| None -> araise "Error" (Some (ghexp (Pexp_constant (Const_string ""))))
|
1997-06-18 08:02:04 -07:00
|
|
|
;;
|
|
|
|
|
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 ->
|
1999-09-08 10:42:36 -07:00
|
|
|
let countexpr = afun "count" [ghexp sexp] in
|
|
|
|
ghexp (Pexp_match (countexpr, [(ep, e)]))
|
1995-11-09 03:03:37 -08:00
|
|
|
| _ -> e
|
|
|
|
end
|
|
|
|
| (spc, err) :: spcl ->
|
1997-06-18 08:02:04 -07:00
|
|
|
(* success continuation *)
|
|
|
|
let skont = stream_pattern epo e ekont1 spcl in
|
1995-11-13 05:28:09 -08:00
|
|
|
let (tst, p, e) = stream_pattern_component skont spc in
|
|
|
|
let ckont = ekont err in
|
1999-09-08 10:42:36 -07:00
|
|
|
ghexp
|
1995-11-09 03:03:37 -08:00
|
|
|
(Pexp_match
|
|
|
|
(tst,
|
1999-09-08 10:42:36 -07:00
|
|
|
[(ghpat (Ppat_construct (Lident "Some", Some p, false)), e);
|
|
|
|
(ghpat Ppat_any, ckont)]))
|
1995-11-09 03:03:37 -08:00
|
|
|
|
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
|
2000-01-07 08:35:15 -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
|
1999-09-08 10:42:36 -07:00
|
|
|
Some bp -> ghexp (Pexp_match (afun "count" [ghexp sexp], [(bp, pc)]))
|
1995-11-09 03:03:37 -08:00
|
|
|
| None -> pc
|
|
|
|
in
|
1999-07-09 02:22:17 -07:00
|
|
|
let p =
|
|
|
|
let t =
|
1999-09-08 10:42:36 -07:00
|
|
|
ghtyp (Ptyp_constr (Ldot (Lident "Stream", "t"), [ghtyp Ptyp_any]))
|
1999-07-09 02:22:17 -07:00
|
|
|
in
|
1999-09-08 10:42:36 -07:00
|
|
|
ghpat (Ppat_constraint (ghpat spat, t))
|
1999-07-09 02:22:17 -07:00
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
mkexp (Pexp_function ("", None, [p, e]))
|
1995-11-09 03:03:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
(* streams *)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let clazy e = ghexp (Pexp_function ("", None, [ghpat Ppat_any, e]))
|
1995-11-09 03:03:37 -08:00
|
|
|
|
1999-09-14 10:20:03 -07:00
|
|
|
let rec cstream_aux =
|
2000-01-07 08:35:15 -08:00
|
|
|
function
|
1999-09-14 10:20:03 -07:00
|
|
|
| [] -> ghexp (Pexp_ident (Ldot (Lident "Stream", "sempty")))
|
|
|
|
| Sexp_term e :: secl -> afun "lcons" [clazy e; cstream_aux secl]
|
|
|
|
| Sexp_nterm e :: secl -> afun "lapp" [clazy e; cstream_aux secl]
|
|
|
|
;;
|
|
|
|
|
|
|
|
let cstream l =
|
|
|
|
match cstream_aux l with
|
|
|
|
| {pexp_desc = d; pexp_loc = l}
|
|
|
|
-> {pexp_desc = d; pexp_loc = {l with loc_ghost = false}}
|
|
|
|
;;
|