Browse thread
Question re: camlp4 parser
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Paul Snively <psnively@m...> |
| Subject: | Re: [Caml-list] Question re: camlp4 parser |
-----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 = <fun>
>
> 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 = <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.
>
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-----