caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stephane Glondu <Stephane.Glondu@crans.org>
To: Paul Snively <psnively@mac.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question re: camlp4 parser
Date: Mon, 25 Jul 2005 18:17:41 -0700	[thread overview]
Message-ID: <42E58F35.8070204@crans.org> (raw)
In-Reply-To: <3FA9259E-584B-45D7-BACD-A648222E5A0B@mac.com>

Paul Snively wrote:
> For example, I'd like a parser that matches one or more printable  ASCII
> characters. Something that looks like:
> 
> let rec printable = parser [< '' '..'~'; x = printable >] -> x

The inferred type should have given you a warning:
--> val printable : char Stream.t -> 'a = <fun>

In other word, your function never returns a correct value.

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 = <fun>

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.

> Many thanks and best regards,

You're welcome.

-- 

Stephane Glondu.


  reply	other threads:[~2005-07-26  1:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-26  0:11 Paul Snively
2005-07-26  1:17 ` Stephane Glondu [this message]
2005-07-26 16:43   ` [Caml-list] " Paul Snively
2005-07-26 17:05     ` Stephane Glondu
2005-07-27  7:04     ` Virgile Prevosto
2005-07-28  1:27       ` Paul Snively

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42E58F35.8070204@crans.org \
    --to=stephane.glondu@crans.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=psnively@mac.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).