text
stringlengths 12
786k
|
---|
let setup_app level port host application_id https = let u2f = U2f . create application_id in let level = match level with None -> None | Some Logs . Debug -> Some ` Debug | Some Info -> Some ` Info | Some Warning -> Some ` Warning | Some Error -> Some ` Error | Some App -> None in Dream . initialize_log ? level ( ) ; Dream . run ~ port ~ interface : host ~ https @@ Dream . logger @@ Dream . memory_sessions @@ Flash_message . flash_messages @@ add_routes u2f @@ Dream . not_found |
let port = let doc = " port " in Arg . ( value & opt int 4000 & info [ " p " ; " port " ] ~ doc ) |
let host = let doc = " host " in Arg . ( value & opt string " 0 . 0 . 0 . 0 " & info [ " h " ; " host " ] ~ doc ) |
let application_id = let doc = " the u2f application id - usually protocol :// host ( : port ) " in Arg . ( value & opt string " https :// u2f - demo . robur . coop " & info [ " application - id " ] ~ doc ) |
let tls = let doc = " tls " in Arg . ( value & flag & info [ " tls " ] ~ doc ) |
let ( ) = let term = Term . ( pure setup_app $ Logs_cli . level ( ) $ port $ host $ application_id $ tls ) in let info = Term . info " U2f app " ~ doc " : U2f app " ~ man [ ] : in match Term . eval ( term , info ) with | ` Ok ( ) -> exit 0 | ` Error _ -> exit 1 | _ -> exit 0 |
let is_spec attr = attr . attr_name . txt = " gospel " |
let rec get_spec_attr = function | [ ] -> ( None , [ ] ) | h :: t when is_spec h -> ( Some h , t ) | h :: t -> let elt , rest = get_spec_attr t in ( elt , h :: rest ) |
let get_spec_content attr = match attr . attr_payload with | PStr [ { pstr_desc = Pstr_eval ( { pexp_desc = Pexp_constant ( Pconst_string ( spec , _ , _ ) ) ; _ } , _ ) ; _ ; } ; ] -> ( spec , attr . attr_loc ) | _ -> assert false |
let get_inner_spec attr = match attr . attr_payload with | PStr [ { pstr_desc = Pstr_eval ( _ , attrs ) ; _ } ] -> get_spec_attr attrs | _ -> assert false |
let set_position ( lexbuf : Lexing . lexbuf ) ( position : Lexing . position ) = lexbuf . lex_curr_p <- { position with pos_fname = lexbuf . lex_curr_p . pos_fname } ; lexbuf . lex_abs_pos <- position . pos_cnum |
let set_filename ( lexbuf : Lexing . lexbuf ) ( fname : string ) = lexbuf . lex_curr_p <- { lexbuf . lex_curr_p with pos_fname = fname } |
let parse_gospel ~ filename parse attr = let spec , _ = get_spec_content attr in let lb = Lexing . from_string spec in set_position lb attr . attr_loc . loc_start ; set_filename lb filename ; try ( spec , parse Ulexer . token lb ) with Uparser . Error -> let loc = { loc_start = lb . lex_start_p ; loc_end = lb . lex_curr_p ; loc_ghost = false } in W . error ~ loc W . Syntax_error |
let type_declaration ~ filename t = let spec_attr , other_attrs = get_spec_attr t . ptype_attributes in let parse attr = let ty_text , spec = parse_gospel ~ filename Uparser . type_spec attr in { spec with ty_text ; ty_loc = attr . attr_loc } in let spec = Option . map parse spec_attr in { tname = t . ptype_name ; tparams = t . ptype_params ; tcstrs = t . ptype_cstrs ; tkind = t . ptype_kind ; tprivate = t . ptype_private ; tmanifest = t . ptype_manifest ; tattributes = other_attrs ; tspec = spec ; tloc = t . ptype_loc ; } |
let val_description ~ filename v = let spec_attr , other_attrs = get_spec_attr v . pval_attributes in let parse attr = let sp_text , spec = parse_gospel ~ filename Uparser . val_spec attr in { spec with sp_text ; sp_loc = attr . attr_loc } in let spec = Option . map parse spec_attr in { vname = v . pval_name ; vtype = v . pval_type ; vprim = v . pval_prim ; vattributes = other_attrs ; vspec = spec ; vloc = v . pval_loc ; } |
let ghost_spec ~ filename attr = let spec , loc = get_spec_content attr in let lb = Lexing . from_string spec in let sigs = try Parse . interface lb with _ -> W . error ~ loc W . Syntax_error in match sigs with | [ { psig_desc = Psig_type ( r , [ t ] ) ; _ } ] -> let type_ = type_declaration ~ filename t in if type_ . tspec = None then let tspec = get_inner_spec attr |> fst |> Option . map ( parse_gospel ~ filename Uparser . type_spec ) |> Option . map ( fun ( ty_text , spec ) -> { spec with ty_text ; ty_loc = attr . attr_loc } ) in Sig_ghost_type ( r , [ { type_ with tspec ; tloc = attr . attr_loc } ] ) else Sig_ghost_type ( r , [ type_ ] ) | [ { psig_desc = Psig_value vd ; _ } ] -> let val_ = val_description ~ filename vd in if val_ . vspec = None then let vspec = get_inner_spec attr |> fst |> Option . map ( parse_gospel ~ filename Uparser . val_spec ) |> Option . map ( fun ( sp_text , spec ) -> { spec with sp_text ; sp_loc = attr . attr_loc } ) in Sig_ghost_val { val_ with vspec ; vloc = attr . attr_loc } else Sig_ghost_val val_ | [ { psig_desc = Psig_open od ; _ } ] -> Sig_ghost_open { od with popen_loc = attr . attr_loc } | _ -> assert false |
let floating_spec ~ filename a = try let fun_text , fun_ = parse_gospel ~ filename Uparser . func a in let fun_ = { fun_ with fun_text } in if fun_ . fun_spec = None then let fun_spec = get_inner_spec a |> fst |> Option . map ( parse_gospel ~ filename Uparser . func_spec ) |> Option . map ( fun ( fun_text , ( spec : fun_spec ) ) -> { spec with fun_text ; fun_loc = a . attr_loc } ) in Sig_function { fun_ with fun_spec } else Sig_function fun_ with W . Error ( _ , W . Syntax_error ) -> ( try let ax_text , axiom = parse_gospel ~ filename Uparser . axiom a in Sig_axiom { axiom with ax_text ; ax_loc = a . attr_loc } with W . Error ( _ , W . Syntax_error ) -> ghost_spec ~ filename a ) |
let with_constraint c = let no_spec_type_decl t = { tname = t . ptype_name ; tparams = t . ptype_params ; tcstrs = t . ptype_cstrs ; tkind = t . ptype_kind ; tprivate = t . ptype_private ; tmanifest = t . ptype_manifest ; tattributes = t . ptype_attributes ; tspec = None ; tloc = t . ptype_loc ; } in match c with | Pwith_type ( l , t ) -> Wtype ( l , no_spec_type_decl t ) | Pwith_module ( l1 , l2 ) -> Wmodule ( l1 , l2 ) | Pwith_typesubst ( l , t ) -> Wtypesubst ( l , no_spec_type_decl t ) | Pwith_modsubst ( l1 , l2 ) -> Wmodsubst ( l1 , l2 ) | Pwith_modtype ( l1 , l2 ) -> Wmodtype ( l1 , l2 ) | Pwith_modtypesubst ( l1 , l2 ) -> Wmodtypesubst ( l1 , l2 ) |
let rec signature_item_desc ~ filename = function | Psig_value v -> Sig_val ( val_description ~ filename v ) | Psig_type ( r , tl ) -> Sig_type ( r , List . map ( type_declaration ~ filename ) tl ) | Psig_attribute a -> if not ( is_spec a ) then Sig_attribute a else floating_spec ~ filename a | Psig_module m -> Sig_module ( module_declaration ~ filename m ) | Psig_recmodule d -> Sig_recmodule ( List . map ( module_declaration ~ filename ) d ) | Psig_modtype d -> Sig_modtype ( module_type_declaration ~ filename d ) | Psig_typext t -> Sig_typext t | Psig_exception e -> Sig_exception e | Psig_open o -> Sig_open o | Psig_include i -> Sig_include i | Psig_class c -> Sig_class c | Psig_class_type c -> Sig_class_type c | Psig_extension ( e , a ) -> Sig_extension ( e , a ) | Psig_typesubst s -> Sig_typesubst ( List . map ( type_declaration ~ filename ) s ) | Psig_modsubst s -> Sig_modsubst s | Psig_modtypesubst s -> Sig_modtypesubst ( module_type_declaration ~ filename s ) List . map ( fun { psig_desc ; psig_loc } -> { sdesc = signature_item_desc ~ filename psig_desc ; sloc = psig_loc } ) sigs | Pmty_ident id -> Mod_ident id | Pmty_signature s -> Mod_signature ( signature ~ filename s ) | Pmty_functor ( fp , mt ) -> Mod_functor ( functor_parameter ~ filename fp , module_type ~ filename mt ) | Pmty_with ( m , c ) -> Mod_with ( module_type ~ filename m , List . map with_constraint c ) | Pmty_typeof m -> Mod_typeof m | Pmty_extension e -> Mod_extension e | Pmty_alias a -> Mod_alias a | Unit -> Unit | Named ( s , m ) -> Named ( s , module_type ~ filename m ) { mdesc = module_type_desc ~ filename m . pmty_desc ; mloc = m . pmty_loc ; mattributes = m . pmty_attributes ; } { mdname = m . pmd_name ; mdtype = module_type ~ filename m . pmd_type ; mdattributes = m . pmd_attributes ; mdloc = m . pmd_loc ; } { mtdname = m . pmtd_name ; mtdtype = Option . map ( module_type ~ filename ) m . pmtd_type ; mtdattributes = m . pmtd_attributes ; mtdloc = m . pmtd_loc ; } |
let err_no_pred = " U + 0000 has no predecessor " |
let err_no_succ = " U + 10FFFF has no successor " |
let err_not_sv i = format_int " % X " i ^ " is not an Unicode scalar value " |
let err_not_latin1 u = " U " + ^ format_int " % 04X " u ^ " is not a latin1 character " |
let succ u = if u = lo_bound then hi_bound else if u = max then invalid_arg err_no_succ else u + 1 |
let pred u = if u = hi_bound then lo_bound else if u = min then invalid_arg err_no_pred else u - 1 |
let is_valid i = ( min <= i && i <= lo_bound ) || ( hi_bound <= i && i <= max ) |
let of_int i = if is_valid i then i else invalid_arg ( err_not_sv i ) |
let is_char u = u < 256 |
let of_char c = Char . code c |
let to_char u = if u > 255 then invalid_arg ( err_not_latin1 u ) else Char . unsafe_chr u |
let equal : int -> int -> bool = ( = ) |
let compare : int -> int -> int = Stdlib . compare |
let log_err s = Printf . printf " % s : % s \ n " %! ( Filename . basename Sys . executable_name ) s |
let is_white = function ' ' | ' \ t ' . . ' \ r ' -> true | _ -> false |
let is_digit = function ' 0 ' . . ' 9 ' -> true | _ -> false |
let hex_digit_value c = let c = Char . code c in if c <= 0x39 then c - 48 else if c <= 0x46 then c - 55 else c - 87 |
let str_starts s pre = let plen = String . length pre in let slen = String . length s in if plen > slen then false else try for i = 0 to plen - 1 do if pre . [ i ] <> s . [ i ] then raise Exit ; done ; true with Exit -> false |
let str_is_ascii s = let max = String . length s - 1 in let rec loop s i = if i > max then true else if Char . code s . [ i ] > 0x7F then false else loop s ( i + 1 ) in loop s 0 |
let esc_non_ascii s = let b = Buffer . create 255 in let add_byte = function | c when Char . code c > 0x7F -> Buffer . add_string b ( strf " \\ x % 02X " ( Char . code c ) ) | c -> Buffer . add_char b c in String . iter add_byte s ; Buffer . contents b |
let uchars_to_utf_bytes utf uchars = let b = Buffer . create 255 in let add_utf = match utf with | ` UTF_8 -> Uutf . Buffer . add_utf_8 | ` UTF_16BE -> Uutf . Buffer . add_utf_16be | ` UTF_16LE -> Uutf . Buffer . add_utf_16le in List . iter ( add_utf b ) uchars ; Buffer . contents b |
let uchars_to_utf_bytes_esc utf uchars = let utf = uchars_to_utf_bytes utf uchars in let b = Buffer . create 255 in let add_byte c = Buffer . add_string b ( strf " \\ x % 02X " ( Char . code c ) ) in String . iter add_byte utf ; Buffer . contents b |
let uchars_to_cps uchars = let to_str u = strf " U +% 04X " ( Uchar . to_int u ) in String . concat " " ( List . map to_str uchars ) |
let uchars_to_named_cps uchars = let to_str u = strf " % s ( U +% 04X ) " ( Uucp . Name . name u ) ( Uchar . to_int u ) in String . concat " " ( List . map to_str uchars ) |
let uchar_of_utf utf s = let fold = match utf with | ` UTF_8 -> Uutf . String . fold_utf_8 | ` UTF_16BE -> Uutf . String . fold_utf_16be | ` UTF_16LE -> Uutf . String . fold_utf_16le in match fold ( fun acc _ decode -> decode :: acc ) [ ] s with | [ ` Uchar u ] -> Some u | _ -> None |
let try_uchar_of_utfs s = let rec try_decs s = function | [ ] -> None | enc :: encs -> match uchar_of_utf enc s with | None -> try_decs s encs | Some _ as u -> u in try_decs s [ ` UTF_8 ; ` UTF_16BE ; ` UTF_16LE ] |
let parse_decimal_esc s = let max = String . length s - 1 in let rec loop s i acc = match i = max with | true -> if s . [ max ] = ' ; ' && Uchar . is_valid acc then Some ( Uchar . of_int acc ) else None | false -> if not ( is_digit s . [ i ] ) then None else loop s ( i + 1 ) ( acc * 10 + ( Char . code s . [ i ] - 48 ) ) in loop s 0 0 |
let parse_hex_esc s ~ finish = let max = String . length s - 1 in let parse_hex s start = let rec loop s i acc = if i > max then acc , max else if not ( is_hex_digit s . [ i ] ) then acc , i - 1 else loop s ( i + 1 ) ( acc * 16 + hex_digit_value s . [ i ] ) in loop s start 0 in let hex , last = parse_hex s 0 in let to_uchar hex = if Uchar . is_valid hex then Some ( Uchar . of_int hex ) else None in match finish with | ` Empty -> if last = max then to_uchar hex else None | ` Char c -> if last = max - 1 && s . [ max ] = c then to_uchar hex else None | ` Maybe_surrogate -> if last = max then to_uchar hex else match 0xD800 <= hex && hex <= 0xDBFF && last <= max - 3 && s . [ last + 1 ] = ' ' \\ && s . [ last + 2 ] = ' u ' with | false -> None | true -> let lo , last ' = parse_hex s ( last + 3 ) in match last ' = max && 0xDC00 <= lo && lo <= 0xDFFF with | false -> None | true -> Some ( Uchar . of_int @@ ( ( ( hex land 0x3FF ) lsl 10 ) lor ( lo land 0x3FF ) ) + 0x10000 ) |
let uchar_of_uchar_esc s = let prefixes = [ " U " ; + " u " ; + " U " ; " u " ; " \\ u { " ; " \\ U { " ; " \\ u " ; " \\ U " ; " &# x " ; " " &# ] in match try Some ( List . find ( str_starts s ) prefixes ) with Not_found -> None with | None -> None | Some pre -> let pre_len = String . length pre in let s = String . sub s pre_len ( String . length s - pre_len ) in match pre with | " " &# -> parse_decimal_esc s | " &# x " -> parse_hex_esc s ~ finish ( ` : Char ' ; ' ) | " \\ u " -> parse_hex_esc s ~ finish ` : Maybe_surrogate | " \\ u { " | " \\ U { " -> parse_hex_esc s ~ finish ( ` : Char ' } ' ) | _ -> parse_hex_esc s ~ finish ` : Empty |
let uchar_of_utf_8_bytes_esc s = let b = Buffer . create 255 in let max = String . length s - 1 in let parse_hex_byte s i = match i + 1 <= max && is_hex_digit s . [ i ] && is_hex_digit s . [ i + 1 ] with | false -> None | true -> Some ( 16 * ( hex_digit_value s . [ i ] ) + ( hex_digit_value s . [ i + 1 ] ) ) in let rec loop s i = if i > max then uchar_of_utf ` UTF_8 ( Buffer . contents b ) else if is_white s . [ i ] then loop s ( i + 1 ) else if i + 1 > max then None else let i = match s . [ i ] with | ( ' 0 ' | ' ' ) \\ when s . [ i + 1 ] = ' x ' -> i + 2 | ' x ' -> i + 1 | _ -> i in match parse_hex_byte s i with | None -> None | Some byte -> Buffer . add_char b ( Char . chr byte ) ; loop s ( i + 2 ) in loop s 0 |
let guess_spec s = match str_is_ascii s with match uchar_of_uchar_esc s with | Some _ as u -> u | None -> uchar_of_utf_8_bytes_esc s |
let parse_spec spec_fmt s = match spec_fmt with |
let unicode_version ( ) = Printf . printf " % s \ n " %! Uucp . unicode_version |
type out_fmt = { ascii : bool ; no_labels : bool ; raw_bytes : bool ; } |
let str ( pp , key ) ( out_fmt : out_fmt ) u = strf " % a " pp ( key u ) |
let str_bool key ( out_fmt : out_fmt ) u = strf " % b " ( key u ) |
let str_int key ( out_fmt : out_fmt ) u = strf " % d " ( key u ) |
let str_uchar ( out_fmt : out_fmt ) u = uchars_to_cps [ u ] |
let str_str key ( out_fmt : out_fmt ) u = key u |
let str_utf utf out_fmt u = match out_fmt . raw_bytes with |
let str_cps out_fmt uchars = let cps = uchars_to_cps uchars in match out_fmt . ascii with | true -> cps | false -> let utf_8 = uchars_to_utf_bytes ` UTF_8 uchars in strf " % s ( % s ) " utf_8 cps |
let str_case_map map ( out_fmt : out_fmt ) u = let uchars = match map u with ` Self -> [ u ] | ` Uchars uchars -> uchars in str_cps out_fmt uchars |
let str_decomposition_mapping ( out_fmt : out_fmt ) u = let uchars = match Uunf . decomp u with | [ ] || -> [ u ] | a -> let to_uchar i u = match i = 0 with | true -> Uunf . d_uchar u | false -> Uchar . of_int u in Array . ( to_list @@ mapi to_uchar a ) in str_cps out_fmt uchars |
let str_name_alias key ( out_fmt : out_fmt ) u = let str_alias ( t , n ) = strf " % s ( % a ) " n Uucp . Name . pp_alias_tag t in String . concat " , " ( List . map str_alias ( key u ) ) |
let str_script_extensions key ( out_fmt : out_fmt ) u = String . concat " , " ( List . map ( strf " % a " Uucp . Script . pp ) ( key u ) ) |
let all_keys = [ ` P " Age " , str Uucp . Age . ( pp , age ) ; ` P " Alphabetic " , str_bool Uucp . Alpha . is_alphabetic ; ` P " Block " , str Uucp . Block . ( pp , block ) ; ` P " Line_Break " , str Uucp . Break . ( pp_line , line ) ; ` P " Grapheme_Cluster_Break " , str Uucp . Break . ( pp_grapheme_cluster , grapheme_cluster ) ; ` P " Word_Break " , str Uucp . Break . ( pp_word , word ) ; ` P " Sentence_Break " , str Uucp . Break . ( pp_sentence , sentence ) ; ` P " East_Asian_Width " , str Uucp . Break . ( pp_east_asian_width , east_asian_width ) ; ` P " Lowercase " , str_bool Uucp . Case . is_lower ; ` P " Uppercase " , str_bool Uucp . Case . is_upper ; ` P " Cased " , str_bool Uucp . Case . is_cased ; ` P " Case_ignorable " , str_bool Uucp . Case . is_case_ignorable ; ` P " Lowercase_Mapping " , str_case_map Uucp . Case . Map . to_lower ; ` P " Uppercase_Mapping " , str_case_map Uucp . Case . Map . to_upper ; ` P " Titlecase_Mapping " , str_case_map Uucp . Case . Map . to_title ; ` P " Case_Folding " , str_case_map Uucp . Case . Fold . fold ; ` P " NFKC_Casefold " , str_case_map Uucp . Case . Nfkc_fold . fold ; ` P " Ideographic " , str_bool Uucp . Cjk . is_ideographic ; ` P " IDS_Binary_Operator " , str_bool Uucp . Cjk . is_ids_bin_op ; ` P " IDS_Trinary_Operator " , str_bool Uucp . Cjk . is_ids_tri_op ; ` P " Radical " , str_bool Uucp . Cjk . is_radical ; ` P " Unified_Ideograph " , str_bool Uucp . Cjk . is_unified_ideograph ; ` P " Emoji " , str_bool Uucp . Emoji . is_emoji ; ` P " Emoji_Presentation " , str_bool Uucp . Emoji . is_emoji_presentation ; ` P " Emoji_Modifier " , str_bool Uucp . Emoji . is_emoji_modifier ; ` P " Emoji_Modifier_Base " , str_bool Uucp . Emoji . is_emoji_modifier_base ; ` P " Emoji_Component " , str_bool Uucp . Emoji . is_emoji_component ; ` P " Extended_Pictographic " , str_bool Uucp . Emoji . is_extended_pictographic ; ` P " Dash " , str_bool Uucp . Func . is_dash ; ` P " Diacritic " , str_bool Uucp . Func . is_diacritic ; ` P " Extender " , str_bool Uucp . Func . is_extender ; ` P " Grapheme_Base " , str_bool Uucp . Func . is_grapheme_base ; ` P " Grapheme_Extend " , str_bool Uucp . Func . is_grapheme_extend ; ` P " Math " , str_bool Uucp . Func . is_math ; ` P " Quotation_Mark " , str_bool Uucp . Func . is_quotation_mark ; ` P " Soft_Dotted " , str_bool Uucp . Func . is_soft_dotted ; ` P " Terminal_Punctuation " , str_bool Uucp . Func . is_terminal_punctuation ; ` P " Regional_Indicator " , str_bool Uucp . Func . is_regional_indicator ; ` P " Join_Control " , str_bool Uucp . Func . is_join_control ; ` P " General_Category " , str Uucp . Gc . ( pp , general_category ) ; ` P " Default_Ignorable_Code_Point " , str_bool Uucp . Gen . is_default_ignorable ; ` P " Deprecated " , str_bool Uucp . Gen . is_deprecated ; ` P " Logical_Order_Exception " , str_bool Uucp . Gen . is_logical_order_exception ; ` P " Noncharacter_Code_Point " , str_bool Uucp . Gen . is_non_character ; ` P " Variation_Selector " , str_bool Uucp . Gen . is_variation_selector ; ` P " Hangul_Syllable_Type " , str Uucp . Hangul . ( pp_syllable_type , syllable_type ) ; ` P " ID_Start " , str_bool Uucp . Id . is_id_start ; ` P " ID_Continue " , str_bool Uucp . Id . is_id_continue ; ` P " XID_Start " , str_bool Uucp . Id . is_xid_start ; ` P " XID_Continue " , str_bool Uucp . Id . is_xid_continue ; ` P " Pattern_Syntax " , str_bool Uucp . Id . is_pattern_syntax ; ` P " Pattern_White_Space " , str_bool Uucp . Id . is_pattern_white_space ; ` P " Name " , str_str Uucp . Name . name ; ` P " Name_alias " , str_name_alias Uucp . Name . name_alias ; ` P " ASCII_Hex_Digit " , str_bool Uucp . Num . is_ascii_hex_digit ; ` P " Hex_Digit " , str_bool Uucp . Num . is_hex_digit ; ` P " Numeric_Type " , str Uucp . Num . ( pp_numeric_type , numeric_type ) ; ` P " Numeric_Value " , str Uucp . Num . ( pp_numeric_value , numeric_value ) ; ` P " Script " , str Uucp . Script . ( pp , script ) ; ` P " Script_extensions " , str_script_extensions Uucp . Script . script_extensions ; ` P " White_Space " , str_bool Uucp . White . is_white_space ; ` P " Canonical_Combining_Class " , str_int Uunf . ccc ; ` P " Decomposition_Mapping " , str_decomposition_mapping ; ` N " Uchar " , str_uchar ; ` N " UTF - 8 " , str_utf ` UTF_8 ; ` N " UTF - 16BE " , str_utf ` UTF_16BE ; ` N " UTF - 16LE " , str_utf ` UTF_16LE ; ` H " Tty_width_hint " , str_int Uucp . Break . tty_width_hint ; ] |
let compare_key ( k0 , _ ) ( k1 , _ ) = match k0 , k1 with |
let eq_key k ( k ' , _ ) = match k ' with String . ( compare ( lowercase_ascii k ) ( lowercase_ascii k ' ) = 0 ) |
let exist_key k = List . exists ( eq_key k ) all_keys |
let find_key k = List . find ( eq_key k ) all_keys |
let key_id ( k , _ ) = match k with ` P k | ` N k | ` H k -> k |
let list_keys ( ) = let keys = List . sort compare_key all_keys in let pr_key_name ( n , _ ) = match n with | ` P k -> Printf . printf " % s ( UCD ) \ n " k | ` N k -> Printf . printf " % s \ n " k | ` H k -> Printf . printf " % s ( Uucp library heuristic ) \ n " k in List . iter pr_key_name keys |
let get_keys keys = try Ok ( List . map find_key keys ) with let _ , not = List . partition exist_key keys in match not with | [ p ] -> Error ( strf " Unknown key : % s " p ) | ps -> Error ( strf " Unknown keys : % s " ( String . concat " , " ps ) ) |
let default_keys = [ " Name " ; " Uchar " ; " Age " ; " Block " ; " Script " ; " Script_extensions " ; " General_Category " ; " Decomposition_Mapping " ; " UTF - 8 " ; " UTF - 16BE " ; " UTF - 16LE " ] |
let case_keys = [ " Lowercase " ; " Uppercase " ; " Cased " ; " Case_ignorable " ; " Lowercase_Mapping " ; " Uppercase_Mapping " ; " Titlecase_Mapping " ; " Case_Folding " ; " NFKC_Casefold " ] |
let cjk_keys = [ " Ideographic " ; " IDS_Binary_Operator " ; " IDS_Trinary_Operator " ; " Radical " ; " Unified_Ideograph " ] |
let emoji_keys = [ " Emoji " ; " Emoji_Presentation " ; " Emoji_Modifier " ; " Emoji_Modifier_Base " ; " Emoji_Component " ; " Extended_Pictographic " ] |
let id_keys = [ " ID_Start " ; " ID_Continue " ; " XID_Start " ; " XID_Continue " ; " Pattern_Syntax " ; " Pattern_White_Space " ] |
let num_keys = [ " ASCII_Hex_Digit " ; " Hex_Digit " ; " Numeric_Type " ; " Numeric_Value " ] |
let output_key out_fmt uchar ( k , str ) = match out_fmt . no_labels with let l = match k with ` P k | ` N k | ` H k -> k in Printf . printf " % s : % s \ n " l ( str out_fmt uchar ) |
let query_keys keys out_fmt uchar = match get_keys keys with |
let ucharinfo cmd keys spec_fmt out_fmt uspec = match cmd with match uspec with | None -> log_err " No character specified . " ; 1 | Some uspec -> match parse_spec spec_fmt uspec with | None -> log_err ( strf " Could not parse % s from ' % s ' \ n " %! ( str_of_spec_fmt spec_fmt ) ( esc_non_ascii uspec ) ) ; 1 | Some uchar -> match query_keys keys out_fmt uchar with | Error e -> log_err e ; 2 | Ok ( ) -> ( ) ; 0 |
let cmd = let doc = " List available keys . " in let list_keys = ` List_keys , Arg . info [ " l " ; " key - list " ] ~ doc in let doc = " Output supported Unicode version . " in let unicode_version = ` Unicode_version , Arg . info [ " unicode - version " ] ~ doc in Arg . ( value & vflag ` Query [ unicode_version ; list_keys ] ) |
let spec_fmt = let spec_fmt = [ " UTF - 8 " , ` UTF_8 ; " UTF - 16BE " , ` UTF_16BE ; " UTF - 16LE " , ` UTF_16LE ; " uchar - esc " , ` Uchar_esc ; " bytes - esc " , ` Bytes_esc ; " guess " , ` Guess ] in let doc = strf " The character specification format . ( $ docv ) must be one of % s . See CHARACTER SPECIFICATION for details . " ( Arg . doc_alts_enum spec_fmt ) in let spec_format = Arg . enum spec_fmt in Arg . ( value & opt spec_format ` Guess & info [ " s " ; " spec - format " ] ~ doc ~ docv " : FMT " ) |
let out_fmt = let no_labels = let doc = " Output key values without labels . " in Arg . ( value & flag & info [ " n " ; " no - labels " ] ~ doc ) in let raw_bytes = let doc = " Output byte sequences unescaped . " in Arg . ( value & flag & info [ " b " ; " raw - bytes " ] ~ doc ) in let ascii = let doc = " Output information using only the US - ASCII charset . " in Arg . ( value & flag & info [ " a " ; " ascii " ] ~ doc ) in let out_fmt ascii no_labels raw_bytes = { ascii ; no_labels ; raw_bytes } in Term . ( const out_fmt $ ascii $ no_labels $ raw_bytes ) |
let keys = let all = let doc = " Output value of all keys in alphabetic order . " in Arg . ( value & flag & info [ " all " ] ~ doc ) in let default = let doc = " Output default information . " in Arg . ( value & flag & info [ " d " ; " default " ] ~ doc ) in let case = let doc = " Output values of Unicode case keys . " in Arg . ( value & flag & info [ " case " ] ~ doc ) in let cjk = let doc = " Output values of Unicode CJK keys . " in Arg . ( value & flag & info [ " cjk " ] ~ doc ) in let emoji = let doc = " Output values of Unicode emoji keys . " in Arg . ( value & flag & info [ " emoji " ] ~ doc ) in let id = let doc = " Output values of Unicode identifier keys . " in Arg . ( value & flag & info [ " id " ] ~ doc ) in let num = let doc = " Output values of Unicode numeric keys . " in Arg . ( value & flag & info [ " num " ] ~ doc ) in let keys = let doc = " Output the value of ( $ docv ) . Use ( $ b , - l ) to list available keys . " in Arg . ( value & opt_all string [ ] & info [ " k " ; " key " ] ~ doc ~ docv " : KEY " ) in let choose all default case cjk emoji id num keys = match all with | true -> List . sort compare ( List . map key_id all_keys ) | false -> let keys = if num then num_keys @ keys else keys in let keys = if id then id_keys @ keys else keys in let keys = if cjk then cjk_keys @ keys else keys in let keys = if emoji then emoji_keys @ keys else keys in let keys = if case then case_keys @ keys else keys in let keys = if default then default_keys @ keys else keys in if keys = [ ] then default_keys else keys in Term . ( const choose $ all $ default $ case $ cjk $ emoji $ id $ num $ keys ) |
let uspec = let doc = " The character specification . See CHARACTER SPECIFICATION for details . " in Arg . ( value & pos 0 ( some string ) None & info [ ] ~ doc ~ docv " : UCHAR " ) |
let doc = " Query Unicode character information " |
let exits = Term . exit_info 1 ~ doc " : if the character specification is invalid . " :: Term . exit_info 2 ~ doc " : if a key doesn ' t exist . " :: Term . default_exits |
let man = [ ` S Manpage . s_description ; ` P " ( $ mname ) outputs information about an Unicode character specified on the command line . " ; ` P " The information to output is selected by specifying keys , use the ( $ b , - l ) option to list them . By default the tool outputs a selection of keys for a character . To output all its keys in alphabetic order use the ( $ b , -- all ) option . To output specific keys use the repeatable ( $ b , - k ) option . " ; ` S " CHARACTER SPECIFICATION " ; ` P " The character specification must represent an Unicode scalar value , that is a code point in the range U + 0000 . . U + D7FF or U + E000 . . U + 10FFFF . ( $ mname ) errors on the textually meaningless range U + D800 . . U + DFFF of surrogate code points . " ; ` P " You need to make sure the character specification is passed in a single command line argument by adding appropriate shell quotes if needed . Then , unless the ( $ b , -- spec - format ) option is used the following heuristic is applied , in order , to guess the input format " ; : ` I ( " 1 . UTF - X decode . " , " If the specification has a non US - ASCII byte , the byte sequence is decoded as UTF - 8 , UTF - 16BE or UTF - 16LE ( in that order ) to a single Unicode character , taking the first decode that succeeds . " ) ; ` I ( " 2 . US - ASCII character . " , " If the specification is made of a single US - ASCII character , the corresponding Unicode character is used . " ) ; ` I ( " 3 . Unicode escapes . " , " If all the bytes are US - ASCII and have one of the following Unicode escape form , the unescaped Unicode character is used : U + H u + H uH UH \\\\ uH \\\\ uH \\\\ uH \\\\ UH \\\\ u { H } \\\\ U { H } &# D ; &# xH ; with H and D respectively denoting non empty sequence of caseless hexadecimal and decimal digits . " ) ; ` I ( " 4 . UTF - 8 byte sequence escapes . " , " If all the bytes are US - ASCII and match an escaped hexadecimal byte sequence of the form : ( ( 0 ) |\\\\? x ) ? hh [ ] ) *+ with h denoting a caseless hexadecimal digit , the unescaped byte sequence is decoded from UTF - 8 to a single Unicode character . " ) ; ` P " If none of this succeeds the tool errors . " ; ` P " For example all the following specifications , appropriately quoted for your shell , are acceptable specifications for the Unicode character U + 1F42B " ; : ` Pre " ( $ b , U + 1F42B ) ( $ b , u + 1F42B ) ( $ b , U1F42B ) ( $ b , u1F42B ) \ n \ \ ( $ b , \\\\ u1F42B ) ( $ b , \\\\ u { 1F42B } ) ( $ b , \\\\ U { 1F42B } ) \ n \ \ ( $ b , \\\\ uD83D \\\\ uDC2B ) \ # Surrogate escape ( e . g . JSON / Java [ Script ] ) \ n \ \ ( $ b , &# x1f42b ; ) # HTML / XML hexadecimal character reference \ n \ \ ( $ b , &# 128043 ; ) # HTML / XML decimal character reference \ n \ \ ( $ b , f0 9f 90 ab ) # UTF - 8 escaped byte sequence \ n \ \ ( $ b , F09F90AB ) # Idem \ n \ \ ( $ b , 0xF0 0x9F 0x90 0xAB ) # Idem \ n \ \ ( $ b , \\\\ xF0 \\\\ x9f \\\\ x90 \\\\ xAB ) # Idem " ; ` S Manpage . s_examples ; ` Pre " ( $ mname ) ( $ b , -- all f09F90ab ) # \ All keys of this UTF - 8 byte sequence \ n \ \ ( $ mname ) ( $ b , - k utf - 8 U + 1F42B ) # Escaped UTF - 8 for U + 1F42B \ n \ \ ( $ mname ) ( $ b , - k utf - 8 - n - b U + 1F42B ) # UTF - 8 for U + 1F42B \ n \ \ ( $ mname ) ( $ b , - k name U + 1F42B ) # Name of U + 1F42B \ n \ \ ( $ mname ) ( $ b , - k name ' \\\\ uD83D \\\\ uDC2B ' ) # Idem \ n \ \ ( $ mname ) ( $ b , - k name - k age U + 1f42B ) # Name and age of U + 1F42B \ n \ \ ( $ mname ) ( $ b , - l ) # List keys " ; ` S Manpage . s_bugs ; ` P " This program is distributed with the Uucp OCaml library . See ( $ i , %% PKG_HOMEPAGE ) %% for contact information . " ; ` S " REFERENCES " ; ` P " UAX # 44 Unicode Character Database . ( $ i , http :// www . unicode . org / reports / tr44 ) " ; / ] |
let ucharinfo = Term . ( const ucharinfo $ cmd $ keys $ spec_fmt $ out_fmt $ uspec ) , Term . info " ucharinfo " ~ version " :%% VERSION " %% ~ doc ~ exits ~ man |
let ( ) = Term . ( exit_status @@ eval ucharinfo ) |
type ' a tbl = ' a Tbl31 . tbl |
type ' a t = ' a tbl |
let get tbl u = Tbl31 . get tbl ( UChar . uint_code u ) u |
module type Type = sig type elt type t = elt tbl val get : elt tbl -> UChar . t -> elt val of_map : elt -> elt UMap . t -> t end |
module Make ( H : Hashtbl . HashedType ) HashedType = struct module T31 = Tbl31 . Make ( H ) H type elt = T31 . elt type t = H . t tbl let get = get let of_map v m = T31 . of_map v ( UMap . imap_of_umap m ) m end |
module Bool = struct type t = Tbl31 . Bool . t let get tbl u = Tbl31 . Bool . get tbl ( UChar . uint_code u ) u let of_set s = Tbl31 . Bool . of_set ( USet . iset_of_uset s ) s end |
module Bits = struct type t = Tbl31 . Bits . t let get tbl u = Tbl31 . Bits . get tbl ( UChar . uint_code u ) u let of_map v m = Tbl31 . Bits . of_map v ( UMap . imap_of_umap m ) m end |
module Bytes = struct type t = Tbl31 . Bytes . t let get tbl u = Tbl31 . Bytes . get tbl ( UChar . uint_code u ) u let of_map v m = Tbl31 . Bytes . of_map v ( UMap . imap_of_umap m ) m end |
module Char = struct type t = Tbl31 . Char . t let get tbl u = Tbl31 . Char . get tbl ( UChar . uint_code u ) u let of_map v m = Tbl31 . Char . of_map v ( UMap . imap_of_umap m ) m end |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.