Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: ocaml comfort, where is stringOfCharList?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Fritz Heinrichmeyer <fritz.heinrichmeyer@f...>
Subject: Re: ocaml comfort, where is stringOfCharList?
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 ---------------------------