From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA17288; Mon, 29 Sep 2003 23:10:57 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA21249 for ; Mon, 29 Sep 2003 23:10:56 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h8TLAsH05594; Mon, 29 Sep 2003 23:10:54 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA16929; Mon, 29 Sep 2003 23:10:53 +0200 (MET DST) From: Pierre Weis Message-Id: <200309292110.XAA16929@pauillac.inria.fr> Subject: Re: [Caml-list] unread_char function In-Reply-To: <20030929184457.GA31008@bourg.inria.fr> from Basile Starynkevitch at "Sep 29, 103 08:44:57 pm" To: basile.starynkevitch@inria.fr (Basile Starynkevitch) Date: Mon, 29 Sep 2003 23:10:53 +0200 (MET DST) Cc: miles@caddr.com, caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; pierre:01 weis:01 pierre:01 weis:01 caml-list:01 char:01 egan:01 char:01 3.07:01 basile:01 basile:01 scanf:01 tok:99 scanf:01 bscanf:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > On Mon, Sep 29, 2003 at 08:42:13AM -0700, Miles Egan wrote: > > I need an "unread_char" function to go along with the input_char > > function for a tool I'm writing in OCaml, but I can't seem to find one > > in OCaml 3.07beta. > > Perhaps you might want to use streams? They provide some form of lookahead > (by factorising common prefixes). > > Ungetting functions might be not always possible in general (i.e. > considering reading from a raw serial line, or a raw network...). > > -- > Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr > Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359 > http://cristal.inria.fr/~starynke --- all opinions are only mine You may also consider scanf and the format %0c that gives you access to the current character without incorporating it to the current token (as Basile said, this is some form of lookahead, limited to 1 character as is normally the case for streams). To give a more concrete example, here is an elementary scanner for strings, numbers, chars, idents and single character symbols: the scanneer just decides on the first character (after proper skipping of spaces) what is the kind of token to read. let scan_tok sb = Scanf.bscanf sb " %0c" (function | '"' -> Scanf.bscanf sb "%S" (fun v -> String v) | '0' .. '9' | '-' | '+' -> Scanf.bscanf sb "%f" (fun v -> Float v) | '\'' -> Scanf.bscanf sb "%C" (fun v -> Char v) | 'a' .. 'z' | 'A' .. 'Z' -> Scanf.bscanf sb "%[a..zA..Z0..9]" (fun v -> Ident v) | c -> Scanf.bscanf sb "%1c" (fun v -> Symbol v));; val scan_tok : Scanf.Scanning.scanbuf -> tok = Cute, isn't it ? Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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