Version française
Home     About     Download     Resources     Contact us    
Browse thread
Question re: camlp4 parser
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Stephane Glondu <Stephane.Glondu@c...>
Subject: Re: [Caml-list] Question re: camlp4 parser
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.