text
stringlengths
0
601k
let rec bindings_aux accu = function Empty -> accu | Node ( l , v , d , r , _ ) -> bindings_aux ( ( v , d ) :: bindings_aux accu r ) l
let bindings s = bindings_aux [ ] s
type position = [ ` Length | ` Position of int | ` End ]
type value = [ ` Int of int | ` Char of char ]
type ' a tree = [ ` Node of ( position * ( value * ' a tree ) list ) | ` Branch of ( ( position * value ) list * ' a tree ) | ` Leaf of ' a ]
let group_by f l = let tbl = Hashtbl . create 20 in List . iter ( fun x -> let k = f x in let r = try Hashtbl . find tbl k with Not_found -> let r = ref [ ] in Hashtbl . add tbl k r ; r in r := x :: ! r ) l ; let l = Hashtbl . fold ( fun k r l -> ( k , List . rev ! r ) :: l ) tbl [ ] in List . sort ( fun ( k1 , _ ) ( k...
let rec finish s pos = match pos with ` End -> [ ] | ` Length -> ( ` Length , ` Int ( String . length s ) ) :: finish s ( ` Position 0 ) | ` Position i -> if i < String . length s then ( pos , ` Char s . [ i ] ) :: finish s ( ` Position ( i + 1 ) ) else finish s ` End
let make_end_branch s pos x = match finish s pos with [ ] -> ` Leaf x | l -> ` Branch ( l , ` Leaf x )
let rec make_branches ( x : ' a tree ) : ' a tree = match x with ` Leaf _ -> x | ` Branch ( l , x ) -> ( match make_branches x with ` Branch ( l2 , x2 ) -> ` Branch ( ( l @ l2 ) , x2 ) | x -> ` Branch ( l , x ) ) | ` Node ( pos , [ value , x ] ) -> ( match make_branches x with ` Branch ( l2 , x2 ) -> ` Branch ( ( ( pos...
let make_initial_tree l : ' a tree = let rec aux i = function [ ] -> assert false | [ ( s , x ) ] -> let pos = if i < String . length s then ` Position i else ` End in make_end_branch s pos x | ( ( s , _ ) :: _ ) as l -> if i < String . length s then let groups = group_by ( fun ( s , _ ) -> ` Char s . [ i ] ) l in ` No...
let make_tree l = make_branches ( make_initial_tree l )
let test ( ) = let l = [ " abcdeg " ; " abcdef " ; " abdefh " ; " bcd " ; " " ; ] in make_tree ( List . map ( fun s -> ( s , s ) ) l )
let get_value string_id pos_id pos = match pos with ` Length -> " len " | ` Position i -> if i = 0 then sprintf " String . unsafe_get % s % s " string_id pos_id else sprintf " String . unsafe_get % s ( % s +% i ) " string_id pos_id i | ` End -> assert false
let make_pattern value = match value with ` Int i -> string_of_int i | ` Char c -> sprintf " % C " c
let cond test if_true if_false = [ Line ( sprintf " if % s then ( " test ) ; Block if_true ; Line " ) " ; Line " else ( " ; Block if_false ; Line " ) " ; ]
let make_branch_test string_id pos_id = function ( ` Length , ` Int n ) -> sprintf " len = % i " n | ( ` Position i , ` Char c ) -> if i = 0 then sprintf " String . unsafe_get % s % s = % C " string_id pos_id c else sprintf " String . unsafe_get % s ( % s +% i ) = % C " string_id pos_id i c | _ -> assert false
let make_branch_tests string_id pos_id l = String . concat " && " ( List . map ( make_branch_test string_id pos_id ) l )
let rec map_to_ocaml string_id pos_id e = function ` Leaf expr -> expr | ` Branch ( l , x ) -> cond ( make_branch_tests string_id pos_id l ) ( map_to_ocaml string_id pos_id e x ) e | ` Node ( pos , l ) -> [ Line ( sprintf " match % s with " ( get_value string_id pos_id pos ) ) ; Block [ Inline ( List . map ( make_case ...
type exit_with = [ ` Exn of string | ` Expr ]
let make_ocaml_expr_factored ( ? string_id = " s " ) ( ? pos_id = " pos " ) ( ? exit_with = ` Exn " Exit " ) ~ error_expr cases : Indent . t list = let exit_expr , catch = match exit_with with ` Expr -> error_expr , ( fun x -> x ) | ` Exn error_exn -> let exit_expr = [ Line ( sprintf " raise ( % s ) " error_exn ) ] in ...
let test2 ( ) = let l = [ " abc " ; " abcd " ; " abde " ; " bcd " ; " " ; ] in let cases = List . map ( fun s -> ( s , [ Line ( sprintf " Some ` Case_ % s " s ) ] ) ) l in let expr = make_ocaml_expr_factored ~ error_expr [ : Line " None " ] cases in Indent . to_stdout ( List . map Indent . strip expr )
let make_ocaml_expr_naive ( ? string_id = " s " ) ~ error_expr cases = let map ( s , expr ) = Inline [ Line ( sprintf " | % S " -> s ) ; Block expr ; ] in [ Line ( sprintf " match % s with " string_id ) ; Block [ Inline ( List . map map cases ) ; Line " | _ " ; -> Block error_expr ; ] ]
let make_ocaml_expr ~ optimized ? string_id ? pos_id ? exit_with ~ error_expr cases : Indent . t list = if optimized then make_ocaml_expr_factored ? string_id ? pos_id ? exit_with ~ error_expr cases else make_ocaml_expr_naive ? string_id ~ error_expr cases
let make_ocaml_int_mapping ( ? string_id = " s " ) ( ? pos_id = " pos " ) ( ? len_id = " len " ) ? exit_with ~ error_expr1 ( ? error_expr2 = [ Line " assert false " ] ) ( ? int_id = " i " ) cases : Indent . t list * Indent . t list = let a = Array . of_list cases in let int_cases = Array . mapi ( fun i ( s , _ ) -> ( s...
type t = elt Set_gen . t
let rec split x ( tree : _ Set_gen . t ) t : _ Set_gen . t * bool * _ Set_gen . t = match tree with | Empty -> ( Empty , false , Empty ) Empty | Node ( Nodel , v , r , _ ) _ -> let c = compare_elt x v in if c = 0 then ( l , true , r ) r else if c < 0 then let ( ll , pres , rl ) rl = split x l in ( ll , pres , Set_gen ....
let rec add x ( tree : _ Set_gen . t ) t : _ Set_gen . t = match tree with | Empty -> Node ( NodeEmpty , x , Empty , 1 ) 1 | Node ( Nodel , v , r , _ ) _ as t -> let c = compare_elt x v in if c = 0 then t else if c < 0 then Set_gen . internal_bal ( add x l ) l v r else Set_gen . internal_bal l v ( add x r ) r
let rec union ( s1 : _ Set_gen . t ) t ( s2 : _ Set_gen . t ) t : _ Set_gen . t = match ( s1 , s2 ) s2 with | ( Empty , t2 ) t2 -> t2 | ( t1 , Empty ) Empty -> t1 | ( Node ( Nodel1 , v1 , r1 , h1 ) h1 , Node ( Nodel2 , v2 , r2 , h2 ) h2 ) h2 -> if h1 >= h2 then if h2 = 1 then add v2 s1 else begin let ( l2 , _ , r2 ) r2...
let rec inter ( s1 : _ Set_gen . t ) t ( s2 : _ Set_gen . t ) t : _ Set_gen . t = match ( s1 , s2 ) s2 with | ( Empty , t2 ) t2 -> Empty | ( t1 , Empty ) Empty -> Empty | ( Node ( Nodel1 , v1 , r1 , _ ) _ , t2 ) t2 -> begin match split v1 t2 with | ( l2 , false , r2 ) r2 -> Set_gen . internal_concat ( inter l1 l2 ) l2 ...
let rec diff ( s1 : _ Set_gen . t ) t ( s2 : _ Set_gen . t ) t : _ Set_gen . t = match ( s1 , s2 ) s2 with | ( Empty , t2 ) t2 -> Empty | ( t1 , Empty ) Empty -> t1 | ( Node ( Nodel1 , v1 , r1 , _ ) _ , t2 ) t2 -> begin match split v1 t2 with | ( l2 , false , r2 ) r2 -> Set_gen . internal_join ( diff l1 l2 ) l2 v1 ( di...
let rec mem x ( tree : _ Set_gen . t ) t = match tree with | Empty -> false | Node ( Nodel , v , r , _ ) _ -> let c = compare_elt x v in c = 0 || mem x ( if c < 0 then l else r ) r
let rec remove x ( tree : _ Set_gen . t ) t : _ Set_gen . t = match tree with | Empty -> Empty | Node ( Nodel , v , r , _ ) _ -> let c = compare_elt x v in if c = 0 then Set_gen . internal_merge l r else if c < 0 then Set_gen . internal_bal ( remove x l ) l v r else Set_gen . internal_bal l v ( remove x r ) r
let compare s1 s2 = Set_gen . compare compare_elt s1 s2
let equal s1 s2 = compare s1 s2 = 0
let rec subset ( s1 : _ Set_gen . t ) t ( s2 : _ Set_gen . t ) t = match ( s1 , s2 ) s2 with | Empty , _ -> true | _ , Empty -> false | Node ( l1 , v1 , r1 , _ ) _ , ( Node ( l2 , v2 , r2 , _ ) _ as t2 ) t2 -> let c = compare_elt v1 v2 in if c = 0 then subset l1 l2 && subset r1 r2 else if c < 0 then subset ( Node ( l1 ...
let rec find x ( tree : _ Set_gen . t ) t = match tree with | Empty -> raise Not_found | Node ( Nodel , v , r , _ ) _ -> let c = compare_elt x v in if c = 0 then v else find x ( if c < 0 then l else r ) r
let of_list l = match l with | [ ] -> empty | [ x0 ] x0 -> singleton x0 | [ x0 ; x1 ] x1 -> add x1 ( singleton x0 ) x0 | [ x0 ; x1 ; x2 ] x2 -> add x2 ( add x1 ( singleton x0 ) x0 ) x0 | [ x0 ; x1 ; x2 ; x3 ] x3 -> add x3 ( add x2 ( add x1 ( singleton x0 ) x0 ) x0 ) x0 | [ x0 ; x1 ; x2 ; x3 ; x4 ] x4 -> add x4 ( add x3...
let of_array l = Array . fold_left ( fun acc x -> add x acc ) acc empty l
let invariant t = Set_gen . check t ; Set_gen . is_ordered compare_elt t
let nybble_bits = function | 0x0 -> [ false ; false ; false ; false ] | 0x1 -> [ false ; false ; false ; true ] | 0x2 -> [ false ; false ; true ; false ] | 0x3 -> [ false ; false ; true ; true ] | 0x4 -> [ false ; true ; false ; false ] | 0x5 -> [ false ; true ; false ; true ] | 0x6 -> [ false ; true ; true ; false ] |...
let char_bits c = let open Core_kernel in let n = Char . to_int c in let hi = Int ( . shift_right ( bit_and n 0xF0 ) 0xF0 4 ) 4 in let lo = Int . bit_and n 0x0F in List . concat_map [ hi ; lo ] ~ f : nybble_bits
let string_to_input s = Random_oracle . Input . Legacy . { field_elements = [ ] || ; bitstrings = Stdlib ( . Array . of_seq ( Seq . map char_bits ( String . to_seq s ) s ) s ) s }
let verify ? signature_kind signature pk s = let m = string_to_input s in let inner_curve = Inner_curve . of_affine pk in Schnorr . Legacy . verify ? signature_kind signature inner_curve m
let sign ? signature_kind sk s = let m = string_to_input s in Schnorr . Legacy . sign ? signature_kind sk m ( module struct let keypair : Signature_lib . Keypair . t = let public_key = Signature_lib . Public_key . Compressed . of_base58_check_exn " B62qnNkiQn1t1Nhof2fyTtBTbHLbXcUDVX2BWpjGKKK3HsfP8LPhYgE " |> Signature_...
let ff x = let a = match x with | " 0 " " | 1 " " | 2 " -> 3 | " 3 " -> 4 | " 4 " -> 6 | " 7 " -> 7 | _ -> 8 in a + 3
let gg x = let a = match x with | 0 | 1 | 2 -> 3 | 3 -> 4 | 4 -> 6 | 8 -> 7 | _ -> 8 in a + 3
let rev_split_by_char c s = let rec loop i l = try let i ' = String . index_from s i c in let s ' = String . sub s i ( i ' - i ) i in loop ( i ' + 1 ) 1 ( if s ' = " " then l else s ' :: l ) l with Not_found -> ( String . sub s i ( String . length s - i ) i ) :: l in loop 0 [ ]
let xsplit ~ delim s = let rec loop l = function | 0 -> l | i -> ( match String . rindex_from s ( i - 1 ) 1 delim with | i ' -> let l = String . sub s ( i ' + 1 ) 1 ( i - i ' - 1 ) 1 :: l in let l = if i ' = 0 then " " :: l else l in loop l i ' | exception Not_found -> String . sub s 0 i :: l ) in let len = String . le...
let string_of_chars x = String . concat " " @@ List . map string_of_char x " mutliple switch " , ( fun _ -> Eq ( Eq9 , ff " 4 ) ) " ; " int switch " , ( fun _ -> Eq ( Eq9 , gg 4 ) 4 ) 4 ; " escape_normal " , ( fun _ -> Eq ( Eq " haha " , String . escaped " haha ) ) " ; " escape_bytes " , ( fun _ -> Eq ( Eq Bytes . of_s...
let ( ) = init ( )
let take s n = String . take s n |> string |> print_dyn
let drop s n = String . drop s n |> string |> print_dyn
let split_n s n = String . split_n s n |> pair string string |> print_dyn take " foobar " 3 ; [ % expect { | " foo " } ] | take " foobar " 0 ; [ % expect { | " " } ] | take " foo " 10 ; [ % expect { | " foo " } ] | take " " 10 ; [ % expect { | " " } ] | take " " 0 ; [ % expect { | " " } ] | drop " " 0 ; [ % expect { | ...
let string_of_char = String . make 1
let string_before s n = String . sub s 0 n
let string_after s n = String . sub s n ( String . length s - n )
let string_starts_with long short = try let long = String . sub long 0 ( String . length short ) in long = short with Invalid_argument _ -> false
let string_ends_with long short = try let len = String . length short in let long = String . sub long ( String . length long - len ) len in long = short with Invalid_argument _ -> false
let substring_index needle = let needle_len = String . length needle in if needle_len = 0 then raise ( Invalid_argument needle ) ; let table = Array . make needle_len 0 in table . ( 0 ) <- ( - 1 ) ; let pos = ref 2 and cnd = ref 0 in while ! pos < needle_len do if needle . [ ! pos - 1 ] = needle . [ ! cnd ] then ( tabl...
let is_substring needle = let substring_index_memo = substring_index needle in fun haystack -> ( substring_index_memo haystack ) >= 0
let lstrip s prefix = let prefix_length = String . length prefix in if string_starts_with s prefix then String . sub s prefix_length ( String . length s - prefix_length ) else s
let rstrip s suffix = let result_length = String . length s - String . length suffix in if string_ends_with s suffix then String . sub s 0 result_length else s
let rpartition s c = let sep_idx = String . rindex s c in let first = String . sub s 0 sep_idx in let second = String . sub s ( sep_idx + 1 ) ( String . length s - sep_idx - 1 ) in first , second
let is_lowercase_char = let a_code , z_code = Char . code ' a ' , Char . code ' z ' in fun chr -> let code = Char . code chr in a_code <= code && code <= z_code
let rec is_not_lowercase str i j = if is_lowercase_char str . [ i ] then false else if i = j then true else is_not_lowercase str ( i + 1 ) j
let fold_left ~ f ~ acc str = let acc = ref acc in String . iter ( fun c -> acc := f ( ! acc ) c ) str ; ! acc
let split c = Str . split ( Str . regexp @@ Char . escaped c )
let replace_char needle replacement = String . map ( fun c -> if c = needle then replacement else c )
let split_into_lines str = let _ , ( last_start , lines ) = fold_left ~ f : ( fun ( idx , ( start , lines ) ) c -> if c = ' \ n ' && idx > 0 && String . get str ( idx - 1 ) = ' \ r ' then idx + 1 , ( idx + 1 , lines ) else if c = ' \ n ' || c = ' \ r ' then idx + 1 , ( idx + 1 , ( start , idx - start ) :: lines ) else ...
let split_on_newlines content = let re = Str . regexp " [ \ n ] " in let lines = Str . split_delim re content in match List . rev lines with | " " :: rest -> List . rev rest | _ -> lines
type part = | Text of string | Pform of Dune_lang . Template . Pform . t * Pform . t | Error of Dune_lang . Template . Pform . t * User_message . t
type t = { quoted : bool ; parts : part list ; loc : Loc . t }
let compare_no_loc { quoted ; parts ; loc = _ } t = let open Ordering . O in let = ( ) = Bool . compare quoted t . quoted in List . compare parts t . parts ~ compare ( : fun a b -> match ( a , b ) with | Text a , Text b -> String . compare a b | Pform ( _ , a ) , Pform ( _ , b ) -> Pform . compare a b | Error ( _ , a )...
let equal_no_loc t1 t2 = Ordering . is_eq ( compare_no_loc t1 t2 )
let make_text ( ? quoted = false ) loc s = { quoted ; loc ; parts = [ Text s ] }
let make_pform ( ? quoted = false ) loc pform = let source = match Pform . encode_to_latest_dune_lang_version pform with | Success { name ; payload } -> { Dune_lang . Template . Pform . loc ; name ; payload } | Pform_was_deleted -> assert false in { quoted ; loc ; parts = [ Pform ( source , pform ) ] }
let literal ~ quoted ~ loc s = { parts = [ Text s ] ; quoted ; loc }
let decoding_env_key = Univ_map . Key . create ~ name " : pform decoding environment " Pform . Env . to_dyn
let set_decoding_env env = Dune_lang . Decoder . set decoding_env_key env
let add_user_vars_to_decoding_env vars = Dune_lang . Decoder . update_var decoding_env_key ~ f ( : function | None -> Code_error . raise " Decoding env not set " [ ] | Some env -> Some ( Pform . Env . add_user_vars env vars ) )
let decode_manually f = let open Dune_lang . Decoder in let + env = get decoding_env_key and + x = raw in let env = match env with | Some env -> env | None -> Code_error . raise ~ loc ( : Dune_lang . Ast . loc x ) " pform decoding environment not set " [ ] in match x with | Atom ( loc , A s ) -> literal ~ quoted : fals...
let decode = decode_manually Pform . Env . parse
let loc t = t . loc
let virt_pform ? quoted pos pform = let loc = Loc . of_pos pos in make_pform ? quoted loc pform
let virt_text pos s = let loc = Loc . of_pos pos in { parts = [ Text s ] ; loc ; quoted = true }
let concat_rev = function | [ ] -> " " | [ s ] -> s | l -> String . concat ( List . rev l ) ~ sep " " :
module Mode = struct type _ t = | Single : Value . t t | Many : Value . t list t | At_least_one : ( Value . t * Value . t list ) t let string : type a . a t -> string -> a = fun t s -> match t with | Single -> Value . String s | Many -> [ Value . String s ] | At_least_one -> ( Value . String s , [ ] ) let invalid_multi...
type known_suffix = | Full of string | Partial of { source_pform : Dune_lang . Template . Pform . t ; suffix : string }
let known_suffix = let rec go t acc = match t with | Text s :: rest -> go rest ( s :: acc ) | [ ] -> Full ( String . concat ~ sep " " : acc ) | ( Pform ( p , _ ) | Error ( p , _ ) ) :: _ -> Partial { source_pform = p ; suffix = String . concat ~ sep " " : acc } in fun t -> go ( List . rev t . parts ) [ ]
type known_prefix = | Full of string | Partial of { prefix : string ; source_pform : Dune_lang . Template . Pform . t }
let known_prefix = let rec go t acc = match t with | Text s :: rest -> go rest ( s :: acc ) | [ ] -> Full ( String . concat ~ sep " " : ( List . rev acc ) ) | ( Pform ( p , _ ) | Error ( p , _ ) ) :: _ -> Partial { prefix = String . concat ~ sep " " : ( List . rev acc ) ; source_pform = p } in fun t -> go t . parts [ ]
let fold_pforms = let rec loop parts acc f = match parts with | [ ] -> acc | ( Text _ | Error _ ) :: parts -> loop parts acc f | Pform ( p , v ) :: parts -> loop parts ( f ~ source : p v acc ) f in fun t ~ init ~ f -> loop t . parts init f
type ' a expander = source : Dune_lang . Template . Pform . t -> Pform . t -> ' a
type yes_no_unknown = | Yes | No | Unknown of { source_pform : Dune_lang . Template . Pform . t }
let is_suffix t ~ suffix : want = match known_suffix t with | Full s -> if String . is_suffix ~ suffix : want s then Yes else No | Partial { suffix = have ; source_pform } -> if String . is_suffix ~ suffix : want have then Yes else if String . is_suffix ~ suffix : have want then Unknown { source_pform } else No
let is_prefix t ~ prefix : want = match known_prefix t with | Full s -> if String . is_prefix ~ prefix : want s then Yes else No | Partial { prefix = have ; source_pform } -> if String . is_prefix ~ prefix : want have then Yes else if String . is_prefix ~ prefix : have want then Unknown { source_pform } else No
module type Expander = sig type ' a app val expand : t -> mode ' : a Mode . t -> dir : Path . t -> f : Value . t list app expander -> ' a app val expand_as_much_as_possible : t -> dir : Path . t -> f : Value . t list option app expander -> t app end
module Make_expander ( A : Applicative ) : Expander with type ' a app := ' a A . t = struct open A . O let expand : type a . t -> mode : a Mode . t -> dir : Path . t -> f : Value . t list A . t expander -> a A . t = fun t ~ mode ~ dir ~ f -> match t . parts with | [ ] -> A . return ( Mode . string mode " " ) | [ Text s...
let is_pform t pform = match t . parts with | [ Pform ( _ , pform ' ) ] -> Pform . compare pform pform ' = Eq | _ -> false
let text_only t = match t . parts with | [ Text s ] -> Some s | _ -> None