Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] camlp4: How to hook into the char stream of an existing grammar?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-10-30 (19:23)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] camlp4: How to hook into the char stream of an existing grammar?

On Wed, Oct 30, 2002 at 07:33:20PM +0100, Hendrik Tews wrote:
> I have the following question: Suppose I use camlp4 with a
> predefined grammar (such as pa_o.cmo). Is it possible to install
> a function that intercept the character stream that flows into
> the lexer of camlp4 (for instance to record the positions of
> newlines)?

You can patch the function which is called to parse implementations,
which is Pcaml.parse_implem. It is a reference which is defined by
default as: Grammar.Entry.parse Pcaml.implem.

Try the below file "" as syntax extension:
     ocamlc -c -I +camlp4

This command prints the positions of newlines in file
     camlp4o ./foo.cmo pr_null.cmo

(* file *)
let f cs =
  let mycs =
      (fun i ->
           let c = cs in
           if c = '\n' then begin
             Printf.eprintf "newline at pos %d\n" i;
             flush stderr;
           Some c
           Stream.Failure -> None)
  Grammar.Entry.parse Pcaml.implem mycs

let _ = Pcaml.parse_implem := f

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: