text
stringlengths
0
601k
let parse_kind_prefix s = case s |> ( { m |^\ s ( * val | type | typext | exception | module | module \ s + type | class | class \ s + type | field | constructor | method ) \ s +| m } ==> fun r -> let k = from_Some & Sig . parse_k r # _1 in Some ( k , r # _right ) ) |> default ( fun ( ) -> None )
let parse s = let prefixed = match parse_kind_prefix s with | None -> [ ] | Some ( k , s ) -> let parse_with_prefix s = filter_map id [ parse_as_path_type s ; parse_path s ; parse_as_wildcard_colon_type s ] in map ( fun x -> Some k , x ) & parse_with_prefix s in let non_prefixed = let parse_without_prefix s = filter_ma...
module PackageSpec = struct type t = | Just of string list | All_but of string list | Vanilla of string list [ @@ deriving conv { ocaml_of } ] let vanilla = [ " predef " ; " bigarray " ; " bytes " ; " dynlink " ; " graphics " ; " num " ; " stdlib " ; " str " ; " threads " ; " unix " ] let check pspec toppack = match ps...
let query { DB . items = fs_t_list ; top_types = ts } pspec qs0 = let module Match = Sigmatch . Make ( struct let cache = Levenshtein . StringWithHashtbl . create_cache 1023 end ) in let qs = flip map qs0 & fun ( ko , s ) -> ( ko , s , match s with Path _ -> Array . create 0 ` None | _ -> Array . ( create ( length ts )...
let test_parse_query context = let assert_parses serialized query = let type_query_request_equal left right = let expression_equal left right = Expression . location_insensitive_compare left right = 0 in match left , right with | ( Query . Request . IsCompatibleWith ( left_first , left_second ) , Query . Request . IsCo...
let assert_queries_with_local_root ? custom_source_root ~ context ~ sources queries_and_responses = let test_handle_query client = let handle_one_query ( query , build_expected_response ) = let open Lwt . Infix in Client . send_request client ( Request . Query query ) >>= fun actual_response -> let expected_response = ...
let test_handle_query_basic context = let open Query . Response in let assert_type_query_response_with_local_root ? custom_source_root ( ? handle = " test . py " ) ~ source ~ query build_expected_response = let build_expected_response local_root = Response . Query ( build_expected_response local_root ) |> Response . to...
let test_handle_query_with_build_system context = let custom_source_root = bracket_tmpdir context |> PyrePath . create_absolute ~ follow_symbolic_links : true in let build_system_initializer = let initialize ( ) = let lookup_artifact _ = [ Test . relative_artifact_path ~ root : custom_source_root ~ relative " : redirec...
let test_handle_query_pysa context = let queries_and_expected_responses = [ ( " callees_with_location ( wait . bar ) " , { | { " response " : { " callees " : [ { " locations " : [ { " path " " : wait . py " , " start " : { " line " : 4 , " column " : 2 } , " stop " : { " line " : 4 , " column " : 10 } } ] , " kind " : ...
let test_inline_decorators context = let queries_and_expected_responses = [ ( " inline_decorators ( test . foo ) " , { | { " response " : { " definition " : " def test . foo ( a : int ) -> int : def _original_function ( a : int ) -> int : return a } | ^ " \ n " ^ { | def _inlined_identity ( a : int ) -> int : _args = (...
let test_location_of_definition context = let sources = [ " stubbed_library . py " , { | def identity ( a ) : return a } ; | " stubbed_library . pyi " , { | def identity ( a : int ) -> int : . . . } ; | ( " foo . py " , { | def foo ( a : int ) -> int : return a class Base : def __init__ ( self , x : int ) -> None : sel...
let test_find_references context = let sources = [ " stubbed_library . py " , { | def identity ( a ) : return a } ; | " stubbed_library . pyi " , { | def identity ( a : int ) -> int : . . . } ; | ( " foo . py " , { | def foo ( a : int ) -> int : x = a return x class Base : def __init__ ( self , x : int ) -> None : self...
let ( ) = " query " >::: [ " parse_query " >:: test_parse_query ; " handle_query_basic " >:: OUnitLwt . lwt_wrapper test_handle_query_basic ; " handle_query_with_build_system " >:: OUnitLwt . lwt_wrapper test_handle_query_with_build_system ; " handle_query_pysa " >:: OUnitLwt . lwt_wrapper test_handle_query_pysa ; " in...
let print_completion_entries ~ with_types config source entries = if with_types then let input_ref = ref [ ] and output_ref = ref [ ] in let preprocess entry = match Completion . raw_info_printer entry with | ` String s -> ` String s | ` Print t -> let r = ref " " in input_ref := t :: ! input_ref ; output_ref := r :: !...
let for_completion pipeline position = let pipeline = Mpipeline . for_completion position pipeline in let typer = Mpipeline . typer_result pipeline in ( pipeline , typer )
let verbosity pipeline = Mconfig . ( ( Mpipeline . final_config pipeline ) . query . verbosity )
let dump pipeline = function | [ ` String " ppxed - source " ] -> let ppf , to_string = Format . to_string ( ) in begin match Mpipeline . ppx_parsetree pipeline with | ` Interface s -> Pprintast . signature ppf s | ` Implementation s -> Pprintast . structure ppf s end ; Format . pp_print_newline ppf ( ) ; Format . pp_f...
let reconstruct_identifier pipeline pos = function | None -> let path = Mreader . reconstruct_identifier ( Mpipeline . input_config pipeline ) ( Mpipeline . raw_source pipeline ) pos in let path = Mreader_lexer . identifier_suffix path in Logger . log ~ section : Type_enclosing . log_section ~ title " : reconstruct - i...
let dispatch pipeline ( type a ) : a Query_protocol . t -> a = function | Type_expr ( source , pos ) -> let typer = Mpipeline . typer_result pipeline in let pos = Mpipeline . get_lexing_pos pipeline pos in let env , _ = Mbrowse . leaf_node ( Mtyper . node_at typer pos ) in let ppf , to_string = Format . to_string ( ) i...
let build_where : type a . placeholder ( : int -> string ) -> handover : Expr . handover -> build_step -> a Expr . t option -> build_step = fun ~ placeholder ~ handover st -> function | Some expr -> let st = Expr . build ~ placeholder ~ handover st expr in { st with repr = sprintf " WHERE ( % s ) " st . repr } | None -...
let build_limit placeholder st = function | Some ( 0 , lim ) -> { st with repr = sprintf " LIMIT % s " ( placeholder st . pos ) ; params = [ Param . Int lim ] ; pos = st . pos + 1 } | Some ( off , lim ) -> let repr = sprintf " LIMIT % s , % s " ( placeholder st . pos ) ( placeholder ( st . pos + 1 ) ) in { st with repr...
module UpdateDeleteExpr = struct type _ Expr . t += | Field : ( ' t , ' a ) Field . t * ' t Table . t -> ' a Expr . t | Foreign : ( ' t , ' u ) Field . foreign_key * ' t Table . t -> ' a Expr . t let field fld = fun table -> Field ( fld , table ) let foreign_key fk = fun table -> Foreign ( fk , table ) let rec build : ...
module InsertReplace = struct module Vector = Vector . Make ( struct type ( ' t , ' a ) elem = ( ' t , ' a ) Field . t end ) type _ t = | I : { table : ' t Table . t ; fields : ( ' t , ' a , ' n Nat . s ) Vector . t ; values : ( ' u , ' a , ' m Nat . s , ' n Nat . s ) Lit . Vector . matrix } -> ' t t let create = fun ~...
module type Query_handler = sig type config with sexp val name : string type t val create : config -> t val eval : t -> Sexp . t -> Sexp . t Or_error . t end
module Unique = struct type config = int with sexp type t = { mutable next_id : int } let name = " unique " let create start_at = { next_id = start_at } let eval t sexp = match Or_error . try_with ( fun ( ) -> unit_of_sexp sexp ) with | Error _ as err -> err | Ok ( ) -> let response = Ok ( Int . sexp_of_t t . next_id )...
module List_dir = struct type config = string with sexp type t = { cwd : string } let is_abs p = String . length p > 0 && p . [ 0 ] = ' ' / let name = " ls " let create cwd = { cwd } let eval t sexp = match Or_error . try_with ( fun ( ) -> string_of_sexp sexp ) with | Error _ as err -> err | Ok dir -> let dir = if is_a...
module type Query_handler_instance = sig module Query_handler : Query_handler val this : Query_handler . t end
let build_instance ( type a ) ( module Q : Query_handler with type config = a ) config = ( module struct module Query_handler = Q let this = Q . create config end : Query_handler_instance )
let build_dispatch_table handlers = let table = String . Table . create ( ) in List . iter handlers ~ f ( : fun ( ( module I : Query_handler_instance ) as instance ) -> Hashtbl . replace table ~ key : I . Query_handler . name ~ data : instance ) ; table
let dispatch dispatch_table name_and_query = match name_and_query with | Sexp . List [ Sexp . Atom name ; query ] -> begin match Hashtbl . find dispatch_table name with | None -> Or_error . error " Could not find matching handler " name String . sexp_of_t | Some ( module I : Query_handler_instance ) -> I . Query_handle...
let rec cli dispatch_table = printf " >>> " ; %! let result = match In_channel . input_line stdin with | None -> ` Stop | Some line -> match Or_error . try_with ( fun ( ) -> Sexp . of_string line ) with | Error e -> ` Continue ( Error . to_string_hum e ) | Ok ( Sexp . Atom " quit " ) -> ` Stop | Ok query -> begin match...
let unique_instance = build_instance ( module Unique ) 0 ; ;
let list_dir_instance = build_instance ( module List_dir ) " / var " ; ;
module Loader = struct type config = ( module Query_handler ) list sexp_opaque with sexp type t = { known : ( module Query_handler ) String . Table . t ; active : ( module Query_handler_instance ) String . Table . t } let name = " loader " let create known_list = let active = String . Table . create ( ) in let known = ...
let dump ( type a ) : a t -> json = let mk command args = ` Assoc ( ( " command " , ` String command ) :: args ) in let mk_position = function | ` Start -> ` String " start " | ` End -> ` String " end " | ` Offset n -> ` Assoc [ " offset " , ` Int n ] | ` Logical ( line , col ) -> ` Assoc [ " line " , ` Int line ; " co...
let string_of_completion_kind = function | ` Value -> " Value " | ` Variant -> " Variant " | ` Constructor -> " Constructor " | ` Label -> " Label " | ` Module -> " Module " | ` Modtype -> " Signature " | ` Type -> " Type " | ` Method -> " Method " | ` MethodCall -> " " # | ` Exn -> " Exn " | ` Class -> " Class " | ` K...
let with_location ( ? skip_none = false ) loc assoc = if skip_none && loc = Location . none then ` Assoc assoc else ` Assoc ( ( " start " , Lexing . json_of_position loc . Location . loc_start ) :: ( " end " , Lexing . json_of_position loc . Location . loc_end ) :: assoc )
let json_of_type_loc ( loc , desc , tail ) = with_location loc [ " type " , ( match desc with | ` String _ as str -> str | ` Index n -> ` Int n ) ; " tail " , ` String ( match tail with | ` No -> " no " | ` Tail_position -> " position " | ` Tail_call -> " call " ) ]
let json_of_error ( error : Location . error ) = let of_sub loc sub = let msg = Location . print_sub_msg Format . str_formatter sub ; String . trim ( Format . flush_str_formatter ( ) ) in with_location ~ skip_none : true loc [ " message " , ` String msg ] in let loc = Location . loc_of_report error in let msg = Locatio...
let json_of_completion { Compl . name ; kind ; desc ; info ; deprecated } = ` Assoc [ " name " , ` String name ; " kind " , ` String ( string_of_completion_kind kind ) ; " desc " , ` String desc ; " info " , ` String info ; " deprecated " , ` Bool deprecated ]
let json_of_completions { Compl . entries ; context } = ` Assoc [ " entries " , ` List ( List . map ~ f : json_of_completion entries ) ; " context " , ( match context with | ` Unknown -> ` Null | ` Application { Compl . argument_type ; labels } -> let label ( name , ty ) = ` Assoc [ " name " , ` String name ; " type " ...
let rec json_of_outline outline = let json_of_item { outline_name ; outline_kind ; outline_type ; location ; children ; deprecated } = with_location location [ " name " , ` String outline_name ; " kind " , ` String ( string_of_completion_kind outline_kind ) ; " type " , ( match outline_type with | None -> ` Null | Some...
let rec json_of_shape { shape_loc ; shape_sub } = with_location shape_loc [ " children " , ` List ( List . map ~ f : json_of_shape shape_sub ) ; ]
let json_of_locate resp = match resp with | ` At_origin -> ` String " Already at definition point " | ` Builtin s -> ` String ( sprintf " % S is a builtin , and it is therefore impossible \ to jump to its definition " s ) | ` Invalid_context -> ` String " Not a valid identifier " | ` Not_found ( id , None ) -> ` String...
let json_of_response ( type a ) ( query : a t ) ( response : a ) : json = match query , response with | Type_expr _ , str -> ` String str | Type_enclosing _ , results -> ` List ( List . map ~ f : json_of_type_loc results ) | Enclosing _ , results -> ` List ( List . map ~ f ( : fun loc -> with_location loc [ ] ) results...
module Compl = struct type ' desc raw_entry = { name : string ; kind : [ ` Value ` | Constructor ` | Variant ` | Label | ` Module ` | Modtype ` | Type ` | MethodCall ` | Keyword ] ; desc : ' desc ; info : ' desc ; deprecated : bool ; } type entry = string raw_entry type application_context = { argument_type : string ; ...
type outline = item list outline_name : string ; outline_kind : [ | ` Value | ` Constructor | ` Label | ` Module | ` Modtype | ` Type | ` Exn | ` Class | ` Method ] ; outline_type : string option ; deprecated : bool ; location : Location_aux . t ; children : outline ; }
type shape = { shape_loc : Location_aux . t ; shape_sub : shape list ; }
type error_filter = { lexing : bool ; parsing : bool ; typing : bool ; }
type is_tail_position = [ ` No | ` Tail_position | ` Tail_call ]
type _ _bool = bool
type _ t = | Type_expr : string * Msource . position -> string t | Type_enclosing : ( string * int ) option * Msource . position * int option -> ( Location . t * [ ` String of string | ` Index of int ] * is_tail_position ) list t | Enclosing : Msource . position -> Location . t list t | Complete_prefix : string * Msour...
let yes_or_no message = if ! interactif then let old_prompt = ! current_prompt in try current_prompt := message ^ " ? ( y or n ) " ; let answer = let rec ask ( ) = resume_user_input ( ) ; let line = string_trim ( Lexer . line ( Lexing . from_function read_user_input ) ) in stop_user_input ( ) ; match ( if String . leng...
module type Queue = sig type ' a t val empty : ' a t val is_empty : ' a t -> bool val enqueue : ' a -> ' a t -> ' a t val peek : ' a t -> ' a option val dequeue : ' a t -> ' a t option end
module ListQueue : Queue = struct type ' a t = ' a list let empty = [ ] let is_empty q = q = [ ] let enqueue i q = q @ [ i ] let peek = function | [ ] -> None | hd :: _ -> Some hd let dequeue = function | [ ] -> None | _ :: tl -> Some tl end
module TwoListQueue : Queue = struct type ' a t = { front : ' a list ; back : ' a list } let empty = { front = [ ] ; back = [ ] } let is_empty = function | { front = [ ] ; back = [ ] } -> true | _ -> false let norm = function | { front = [ ] ; back } -> { front = List . rev back ; back = [ ] } | q -> q let enqueue i q ...
module type ATOM = sig type t val compare : t -> t -> int val to_string : t -> string end
module Make ( Atom : ATOM ) = struct type atom = Atom . t type node = { content : atom ; mutable next : cell } and cell = Nil | Cons of node exception Empty type t = { mutable length : int ; mutable first : cell ; mutable last : cell } let empty = { length = 0 ; first = Nil ; last = Nil } let create ( ) = { length = 0 ...
module type S = sig type ' a t [ @@ deriving_inline sexp , sexp_grammar ] include Sexplib0 . Sexpable . S1 with type ' a t := ' a t val t_sexp_grammar : ' a Sexplib0 . Sexp_grammar . t -> ' a t Sexplib0 . Sexp_grammar . t [ @@@ end ] include Indexed_container . S1 with type ' a t := ' a t val singleton : ' a -> ' a t v...
module type Queue = sig module type S = S type ' a t [ @@ deriving_inline compare ] include Ppx_compare_lib . Comparable . S1 with type ' a t := ' a t [ @@@ end ] include S with type ' a t := ' a t include Equal . S1 with type ' a t := ' a t include Invariant . S1 with type ' a t := ' a t val create : ? capacity : int ...
let print_usage_and_exit ( ) = print_endline @@ " Usage : " ^ Sys . argv . ( 0 ) ^ " < num_domains > < num_items " ; > exit ( 0 )
let ( num_doms , num_items ) = if Array . length Sys . argv < 3 then print_usage_and_exit ( ) else try let a = int_of_string ( Sys . argv . ( 1 ) ) in let b = int_of_string ( Sys . argv . ( 2 ) ) in ( a , b ) with | Failure _ -> print_usage_and_exit ( )
let ( ) = if num_doms mod 2 <> 0 then ( print_endline @@ " < num_domains > must be multiple of 2 " ; exit 0 )
let items_per_dom = num_items / num_doms
module M = struct let num_domains = num_doms let is_affine = false end
module S = Sched_ws . Make ( M )
let ( ) = Printf . printf " [ % d ] items_per_domain = % d \ n " %! ( S . get_qid ( ) ) items_per_dom
let id_str ( ) = Printf . sprintf " % d :% d " ( S . get_qid ( ) ) ( S . get_tid ( ) )
module type BARRIER = sig type t val create : int -> t val finish : t -> unit end
module Reagents = Reagents . Make ( S )
module type QUEUE = sig type ' a t val create : unit -> ' a t val push : ' a t -> ' a -> unit val pop : ' a t -> ' a option end
module type RQUEUE = sig type ' a t val create : unit -> ' a t val push : ' a t -> ( ' a , unit ) Reagents . t val try_pop : ' a t -> ( unit , ' a option ) Reagents . t end
module MakeQ ( RQ : RQUEUE ) : QUEUE = struct type ' a t = ' a RQ . t let create = RQ . create let push q v = Reagents . run ( RQ . push q ) v let pop q = Reagents . run ( RQ . try_pop q ) ( ) end
module Benchmark = struct let get_mean_sd l = let get_mean l = ( List . fold_right ( fun a v -> a . + v ) l 0 . ) . / ( float_of_int @@ List . length l ) in let mean = get_mean l in let sd = get_mean @@ List . map ( fun v -> abs_float ( v . - mean ) ** 2 . ) l in ( mean , sd ) let benchmark f n = let rec run acc = func...
module Test ( Q : QUEUE ) = struct let run num_doms items_per_domain = let q : int Q . t = Q . create ( ) in let b = CDL . create num_doms in let rec produce = function | 0 -> ( ) | i -> Q . push q i ; produce ( i - 1 ) in let rec consume i = match Q . pop q with | None -> ( ) | Some _ -> consume ( i + 1 ) in for i = 1...
let main ( ) = let module M = Test ( Lock_queue ) in let ( m , sd ) = Benchmark . benchmark ( fun ( ) -> M . run num_doms items_per_dom ) 5 in printf " Lock_queue : mean = % f , sd = % f tp =% f \ n " %! m sd ( float_of_int num_items . / m ) ; let module M = Test ( MakeQ ( Data . MichaelScott_queue ) ) in let ( m , sd ...
let ( ) = S . run main
module Random = struct include Random let int n = int ( max n 1 ) let char : char -> char = fun lim -> let l = Char . code lim in let i = int l in Char . chr i let int_range : int * int -> int = fun ( lo , hi ) -> lo + int ( hi - lo ) let int32_range : Int32 . t * Int32 . t -> Int32 . t = fun ( lo , hi ) -> Int32 . add...
module List = struct include List let rec span : ( ' a -> bool ) -> ' a list -> ' a list * ' a list = fun p -> function [ ] -> [ ] , [ ] | x :: xs when p x -> let ys , zs = span p xs in ( x :: ys , zs ) | xs -> [ ] , xs let rec groupBy : ( ' a -> ' a -> bool ) -> ' a list -> ' a list list = fun p -> function [ ] -> [ ]...
type ' a gen = Gen of ( int -> ' a )
type pretty_str = Format . formatter -> unit -> unit
module type PSHOW = sig type t val show : t -> pretty_str end
module type SHOW = sig type t val show : t -> string end
module Show ( P : PSHOW ) = struct open Buffer open Format type t = P . t let show : t -> string = fun x -> let f _ = let str = contents stdbuf in clear stdbuf ; str in clear stdbuf ; kfprintf f str_formatter " [ @% a ] " @@? ( P . show x ) ( ) end
module PShow_list ( Elt : PSHOW ) = struct type t = Elt . t list let show : t -> pretty_str = fun xs fmt ( ) -> let pp = Format . fprintf in match List . map Elt . show xs with [ ] -> pp fmt " [ ] " | a1 :: an -> let pprest f = List . iter ( fun e -> pp f " ; @ % a " e ( ) ) in end
module PShow_char = struct type t = char let show : t -> pretty_str = fun c fmt ( ) -> Format . fprintf fmt " % C " c end
module PShow_int = struct type t = int let show : t -> pretty_str = fun c fmt ( ) -> Format . fprintf fmt " % d " c end
let sized : ( int -> ' a gen ) -> ' a gen = fun f -> Gen ( fun n -> let Gen m = f n in m n )
let resize : int -> ' a gen -> ' a gen = fun n ( Gen m ) -> Gen ( fun _ -> m n )
let promote : ( ' a -> ' b gen ) -> ( ' a -> ' b ) gen = fun f -> Gen ( fun n -> fun a -> let Gen m = f a in m n )
let variant : int -> ' a gen -> ' a gen = fun _v ( Gen m ) -> Gen ( fun n -> m n )
let generate : int -> ' a gen -> ' a = fun n ( Gen m ) -> let size = Random . int n in m size
let map_gen : ( ' a -> ' b ) -> ' a gen -> ' b gen = fun f ( Gen m ) -> Gen ( fun n -> let v = m n in f v )
let ret_gen : ' a -> ' a gen = fun a -> Gen ( fun _n -> a )
let ( ) >>= : ' a gen -> ( ' a -> ' b gen ) -> ' b gen = fun ( Gen m ) k -> Gen ( fun n -> let v = m n in let Gen m ' = k v in m ' n )
let lift_gen : ( ' a -> ' b ) -> ' a -> ' b gen = fun f -> fun a -> Gen ( fun _ -> f a )
let choose_int = lift_gen Random . int_range
let choose_int0 = lift_gen Random . int
let choose_char = lift_gen Random . char_range
let choose_float = lift_gen Random . float_range