caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] camlp4 -- troubles with printer
@ 2002-09-11 16:59 Max Kirillov
  2002-09-12 14:36 ` Daniel de Rauglaudre
  0 siblings, 1 reply; 3+ messages in thread
From: Max Kirillov @ 2002-09-11 16:59 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 767 bytes --]

Hello.

Let's consider the attached file. It splits a file at words
"WHERE", reverses the order of the pieces, and concat them
back.

Trying to print the result, I got an error:

: $cat test.ml 
: let _ = print_string "1\n"; flush stdout
: WHERE
: let _ = print_string "2\n"; flush stdout
: $camlp4o ./syntax1.cmo pr_o.cmo test.ml                                 
: let _ = print_string "1\n"; flush stdout
: WHERE
: let _ = print_string "2\n"; flush stdoutUncaught exception: End_of_file

But, the code works:

: $ocamlc -pp "camlp4o ./syntax1.cmo"  test.ml
: $./a.out 
: 2
: 1

More complex code works as well, but the printing always fails.

I suspect this is due to 'loc' values are not accendind. The
error is displayed at ocaml-3.06 as well as ocaml-3.04

Max.

[-- Attachment #2: syntax1.ml --]
[-- Type: text/plain, Size: 1056 bytes --]

(*
    ocamlc -pp "camlp4o pa_extend.cmo q_MLast.cmo" -I +camlp4 -c syntax1.ml
*)

open Pcaml;;

EXTEND
    str_item: LAST
	[[ EOI -> assert false
	| "WHERE" -> <:str_item< $lid:"wHERE"$ >> ]];
END;;

(* generally, this splits a list, then reverses the large parts
    and concat  them back *)
let wrap f x = 
    let split pred l =
    (* cound not found it *)
	let rec s1 rl add argl = 
	    try let e = List.hd argl
		and tl = List.tl argl in
		match (pred e,add,rl)
		    with (false,true,rl) -> s1 ([e]::rl) false tl
		    | (false,false,re::rl') -> s1 ((e::re)::rl') false tl
		    | (false,false,[]) -> assert false
		    | (true,false,rl) -> s1 rl true tl
		    | (true,true,rl) -> s1 ([]::rl) true tl
	    with Failure "hd" | Failure "tl" -> rl
	in
	s1 [] true l in
    let wrapV (l,fl) =
	let m1 = function (<:str_item< $lid:"wHERE"$ >>,_) -> true
			| _ -> false in
	let (&>) f1 f2 x = f2 (f1 x) in
	let res = (split m1 &> List.map List.rev &> List.concat) l in
	(res,fl) in
    wrapV (f x);;

Pcaml.parse_implem := wrap (!Pcaml.parse_implem);;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] camlp4 -- troubles with printer
  2002-09-11 16:59 [Caml-list] camlp4 -- troubles with printer Max Kirillov
@ 2002-09-12 14:36 ` Daniel de Rauglaudre
  2002-09-12 17:59   ` Max Kirillov
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel de Rauglaudre @ 2002-09-12 14:36 UTC (permalink / raw)
  To: caml-list

Hi,

On Wed, Sep 11, 2002 at 11:59:22PM +0700, Max Kirillov wrote:

> Let's consider the attached file. It splits a file at words
> "WHERE", reverses the order of the pieces, and concat them
> back.
> [...]
> : $camlp4o ./syntax1.cmo pr_o.cmo test.ml                                 
> : let _ = print_string "1\n"; flush stdout
> : WHERE
> : let _ = print_string "2\n"; flush stdoutUncaught exception: End_of_file

The problem is indeed due to the locations in your resulting syntax tree
which are not (no more) in order.

By default pr_o.cmo considers that the locations of the phrases of the
resulting syntax tree are in increasing order. It uses them to print
the comments and/or the empty lines between the phrases (and in the
beginning and the end of the file).

The first two lines of your printed result:
   let _ = print_string "1\n"; flush stdout
   WHERE
corresponds for pr_o.cmo to the "comments" of the beginning of the file,
since your first phrase is actually the one with print_string "2\n".

If your resulting syntax tree does not respect the order of locations,
the solution is to use the option -sep added by pr_o.cmo:

   camlp4o ./syntax1.cmo pr_o.cmo -sep "\n\n" test.ml

This options tells pr_o.cmo not to read the source file to print the
comments between phrases, but print an empty line ("\n\n") instead.

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] camlp4 -- troubles with printer
  2002-09-12 14:36 ` Daniel de Rauglaudre
@ 2002-09-12 17:59   ` Max Kirillov
  0 siblings, 0 replies; 3+ messages in thread
From: Max Kirillov @ 2002-09-12 17:59 UTC (permalink / raw)
  To: caml-list

On Thu, Sep 12, 2002 at 04:36:05PM +0200, Daniel de Rauglaudre wrote:
>    camlp4o ./syntax1.cmo pr_o.cmo -sep "\n\n" test.ml

Thank you!

Max.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-09-12 18:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-11 16:59 [Caml-list] camlp4 -- troubles with printer Max Kirillov
2002-09-12 14:36 ` Daniel de Rauglaudre
2002-09-12 17:59   ` Max Kirillov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).