text
stringlengths
12
786k
let pp ppf ( Oid ( v1 , v2 , vs ) ) = Format . fprintf ppf " % d . % d % a " v1 v2 ( fun ppf -> List . iter ( Format . fprintf ppf " . % d " ) ) vs
let of_string s = let rec go ic = if Scanf . Scanning . end_of_input ic then [ ] else Scanf . bscanf ic " . % d % r " go ( fun n ns -> n :: ns ) in try Scanf . sscanf s " % d . % d % r " go of_nodes with End_of_file | Scanf . Scan_failure _ -> None
let compare ( Oid ( v1 , v2 , vs ) ) ( Oid ( v1 ' , v2 ' , vs ' ) ) = let rec cmp ( xs : int list ) ys = match ( xs , ys ) with | ( [ ] , [ ] ) -> 0 | ( [ ] , _ ) -> - 1 | ( _ , [ ] ) -> 1 | ( x :: xs , y :: ys ) -> match compare x y with 0 -> cmp xs ys | r -> r in match compare v1 v1 ' with | 0 -> ( match compare v2 v2 ' with 0 -> cmp vs vs ' | r -> r ) | r -> r
let equal o1 o2 = compare o1 o2 = 0
let seeded_hash seed ( Oid ( v1 , v2 , vs ) ) = Hashtbl . ( List . fold_left seeded_hash ( seeded_hash ( seeded_hash seed v1 ) v2 ) vs )
let hash o = seeded_hash 0 o
module type Prim = sig type t val of_cstruct : Cstruct . t -> t val to_writer : t -> Writer . t val random : unit -> t end
module type Prim_s = sig include Prim val random : ? size : int -> unit -> t val concat : t list -> t val length : t -> int end
let rec replicate_l n f = if n < 1 then [ ] else f ( ) :: replicate_l ( pred n ) f
let max_r_int = ( 1 lsl 30 ) - 1
let random_int ( ) = Random . int max_r_int
let random_int_r a b = a + Random . int ( b - a )
let random_size = function | Some size -> size | None -> Random . int 20
let random_string ? size ~ chars ( : lo , hi ) ( ) = String . init ( random_size size ) ( fun _ -> Char . chr ( random_int_r lo hi ) )
module Int64 = struct include Int64 let ( + ) = add and ( - ) = sub and ( * ) = mul and ( / ) = div and ( lsl ) = shift_left and ( lsr ) = shift_right_logical and ( asr ) = shift_right and ( lor ) = logor and ( land ) = logand let max_p_int = Int64 . of_int Stdlib . max_int let to_nat_checked i64 = if i64 < 0L || i64 > max_p_int then None else Some ( to_int i64 ) end
module Boolean : Prim with type t = bool = struct type t = bool let of_cstruct cs = if cs . Cstruct . len = 1 then Cstruct . get_uint8 cs 0 <> 0x00 else parse_error " BOOLEAN : % a " pp_cs cs let to_writer b = Writer . of_byte ( if b then 0xff else 0x00 ) let random = Random . bool end
module Null : Prim with type t = unit = struct type t = unit let of_cstruct cs = if cs . Cstruct . len <> 0 then parse_error " NULL : % a " pp_cs cs let to_writer ( ) = Writer . empty let random ( ) = ( ) end
module Integer : Prim with type t = Z . t = struct type t = Z . t let of_int8 x = Z . of_int ( if x >= 0x80 then x - 0x100 else x ) [ @@ inline ] let of_int16 x = Z . of_int ( if x >= 0x8000 then x - 0x10000 else x ) [ @@ inline ] let of_cstruct cs = let open Cstruct in let rec go acc i = function n when n >= 8 -> let w = Z . of_int64 ( BE . get_uint64 cs i ) in go Z . ( ( acc lsl 64 ) lor ( extract w 0 64 ) ) ( i + 8 ) ( n - 8 ) | n when n >= 4 -> let w = Z . of_int32 ( BE . get_uint32 cs i ) in go Z . ( ( acc lsl 32 ) lor ( extract w 0 32 ) ) ( i + 4 ) ( n - 4 ) | n when n >= 2 -> go Z . ( ( acc lsl 16 ) lor ( ~$ BE . get_uint16 cs i ) ) ( i + 2 ) ( n - 2 ) | 1 -> Z . ( ( acc lsl 8 ) lor ( ~$ get_uint8 cs i ) ) | _ -> acc in match cs . Cstruct . len with 0 -> parse_error " INTEGER : length 0 " | 1 -> of_int8 ( get_uint8 cs 0 ) | n -> let w0 = BE . get_uint16 cs 0 in match w0 land 0xff80 with 0x0000 | 0xff80 -> parse_error " INTEGER : redundant form " | _ -> go ( of_int16 w0 ) 2 ( n - 2 ) let last8 z = Z . ( extract z 0 8 |> to_int ) let to_writer n = let sz = Z . size n * 8 + 1 in let sz1 = sz - 1 in let cs = Cstruct . create sz in let rec write i n = if n = Z . ( ( ~$- 1 ) ) || n = Z . zero then i else ( Cstruct . set_uint8 cs i ( last8 n ) ; write ( pred i ) Z . ( n asr 8 ) ) in let ( bad_b0 , padding ) = if n >= Z . zero then ( ( ) <= 0x80 , 0x00 ) else ( ( ) > 0x80 , 0xff ) in let off = let i = write sz1 n in if i = sz1 || bad_b0 ( Cstruct . get_uint8 cs ( succ i ) ) then ( Cstruct . set_uint8 cs i padding ; i ) else succ i in Writer . of_cstruct Cstruct . ( sub cs off ( sz - off ) ) let random ( ) = Z . of_int ( Random . int max_r_int - max_r_int / 2 ) end
module Gen_string : Prim_s with type t = string = struct type t = string let of_cstruct x = Cstruct . to_string x let to_writer = Writer . of_string let random ? size ( ) = random_string ? size ~ chars ( : 32 , 127 ) ( ) let ( concat , length ) = String . ( concat " " , length ) end
module Octets : Prim_s with type t = Cstruct . t = struct type t = Cstruct . t let of_cstruct { Cstruct . buffer ; off ; len } = Cstruct . of_bigarray @@ Bigarray . Array1 . sub buffer off len let to_writer = Writer . of_cstruct let random ? size ( ) = random_string ? size ~ chars ( : 0 , 256 ) ( ) |> Cstruct . of_string let concat = Cstruct . concat let length = Cstruct . length end
module Bits : sig include Prim_s with type t = bits val to_array : t -> bool array val of_array : bool array -> t struct type t = int * Cstruct . t let of_cstruct cs = let n = Cstruct . length cs in if n = 0 then parse_error " BITS " else let unused = Cstruct . get_uint8 cs 0 in if n = 1 && unused > 0 || unused > 7 then parse_error " BITS " else unused , Octets . of_cstruct ( Cstruct . shift cs 1 ) let to_writer ( unused , cs ) = let size = Cstruct . length cs in let write off cs ' = Cstruct . set_uint8 cs ' off unused ; Cstruct . blit cs 0 cs ' ( off + 1 ) size in Writer . immediate ( size + 1 ) write let to_array ( unused , cs ) = Array . init ( Cstruct . length cs * 8 - unused ) @@ fun i -> let byte = ( Cstruct . get_uint8 cs ( i / 8 ) ) lsl ( i mod 8 ) in byte land 0x80 = 0x80 let ( ) |< n = function | true -> ( n lsl 1 ) lor 1 | false -> ( n lsl 1 ) let of_array arr = let cs = Cstruct . create ( ( Array . length arr + 7 ) / 8 ) in match Array . fold_left ( fun ( n , acc , i ) bit -> if n = 8 then ( Cstruct . set_uint8 cs i acc ; ( 1 , 0 |< bit , i + 1 ) ) else ( n + 1 , acc |< bit , i ) ) ( 0 , 0 , 0 ) arr with | ( 0 , _acc , _ ) -> ( 0 , cs ) | ( n , acc , i ) -> Cstruct . set_uint8 cs i ( acc lsl ( 8 - n ) ) ; ( 8 - n , cs ) let random ? size ( ) = ( 0 , Octets . random ? size ( ) ) let concat css = let ( unused , css ' ) = let rec go = function | [ ] -> ( 0 , [ ] ) | [ ( u , cs ) ] -> ( u , [ cs ] ) | ( _ , cs ) :: ucs -> let ( u , css ' ) = go ucs in ( u , cs :: css ' ) in go css in ( unused , Cstruct . concat css ' ) and length ( unused , cs ) = Cstruct . length cs - unused end
module OID = struct open Asn_oid let uint64_chain cs i n = let rec go acc cs i = function 0 -> parse_error " OID : unterminated component " | n -> match Cstruct . get_uint8 cs i with 0x80 when acc = 0L -> parse_error " OID : redundant form " | b -> let lo = b land 0x7f in let acc = Int64 . ( logor ( shift_left acc 7 ) ( of_int lo ) ) in if b < 0x80 then ( acc , i + 1 ) else go acc cs ( i + 1 ) ( n - 1 ) in if n < 1 then parse_error " OID : 0 length component " else go 0L cs i ( min n 8 ) let int_chain cs i n = let ( n , i ) = uint64_chain cs i n in match Int64 . to_nat_checked n with Some n -> ( n , i ) | _ -> parse_error " OID : component out of range " let of_cstruct cs = let rec components cs i = function 0 -> [ ] | n -> let ( c , i ' ) = int_chain cs i n in c :: components cs i ' ( n + i - i ' ) in match Cstruct . length cs with 0 -> parse_error " OID : 0 length " | n -> let ( b1 , i ) = int_chain cs 0 n in let v1 = b1 / 40 and v2 = b1 mod 40 in match base_opt v1 v2 with Some b -> b <|| components cs i ( n - i ) | None -> parse_error " OID : invalid base " let to_writer = fun ( Oid ( v1 , v2 , vs ) ) -> let cons x = function [ ] -> [ x ] | xs -> x lor 0x80 :: xs in let rec component xs x = if x < 0x80 then cons x xs else component ( cons ( x land 0x7f ) xs ) ( x lsr 7 ) and values = function | [ ] -> Writer . empty | v :: vs -> Writer . ( of_list ( component [ ] v ) <+> values vs ) in Writer . ( of_byte ( v1 * 40 + v2 ) <+> values vs ) let random ( ) = Random . ( base ( int 3 ) ( int 40 ) <|| replicate_l ( int 10 ) random_int ) end
module Time = struct let ps_per_ms = 1_000_000_000L let pp_tz ppf = function | 0 -> pf ppf " Z " | tz -> pf ppf " % c % 02d % 02d " ( if tz < 0 then ' ' + else ' ' ) - ( abs tz / 3600 ) ( ( abs tz mod 3600 ) / 60 ) let pp_utc_time ppf t = let ( ( y , m , d ) , ( ( hh , mm , ss ) , tz ) ) = Ptime . to_date_time ~ tz_offset_s : 0 t in pf ppf " % 02d % 02d % 02d % 02d % 02d % 02d % a " ( y mod 100 ) m d hh mm ss pp_tz tz let pp_gen_time ppf t = let ( ( y , m , d ) , ( ( hh , mm , ss ) , tz ) ) = Ptime . to_date_time ~ tz_offset_s : 0 t in let pp_frac ppf t = match Ptime . ( frac_s t |> Span . to_d_ps ) with | ( _ , 0L ) -> ( ) | ( _ , f ) -> pf ppf " . % 03Ld " Int64 . ( f / ps_per_ms ) in pf ppf " % 04d % 02d % 02d % 02d % 02d % 02d % a % a " y m d hh mm ss pp_frac t pp_tz tz let of_utc_time = Format . asprintf " % a " pp_utc_time and of_gen_time = Format . asprintf " % a " pp_gen_time let catch pname f s = try f s with | End_of_file -> parse_error " % s : unexpected end : % s " pname s | Scanf . Scan_failure _ -> parse_error " % s : invalid format : % s " pname s let tz ic = try Scanf . bscanf ic " % 1 [ ] +-% 2u % 2u " %! @@ fun sgn h m -> ( match sgn with " " - -> - 1 | _ -> 1 ) * ( 3600 * h + 60 * m ) with _ -> Scanf . bscanf ic " Z " 0 let utc_time_of_string = catch " UTCTime " @@ fun s -> Scanf . sscanf s " % 2u % 2u % 2u % 2u % 2u % r % r " %! ( fun ic -> try Scanf . bscanf ic " % 2u " id with _ -> 0 ) tz @@ fun y m d hh mm ss tz -> let y = ( if y > 50 then 1900 else 2000 ) + y in let dt = ( ( y , m , d ) , ( ( hh , mm , ss ) , tz ) ) in match Ptime . of_date_time dt with Some t -> t | _ -> parse_error " UTCTime : out of range : % s " s let gen_time_of_string = catch " GeneralizedTime " @@ fun s -> let m_s_f ic = try Scanf . bscanf ic " % 2u % r " ( fun ic -> try Scanf . bscanf ic " % 2u % r " ( fun ic -> try Scanf . bscanf ic " . % 3u " @@ fun ms -> Int64 . ( of_int ms * ps_per_ms ) with _ -> 0L ) @@ fun ss ms -> ss , ms with _ -> 0 , 0L ) @@ fun mm ssms -> mm , ssms with _ -> 0 , ( 0 , 0L ) in Scanf . sscanf s " % 4u % 2u % 2u % 2u % r % r " %! m_s_f ( fun ic -> try tz ic with _ -> 0 ) @@ fun y m d hh ( mm , ( ss , ps ) ) tz -> let dt = ( ( y , m , d ) , ( ( hh , mm , ss ) , tz ) ) in match match Ptime . of_date_time dt with Some t -> Ptime . ( Span . v ( 0 , ps ) |> add_span t ) | _ -> None with Some t -> t | _ -> parse_error " GeneralizedTime : out of range : % s " s let get = function Some x -> x | _ -> assert false let date y m d = Ptime . of_date ( y , m , d ) |> get let r_date ~ start ~ fin = let dd , dps = match Ptime . ( diff fin start |> Span . to_d_ps ) with | ( dd , 0L ) -> Random . ( int dd , int64 86_400_000_000_000_000L ) | ( dd , dps ) -> Random . ( int ( dd + 1 ) , int64 dps ) in Ptime . ( Span . ( v Random . ( int ( dd + 1 ) , int64 dps ) ) |> add_span start ) |> get let random ( ? frac = false ) ( ) = Ptime . truncate ~ frac_s ( : if frac then 3 else 0 ) @@ r_date ~ start ( : date 1970 1 1 ) ~ fin ( : date 2050 12 31 ) end
let replicate n f a = let rec loop acc n = if n <= 0 then acc else loop ( f a :: acc ) ( pred n ) in loop [ ] n
let r_prim : type a . a prim -> a = function | Bool -> Boolean . random ( ) | Int -> Integer . random ( ) | Bits -> Bits . random ( ) | Octets -> Octets . random ( ) | Null -> ( ) | OID -> OID . random ( ) | CharString -> Gen_string . random ( )
let rec r_element : type a . a element -> a = function | Required ( _ , asn ) -> r_asn asn | Optional ( _ , asn ) -> if Random . int 3 = 0 then None else Some ( r_asn asn ) | Last e -> r_element e | Pair ( e , es ) -> ( r_element e , r_seq es ) replicate Random . ( int 10 ) r_asn asn | Iso ( f , _ , None , asn ) -> f @@ r_asn asn | Iso ( _ , _ , Some rnd , _ ) -> rnd ( ) | Fix ( f , _ ) as fix -> r_asn ( f fix ) | Sequence asns -> r_seq asns | Set asns -> r_seq asns | Sequence_of asn -> r_seq_of asn | Set_of asn -> r_seq_of asn | Choice ( asn1 , asn2 ) -> if Random . bool ( ) then L ( r_asn asn1 ) else R ( r_asn asn2 ) | Implicit ( _ , asn ) -> r_asn asn | Explicit ( _ , asn ) -> r_asn asn | Prim p -> r_prim p
let cs_lex_compare cs1 cs2 = let ( s1 , s2 ) = Cstruct . ( length cs1 , length cs2 ) in let rec go i lim = if i = lim then compare s1 s2 else match compare ( Cstruct . get_uint8 cs1 i ) ( Cstruct . get_uint8 cs2 i ) with | 0 -> go ( succ i ) lim | n -> n in go 0 ( min s1 s2 )
type t = int * ( int -> Cstruct . t -> unit )
let immediate n f = ( n , f )
let len ( n , _ ) = n
let empty = ( 0 , ( fun _ _ -> ( ) ) )
let ( ) <+> ( l1 , w1 ) ( l2 , w2 ) = let w off buf = ( w1 off buf ; w2 ( off + l1 ) buf ) in ( l1 + l2 , w )
let rec concat = function | [ ] -> empty | w :: ws -> w <+> concat ws
let of_list lst = let open List in let w off cs = iteri ( fun i -> Cstruct . set_uint8 cs ( off + i ) ) lst in ( length lst , w )
let of_string str = let n = String . length str in ( n , fun off cs -> Cstruct . blit_from_string str 0 cs off n )
let of_cstruct cs ' = let n = Cstruct . length cs ' in ( n , fun off cs -> Cstruct . blit cs ' 0 cs off n )
let of_byte b = ( 1 , fun off cs -> Cstruct . set_uint8 cs off b )
let to_cstruct ( n , w ) = let cs = Cstruct . create n in ( w 0 cs ; cs )
let to_writer ( n , w ) = ( n , fun cs -> w 0 cs )
type result = { abbrev_table : Abbreviations_table . t ; dies : Debugging_information_entry . t list ; compilation_unit_die : Debugging_information_entry . t option ; dwarf_4_location_lists : Dwarf_4_location_list . t list }
let run ~ proto_die_root = let abbrev_table , dies_rev , compilation_unit_die , location_lists_rev = let next_abbreviation_code = ref 1 in Proto_die . depth_first_fold proto_die_root ~ init ( : Abbreviations_table . create ( ) , [ ] , None , [ ] ) ~ f : ( fun ( abbrev_table , dies , compilation_unit_die , location_lists_rev ) ( action : Proto_die . fold_arg ) -> let abbrev_table , die , compilation_unit_die , location_lists_rev = match action with | End_of_siblings -> ( abbrev_table , DIE . create_null ( ) , compilation_unit_die , location_lists_rev ) | DIE { tag ; has_children ; attribute_values ; label ; name ; location_list_in_debug_loc_table } -> let attribute_specs = ASS . Map . keys attribute_values in let abbrev_table , abbreviation_code = match Abbreviations_table . find abbrev_table ~ tag ~ has_children ~ attribute_specs with | Some abbrev_code -> abbrev_table , abbrev_code | None -> let abbreviation_code = Abbreviation_code . of_int ! next_abbreviation_code tag in incr next_abbreviation_code ; let abbrev_table_entry = Abbreviations_table_entry . create ~ abbreviation_code ~ tag ~ has_children ~ attribute_specs in ( Abbreviations_table . add abbrev_table abbrev_table_entry , abbreviation_code ) in let die = DIE . create ~ label ~ abbreviation_code ~ attribute_values ~ name in let compilation_unit_die = let is_compilation_unit = match tag with Compile_unit -> true | _ -> false in if not is_compilation_unit then compilation_unit_die else match compilation_unit_die with | None -> Some die | Some _ -> Misc . fatal_error " More than one ` Compile_unit ' DIE is present " in let location_lists_rev = match location_list_in_debug_loc_table with | None -> location_lists_rev | Some location_list -> location_list :: location_lists_rev in abbrev_table , die , compilation_unit_die , location_lists_rev in abbrev_table , die :: dies , compilation_unit_die , location_lists_rev ) in { abbrev_table ; dies = List . rev dies_rev ; compilation_unit_die ; dwarf_4_location_lists = List . rev location_lists_rev }
type identifier = string | Module of ' a stmt list * ' a | Interactive of ' a stmt list * ' a | Expression of ' a expr * ' a | Suite of ' a stmt list * ' a | FunctionDef of identifier * ' a arguments * ' a stmt list * ' a expr list * ' a | ClassDef of identifier * ' a expr list * ' a stmt list * ' a expr list * ' a | Return of ' a expr option * ' a | Delete of ' a expr list * ' a | Assign of ' a expr list * ' a expr * ' a | AugAssign of ' a expr * operator * ' a expr * ' a | Print of ' a expr option * ' a expr list * bool * ' a | For of ' a expr * ' a expr * ' a stmt list * ' a stmt list * ' a | While of ' a expr * ' a stmt list * ' a stmt list * ' a | If of ' a expr * ' a stmt list * ' a stmt list * ' a | With of ' a expr * ' a expr option * ' a stmt list * ' a | Raise of ' a expr option * ' a expr option * ' a expr option * ' a | TryExcept of ' a stmt list * ' a excepthandler list * ' a stmt list * ' a | TryFinally of ' a stmt list * ' a stmt list * ' a | Assert of ' a expr * ' a expr option * ' a | Import of alias list * ' a | ImportFrom of identifier * alias list * int option * ' a | Exec of ' a expr * ' a expr option * ' a expr option * ' a | Global of identifier list * ' a | Expr of ' a expr * ' a | Pass of ' a | Break of ' a | Continue of ' a | BoolOp of boolop * ' a expr list * ' a | BinOp of ' a expr * operator * ' a expr * ' a | UnaryOp of unaryop * ' a expr * ' a | Lambda of ' a arguments * ' a expr * ' a | IfExp of ' a expr * ' a expr * ' a expr * ' a | Dict of ' a expr list * ' a expr list * ' a | ListComp of ' a expr * ' a comprehension list * ' a | GeneratorExp of ' a expr * ' a comprehension list * ' a | Yield of ' a expr option * ' a | Compare of ' a expr * cmpop list * ' a expr list * ' a | Call of ' a expr * ' a expr list * ' a keyword list * ' a expr option * ' a expr option * ' a | Repr of ' a expr * ' a | Num of number * ' a | Str of string * ' a | Attribute of ' a expr * identifier * expr_context * ' a | Subscript of ' a expr * ' a slice * expr_context * ' a | Name of identifier * expr_context * ' a | List of ' a expr list * expr_context * ' a | Tuple of ' a expr list * expr_context * ' a | Ellipsis | Slice of ' a expr option * ' a expr option * ' a expr option | ExtSlice of ' a slice list | Index of ' a expr | RShift | BitOr | BitXor | BitAnd | FloorDiv | Int of int | LongInt of int | Float of float | Imag of string
let name_of_mod = function | Module _ -> " Module " | Interactive _ -> " Interactive " | Expression _ -> " Expression " | Suite _ -> " Suite " | FunctionDef _ -> " FunctionDef " | ClassDef _ -> " ClassDef " | Return _ -> " Return " | Delete _ -> " Delete " | Assign _ -> " Assign " | AugAssign _ -> " AugAssign " | Print _ -> " Print " | For _ -> " For " | While _ -> " While " | If _ -> " If " | With _ -> " With " | Raise _ -> " Raise " | TryExcept _ -> " TryExcept " | TryFinally _ -> " TryFinally " | Assert _ -> " Assert " | Import _ -> " Import " | ImportFrom _ -> " ImportFrom " | Exec _ -> " Exec " | Global _ -> " Global " | Expr _ -> " Expr " | Pass _ -> " Pass " | Break _ -> " Break " | Continue _ -> " Continue " | BoolOp _ -> " BoolOp " | BinOp _ -> " BinOp " | UnaryOp _ -> " UnaryOp " | Lambda _ -> " Lambda " | IfExp _ -> " IfExp " | Dict _ -> " Dict " | ListComp _ -> " ListComp " | GeneratorExp _ -> " GeneratorExp " | Yield _ -> " Yield " | Compare _ -> " Compare " | Call _ -> " Call " | Repr _ -> " Repr " | Num _ -> " Num " | Str _ -> " Str " | Attribute _ -> " Attribute " | Subscript _ -> " Subscript " | Name _ -> " Name " | List _ -> " List " | Tuple _ -> " Tuple " | Load -> " Load " | Store -> " Store " | Del -> " Del " | AugLoad -> " AugLoad " | AugStore -> " AugStore " | Param -> " Param " | Ellipsis -> " Ellipsis " | Slice _ -> " Slice " | ExtSlice _ -> " ExtSlice " | Index _ -> " Index " | And -> " And " | Or -> " Or " | Add -> " Add " | Sub -> " Sub " | Mult -> " Mult " | Div -> " Div " | Mod -> " Mod " | Pow -> " Pow " | LShift -> " LShift " | RShift -> " RShift " | BitOr -> " BitOr " | BitXor -> " BitXor " | BitAnd -> " BitAnd " | FloorDiv -> " FloorDiv " | Invert -> " Insert " | Not -> " Not " | UAdd -> " UAdd " | USub -> " USub " | Eq -> " Eq " | NotEq -> " NotEq " | Lt -> " Lt " | LtE -> " LtE " | Gt -> " Gt " | GtE -> " GtE " | Is -> " Is " | IsNot -> " IsNot " | In -> " In " | NotIn -> " NotIn " | ExceptHandler _ -> " ExceptHandler " | Int _ -> " Int " | LongInt _ -> " LongInt " | Float _ -> " Float " | Imag _ -> " Imag "
let annot_of_mod = function | Module ( _ , a ) | Interactive ( _ , a ) | Expression ( _ , a ) | Suite ( _ , a ) -> a | FunctionDef ( _ , _ , _ , _ , a ) | ClassDef ( _ , _ , _ , _ , a ) | Return ( _ , a ) | Delete ( _ , a ) | Assign ( _ , _ , a ) | AugAssign ( _ , _ , _ , a ) | Print ( _ , _ , _ , a ) | For ( _ , _ , _ , _ , a ) | While ( _ , _ , _ , a ) | If ( _ , _ , _ , a ) | With ( _ , _ , _ , a ) | Raise ( _ , _ , _ , a ) | TryExcept ( _ , _ , _ , a ) | TryFinally ( _ , _ , a ) | Assert ( _ , _ , a ) | Import ( _ , a ) | ImportFrom ( _ , _ , _ , a ) | Exec ( _ , _ , _ , a ) | Global ( _ , a ) | Expr ( _ , a ) | Pass ( a ) | Break ( a ) | Continue ( a ) -> a | BoolOp ( _ , _ , a ) | BinOp ( _ , _ , _ , a ) | UnaryOp ( _ , _ , a ) | Lambda ( _ , _ , a ) | IfExp ( _ , _ , _ , a ) | Dict ( _ , _ , a ) | ListComp ( _ , _ , a ) | GeneratorExp ( _ , _ , a ) | Yield ( _ , a ) | Compare ( _ , _ , _ , a ) | Call ( _ , _ , _ , _ , _ , a ) | Repr ( _ , a ) | Num ( _ , a ) | Str ( _ , a ) | Attribute ( _ , _ , _ , a ) | Subscript ( _ , _ , _ , a ) | Name ( _ , _ , a ) | List ( _ , _ , a ) | Tuple ( _ , _ , a ) -> a | ExceptHandler ( _ , _ , _ , a ) -> a
let context_of_expr = function | Attribute ( _ , _ , ctx , _ ) -> Some ctx | Subscript ( _ , _ , ctx , _ ) -> Some ctx | Name ( _ , ctx , _ ) -> Some ctx | List ( _ , ctx , _ ) -> Some ctx | Tuple ( _ , ctx , _ ) -> Some ctx | _ -> None
let string_of_boolop = function | And -> " and " | Or -> " or "
let string_of_operator = function | Add -> " " + | Sub -> " " - | Mult -> " " * | Div -> " " / | Mod -> " " % | Pow -> " " ** | LShift -> " " << | RShift -> " " >> | BitOr -> " " | | BitXor -> " " ^ | BitAnd -> " " & | FloorDiv -> " " //
let string_of_unaryop = function | Invert -> " " ~ | Not -> " not " | UAdd -> " " + | USub -> " " -
let string_of_cmpop = function | Eq -> " " == | NotEq -> " " != | Lt -> " " < | LtE -> " " <= | Gt -> " " > | GtE -> " " >= | Is -> " is " | IsNot -> " is not " | In -> " in " | NotIn -> " not in "
let string_of_number = function | Int ( n ) -> string_of_int n | LongInt ( n ) -> ( string_of_int n ) ^ " L " | Float ( n ) -> ( string_of_float n ) | Imag ( n ) -> n
module type Annot = sig type t val of_pos : Lexing . position -> t end
module Pos : Annot = struct type t = Lexing . position let of_pos pos = pos end
let err_empty_string = " the string is empty "
let err_empty_sep = " ~ sep is an empty string "
let err_neg_max max = strf " negative ~ max ( % d ) " max
let err_neg_min max = strf " negative ~ min ( % d ) " max
let err_neg_len len = strf " negative length ( % d ) " len
let err_max_string_len = " Sys . max_string_length exceeded "
let for_all sat s ~ first ~ last = let rec loop i = if i > last then true else if sat ( string_unsafe_get s i ) then loop ( i + 1 ) else false in loop first
let exists sat s ~ first ~ last = let rec loop i = if i > last then false else if sat ( string_unsafe_get s i ) then true else loop ( i + 1 ) in loop first
let fold_left f acc s ~ first ~ last = let rec loop acc i = if i > last then acc else loop ( f acc ( string_unsafe_get s i ) ) ( i + 1 ) in loop acc first
let fold_right f s acc ~ first ~ last = let rec loop i acc = if i < first then acc else loop ( i - 1 ) ( f ( string_unsafe_get s i ) acc ) in loop last acc
let of_char c = let b = Bytes . create 1 in bytes_unsafe_set b 0 c ; bytes_unsafe_to_string b
let to_char s = match string_length s with
let to_bool s = try Some ( bool_of_string s ) with Invalid_argument _ -> None
let to_int s = try Some ( int_of_string s ) with Failure _ -> None
let to_nativeint s = try Some ( Nativeint . of_string s ) with Failure _ -> None
let to_int32 s = try Some ( Int32 . of_string s ) with Failure _ -> None
let to_int64 s = try Some ( Int64 . of_string s ) with Failure _ -> None
let to_float s = try Some ( float_of_string s ) with Failure _ -> None
let err_byte b = Printf . sprintf " % d is not a byte " b
let of_byte b = if b < 0 || b > 255 then invalid_arg ( err_byte b ) else unsafe_of_byte b
let of_int b = if b < 0 || b > 255 then None else ( Some ( unsafe_of_byte b ) )
let hash c = Hashtbl . hash c
let equal : t -> t -> bool = fun c0 c1 -> c0 = c1
let compare : t -> t -> int = fun c0 c1 -> compare c0 c1
module Ascii = struct let max_ascii = ' \ x7F ' let is_valid : t -> bool = fun c -> c <= max_ascii let is_digit = function ' 0 ' . . ' 9 ' -> true | _ -> false let is_hex_digit = function | ' 0 ' . . ' 9 ' | ' A ' . . ' F ' | ' a ' . . ' f ' -> true | _ -> false let is_upper = function ' A ' . . ' Z ' -> true | _ -> false let is_lower = function ' a ' . . ' z ' -> true | _ -> false let is_letter = function ' A ' . . ' Z ' | ' a ' . . ' z ' -> true | _ -> false let is_alphanum = function | ' 0 ' . . ' 9 ' | ' A ' . . ' Z ' | ' a ' . . ' z ' -> true | _ -> false let is_white = function ' ' | ' \ t ' . . ' \ r ' -> true | _ -> false let is_blank = function ' ' | ' \ t ' -> true | _ -> false let is_graphic = function ' ' ! . . ' ' ~ -> true | _ -> false let is_print = function ' ' . . ' ' ~ -> true | _ -> false let is_control = function ' \ x00 ' . . ' \ x1F ' | ' \ x7F ' -> true | _ -> false let uppercase = function | ' a ' . . ' z ' as c -> unsafe_of_byte @@ to_int c - 0x20 | c -> c let lowercase = function | ' A ' . . ' Z ' as c -> unsafe_of_byte @@ to_int c + 0x20 | c -> c let escape = Astring_escape . char_escape let escape_char = Astring_escape . char_escape_char end
let dump ppf c = Format . pp_print_char ppf ' ' ' ; \ Format . pp_print_string ppf ( Ascii . escape_char c ) ; Format . pp_print_char ppf ' ' ' ; \ ( )
let hex_escape b k c = let byte = char_to_byte c in let hi = byte / 16 in let lo = byte mod 16 in bytes_unsafe_set b ( k ) ' ' ; \\ bytes_unsafe_set b ( k + 1 ) ' x ' ; bytes_unsafe_set b ( k + 2 ) ( array_unsafe_get hex_digit hi ) ; bytes_unsafe_set b ( k + 3 ) ( array_unsafe_get hex_digit lo ) ; ( )
let letter_escape b k letter = bytes_unsafe_set b ( k ) ' ' ; \\ bytes_unsafe_set b ( k + 1 ) letter ; ( )
let char_escape = function let b = Bytes . create 1 in bytes_unsafe_set b 0 c ; bytes_unsafe_to_string b let b = Bytes . create 4 in hex_escape b 0 c ; bytes_unsafe_to_string b
let char_escape_char = function let b = Bytes . create 1 in bytes_unsafe_set b 0 c ; bytes_unsafe_to_string b let b = Bytes . create 4 in hex_escape b 0 c ; bytes_unsafe_to_string b
let escape s = let max_idx = string_length s - 1 in let rec escaped_len i l = if i > max_idx then l else match string_unsafe_get s i with | ' ' \\ -> escaped_len ( i + 1 ) ( l + 2 ) | ' \ x20 ' . . ' \ x7E ' -> escaped_len ( i + 1 ) ( l + 1 ) | _ -> escaped_len ( i + 1 ) ( l + 4 ) in let escaped_len = escaped_len 0 0 in if escaped_len = string_length s then s else let b = Bytes . create escaped_len in let rec loop i k = if i > max_idx then bytes_unsafe_to_string b else match string_unsafe_get s i with | ' ' \\ -> letter_escape b k ' ' ; \\ loop ( i + 1 ) ( k + 2 ) | ' \ x20 ' . . ' \ x7E ' as c -> bytes_unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) | c -> hex_escape b k c ; loop ( i + 1 ) ( k + 4 ) in loop 0 0
let escape_string s = let max_idx = string_length s - 1 in let rec escaped_len i l = if i > max_idx then l else match string_unsafe_get s i with | ' \ b ' | ' \ t ' | ' \ n ' | ' \ r ' | ' " ' \ | ' ' \\ -> escaped_len ( i + 1 ) ( l + 2 ) | ' \ x20 ' . . ' \ x7E ' -> escaped_len ( i + 1 ) ( l + 1 ) | _ -> escaped_len ( i + 1 ) ( l + 4 ) in let escaped_len = escaped_len 0 0 in if escaped_len = string_length s then s else let b = Bytes . create escaped_len in let rec loop i k = if i > max_idx then bytes_unsafe_to_string b else match string_unsafe_get s i with | ' \ b ' -> letter_escape b k ' b ' ; loop ( i + 1 ) ( k + 2 ) | ' \ t ' -> letter_escape b k ' t ' ; loop ( i + 1 ) ( k + 2 ) | ' \ n ' -> letter_escape b k ' n ' ; loop ( i + 1 ) ( k + 2 ) | ' \ r ' -> letter_escape b k ' r ' ; loop ( i + 1 ) ( k + 2 ) | ' " ' \ -> letter_escape b k ' " ' ; loop ( i + 1 ) ( k + 2 ) | ' ' \\ -> letter_escape b k ' ' ; \\ loop ( i + 1 ) ( k + 2 ) | ' \ x20 ' . . ' \ x7E ' as c -> bytes_unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) | c -> hex_escape b k c ; loop ( i + 1 ) ( k + 4 ) in loop 0 0
let unescaped_len ~ ocaml s = let max_idx = string_length s - 1 in let rec loop i l = if i > max_idx then Some l else if string_unsafe_get s i <> ' ' \\ then loop ( i + 1 ) ( l + 1 ) else let i = i + 1 in if i > max_idx then None else match string_unsafe_get s i with | ' ' \\ -> loop ( i + 1 ) ( l + 1 ) | ' x ' -> let i = i + 2 in if i > max_idx then None else if not ( is_hex_digit ( string_unsafe_get s ( i - 1 ) ) && is_hex_digit ( string_unsafe_get s ( i ) ) ) then None else loop ( i + 1 ) ( l + 1 ) | ( ' b ' | ' t ' | ' n ' | ' r ' | ' " ' | ' ' ' ) \ when ocaml -> loop ( i + 1 ) ( l + 1 ) | c -> None in loop 0 0
let _unescape ~ ocaml s = match unescaped_len ~ ocaml s with let b = Bytes . create l in let max_idx = string_length s - 1 in let rec loop i k = if i > max_idx then Some ( bytes_unsafe_to_string b ) else let c = string_unsafe_get s i in if c <> ' ' \\ then ( bytes_unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) ) else let i = i + 1 in match string_unsafe_get s i with | ' ' \\ -> bytes_unsafe_set b k ' ' ; \\ loop ( i + 1 ) ( k + 1 ) | ' x ' -> let i = i + 2 in let hi = hex_value @@ string_unsafe_get s ( i - 1 ) in let lo = hex_value @@ string_unsafe_get s ( i ) in let c = char_unsafe_of_byte @@ ( hi lsl 4 ) + lo in bytes_unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) | ' b ' -> bytes_unsafe_set b k ' \ b ' ; loop ( i + 1 ) ( k + 1 ) | ' t ' -> bytes_unsafe_set b k ' \ t ' ; loop ( i + 1 ) ( k + 1 ) | ' n ' -> bytes_unsafe_set b k ' \ n ' ; loop ( i + 1 ) ( k + 1 ) | ' r ' -> bytes_unsafe_set b k ' \ r ' ; loop ( i + 1 ) ( k + 1 ) | ' " ' -> bytes_unsafe_set b k ' " ' ; \ loop ( i + 1 ) ( k + 1 ) | ' ' ' \ -> bytes_unsafe_set b k ' ' ' ; \ loop ( i + 1 ) ( k + 1 ) | c -> assert false in loop 0 0
let unescape s = _unescape ~ ocaml : false s
let unescape_string s = _unescape ~ ocaml : true s
let v ~ len f = let b = Bytes . create len in for i = 0 to len - 1 do bytes_unsafe_set b i ( f i ) done ; bytes_unsafe_to_string b
let get_byte s i = char_to_byte ( get s i )
let unsafe_get_byte s i = char_to_byte ( unsafe_get s i )
let head ( ? rev = false ) s = let len = length s in if len = 0 then None else Some ( string_unsafe_get s ( if rev then len - 1 else 0 ) )
let get_head ( ? rev = false ) s = let len = length s in if len = 0 then invalid_arg Astring_base . err_empty_string else string_unsafe_get s ( if rev then len - 1 else 0 )
let hash c = Hashtbl . hash c
let append s0 s1 = let l0 = length s0 in if l0 = 0 then s1 else let l1 = length s1 in if l1 = 0 then s0 else let b = Bytes . create ( l0 + l1 ) in bytes_unsafe_blit_string s0 0 b 0 l0 ; bytes_unsafe_blit_string s1 0 b l0 l1 ; bytes_unsafe_to_string b
let concat ( ? sep = empty ) = function let s_len = length s in let sep_len = length sep in let rec cat_len sep_count l ss = if l < 0 then l else match ss with | s :: ss -> cat_len ( sep_count + 1 ) ( l + length s ) ss | [ ] -> if sep_len = 0 then l else let max_sep_count = Sys . max_string_length / sep_len in if sep_count < 0 || sep_count > max_sep_count then - 1 else sep_count * sep_len + l in let cat_len = cat_len 0 s_len ss in if cat_len < 0 then invalid_arg Astring_base . err_max_string_len else let b = Bytes . create cat_len in bytes_unsafe_blit_string s 0 b 0 s_len ; let rec loop i = function | [ ] -> bytes_unsafe_to_string b | str :: ss -> let sep_first = i in let str_first = i + sep_len in let str_len = length str in bytes_unsafe_blit_string sep 0 b sep_first sep_len ; bytes_unsafe_blit_string str 0 b str_first str_len ; loop ( str_first + str_len ) ss in loop s_len ss
let is_empty s = length s = 0
let is_prefix ~ affix s = let len_a = length affix in let len_s = length s in if len_a > len_s then false else let max_idx_a = len_a - 1 in let rec loop i = if i > max_idx_a then true else if unsafe_get affix i <> unsafe_get s i then false else loop ( i + 1 ) in loop 0
let is_infix ~ affix s = let len_a = length affix in let len_s = length s in if len_a > len_s then false else let max_idx_a = len_a - 1 in let max_idx_s = len_s - len_a in let rec loop i k = if i > max_idx_s then false else if k > max_idx_a then true else if k > 0 then if unsafe_get affix k = unsafe_get s ( i + k ) then loop i ( k + 1 ) else loop ( i + 1 ) 0 else if unsafe_get affix 0 = unsafe_get s i then loop i 1 else loop ( i + 1 ) 0 in loop 0 0
let is_suffix ~ affix s = let max_idx_a = length affix - 1 in let max_idx_s = length s - 1 in if max_idx_a > max_idx_s then false else let rec loop i = if i > max_idx_a then true else if unsafe_get affix ( max_idx_a - i ) <> unsafe_get s ( max_idx_s - i ) then false else loop ( i + 1 ) in loop 0