Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: oiwa@yl.is.s.u-tokyo.ac.jp (Yutaka OIWA)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Announce: Regexp/OCaml syntax extension
Date: Fri, 28 Mar 2003 21:13:06 +0100 (MET)	[thread overview]
Message-ID: <200303282013.VAA20483@pauillac.inria.fr> (raw)
In-Reply-To: <vfiwuiuugmb.fsf@tuba.is.s.u-tokyo.ac.jp> from Yutaka OIWA at "Mar 21, 103 01:50:36 am"

> Hello subscribers:
> 
> I release a camlp4-macro package called Regexp/OCaml.
[...]
> For example, parsing an entry for some log file becomes as easy as follows:
> 
>   try
>     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 ->
>              ...
>         | _ -> ()
>     done
>   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.
[...]
> -- 
> 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

Your camlp4 expertise is impressive :)

However, since you propose to compare with other way to program your
example, I would suggest the simple use of Scanf, that gives:

 try
    while true do
      let ib = Scanning.from_string (input_line ic) in
      match bscanf ib "%c" (fun x -> x) with
      | '(' ->
          bscanf ib "%d:%d:%d) [%s] %s"
           (fun hour min sec name line ->
              let time = hour * 3600 + min * 60 + sec in
              ...)
      | '#' ->
          Scanf.scanf " %s" (fun meta_info -> ...)
      | _ -> ()
    done
  with End_of_file -> ()

This sounds pretty simple, compact, and easy to understand as well...

Best regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


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


      parent reply	other threads:[~2003-03-28 20:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-03-20 16:50 Yutaka OIWA
2003-03-21 10:18 ` Richard W.M. Jones
2003-03-28 20:13 ` Pierre Weis [this message]

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=200303282013.VAA20483@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=oiwa@yl.is.s.u-tokyo.ac.jp \
    /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