English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Announce: Regexp/OCaml syntax extension
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-03-20 (16:50)
From: Yutaka OIWA <oiwa@y...>
Subject: [Caml-list] Announce: Regexp/OCaml syntax extension
Hello subscribers:

I release a camlp4-macro package called Regexp/OCaml.

I believe Objective Caml is a powerful tool not only for writing an
interpreter and compiler but also for writing casual "script"
applications which were in a territory of Perl, Python and
Ruby. However, string decomposition is one of OCaml's weak points when
compared to those scripting languages: the interfaces of both Str
module and Pcre module are very primitive and cumbersome if they are
heavily used.  Regexp/OCaml solves this point by providing syntax
support for regular expression matching.

Regexp/OCaml provides convenient syntax sugar for regular expression
match against strings using PCRE/OCaml library. The features of this
macro package are the following:

    * Convenient syntax: similar to standard match-with expressions
    * Binding matching substrings to variables: no more $1, $2, ...
    * Automagical easy-to-use type-coercion: no flood of int_of_string etc.
    * Support for optional-patterns: gives string option type etc.
    * Default values for optional-patterns 

For example, parsing an entry for some log file becomes as easy as follows:

    while true do
      let line = input_line ic in
        Regexp.match line with
          "^\((\d\d):(\d\d):(\d\d)\)\[(.*?)\] (.*)$" 
           as hour : int, min: int, sec : int, name, line ->
             let time = hour * 3600 + min * 60 + sec in
        | "^# (.*)$" as meta_info ->
        | _ -> ()
  with End_of_file -> ()

This short code parses both line in format like
  "(00:34:32) [foobar] something" and "# some meta info"
and binds appropriate data into variables which can be used inside "...".
Compare the code above with an equivalent without using syntax extension.

Regexp/OCaml is downloadable from the web location
http://www.yl.is.s.u-tokyo.ac.jp/~oiwa/caml/ .

In addition to the main macro called pa_regexp_match, the package also
contains two tiny macros:

 1) pa_pragma changes an option for loaded camlp4 macros inside source code.
 2) pa_once provides "once" construct which evaluates any expression
    only once per execution (pa_regexp_match uses this internally).

Any comments will be greatly appreciated.

Yutaka Oiwa              Yonezawa Lab., Dept. of Computer Science,
      Graduate School of Information Sci. & Tech., Univ. of Tokyo.
      <oiwa@yl.is.s.u-tokyo.ac.jp>, <yutaka@oiwa.shibuya.tokyo.jp>
PGP fingerprint = C9 8D 5C B8 86 ED D8 07  EA 59 34 D8 F4 65 53 61

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