Considering the sample code below that contains non-functional stuff
that mimics a simple XmlScanner from my company.

type event = StartElement | EndElement | Text | EndOfDocument

let cur_name = ref None
let name () = match !cur_name with None -> failwith "None" | Some name
-> name
let next it =
         match !it with
         | [] -> failwith "Unexpected end of source"
         | hd :: tl -> it := tl; cur_name := Some (snd hd); fst hd

let read_section_while source test f a =
        let rec handle_next a =
                match next source, name () with
                | EndOfDocument, _      -> failwith "Unexpected end of document"
                | Text, _                       -> handle_next (f a "Text")
                | StartElement, "titi"  -> handle_next (f a "Start titi")
                | EndElement,   "tata"  -> handle_next (f a "End tata")
                | xml                           -> if test xml then handle_next 
a else a
        in
        handle_next a

let source = [
        (StartElement, "toto");
                (StartElement, "titi");
                (EndElement, "titi");
                (StartElement, "tata");
                (EndElement, "tata");
        (EndElement, "toto");
        (EndOfDocument, "dummy")
];;

read_section_while (ref source) ((<>) (EndElement, "toto")) (fun a x -
> x :: a) [];;

==== END SAMPLE ===

I have an exception "Unexpected end of source" while the code was
supposed to stop normally on (EndElement, "toto")

The same code works as expected if I just change slightly the default
pattern:
  | ev, name -> if test (ev, name) then handle_next a else a
instead of:
  | xml -> if test xml then handle_next a else a

Again, the same code works if I make the tuple resulting from (next
it, name ()) just before the pattern matching:
  let x = next source, name () in match next x with
instead of:
  match next source, name () with

>From my point of view, I thought that there was no fundamental
difference between the code that works and the code that fails. Maybe
I missed something...

I am using Ocaml 3.11.0 and I don't plan to update for the moment
since I am experiencing 3rd party library link problems as soon as
something change in the Ocaml+MinGW+Cygwin+FlexDLL toolchain.

-- 
You received this message because you are subscribed to the Google Groups 
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html

Reply via email to