Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] Announce: Regexp/OCaml syntax extension
> 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