English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
ocamllex -- ungetting a character?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-07-18 (09:28)
From: Jean-Christophe Filliatre <filliatr@l...>
Subject: Re: [Caml-list] ocamllex -- ungetting a character?

Stefano Zacchiroli writes:
 > On Sat, Jul 15, 2006 at 08:57:51PM -0700, mvanier wrote:
 > > I'm writing a simple program using ocamllex to do some text processing, and 
 > > one feature I would really like is to be able to put a matched character 
 > > back onto the front of the lexing buffer.  I can't figure out a way to do 
 > > this from the documentation (if it's possible, it's not in the manual).  
 > > Does anyone know of a way to do this?
 > I don't think it is possible with ocamllex. 

Well, there  is at least an  ugly way to  do that, which is  to modify
lexbuf.lex_curr_pos manually, as in

  | <your regexp>   { lexbuf.lex_curr_pos <- lexbuf.lex_curr_pos - 1;
	              <your action> }

I've already used this with success  (yes, I feel ashamed :-). I would
like a  member of  the ocaml  development team to  confirm it,  but it
seems  safe, as long  as you  do not  backtrack before  the characters
matched by the  left hand-side regexp. Indeed, the  lexing engine will
not modify the lexbuf once the action is triggered and before the next
call to the lexer.

Hope this helps,
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)