CamlP4を使ってトークンを1つ取り出す

とりあえず、取り出すだけなら以下のコードで出来ました。

(*
$ ocamlc -pp camlp4o test2.ml
*)
open Format

type token =
  Number of float

let print_token ppf = function
  | Number n -> fprintf ppf "Number(%f)@?" n

let rec lex = parser
  (* Skip any whitespace. *)
  | [< ' (' ' | '\n' | '\r' | '\t'); stream >] -> lex stream

  (* number: [0-9.]+ *)
  | [< ' ('0' .. '9' as c); stream >] ->
    let buffer = Buffer.create 1 in
    Buffer.add_char buffer c;
    lex_number buffer stream
  (* end of stream. *)
  | [< >] -> [< >]

and lex_number buffer = parser
  | [< ' ('0' .. '9' | '.' as c); stream >] ->
    Buffer.add_char buffer c;
    lex_number buffer stream
  | [< stream=lex >] ->
    [< 'Number (float_of_string (Buffer.contents buffer)); stream >]

let _ = 
  (*match Stream.peek(lex (Stream.of_channel stdin)) with*)
  match Stream.peek(lex [< ''1'; ''2'; ''3' >]) with
  | Some t -> printf "%a\n" print_token t
  | None -> printf "ng\n"