text
stringlengths 12
786k
|
---|
let is_bit_set ' bit x = is_bit_set x bit |
let set_bit x bit = x lor ( one lsl ( Char . unsafe_chr bit ) ) |
let set_bit ' bit x = x lor ( one lsl ( Char . unsafe_chr bit ) ) |
let unset_bit x bit = x land ( lnot ( one lsl ( Char . unsafe_chr bit ) ) ) |
let unset_bit ' bit x = x land ( lnot ( one lsl ( Char . unsafe_chr bit ) ) ) |
let toggle_bit x bit = x lxor ( one lsl ( Char . unsafe_chr bit ) ) |
let show_hex x = Printf . sprintf " 0x % 02X " ( code x ) |
let show_hex ' x = Printf . sprintf " % 02X " ( code x ) |
let show_bin d = if d = zero then " 0 " else let rec aux acc d = if d = zero then acc else aux ( string_of_int ( code ( d land one ) ) :: acc ) ( d lsr one ) in String . concat " " ( aux [ ] d ) |
let get_n_bits x n = ( x lsl n ) lsr n |
module MakeInfix ( B : Uints_intf . Basics ) : Uints_intf . Infix with type t := B . t = struct open B let ( + ) = add let ( - ) = sub let ( * ) = mul let ( / ) = div let ( = ) = equal let ( <> ) = ( fun a b -> not ( equal a b ) ) let ( <= ) = le let ( mod ) = rem let ( land ) = logand let ( lor ) = logor let ( lxor ) = logxor let ( lsl ) = shift_left let ( lsr ) = shift_right end |
module Uint8 = struct module B = struct type t = int let max_int = 0xFF let zero = 0 let one = 1 let compare = Int . compare let equal x y = compare x y = 0 let le x y = ( x <= y ) let add x y = ( x + y ) land max_int let sub x y = ( x - y ) land max_int let mul x y = ( x * y ) land max_int let div x y = ( x / y ) land max_int let rem x y = ( x mod y ) land max_int let succ x = add x one let pred x = sub x one let logand x y = ( x land y ) let logor x y = ( x lor y ) let logxor = ( lxor ) let shift_left x y = ( x lsl y ) land max_int let shift_right = ( lsr ) let of_int x = x land max_int let of_char c = Char . code c let to_char t = Char . unsafe_chr t external to_int : t -> int = " % identity " let show = Printf . sprintf " $% 02X " end include B module Infix = MakeInfix ( B ) include Infix end |
module Uint16 = struct module B = struct type t = int let max_int = 0xFFFF let zero = 0 let one = 1 let compare = Int . compare let equal x y = compare x y = 0 let le x y = ( x <= y ) let add x y = ( x + y ) land max_int let sub x y = ( x - y ) land max_int let mul x y = ( x * y ) land max_int let div x y = ( x / y ) land max_int let rem x y = ( x mod y ) land max_int let succ x = add x one let pred x = sub x one let logand x y = ( x land y ) let logor x y = ( x lor y ) let logxor = ( lxor ) let shift_left x y = ( x lsl y ) land max_int let shift_right = ( lsr ) let of_int x = x land max_int external to_int : t -> int = " % identity " let show = Printf . sprintf " $% 04X " end include B module Infix = MakeInfix ( B ) include Infix let of_uint8 x = x |> Uint8 . to_int |> of_int let to_uint8 x = x |> to_int |> Uint8 . of_int end |
module Int8 = struct type t = int let of_byte b = b let of_int x = if x < 0 then ( x land 0xFF ) lor 0b10000000 else ( x land 0xFF ) let is_neg t = t land ( 1 lsl 7 ) <> 0 let abs t = if is_neg t then ( t - 1 ) lxor 0b11111111 else t let to_int t = if is_neg t then ( - abs t ) else t let show t = if t land ( 1 lsl 7 ) <> 0 then Printf . sprintf " -% d " ( Int . abs @@ t - 0x100 ) else Printf . sprintf " % d " t end |
module type Basics = sig type t val show : t -> string val max_int : t val zero : t val one : t val compare : t -> t -> int val equal : t -> t -> bool val le : t -> t -> bool val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val succ : t -> t val pred : t -> t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t val shift_right : t -> int -> t val of_int : int -> t val to_int : t -> int end |
module type Infix = sig type t val ( + ) : t -> t -> t val ( - ) : t -> t -> t val ( * ) : t -> t -> t val ( / ) : t -> t -> t val ( = ) : t -> t -> bool val ( <> ) : t -> t -> bool val ( <= ) : t -> t -> bool val ( mod ) : t -> t -> t val ( land ) : t -> t -> t val ( lor ) : t -> t -> t val ( lxor ) : t -> t -> t val ( lsl ) : t -> int -> t val ( lsr ) : t -> int -> t end |
module type S = sig type t include Basics with type t := t module Infix : Infix with type t := t include Infix with type t := t end |
let toggle_element ( projection : Api_types_j . simulation_info option -> bool ) ( content : [ < Html_types . div_content_fun ] Html . elt Html . list_wrap ) = Html . div ~ a [ : Tyxml_js . R . Html . a_class ( React . S . bind State_simulation . model ( fun model -> React . S . const ( if projection ( State_simulation . t_simulation_info model ) then [ " show " ] else [ " hidden " ] ) ) ) ] content |
let option_label label = if String . length label > 10 then ( String . sub label 0 7 ) " . . . " ^ else label |
let export_controls ( ~ export_select_id : string ) ( ~ export_filename_id : string ) ( ~ export_button_id : string ) ( ~ export_data_label : string ) = let export_formats : string list = [ export_data_label ] in let export_filename = Html . input ~ a [ : Html . a_id export_filename_id ; Html . a_input_type ` Text ; Html . a_class [ " form - control " ] ; Html . a_placeholder " file name " ] ( ) in let export_button = Html . button ~ a [ : Html . a_id export_button_id ; Html . Unsafe . string_attrib " role " " button " ; Html . a_class [ " btn " ; " btn - default " ; " pull - right " ] ] [ Html . cdata " export " ] in let export_formats_select = List . map ( fun format -> [ % html { |< option value " } =| format { " } ( |>| Html . cdata format ) { |</ option } ] ) >| export_formats in [ % html { |< div class " = row " > < div class " = col - sm - 12 " > < div class " = form - inline " > < div class " = form - group " > < select class " = form - control " id " } =| export_select_id { " |>< option value " = png " > png </ option >< option value " = svg " > svg </ option } >| export_formats_select { |</ select > </ div > < div class " = form - group " > < label class " = checkbox - inline " > } [ | export_filename ] { | </ label > </ div > < div class " = form - group " > < label class " = checkbox - inline " > } [ | export_button ] { | </ label > </ div > </ div > </ div > </ div } ] >| |
let label_news tab_is_active counter = let count = React . S . map ( fun model -> let simulation_info = State_simulation . t_simulation_info model in counter simulation_info ) State_simulation . model in let bip = ref React . E . never in let labels , set_labels = ReactiveData . RList . create [ ] in let _ = React . S . map ( fun tab_active -> let ( ) = React . E . stop ! bip in if tab_active then ReactiveData . RList . set set_labels [ ] else bip := React . E . map ( fun v -> ReactiveData . RList . set set_labels ( if v > 0 then [ Html . txt " " ; Html . span ~ a [ : Html . a_class [ " label " ; " label - default " ] ] [ Html . txt " New " ; ] ] else [ ] ) ) ( React . S . changes count ) ) tab_is_active in labels |
let badge ( counter : Api_types_j . simulation_info option -> int ) = let badge , badge_handle = ReactiveData . RList . create [ ] in let _ = React . S . map ( fun model -> let simulation_info = State_simulation . t_simulation_info model in let count = counter simulation_info in if count > 0 then ReactiveData . RList . set badge_handle [ Html . txt " " ; Html . span ~ a [ : Html . a_class [ " badge " ] ; ] [ Html . txt ( string_of_int count ) ; ] ; ] else ReactiveData . RList . set badge_handle [ ] ) State_simulation . model in badge |
let arguments ( key : string ) : string list = List . map snd ( List . filter ( fun ( k , _ ) -> key = k ) Url . Current . arguments ) |
let version ( ? test ' : a option = None ) ( ~ prod ' : a ) ( ~ dev ' : a ) ' : a = let version : string list = arguments " version " in match ( test , version ) with | ( Some test , [ " test " ] ) -> test | ( _ , [ " dev " ] ) -> dev | _ -> prod |
let navli label force_class decorations = let default_attributes = [ Html . a_id ( " nav " ^ label ) ; Html . a_role [ " presentation " ] ] in let attributes = match force_class with | None -> default_attributes | Some l -> ( Tyxml_js . R . Html5 . a_class l ) :: default_attributes in let text = ReactiveData . RList . concat ( ReactiveData . RList . singleton ( Html . cdata label ) ) decorations in Html . li ~ a : attributes [ Tyxml_js . R . Html . a ~ a [ : Html . Unsafe . string_attrib " data - toggle " " tab " ; Html . a_role [ " tab " ] ; Html . Unsafe . string_attrib " aria - controls " label ; Html . a_href ( " " #^ label ) ] text ] |
let navtabs nav_tab_id = function | [ ] | ( _ , Some _ , _ ) :: _ -> Common . toss " ui_common . navtabs : missing tabs " | ( ti , None , l ) :: t -> Html . ul ~ a [ : Html . a_id nav_tab_id ; Html . a_class [ " nav " ; " nav - tabs " ] ; Html . Unsafe . string_attrib " role " " tablist " ] ( navli ti ( Some ( React . S . const [ " active " ] ) ) l :: List . map ( fun ( t , a_class , li ) -> navli t a_class li ) t ) |
let onenavcontent label active classes content = Html . div ~ a [ : Html . a_id label ; Html . a_class ( if active then " flex - content " :: " tab - pane " :: " active " :: classes else " flex - content " :: " tab - pane " :: classes ) ; Html . Unsafe . string_attrib " role " " tabpanel " ] content |
let navcontent ? id classes = function | [ ] -> Common . toss " ui_common . navcontent : missing content " | ( t , cl , c ) :: l -> let id : [ > ` Id ] Html . attrib list = match id with | None -> [ ] | Some id -> [ Html . a_id id ] in Html . div ~ a ( [ : Html . a_class ( [ " panel - content " ; " tab - content " ; " flex - content " ] @ classes ) ; ] @ id ) ( onenavcontent t true cl c :: List . map ( fun ( t , cl , c ) -> onenavcontent t false cl c ) l ) |
let level ? debug ? info ? notice ? warning ? error ? fatal ( ) : ' a list = let level : string list = arguments " level " in let extract key value = match value with | None -> [ ] | Some value -> if List . mem key level then [ value ] else [ ] in ( extract " debug " debug ) @ ( extract " info " info ) @ ( extract " notice " notice ) @ ( extract " warning " warning ) @ ( extract " error " error ) @ ( extract " fatal " fatal ) @ [ ] |
let features ( ? default [ ] ) = ( options ( : string * ' a ) list ) : ' a list = let features : string list = arguments " feature " in let matches : ' a list = List . map snd ( List . filter ( fun ( feature , _ ) -> List . mem feature features ) options ) in match matches with | [ ] -> default | _ :: _ -> matches |
let input_change input_dom signal_handler = input_dom . ## onchange := Dom_html . handler ( fun _ -> let ( ) = signal_handler ( Js . to_string ( input_dom . ## value ) ) in Js . _true ) |
module type Menu = sig val content : unit -> [ > ` Button | ` Div | ` Ul | ` A of [ > ` PCDATA | ` Span ] ] Tyxml_js . Html5 . elt list val onload : unit -> unit end ; ; |
module type Div = sig val id : string val content : unit -> Html_types . div_content_fun Tyxml_js . Html . elt list val onload : unit -> unit end ; ; |
module type Tab = sig val navli : unit -> Html_types . flow5_without_interactive Tyxml_js . Html5 . elt ReactiveData . RList . t val content : unit -> Html_types . div_content_fun Tyxml_js . Html5 . elt list val onload : unit -> unit val onresize : unit -> unit end ; ; |
module type SubTab = sig include Tab val parent_hide : unit -> unit val parent_shown : unit -> unit end |
module type Panel = sig val content : unit -> Html_types . div Tyxml_js . Html5 . elt val onload : unit -> unit val onresize : unit -> unit end ; ; |
let id_dom ( id : string ) : ' a Js . t = Js . Unsafe . coerce ( ( Js . Opt . get ( document ## getElementById ( Js . string id ) ) ( fun ( ) -> Common . toss ( Format . sprintf " ui_common . id_dom : could not find id % s " id ) ) ) : Dom_html . element Js . t ) |
let create_modal ( ~ id : string ) ( ~ title_label : string ) ( ~ body : [ < Html_types . div_content_fun ] Html . elt Html . list_wrap ) ( ~ submit_label : string ) ( ~ submit : ( ' self Js . t , _ Js . t ) Dom . event_listener ) : [ > Html_types . div ] Html . elt = let button = Html . button ~ a [ : Html . a_button_type ` Submit ; Html . a_class [ " btn " ; " btn - primary " ; ] ] [ Html . txt submit_label ] in let form = Html . form ~ a [ : Html . a_class [ " modal - content " ] ] [ Html . div ~ a [ : Html . a_class [ " modal - header " ] ] [ Html . button ~ a [ : Html . Unsafe . string_attrib " type " " button " ; Html . a_class [ " close " ] ; Html . Unsafe . string_attrib " data - dismiss " " modal " ; Html . Unsafe . string_attrib " aria - label " " Close " ; ] [ Html . span ~ a [ : Html . Unsafe . string_attrib " aria - hidden " " true " ] [ Html . entity " times " ] ] ; Html . h4 [ Html . cdata title_label ] ] ; Html . div ~ a [ : Html . a_class [ " modal - body " ] ] body ; Html . div ~ a [ : Html . a_class [ " modal - footer " ] ] ( [ Html . button ~ a [ : Html . Unsafe . string_attrib " type " " button " ; Html . a_class [ " btn " ; " btn - default " ] ; Html . Unsafe . string_attrib " data - dismiss " " modal " ; ] [ Html . cdata " Cancel " ] ; ] [ @ button ] ) ] in let ( ) = ( Tyxml_js . To_dom . of_form form ) . ## onsubmit := submit in Html . div ~ a [ : Html . a_class [ " modal " ; " fade " ] ; Html . a_id id ; Html . Unsafe . string_attrib " tabindex " " - 1 " ; Html . Unsafe . string_attrib " role " " dialog " ; ] [ Html . div ~ a [ : Html . a_class [ " modal - dialog " ] ; Html . Unsafe . string_attrib " role " " document " ; ] [ form ] ] |
type token = | DEF | EXTERN | PLUS | MINUS | MUL | DIV | LPAREN | RPAREN | SEMI | COMMA | EOF | NUMBER of float | KWD of char | IDENT of string |
let regexp newline = ( ' \ 010 ' | ' \ 013 ' | " \ 013 \ 010 ) " |
let regexp blank = [ ' ' ' \ 009 ' ' \ 012 ' ] 012 ' |
let regexp blanks = blank + |
let regexp lowercase = [ ' a ' - ' z ' ' \ 223 ' - ' \ 246 ' ' \ 248 ' - ' \ 255 ' ' _ ' ] ' _ ' |
let regexp uppercase = [ ' A ' - ' Z ' ' \ 192 ' - ' \ 214 ' ' \ 216 ' - ' \ 222 ' ] 222 ' |
let regexp whitespace = ( blank | newline ) newline |
let regexp underscore = " _ " |
let regexp tilde = " " ~ |
let regexp identchar = [ ' A ' - ' Z ' ' a ' - ' z ' ' _ ' ' \ 192 ' - ' \ 214 ' ' \ 216 ' - ' \ 246 ' ' \ 248 ' - ' \ 255 ' ' \ ' ' ' 0 ' - ' 9 ' ] ' 9 ' |
let regexp symbolchar = [ ' ! ' ' $ ' ' % ' ' & ' ' * ' ' + ' ' - ' ' . ' ' / ' ' : ' ' < ' ' = ' ' > ' ' ? ' ' @ ' ' ^ ' ' | ' ' ~ ' ] ' |
let regexp lident = lowercase identchar * |
let regexp uidnet = uppercase identchar * |
let regexp decimal_literal = [ ' 0 ' - ' 9 ' ] ' 9 ' [ ' 0 ' - ' 9 ' ' _ ' ] ' _ ' * |
let regexp hex_literal = ' 0 ' [ ' x ' ' X ' ] ' X ' [ ' 0 ' - ' 9 ' ' A ' - ' F ' ' a ' - ' f ' ] ' f ' [ ' f ' ' 0 ' - ' 9 ' ' A ' - ' F ' ' a ' - ' f ' ' _ ' ] ' _ ' * |
let regexp oct_literal = ' 0 ' [ ' o ' ' O ' ] ' O ' [ ' 0 ' - ' 7 ' ] ' 7 ' [ ' 0 ' - ' 7 ' ' _ ' ] ' _ ' * |
let regexp bin_literal = ' 0 ' [ ' b ' ' B ' ] ' B ' [ ' 0 ' - ' 1 ' ] ' 1 ' [ ' 0 ' - ' 1 ' ' _ ' ] ' _ ' * |
let regexp int_literal = decimal_literal | hex_literal | oct_literal | bin_literal |
let regexp float_literal = [ ' 0 ' - ' 9 ' ] ' 9 ' [ ' 0 ' - ' 9 ' ' _ ' ] ' _ ' * ( ' . ' [ ' 0 ' - ' 9 ' ' _ ' ] ' _ ' * ) ? ( [ ' e ' ' E ' ] ' E ' [ ' + ' ' - ' ] ' ? [ ' 0 ' - ' 9 ' ] ' 9 ' [ ' 0 ' - ' 9 ' ' _ ' ] ' _ ' * ) ? |
let first = ref true |
let rec alex pos = lexer | " ; " -> SEMI | ( " " -> LPAREN | ) " " -> RPAREN | " " + -> PLUS | " " - -> MINUS | " " * -> MUL | " " / -> DIV | " extern " -> EXTERN | " def " -> DEF | " , " -> COMMA | " \ n " -> new_line lexbuf pos ; alex pos lexbuf | float_literal -> ( NUMBER ( ( u8l |- float_of_string ) float_of_string lexbuf ) lexbuf ) lexbuf | lident -> IDENT ( u8l lexbuf ) lexbuf | blank + -> alex pos lexbuf | eof -> if ! first then ( first := false ; EOF ) EOF else raise Ulexing . Error | " " # -> comment pos lexbuf | _ -> raise Ulexing . Error | ' \ n ' -> new_line lexbuf pos ; alex pos lexbuf | _ -> comment pos lexbuf |
let test filename = Legacy ( . let revised_parser = menhir_with_ulex alex main in let chan = open_in filename in let lexbuf = Ulexing . from_utf8_channel chan in handle_ulexing_error revised_parser lexbuf ; close_in chan ) |
let test_lexer filename = Legacy ( . let chan = open_in filename in let lexbuf = Ulexing . from_utf8_channel chan in let tokens = tokens_of_buf alex lexbuf in Stream . iter ( dump |- print_string |- print_newline ) print_newline tokens ) |
let _ = test " sample . py " |
type separator = [ ` CR | ` LF | ` CRLF | ` NEL | ` LS | ` PS ] let sp = match op with ` CR -> [ UChar . chr_of_uint 0x000d ] 0x000d | ` LF -> [ UChar . chr_of_uint 0x000a ] 0x000a | ` CRLF -> [ UChar . chr_of_uint 0x000d ; UChar . chr_of_uint 0x000a ] 0x000a | ` NEL -> [ UChar . chr_of_uint 0x0085 ] 0x0085 | ` LS -> [ UChar . chr_of_uint 0x2028 ] 0x2028 | ` PS -> [ UChar . chr_of_uint 0x2029 ] 0x2029 in let sp_hd = List . hd sp in let sp_tl = List . tl sp in object ( self ) self val mutable wait = false val mutable out_buf = [ ] method get ( get ) get = match out_buf with u :: rest -> out_buf <- rest ; u | [ ] -> let u = inchan # get ( get ) get in if wait then begin wait <- false ; match UChar . uint_code u with 0x000a -> out_buf <- sp_tl ; sp_hd | 0x000d -> wait <- true ; out_buf <- sp_tl ; sp_hd | 0x0085 -> out_buf <- sp_tl @ sp ; sp_hd | _ -> out_buf <- sp_tl @ [ u ] u ; sp_hd end else match UChar . uint_code u with 0x000d -> wait <- true ; self # get ( get ) get | 0x000a | 0x0085 -> out_buf <- sp_tl ; sp_hd | _ -> u method close_in ( ) = out_buf <- [ ] ; inchan # close_in ( ) end let sp = match op with ` CR -> [ UChar . chr_of_uint 0x000d ] 0x000d | ` LF -> [ UChar . chr_of_uint 0x000a ] 0x000a | ` CRLF -> [ UChar . chr_of_uint 0x000d ; UChar . chr_of_uint 0x000a ] 0x000a | ` NEL -> [ UChar . chr_of_uint 0x0085 ] 0x0085 | ` LS -> [ UChar . chr_of_uint 0x2028 ] 0x2028 | ` PS -> [ UChar . chr_of_uint 0x2029 ] 0x2029 in object ( self ) self val mutable wait = false method private output_newline = List . iter outchan # put sp method put u = if wait then begin wait <- false ; match UChar . uint_code u with 0x000a -> ( ) | _ -> self # put u end else match UChar . uint_code u with 0x000d -> self # output_newline ; wait <- true | 0x000a | 0x0085 | 0x2028 | 0x2029 -> self # output_newline | _ -> outchan # put u method close_out ( ) = wait <- false ; outchan # close_out ( ) method flush : unit -> unit = outchan # flush end |
module type Type = sig type text class input_line : UChar . t # obj_input_channel -> [ text ] text obj_input_channel class output_line : ? sp [ ` : CR | ` CRLF | ` LF | ` LS | ` NEL | ` PS ] PS -> UChar . t # obj_output_channel -> [ text ] text obj_output_channel end |
module Make ( Text : UnicodeString . Type ) Type = struct type text = Text . t class input_line inchan = object val b = Text . Buf . create 0 val mutable wait = false method get ( get ) get = Text . Buf . clear b ; let rec loop ( ) = let x = wait in wait <- false ; match UChar . uint_code ( inchan # get ( get ) get ) get with 0x0a -> if x then loop ( ) else ( ) | 0x0d -> wait <- true | 0x85 | 0x0c | 0x2028 | 0x2029 -> ( ) | n -> Text . Buf . add_char b ( UChar . chr_of_uint n ) n ; loop ( ) in try loop ( ) ; Text . Buf . contents b with End_of_file -> if Text . length ( Text . Buf . contents b ) b > 0 then Text . Buf . contents b else raise End_of_file method close_in ( ) : unit = Text . Buf . reset b ; inchan # close_in ( ) end class output_line ( ? sp : separator ` = LF ) LF outchan = let sp = match sp with ` CR -> [ UChar . chr_of_uint 0x000d ] 0x000d | ` LF -> [ UChar . chr_of_uint 0x000a ] 0x000a | ` CRLF -> [ UChar . chr_of_uint 0x000d ; UChar . chr_of_uint 0x000a ] 0x000a | ` NEL -> [ UChar . chr_of_uint 0x0085 ] 0x0085 | ` LS -> [ UChar . chr_of_uint 0x2028 ] 0x2028 | ` PS -> [ UChar . chr_of_uint 0x2029 ] 0x2029 in object ( self ) self method private output_newline = List . iter outchan # put sp method put t = Text . iter outchan # put t ; self # output_newline method flush : unit -> unit = outchan # flush method close_out ( ) : unit = outchan # close_out ( ) end end |
let umap_of_imap m = m |
let imap_of_umap m = m |
let add ? eq u v m = IMap . add ? eq ( UChar . uint_code u ) u v m |
let add_range ? eq u1 u2 v m = IMap . add_range ? eq ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 v m |
let find u m = IMap . find ( UChar . uint_code u ) u m |
let remove u m = IMap . remove ( UChar . uint_code u ) u m |
let remove_range u1 u2 m = IMap . remove_range ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 m |
let from u m = IMap . from ( UChar . uint_code u ) u m |
let after u m = IMap . after ( UChar . uint_code u ) u m |
let until u m = IMap . until ( UChar . uint_code u ) u m |
let before u m = IMap . before ( UChar . uint_code u ) u m |
let mem u m = IMap . mem ( UChar . uint_code u ) u m |
let iter f m = let f ' n = f ( UChar . chr_of_uint n ) n in IMap . iter f ' m |
let iter_range f m = let f ' n1 n2 = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 in IMap . iter_range f ' m |
let fold f m a = let f ' n v a = f ( UChar . chr_of_uint n ) n v a in IMap . fold f ' m a |
let fold_range f m a = let f ' n1 n2 v a = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 v a in IMap . fold_range f ' m a |
let mapi ? eq f m = let f ' n v = f ( UChar . chr_of_uint n ) n v in IMap . mapi ? eq f ' m |
let set_to_map s = IMap . set_to_map ( USet . iset_of_uset s ) s |
let domain m = USet . uset_of_iset ( IMap . domain m ) m |
let map_to_set p m = USet . uset_of_iset ( IMap . map_to_set p m ) m |
module UnannotatedDefine = struct type t = { define : Define . Signature . t ; location : Location . WithModule . t ; } [ @@ deriving sexp , compare ] end |
module ImportEntry = struct type t = | Module of { target : Reference . t ; implicit_alias : bool ; } | Name of { from : Reference . t ; target : Identifier . t ; implicit_alias : bool ; } [ @@ deriving sexp , compare ] let deprecated_original_name = function | Module { target ; implicit_alias } -> if implicit_alias then Option . value_exn ( Reference . head target ) else target | Name { from ; target ; _ } -> ( match Reference . show from with | " future . builtins " | " builtins " -> Reference . create target | _ -> Reference . create target |> Reference . combine from ) end |
type t = | SimpleAssign of { explicit_annotation : Expression . t option ; value : Expression . t ; target_location : Location . WithModule . t ; } | TupleAssign of { value : Expression . t ; target_location : Location . WithModule . t ; index : int ; total_length : int ; } | Imported of ImportEntry . t | Define of UnannotatedDefine . t list | Class |
module Collector = struct module Result = struct type unannotated_global = t [ @@ deriving sexp , compare ] type t = { name : Identifier . t ; unannotated_global : unannotated_global ; } [ @@ deriving sexp , compare ] end let from_source { Source . statements ; source_path = { ModulePath . qualifier ; _ } ; _ } = let rec visit_statement ~ qualifier globals { Node . value ; location } = match value with | Statement . Assign { Assign . target = { Node . value = Name ( Name . Identifier identifier ) ; location } ; annotation ; value ; _ ; } -> { Result . name = Identifier . sanitized identifier ; unannotated_global = SimpleAssign { explicit_annotation = annotation ; value ; target_location = Location . with_module ~ module_reference : qualifier location ; } ; } :: globals | Statement . Assign { Assign . target = { Node . value = Tuple elements ; _ } ; value ; _ } -> let valid = let total_length = List . length elements in let is_simple_name index = function | { Node . value = Expression . Name ( Name . Identifier identifier ) ; location } -> Some { Result . name = Identifier . sanitized identifier ; unannotated_global = TupleAssign { value ; target_location = Location . with_module ~ module_reference : qualifier location ; index ; total_length ; } ; } | _ -> None in List . mapi elements ~ f : is_simple_name in List . rev_append ( Option . all valid |> Option . value ~ default [ ] ) : globals | Import { Import . from = None ; imports } -> let collect_module_import sofar { Node . value = { Import . name = target ; alias } ; _ } = let implicit_alias , name = match alias with | None -> true , Reference . as_list target |> List . hd_exn | Some alias -> false , alias in { Result . name ; unannotated_global = Imported ( ImportEntry . Module { target ; implicit_alias } ) ; } :: sofar in List . fold imports ~ init : globals ~ f : collect_module_import | Import { Import . from = Some from ; imports } -> let collect_name_import sofar { Node . value = { Import . name = target ; alias } ; _ } = match Reference . show target with | " " * -> sofar | target -> let implicit_alias , name = match alias with | None -> true , target | Some alias -> false , alias in { Result . name ; unannotated_global = Imported ( ImportEntry . Name { from ; target ; implicit_alias } ) ; } :: sofar in List . fold imports ~ init : globals ~ f : collect_name_import | Class { Class . name ; _ } -> { Result . name = name |> Reference . last ; unannotated_global = Class } :: globals | Define { Define . signature = { Define . Signature . name ; _ } as signature ; _ } -> { Result . name = name |> Reference . last ; unannotated_global = Define [ { define = signature ; location = Location . with_module ~ module_reference : qualifier location ; } ; ] ; } :: globals | If { If . body ; orelse ; _ } -> List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) ( body @ orelse ) | Try { Try . body ; handlers ; orelse ; finally } -> let globals = List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) body in let globals = let handlers_statements = List . concat_map handlers ~ f ( : fun { Try . Handler . body ; _ } -> body ) in List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) handlers_statements in let globals = List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) orelse in List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) finally | With { With . body ; _ } -> List . fold ~ init : globals ~ f ( : visit_statement ~ qualifier ) body | _ -> globals in List . fold ~ init [ ] : ~ f ( : visit_statement ~ qualifier ) statements |> List . rev end |
module ResolvedReference = struct type export = | FromModuleGetattr | Exported of Module . Export . Name . t [ @@ deriving sexp , compare , hash ] type t = | Module of Reference . t | ModuleAttribute of { from : Reference . t ; name : Identifier . t ; export : export ; remaining : Identifier . t list ; } | PlaceholderStub of { stub_module : Reference . t ; remaining : Identifier . t list ; } [ @@ deriving sexp , compare , hash ] end |
let missing_builtin_globals = let assign name annotation = { UnannotatedGlobal . Collector . Result . name ; unannotated_global = UnannotatedGlobal . SimpleAssign { explicit_annotation = Some ( Type . expression annotation ) ; target_location = Location . WithModule . any ; value = Node . create_with_default_location ( Expression . Constant Constant . Ellipsis ) ; } ; } in [ assign " . . . " Type . Any ; assign " __debug__ " Type . bool ] |
let missing_builtin_classes , missing_typing_classes , missing_typing_extensions_classes = let make ( ? bases = [ ] ) ( ? metaclasses = [ ] ) ( ? body = [ ] ) name = let create_base annotation = { Call . Argument . name = None ; value = Type . expression annotation } in let create_metaclass annotation = { Call . Argument . name = Some ( Node . create_with_default_location " metaclass " ) ; value = Type . expression annotation ; } in { Class . name = Reference . create name ; base_arguments = List . map bases ~ f : create_base @ List . map metaclasses ~ f : create_metaclass ; body ; decorators = [ ] ; top_level_unbound_names = [ ] ; } |> Node . create_with_default_location in let single_unary_generic = [ Type . parametric " typing . Generic " [ Single ( Variable ( Type . Variable . Unary . create " typing . _T " ) ) ] ] in let catch_all_generic = [ Type . parametric " typing . Generic " [ Single Any ] ] in let callable_body = [ Statement . Assign { target = Node . create_with_default_location ( Expression . Name ( Ast . Expression . create_name ~ location : Location . any " typing . Callable . __call__ " ) ) ; annotation = Some ( Type . expression Type . object_primitive ) ; value = Node . create_with_default_location ( Expression . Constant Constant . NoneLiteral ) ; } ; ] |> List . map ~ f : Node . create_with_default_location in let make_dunder_get ~ parent ~ host ~ host_type ~ return = let parent = Reference . create parent in Statement . Define { signature = { name = Reference . combine parent ( Reference . create " __get__ " ) ; parameters = [ Node . create_with_default_location { Ast . Expression . Parameter . name = " self " ; value = None ; annotation = None } ; Node . create_with_default_location { Ast . Expression . Parameter . name = " host " ; value = None ; annotation = Some ( Type . expression host ) ; } ; Node . create_with_default_location { Ast . Expression . Parameter . name = " host_type " ; value = Some ( Node . create_with_default_location ( Expression . Constant Constant . NoneLiteral ) ) ; annotation = Some ( Type . expression host_type ) ; } ; ] ; decorators = [ ] ; return_annotation = Some ( Type . expression return ) ; async = false ; generator = false ; parent = Some parent ; nesting_define = None ; } ; captures = [ ] ; unbound_names = [ ] ; body = [ ] ; } in let classmethod_body = [ make_dunder_get ~ parent " : typing . ClassMethod " ~ host : Type . object_primitive ~ host_type ( : Variable ( Type . Variable . Unary . create " typing . _S " ) ) ~ return : ( Type . parametric " BoundMethod " [ Single ( Variable ( Type . Variable . Unary . create " typing . _T " ) ) ; Single ( Variable ( Type . Variable . Unary . create " typing . _S " ) ) ; ] ) ; ] |> List . map ~ f : Node . create_with_default_location in let staticmethod_body = [ make_dunder_get ~ parent " : typing . StaticMethod " ~ host : Type . object_primitive ~ host_type : Type . object_primitive ~ return ( : Variable ( Type . Variable . Unary . create " typing . _T " ) ) ; ] |> List . map ~ f : Node . create_with_default_location in let generic_meta_body = [ Statement . Define { signature = { name = Reference . create " typing . GenericMeta . __getitem__ " ; parameters = [ { Parameter . name = " cls " ; value = None ; annotation = None } |> Node . create_with_default_location ; { Parameter . name = " arg " ; value = None ; annotation = None } |> Node . create_with_default_location ; ] ; decorators = [ ] ; return_annotation = None ; async = false ; generator = false ; parent = Some ( Reference . create " typing . GenericMeta " ) ; nesting_define = None ; } ; captures = [ ] ; unbound_names = [ ] ; body = [ ] ; } |> Node . create_with_default_location ; ] in let typing_classes = [ make " typing . Optional " ~ bases : single_unary_generic ; make " typing . NoReturn " ; make " typing . Annotated " ~ bases : catch_all_generic ; make " typing . Protocol " ~ bases : catch_all_generic ; make " typing . Callable " ~ bases : catch_all_generic ~ body : callable_body ; make " typing . FrozenSet " ~ bases : single_unary_generic ; make " typing . ClassVar " ~ bases : single_unary_generic ; make " typing . Final " ~ bases : catch_all_generic ; make " typing . Literal " ~ bases : catch_all_generic ; make " typing . Union " ~ bases : catch_all_generic ; make ~ metaclasses [ : Primitive " typing . GenericMeta " ] " typing . Generic " ; make " typing . ClassMethod " ~ bases : single_unary_generic ~ body : classmethod_body ; make " typing . StaticMethod " ~ bases : single_unary_generic ~ body : staticmethod_body ; make " typing . GenericMeta " ~ bases [ : Primitive " type " ] ~ body : generic_meta_body ; make " typing . TypeGuard " ~ bases ( : Type . bool :: single_unary_generic ) ; ] in let typing_extension_classes = [ make " typing_extensions . Final " ; make " typing_extensions . Literal " ~ bases : catch_all_generic ; make " typing_extensions . Annotated " ~ bases : catch_all_generic ; make " typing_extensions . TypeAlias " ; make " typing_extensions . TypeGuard " ~ bases ( : Type . bool :: single_unary_generic ) ; ] in let builtin_classes = let t_self_expression = Expression . Name ( Name . Identifier " TSelf " ) |> Node . create_with_default_location in [ make ~ bases [ : Type . parametric " typing . Mapping " [ Single Type . string ; Single Type . object_primitive ] ] ~ body ( : Type . TypedDictionary . defines ~ t_self_expression ~ total : true ) ( Type . TypedDictionary . class_name ~ total : true ) ; make ~ bases [ : Type . parametric " typing . Mapping " [ Single Type . string ; Single Type . object_primitive ] ] ~ body ( : Type . TypedDictionary . defines ~ t_self_expression ~ total : false ) ( Type . TypedDictionary . class_name ~ total : false ) ; make ~ bases : [ Type . parametric " typing . Generic " [ Single ( Type . variable " typing . _T " ) ; Single ( Type . variable " typing . _S " ) ] ; Type . Primitive " typing . Callable " ; ] " BoundMethod " ; ] in builtin_classes , typing_classes , typing_extension_classes |
module KeyTracker = struct module ClassKeyValue = struct type t = Identifier . t list [ @@ deriving compare ] let prefix = Prefix . make ( ) let description = " Class keys " end module DefineKeyValue = struct type t = Reference . t list [ @@ deriving compare ] let prefix = Prefix . make ( ) let description = " Define keys " end module UnannotatedGlobalKeyValue = struct type t = Reference . t list [ @@ deriving compare ] let prefix = Prefix . make ( ) let description = " Class keys " end module ClassKeys = Memory . FirstClass . WithCache . Make ( SharedMemoryKeys . ReferenceKey ) ( ClassKeyValue ) module DefineKeys = Memory . FirstClass . WithCache . Make ( SharedMemoryKeys . ReferenceKey ) ( DefineKeyValue ) module UnannotatedGlobalKeys = Memory . FirstClass . WithCache . Make ( SharedMemoryKeys . ReferenceKey ) ( UnannotatedGlobalKeyValue ) type t = { class_keys : ClassKeys . t ; unannotated_global_keys : UnannotatedGlobalKeys . t ; define_keys : DefineKeys . t ; } let create ( ) = { class_keys = ClassKeys . create ( ) ; unannotated_global_keys = UnannotatedGlobalKeys . create ( ) ; define_keys = DefineKeys . create ( ) ; } let add_class_keys { class_keys ; _ } = ClassKeys . add class_keys let add_define_keys { define_keys ; _ } = DefineKeys . add define_keys let add_unannotated_global_keys { unannotated_global_keys ; _ } = UnannotatedGlobalKeys . add unannotated_global_keys let get_class_keys { class_keys ; _ } qualifiers = ClassKeys . KeySet . of_list qualifiers |> ClassKeys . get_batch class_keys |> ClassKeys . KeyMap . values |> List . filter_map ~ f : Fn . id |> List . concat let get_define_keys { define_keys ; _ } qualifiers = DefineKeys . KeySet . of_list qualifiers |> DefineKeys . get_batch define_keys |> DefineKeys . KeyMap . values |> List . filter_map ~ f : Fn . id |> List . concat let get_unannotated_global_keys { unannotated_global_keys ; _ } qualifiers = UnannotatedGlobalKeys . KeySet . of_list qualifiers |> UnannotatedGlobalKeys . get_batch unannotated_global_keys |> UnannotatedGlobalKeys . KeyMap . values |> List . filter_map ~ f : Fn . id |> List . concat module PreviousKeys = struct type t = { previous_classes_list : Type . Primitive . t list ; previous_classes : Type . Primitive . Set . t ; previous_defines_list : Reference . t list ; previous_defines : Reference . Set . t ; previous_unannotated_globals_list : Reference . t list ; previous_unannotated_globals : Reference . Set . t ; } end let get_previous_keys_and_clear ( { class_keys ; define_keys ; unannotated_global_keys } as key_tracker ) invalidated_modules = let previous_classes_list = get_class_keys key_tracker invalidated_modules in let previous_defines_list = get_define_keys key_tracker invalidated_modules in let previous_unannotated_globals_list = get_unannotated_global_keys key_tracker invalidated_modules in let previous_classes = Type . Primitive . Set . of_list previous_classes_list in let previous_defines = Reference . Set . of_list previous_defines_list in let previous_unannotated_globals = Reference . Set . of_list previous_unannotated_globals_list in ClassKeys . KeySet . of_list invalidated_modules |> ClassKeys . remove_batch class_keys ; DefineKeys . KeySet . of_list invalidated_modules |> DefineKeys . remove_batch define_keys ; UnannotatedGlobalKeys . KeySet . of_list invalidated_modules |> UnannotatedGlobalKeys . remove_batch unannotated_global_keys ; PreviousKeys . { previous_classes_list ; previous_classes ; previous_defines_list ; previous_defines ; previous_unannotated_globals_list ; previous_unannotated_globals ; } end |
module ModuleValue = struct type t = Module . t let prefix = Prefix . make ( ) let description = " Module " let compare = Module . compare end |
module Modules = struct include DependencyTrackedMemory . DependencyTrackedTableWithCache ( SharedMemoryKeys . ReferenceKey ) ( DependencyKey ) ( ModuleValue ) let is_qualifier = true let key_to_reference = Fn . id end |
module ClassSummaryValue = struct type t = ClassSummary . t Node . t let prefix = Prefix . make ( ) let description = " ClassSummary " let compare = Node . compare ClassSummary . compare end |
module ClassSummaries = struct include DependencyTrackedMemory . DependencyTrackedTableWithCache ( SharedMemoryKeys . StringKey ) ( DependencyKey ) ( ClassSummaryValue ) let is_qualifier = false let key_to_reference name = Reference . create name end |
module FunctionDefinitionValue = struct type t = FunctionDefinition . t let description = " FunctionDefinition " let prefix = Prefix . make ( ) let compare = FunctionDefinition . compare end |
module FunctionDefinitions = struct include DependencyTrackedMemory . DependencyTrackedTableWithCache ( SharedMemoryKeys . ReferenceKey ) ( DependencyKey ) ( FunctionDefinitionValue ) let is_qualifier = false let key_to_reference = Fn . id end |
module UnannotatedGlobalValue = struct type t = UnannotatedGlobal . t let prefix = Prefix . make ( ) let description = " UnannotatedGlobal " let compare = UnannotatedGlobal . compare end |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.