From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 7DCB2D179 for ; Tue, 26 Jul 2005 18:43:58 +0200 (CEST) Received: from smtpout.mac.com (smtpout.mac.com [17.250.248.44]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j6QGhvfZ000764 for ; Tue, 26 Jul 2005 18:43:58 +0200 Received: from mac.com (smtpin02-en2 [10.13.10.147]) by smtpout.mac.com (Xserve/8.12.11/smtpout12/MantshX 4.0) with ESMTP id j6QGhlp0006755; Tue, 26 Jul 2005 09:43:47 -0700 (PDT) Received: from [192.168.0.100] (dsl092-032-215.lax1.dsl.speakeasy.net [66.92.32.215]) (authenticated bits=0) by mac.com (Xserve/smtpin02/MantshX 4.0) with ESMTP id j6QGhjtm016995 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NO); Tue, 26 Jul 2005 09:43:47 -0700 (PDT) In-Reply-To: <42E58F35.8070204@crans.org> References: <3FA9259E-584B-45D7-BACD-A648222E5A0B@mac.com> <42E58F35.8070204@crans.org> Mime-Version: 1.0 (Apple Message framework v733) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <0C774C67-0FD2-49F3-9293-B81BB3A0991E@mac.com> Cc: caml-list@yquem.inria.fr Content-Transfer-Encoding: 7bit From: Paul Snively Subject: Re: [Caml-list] Question re: camlp4 parser Date: Tue, 26 Jul 2005 09:43:28 -0700 To: Stephane Glondu X-Pgp-Agent: GPGMail 1.1.1 (Tiger) X-Gpgmail-State: signed X-Mailer: Apple Mail (2.733) X-Miltered: at nez-perce with ID 42E6684D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 parser:01 hash:01 val:01 char:01 buf:01 buffer:01 rec:01 parser:01 buffer:01 char:01 buf:01 val:01 variants:01 caml-list:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, Stephane! On Jul 25, 2005, at 6:17 PM, Stephane Glondu wrote: > > The inferred type should have given you a warning: > --> val printable : char Stream.t -> 'a = > > In other word, your function never returns a correct value. > Excellent point. > Try this: > > let printable s = > let buf = Buffer.create 100 in > let rec aux = parser > [< '' '..'~' as c; x = (Buffer.add_char buf c; aux) >] -> x > | [< >] -> Buffer.contents buf > in aux s ;; > --> val printable : char Stream.t -> string = > > printable (Stream.of_string "Test!\013") ;; > --> - : string = "Test!" > > Notice that you cannot remove the occurrences of "s" (even though it > would have the same type) if you are planning to use this function > several times. > Thanks, this is exactly the kind of thing I was hoping for! So the key points are: 1) Use the | and an empty alternative pattern to capture the "no more matches" case. 2) Use "as" and take advantage of expression sequencing to accumulate the matches into a variable (Buffer, in this case). That makes perfect sense and now seems obvious. :-) One hopefully final question: is there a convenient shorthand for saying something like "all printable characters except '=' or '['?" I assume not--that is, we have ranges (' '..'~') or we have variants ('A' | 'B' | 'C'...) and that's it. I'm somewhat spoiled, I think, by Spirit in C++, and its notion of "character sets" and operations on them, so I can say, e.g. "print_p - '='" that that will match all printable characters other than '='. > >> Many thanks and best regards, >> > > You're welcome. > Thanks again, > -- > > Stephane Glondu. > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > Paul Snively -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (Darwin) iEYEARECAAYFAkLmaEAACgkQO3fYpochAqIc5QCeOaHzKj+bTBOObRMisOSzdyO7 RrkAoKkWokql0JuuFvLUeelr5NgTNsgg =IFMX -----END PGP SIGNATURE-----