Re: ocaml comfort, where is stringOfCharList?

From: Fritz Heinrichmeyer (fritz.heinrichmeyer@fernuni-hagen.de)
Date: Wed Mar 26 1997 - 12:52:22 MET


Date: Wed, 26 Mar 1997 12:52:22 +0100
Message-Id: <199703261152.MAA07229@jfh00.fernuni-hagen.de>
From: Fritz Heinrichmeyer <fritz.heinrichmeyer@fernuni-hagen.de>
To: caml-list@inria.fr
Subject: Re: ocaml comfort, where is stringOfCharList?
In-Reply-To: <199703260819.JAA14740@pauillac.inria.fr>
        <199703260819.JAA14740@pauillac.inria.fr>

This is a repost without MIME structure:

The last days i tried to build a HTML-Posting parser in caml. It was
hard for a beginner used to do such things in scheme. The script below
includes also a small (now untested) tester. I wrote a simple
stringOfList function myself. In scheme i would have written only
(apply string thelist).

Did i miss something? Some examples for the use of the Stream-library
would be helpful, especially for "peek" and "junk".

The parser looks pretty short now, but it was hard work for me. I would
appreciate comments on the style. Could it be shorter?

Is it possible to create an mli file for a module build by a
functor as elegant as the implementation itself?

Now here is postParser.ml:

--------------- snip -------------------

type token =
 Equal | Ampersand | Symbol of string | Value of string
;;

open StringOfList
open StringMap

let charToToken=
  let sol = stringOfList
  in
  let rec wordr st =
    parser
          [<''&';rest=(wordr [])>] -> [<'(Value (sol st));'Ampersand ;rest>]
  | [<''=';rest=(wordr [])>] -> [<'(Symbol (sol st));'Equal;rest>]
  | [<'' '|'\n'|'\t';rest=(wordr st)>] -> rest
  | [<'c;rest=(wordr (c::st))>] -> rest
  | [< >] -> [<'Value(sol st)>]

  in wordr []
;;

let postparser x =
let rec ppr st =
  parser
      [<'Symbol x;'Equal;'Value y;
        rest = (ppr (StringMap.add key: x data: y st ))>] -> rest
| [<'Ampersand;'Symbol x;'Equal;'Value y;
    rest = (ppr (StringMap.add key: x data: y st ))>] -> rest
| [< >] -> st
in ppr StringMap.empty (charToToken x)
;;

(* a small untested (3-26-97) tester
   StringMap.iter
   fun:
   (fun key: a data: b ->(Printf.printf "sym=%s,val=%s\n" a b))
   (postparser (Stream.of_string " ab=1&cd=2&ef=3&gh=4 "))
   ;;
   *)

------------- snip ---------------------------

        



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:10 MET