1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
(* camlp4r q_MLast.cmo *)
(***********************************************************************)
(* *)
(* Camlp4 *)
(* *)
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1998 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* $Id: pr_op.ml,v 2.0 1998/12/01 08:20:51 ddr Exp $ *)
open Pretty;
value loc = (0, 0);
value spatt patt p dg k =
match p with
[ <:patt< $lid:s$ >> ->
if String.length s >= 2 && s.[1] == ''' then
HVbox [: `S LR (" " ^ s); k :]
else patt p dg k
| _ -> patt p dg k ]
;
(* Streams *)
value stream expr patt e _ k =
let rec get =
fun
[ <:expr< Stream.iapp $x$ $y$ >> -> [(False, x) :: get y]
| <:expr< Stream.icons $x$ $y$ >> -> [(True, x) :: get y]
| <:expr< Stream.ising $x$ >> -> [(True, x)]
| <:expr< Stream.lapp (fun _ -> $x$) $y$ >> -> [(False, x) :: get y]
| <:expr< Stream.lcons (fun _ -> $x$) $y$ >> -> [(True, x) :: get y]
| <:expr< Stream.lsing (fun _ -> $x$) >> -> [(True, x)]
| <:expr< Stream.sempty >> -> []
| <:expr< Stream.slazy (fun _ -> $x$) >> -> [(False, x)]
| <:expr< Stream.slazy $x$ >> -> [(False, <:expr< $x$ () >>)]
| e -> [(False, e)] ]
in
let elem e dg k =
match e with
[ (True, e) -> [: `HOVbox [: `S LO "'"; `expr e dg k :] :]
| (False, e) -> [: `expr e dg k :] ]
in
let rec glop e k =
match e with
[ [] -> k
| [e] -> [: elem e "" k :]
| [e :: el] -> [: elem e ";" [: `S RO ";" :]; glop el k :] ]
in
HOVbox [: `S LR "[<"; glop (get e) [: `S LR ">]"; k :] :]
;
(* Parsers *)
type spc =
[ SPCterm of (MLast.patt * option MLast.expr)
| SPCnterm of MLast.patt and MLast.expr
| SPCsterm of MLast.patt ]
;
exception NotImpl;
value rec subst v e =
match e with
[ <:expr< $lid:x$ >> -> if x = "strm__" then <:expr< $lid:v$ >> else e
| <:expr< $uid:_$ >> -> e
| <:expr< $int:_$ >> -> e
| <:expr< $chr:_$ >> -> e
| <:expr< $str:_$ >> -> e
| <:expr< $e1$ . $lab$ >> -> <:expr< $subst v e1$ . $lab$ >>
| <:expr< $x$ $y$ >> -> <:expr< $subst v x$ $subst v y$ >>
| <:expr< let $lid:s1$ = $e1$ in $e2$ >> ->
if s1 = v then <:expr< let $lid:s1$ = $subst v e1$ in $e2$ >>
else <:expr< let $lid:s1$ = $subst v e1$ in $subst v e2$ >>
| <:expr< let _ = $e1$ in $e2$ >> ->
<:expr< let _ = $subst v e1$ in $subst v e2$ >>
| <:expr< ($list:el$) >> -> <:expr< ($list:List.map (subst v) el$) >>
| _ -> raise NotImpl ]
;
value rec is_free v =
fun
[ <:expr< $lid:x$ >> -> x <> v
| <:expr< $uid:_$ >> -> True
| <:expr< $int:_$ >> -> True
| <:expr< $chr:_$ >> -> True
| <:expr< $str:_$ >> -> True
| <:expr< $e$ . $_$ >> -> is_free v e
| <:expr< $x$ $y$ >> -> is_free v x && is_free v y
| <:expr< let $lid:s1$ = $e1$ in $e2$ >> ->
is_free v e1 && (s1 = v || is_free v e2)
| <:expr< let _ = $e1$ in $e2$ >> -> is_free v e1 && is_free v e2
| <:expr< ($list:el$) >> -> List.for_all (is_free v) el
| _ -> raise NotImpl ]
;
value free_var_in_expr c e =
let rec loop_alpha v =
let x = String.make 1 v in
if is_free x e then Some x
else if v = 'z' then None
else loop_alpha (Char.chr (Char.code v + 1))
in
let rec loop_count cnt =
let x = String.make 1 c ^ string_of_int cnt in
if is_free x e then x else loop_count (succ cnt)
in
try
match loop_alpha c with
[ Some v -> v
| None -> loop_count 1 ]
with
[ NotImpl -> "\\%a" ]
;
value parserify =
fun
[ <:expr< $e$ strm__ >> -> e
| e -> <:expr< fun strm__ -> $e$ >> ]
;
value is_raise_failure =
fun
[ <:expr< raise Stream.Failure >> -> True
| _ -> False ]
;
value is_raise_error =
fun
[ <:expr< raise (Stream.Error $_$) >> -> True
| _ -> False ]
;
value semantic e =
try
if is_free "strm__" e then e
else
let v = free_var_in_expr 's' e in
<:expr< let $lid:v$ = strm__ in $subst v e$ >>
with
[ NotImpl -> e ]
;
value rewrite_parser =
rewrite True where rec rewrite top ge =
match ge with
[ <:expr< let $p$ = try $e$ with [ Stream.Failure -> raise $exc$ ] in
$sp_kont$ >> ->
let f = parserify e in
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $rewrite False sp_kont$
| _ -> raise $exc$ ]
>>
| <:expr< let $p$ = Stream.count strm__ in $f$ >> ->
try
if is_free "strm__" f then ge
else
let v = free_var_in_expr 's' f in
<:expr<
let $lid:v$ = strm__ in
let $p$ = Stream.count strm__ in $subst v f$
>>
with
[ NotImpl -> ge ]
| <:expr< let $p$ = strm__ in $e$ >> ->
<:expr< let $p$ = strm__ in $rewrite False e$ >>
| <:expr< let $p$ = $f$ strm__ in $sp_kont$ >> when top ->
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $rewrite False sp_kont$
| _ -> raise Stream.Failure ]
>>
| <:expr< let $p$ = $e$ in $sp_kont$ >> ->
if match e with
[ <:expr< match try Some $_$ with [ Stream.Failure -> None ] with
[ $list:_$ ] >>
| <:expr< match Stream.peek strm__ with [ $list:_$ ] >>
| <:expr< try $_$ with [ Stream.Failure -> $_$ ] >>
| <:expr< let $_$ = Stream.count strm__ in $_$ >> -> True
| _ -> False ]
then
let f = rewrite True <:expr< fun strm__ -> $e$ >> in
let exc =
if top then <:expr< Stream.Failure >>
else <:expr< Stream.Error "" >>
in
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $rewrite False sp_kont$
| _ -> raise $exc$ ]
>>
else semantic ge
| <:expr< match try Some $e$ with [ Stream.Failure -> None ] with
[ Some $p$ -> $sp_kont$
| _ -> $p_kont$ ] >> ->
let f = parserify e in
if not top && is_raise_failure p_kont then semantic ge
else
let (p, f, sp_kont, p_kont) =
if top || is_raise_error p_kont then
(p, f, rewrite False sp_kont, rewrite top p_kont)
else
let f =
<:expr<
fun strm__ ->
match
try Some ($f$ strm__) with [ Stream.Failure -> None ]
with
[ Some $p$ -> $rewrite False sp_kont$
| _ -> $rewrite top p_kont$ ]
>>
in
(<:patt< a >>, f, <:expr< a >>,
<:expr< raise (Stream.Error "") >>)
in
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $sp_kont$
| _ -> $p_kont$ ]
>>
| <:expr< match Stream.peek strm__ with [ $list:pel$ ] >> ->
let rec iter pel =
match pel with
[ [(<:patt< Some $p$ >>, eo,
<:expr< do Stream.junk strm__; return $sp_kont$ >>);
(<:patt< _ >>, None, p_kont) :: _] ->
<:expr<
match Stream.peek strm__ with
[ Some $p$ $when:eo$ ->
do Stream.junk strm__; return $rewrite False sp_kont$
| _ -> $rewrite top p_kont$ ]
>>
| [(<:patt< Some $p$ >>, eo,
<:expr< do Stream.junk strm__; return $sp_kont$ >>) :: pel] ->
let p_kont = iter pel in
<:expr<
match Stream.peek strm__ with
[ Some $p$ $when:eo$ ->
do Stream.junk strm__; return $rewrite False sp_kont$
| _ -> $p_kont$ ]
>>
| _ ->
<:expr< match Stream.peek strm__ with [ $list:pel$ ] >> ]
in
iter pel
| <:expr< try Some $e$ with [ Stream.Failure -> $p_kont$ ] >> ->
let f = parserify e in
let e =
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some a -> Some a
| _ -> $p_kont$ ]
>>
in
rewrite top e
| <:expr< try $e$ with [ Stream.Failure -> $p_kont$ ] >> ->
let f = parserify e in
let e =
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some a -> a
| _ -> $rewrite top p_kont$ ]
>>
in
rewrite top e
| <:expr< $f$ strm__ >> ->
if top then
<:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some a -> a
| _ -> raise Stream.Failure ]
>>
else
let v = free_var_in_expr 's' f in
<:expr< let s = strm__ in $subst v f$ s >>
| e -> semantic e ]
;
value parser_of_expr =
let rec parser_cases e =
match e with
[ <:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $sp_kont$
| _ -> $p_kont$ ]
>> ->
let spc = (SPCnterm p f, None) in
let (sp, epo, e) = kont sp_kont in
[([spc :: sp], epo, e) :: parser_cases p_kont]
| <:expr<
match Stream.peek strm__ with
[ Some $p$ $when:wo$ -> do Stream.junk strm__; return $sp_kont$
| _ -> $p_kont$ ]
>> ->
let spc = (SPCterm (p, wo), None) in
let (sp, epo, e) = kont sp_kont in
[([spc :: sp], epo, e) :: parser_cases p_kont]
| <:expr< let $p$ = strm__ in $sp_kont$ >> ->
let spc = (SPCsterm p, None) in
let (sp, epo, e) = kont sp_kont in
[([spc :: sp], epo, e)]
| <:expr< let $p$ = Stream.count strm__ in $e$ >> -> [([], Some p, e)]
| <:expr< raise Stream.Failure >> -> []
| _ -> [([], None, e)] ]
and kont e =
match e with
[ <:expr<
match try Some ($f$ strm__) with [ Stream.Failure -> None ] with
[ Some $p$ -> $sp_kont$
| _ -> raise (Stream.Error $err$) ]
>> ->
let err =
match err with
[ <:expr< "" >> -> None
| _ -> Some err ]
in
let spc = (SPCnterm p f, err) in
let (sp, epo, e) = kont sp_kont in
([spc :: sp], epo, e)
| <:expr<
match Stream.peek strm__ with
[ Some $p$ $when:wo$ -> do Stream.junk strm__; return $sp_kont$
| _ -> raise (Stream.Error $err$) ]
>> ->
let err =
match err with
[ <:expr< "" >> -> None
| _ -> Some err ]
in
let spc = (SPCterm (p, wo), err) in
let (sp, epo, e) = kont sp_kont in
([spc :: sp], epo, e)
| <:expr< let $p$ = strm__ in $sp_kont$ >> ->
let spc = (SPCsterm p, None) in
let (sp, epo, e) = kont sp_kont in
([spc :: sp], epo, e)
| <:expr< let $p$ = Stream.count strm__ in $e$ >> -> ([], Some p, e)
| _ -> ([], None, e) ]
in
parser_cases
;
value parser_cases expr patt b spel dg k =
let rec parser_cases b spel dg k =
match spel with
[ [] -> [: `HVbox [: b; k :] :]
| [(sp, epo, e)] -> [: `parser_case b sp epo e dg k :]
| [(sp, epo, e) :: spel] ->
[: `parser_case b sp epo e "|" [: :];
parser_cases [: `S LR "|" :] spel dg k :] ]
and parser_case b sp epo e dg k =
let epo =
match epo with
[ Some p -> [: `patt p "" [: `S LR "->" :] :]
| _ -> [: `S LR "->" :] ]
in
HVbox
[: b;
`HOVbox
[: `HOVbox
[: `S LR "[<";
stream_patt [: :] sp [: `S LR ">]"; epo :] :];
`expr e dg k :] :]
and stream_patt b sp k =
match sp with
[ [] -> [: `HVbox [: b; k :] :]
| [(spc, None)] -> [: `stream_patt_comp b spc "" k :]
| [(spc, Some e)] ->
[: `HVbox
[: `stream_patt_comp b spc "" [: :];
`HVbox [: `S LR "?"; `expr e "" k :] :] :]
| [(spc, None) :: spcl] ->
[: `stream_patt_comp b spc ";" [: `S RO ";" :];
stream_patt [: :] spcl k :]
| [(spc, Some e) :: spcl] ->
[: `HVbox
[: `stream_patt_comp b spc "" [: :];
`HVbox [: `S LR "?"; `expr e ";" [: `S RO ";" :] :] :];
stream_patt [: :] spcl k :] ]
and stream_patt_comp b spc dg k =
match spc with
[ SPCterm (p, w) ->
HVbox [: b; `S LO "'"; `spatt patt p "" (when_opt w k) :]
| SPCnterm p e ->
HVbox [: b; `HVbox [: `patt p "" [: `S LR "=" :]; `expr e dg k :] :]
| SPCsterm p -> HVbox [: b; `patt p "" k :] ]
and when_opt wo k =
match wo with
[ Some e -> [: `S LR "when"; `expr e "" k :]
| _ -> k ]
in
parser_cases b spel dg k
;
value parser_body expr patt e dg k =
let (bp, e) =
match e with
[ <:expr< let $bp$ = Stream.count strm__ in $e$ >> -> (Some bp, e)
| e -> (None, e) ]
in
let e = rewrite_parser e in
match parser_of_expr e with
[ [] ->
let spe = ([], None, <:expr< raise Stream.Failure >>) in
HVbox
[: `HVbox
[: `S LR "parser";
match bp with
[ Some p -> [: `patt p "" [: :] :]
| _ -> [: :] ] :];
parser_cases expr patt [: :] [spe] dg k :]
| [spe] ->
HVbox
[: `HVbox
[: `S LR "parser";
match bp with
[ Some p -> [: `patt p "" [: :] :]
| _ -> [: :] ] :];
parser_cases expr patt [: :] [spe] dg k :]
| spel ->
HVbox
[: `HVbox [: :];
`HVbox
[: `S LR "parser";
match bp with
[ Some p -> [: `patt p "" [: :] :]
| _ -> [: :] ] :];
`BEbox
[: `HVbox [: :]; parser_cases expr patt [: :] spel dg k :] :] ]
;
value pmatch expr patt e dg k =
let (me, e) =
match e with
[ <:expr< let strm__ = $me$ in $e$ >> -> (me, e)
| _ -> failwith "Pr_op.pmatch" ]
in
let (bp, e) =
match e with
[ <:expr< let $bp$ = Stream.count strm__ in $e$ >> -> (Some bp, e)
| e -> (None, e) ]
in
let e = rewrite_parser e in
let spel = parser_of_expr e in
HVbox
[: `HVbox [: :];
`HVbox
[: `S LR "match"; `expr me "" [: `S LR "with" :]; `S LR "parser";
match bp with
[ Some p -> [: `patt p "" [: :] :]
| _ -> [: :] ] :];
`BEbox [: `HVbox [: :]; parser_cases expr patt [: :] spel dg k :] :]
;
Pretty.add_pr_fun "stream" stream;
Pretty.add_pr_fun "parser" parser_body;
Pretty.add_pr_fun "parser_m" pmatch;
|