5.13. Charset

Start ocaml section to src/flx_charset.mli[1 /1 ]
     1: # 5988 "./lpsrc/flx_types.ipk"
     2: open Flx_ast
     3: type charset_t
     4: val charset_of_string: string -> charset_t
     5: val charset_of_int_range: int -> int -> charset_t
     6: val charset_of_range: string -> string -> charset_t
     7: val charset_union: charset_t -> charset_t -> charset_t
     8: val charset_inv: charset_t -> charset_t
     9: val regexp_of_charset: charset_t -> regexp_t
    10: val regexp_underscore: regexp_t
    11: val eol: int
    12: val regexp_dot: regexp_t
    13: 
End ocaml section to src/flx_charset.mli[1]
Start ocaml section to src/flx_charset.ml[1 /1 ]
     1: # 6001 "./lpsrc/flx_types.ipk"
     2: open Flx_ast
     3: type charset_t = bool array
     4: 
     5: let charset_of_string s =
     6:   let x = Array.make 256 false in
     7:   for i  = 0 to String.length s - 1 do
     8:     x.(Char.code s.[i]) <- true
     9:   done;
    10:   x
    11: 
    12: 
    13: let charset_of_int_range x1 x2 =
    14:   let x = Array.make 256 false in
    15:   for i = x1 to x2 do
    16:     x.(i) <- true
    17:   done
    18:   ;
    19:   x
    20: 
    21: let charset_of_range s1 s2 =
    22:   if String.length s1 <> 1
    23:   then
    24:     failwith "Charset range(first) requires string length 1"
    25:   ;
    26:   if String.length s2 <> 1
    27:   then
    28:     failwith "Charset range(last) requires string length 1"
    29:   ;
    30:   let x1 = Char.code (s1.[0])
    31:   and x2 = Char.code (s2.[0])
    32:   in
    33:     charset_of_int_range x1 x2
    34: 
    35: let charset_union x1 x2 =
    36:   let x = Array.make 256 false in
    37:   for i = 0 to 255 do
    38:     x.(i) <- x1.(i) || x2.(i)
    39:   done;
    40:   x
    41: 
    42: let charset_inv y =
    43:   let x = Array.make 256 false in
    44:   for i = 0 to 255 do
    45:     x.(i) <- not y.(i)
    46:   done;
    47:   x
    48: 
    49: let regexp_of_charset y =
    50:   let res = ref `REGEXP_epsilon in
    51:   for i = 0 to 255 do
    52:     if y.(i) then res :=
    53:       let r = `REGEXP_string (String.make 1 (Char.chr i)) in
    54:       if !res = `REGEXP_epsilon
    55:       then r
    56:       else `REGEXP_alt ( !res, r)
    57:   done
    58:   ;
    59:   !res
    60: 
    61: let regexp_underscore =
    62:   regexp_of_charset (charset_of_int_range 0 255)
    63: 
    64: let eol = Char.code '\n'
    65: 
    66: let regexp_dot =
    67:   regexp_of_charset
    68:   (
    69:     charset_union
    70:       (charset_of_int_range 0 (eol - 1))
    71:       (charset_of_int_range (eol + 1) 255)
    72:   )
    73: 
    74: 
End ocaml section to src/flx_charset.ml[1]