Source

ocaml-lib / tokens.ml

Full commit
(** Tokens and utility functions. *)

exception Eof

type t =
  | Quote | DoubleQuote | BackQuote
  | Comma | Colon | SemiColon | Dot | Interro | Exclam
  | LeftPar | RightPar | LeftAcc | RightAcc | LeftBra | RightBra
  | Nat of int | MinusNat of int
  | Char of char
  | String of string
  | Symbol of string
  | PP_tilda | PP_space | PP_cut | PP_break of int * int | PP_newline

(* predicate filtering PP_* tokens *)
let is_PP = function PP_tilda | PP_space | PP_cut | PP_break _ | PP_newline -> true | _ -> false

(* get the precision of a float number from its writing *)
let prec_of_sfloat : string -> int =
  fun s ->
      let l = String.length s in
      let i_dot =
        try String.index s '.'
        with Not_found -> try String.index s 'e' - 1
        with Not_found -> l - 1 in
      let i_e,p =
        try
          let i_e = String.index s 'e' in
          i_e,
          match s.[i_e+1] with
          | '+' -> int_of_string (String.sub s (i_e+2) (l-(i_e+2)))
          | _ -> int_of_string (String.sub s (i_e+1) (l-(i_e+1)))
        with Not_found -> l, 0 in
      p - (i_e - (i_dot+1))

(* predicate for testing whether a symbol needs quotes *)
let symbol_needs_quotes s =
  let is_ident = Str.string_match (Str.regexp "^[A-Za-z_][A-Za-z0-9_]*$") s 0 in
  let is_op = Str.string_match (Str.regexp "^[-~@#$%^&*+=|\\/><]+$") s 0 in
  not (is_ident || is_op)