What does `[< >]` mean in OCaml?

1.8k Views Asked by At

I have seen some source code having

let rec parse_document = parser
    | [< len = parse_int32; st; >] ->
      parse_list [] (ES.take_int32 len st)
    | [< >] -> malformed "parse_document"

Can I know what is [< >] inside? it is too hard to google about this kind of signs.

4

There are 4 best solutions below

2
On BEST ANSWER

This is a syntactic sugar for the Stream datatype. Its manipulation is described in detail in this chapter of the book Developping Applications with OCaml.

The syntactic sugar is not built-in in the compiler, it needs to be preprocessed by the Camlp4 preprocessor. To do that, you have to add -pp camlp4o to your compilation command line.

0
On

That's the literal syntax for streams. A stream is just like a list except that only one element is available at a time and you remove the first element by reading it.

It seems primarily used for parser code. Parsers--declared with the parser keyword as in your example--are the functions that can "consume" elements of the stream.

0
On

This is a stream. It is used mainly to create parsers. But streams have been removed from OCaml and are now provided as a camlp4 extension.

0
On

It is part of the Stream parsing syntax extension, and it means the empty stream.