text
stringlengths 12
786k
|
---|
let rec add_size bv s = match s . desc with | Sconst _ -> ( ) | Sname id -> add bv id | Sop ( _ , s1 , s2 ) -> add_size bv s1 ; add_size bv s2 |
let rec add_type_expr bv ty = match ty . desc with | Etypevar _ -> ( ) | Etypeconstr ( id , tel ) -> add bv id ; List . iter ( add_type_expr bv ) tel | Etypetuple tl -> List . iter ( add_type_expr bv ) tl | Etypevec ( ty , s ) -> add_type_expr bv ty ; add_size bv s | Etypefun ( _ , _ , ty1 , ty2 ) -> add_type_expr bv ty1 ; add_type_expr bv ty2 |
let rec add_interface bv i = match i . desc with | Einter_open s -> if not ( StringSet . mem s ! bv ) then bv := StringSet . add s ! bv | Einter_typedecl ( _ , _ , tdl ) -> add_type_decl bv tdl | Einter_constdecl ( _ , te ) -> add_type_expr bv te match td . desc with | Eabstract_type -> ( ) | Eabbrev te -> add_type_expr bv te | Evariant_type _ -> ( ) | Erecord_type l -> List . iter ( fun ( _ , te ) -> add_type_expr bv te ) l match i . desc with | Eopen s -> if not ( StringSet . mem s ! bv ) then bv := StringSet . add s ! bv ; | Etypedecl ( _ , _ , td ) -> add_type_decl bv td | Econstdecl ( _ , _ , e ) -> add_exp bv e | Efundecl ( _ , { f_args ; f_body ; _ } ) -> List . iter ( add_pattern bv ) f_args ; add_exp bv f_body match exp . desc with | Evar l -> add bv l | Econst _ -> ( ) | Econstr0 c -> add bv c | Econstr1 ( c , el ) -> add bv c ; List . iter ( add_exp bv ) el | Elast _ -> ( ) | Eapp ( _ , e , el ) -> add_exp bv e ; List . iter ( add_exp bv ) el | Eop ( op , el ) -> add_op bv op ; List . iter ( add_exp bv ) el | Etuple el -> List . iter ( add_exp bv ) el | Erecord_access ( e , id ) -> add_exp bv e ; add bv id | Erecord fields -> List . iter ( add_field bv ) fields | Erecord_with ( e , fields ) -> add_exp bv e ; List . iter ( add_field bv ) fields | Etypeconstraint ( e , ty ) -> add_exp bv e ; add_type_expr bv ty | Elet ( _ , pel , e ) -> List . iter ( add_eq bv ) pel ; add_exp bv e | Eseq ( e1 , e2 ) -> add_exp bv e1 ; add_exp bv e2 | Eperiod _ -> ( ) | Ematch ( e , mhl ) -> add_exp bv e ; List . iter ( add_match_handler add_exp bv ) mhl | Epresent ( phl , edo ) -> List . iter ( add_present_handler add_exp bv ) phl ; add_opt ( add_default add_exp ) bv edo | Eautomaton ( shl , seo ) -> List . iter ( add_state_handler add_exp bv ) shl ; add_opt add_state_exp bv seo | Ereset ( e1 , e2 ) -> add_exp bv e1 ; add_exp bv e2 | Eblock ( eqsb , body ) -> add_block add_eq_list bv eqsb ; add_exp bv body match op with | Efby | Eunarypre | Eifthenelse | Eminusgreater | Eup | Einitial | Edisc | Etest | Eaccess | Eupdate | Econcat | Eatomic -> ( ) | Eslice ( s1 , s2 ) -> add_size bv s1 ; add_size bv s2 match pat . desc with | Etuplepat pl -> List . iter ( add_pattern bv ) pl | Evarpat _ -> ( ) | Ewildpat -> ( ) | Econstpat _ -> ( ) | Econstr0pat c -> add bv c | Econstr1pat ( c , pl ) -> add bv c ; List . iter ( add_pattern bv ) pl | Ealiaspat ( p , _ ) -> add_pattern bv p | Eorpat ( p1 , p2 ) -> add_pattern bv p1 ; add_pattern bv p2 | Erecordpat pl -> List . iter ( fun ( lbl , p ) -> add bv lbl ; add_pattern bv p ) pl | Etypeconstraintpat ( p , ty ) -> add_pattern bv p ; add_type_expr bv ty match eq . desc with | EQeq ( p , e ) -> add_pattern bv p ; add_exp bv e | EQder ( _ , e , eo , phl ) -> add_exp bv e ; add_opt add_exp bv eo ; List . iter ( add_present_handler add_exp bv ) phl | EQinit ( _ , e ) | EQpluseq ( _ , e ) -> add_exp bv e | EQnext ( _ , e , eo ) -> add_exp bv e ; add_opt add_exp bv eo | EQemit ( _ , eo ) -> add_opt add_exp bv eo | EQautomaton ( shl , seo ) -> List . iter ( add_state_handler add_eq_list bv ) shl ; add_opt add_state_exp bv seo | EQpresent ( phl , bo ) -> List . iter ( add_present_handler ( add_block add_eq_list ) bv ) phl ; add_opt ( add_block add_eq_list ) bv bo | EQmatch ( e , mhl ) -> add_exp bv e ; List . iter ( add_match_handler ( add_block add_eq_list ) bv ) mhl | EQifthenelse ( e , bl , elsebl ) -> add_exp bv e ; add_block add_eq_list bv bl ; add_opt ( add_block add_eq_list ) bv elsebl | EQand eqs | EQbefore eqs -> add_eq_list bv eqs | EQreset ( eql , e ) -> List . iter ( add_eq bv ) eql ; add_exp bv e | EQblock block -> add_block add_eq_list bv block | EQforall handler -> add_forall_handler bv handler List . iter ( add_eq bv ) le fun a bv b -> let bb = b . desc in List . iter ( add_local bv ) bb . b_locals ; a bv bb . b_body let ( _ , eql ) = l . desc in List . iter ( add_eq bv ) eql match sp . desc with | Estate0pat _ -> ( ) | Estate1pat _ -> ( ) match se . desc with | Estate0 _ -> ( ) | Estate1 ( _ , el ) -> List . iter ( add_exp bv ) el add_scondpat bv e . e_cond ; add_opt ( add_block add_eq_list ) bv e . e_block ; add_state_exp bv e . e_next_state match scp . desc with | Econdand ( s1 , s2 ) -> add_scondpat bv s1 ; add_scondpat bv s2 | Econdor ( s1 , s2 ) -> add_scondpat bv s1 ; add_scondpat bv s2 | Econdexp e -> add_exp bv e | Econdon ( s , e ) -> add_scondpat bv s ; add_exp bv e | Econdpat ( e , p ) -> add_exp bv e ; add_pattern bv p fun a bv h -> add_pattern bv h . m_pat ; a bv h . m_body fun a bv h -> add_scondpat bv h . p_cond ; a bv h . p_body fun a bv { desc = h ; _ } -> add_statepat bv h . s_state ; add_block a bv h . s_block ; List . iter ( add_escape bv ) h . s_until ; List . iter ( add_escape bv ) h . s_unless List . iter ( add_index bv ) for_indexes ; List . iter ( add_init bv ) for_init ; add_block add_eq_list bv for_body match index . desc with | Einput ( _ , e ) -> add_exp bv e | Eoutput ( _ , _ ) -> ( ) | Eindex ( _ , e1 , e2 ) -> add_exp bv e1 ; add_exp bv e2 match init . desc with | Einit_last ( _ , e ) -> add_exp bv e |
let file traverse initial_structures file = let bv = ref initial_structures in List . iter ( traverse bv ) file ; ! bv |
let source_file ( ? initial_structures = StringSet . empty ) source = file add_implem initial_structures source |
let interface_file ( ? initial_structures = StringSet . empty ) interface = file add_interface initial_structures interface |
type char_prop = | Printable of int | Other | Null |
let to_array t = Array . of_list ( Zed_utf8 . explode t ) t |
let zero = String . make 1 ( Char . chr 0 ) 0 |
let core t = Zed_utf8 . unsafe_extract t 0 |
let combined t = List . tl ( Zed_utf8 . explode t ) t |
let prop_uChar uChar = match CharInfo_width . width uChar with | - 1 -> Other | 0 -> if UChar . code uChar = 0 then Null else Printable 0 | w -> Printable w |
let prop t = prop_uChar ( Zed_utf8 . unsafe_extract t 0 ) 0 |
let is_printable uChar = match prop_uChar uChar with | Printable _ -> true | _ -> false |
let is_printable_core uChar = match prop_uChar uChar with | Printable w when w > 0 -> true | _ -> false |
let is_combining_mark uChar = match prop_uChar uChar with | Printable w when w = 0 -> true | _ -> false |
let width t = CharInfo_width . width ( Zed_utf8 . unsafe_extract t 0 ) 0 |
let out_of_range t i = i < 0 || i >= size t |
let get_opt t i = try Some ( get t i ) i with _ -> None |
let append ch mark = match prop_uChar mark with | Printable 0 -> ch ^ ( Zed_utf8 . singleton mark ) mark | _ -> failwith " combining mark expected " |
let compare_core t1 t2 = let core1 = Zed_utf8 . unsafe_extract t1 0 and core2 = Zed_utf8 . unsafe_extract t2 0 in UChar . compare core1 core2 |
let mix_uChar zChar uChar = match prop_uChar uChar with | Printable 0 -> Ok ( zChar ^ ( Zed_utf8 . singleton uChar ) uChar ) uChar | _ -> Error ( Zed_utf8 . singleton uChar ) uChar |
let first_core ( ? trim = false ) false uChars = let rec aux uChars = match uChars with | [ ] -> None , [ ] | uChar :: tl -> let prop = prop_uChar uChar in match prop with | Printable w -> if w > 0 then Some ( prop , uChar ) uChar , tl else aux tl | Other -> Some ( prop , uChar ) uChar , tl | Null -> Some ( prop , uChar ) uChar , tl in match uChars with | [ ] -> None , [ ] | uChar :: _ -> if not trim && is_combining_mark uChar then None , uChars else aux uChars |
let rec subsequent uChars = match uChars with | [ ] -> [ ] , [ ] | uChar :: tl -> let prop = prop_uChar uChar in match prop with | Printable w -> if w > 0 then [ ] , uChars else let seq , remain = subsequent tl in uChar :: seq , remain | _ -> [ ] , uChars |
let of_uChars ( ? trim = false ) false ( ? indv_combining = true ) true uChars = match uChars with | [ ] -> None , [ ] | uChar :: tl -> match first_core ~ trim uChars with | None , _ -> if indv_combining then Some ( Zed_utf8 . singleton uChar ) uChar , tl else None , uChars | Some ( Printable _w , uChar ) uChar , tl -> let combined , tl = subsequent tl in Some ( Zed_utf8 . implode ( uChar :: combined ) combined ) combined , tl | Some ( Null , uChar ) uChar , tl -> Some ( Zed_utf8 . singleton uChar ) uChar , tl | Some ( Other , uChar ) uChar , tl -> Some ( Zed_utf8 . singleton uChar ) uChar , tl |
let zChars_of_uChars ( ? trim = false ) false ( ? indv_combining = true ) true uChars = let rec aux zChars uChars = match of_uChars ~ trim ~ indv_combining uChars with | None , tl -> List . rev zChars , tl | Some zChar , tl -> aux ( zChar :: zChars ) zChars tl in aux [ ] uChars |
let unsafe_of_utf8 : string -> t = fun str -> if String . length str > 0 then str else failwith " malformed Zed_char sequence " |
let of_utf8 ( ? indv_combining = true ) true str = match of_uChars ~ indv_combining ( Zed_utf8 . explode str ) str with | Some zChar , [ ] -> zChar | _ -> failwith " malformed Zed_char sequence " |
let to_utf8 : t -> string = id |
let unsafe_of_char c = Zed_utf8 . singleton ( UChar . of_char c ) c |
let unsafe_of_uChar uChar = Zed_utf8 . singleton uChar |
module US ( USUS : UnicodeString . Type ) Type = struct module Convert = Zed_utils . Convert ( ConvertUS ) ConvertUS let of_t t = Zed_utf8 . explode t |> Convert . of_list let to_t us = let len = US . length us in let rec create i = if i < len then US . get us i :: create ( i + 1 ) 1 else [ ] in let uChars = create 0 in of_uChars uChars let to_t_exn us = match to_t us with | Some t , _ -> t | _ -> failwith " to_t_exn " end |
type changes = { position : int ; added : int ; removed : int ; added_width : int ; removed_width : int ; } |
type action = | User_move of int | Text_modification of changes |
type t = { position : int signal ; send : action -> unit ; length : int ref ; changes : changes event ; get_lines : unit -> Zed_lines . t ; coordinates : ( int * int ) int signal ; coordinates_display : ( int * int ) int signal ; line : int signal ; column : int signal ; column_display : int signal ; wanted_column : int signal ; set_wanted_column : int -> unit ; } |
let create length changes get_lines position wanted_column = if position < 0 || position > length then raise Out_of_bounds ; let length = ref length in let user_moves , send = E . create ( ) in let update_position position action = match action with | User_move pos -> pos | Text_modification changes -> let delta = changes . added - changes . removed in length := ! length + delta ; if ! length < 0 then raise Out_of_bounds ; if position > changes . position then begin if delta >= 0 then position + delta else if position < changes . position - delta then changes . position else position + delta end else position in let text_modifications = E . map ( fun x -> Text_modification x ) x changes in let position = S . fold update_position position ( E . select [ user_moves ; text_modifications ] text_modifications ) text_modifications in let compute_coordinates_and_display position = let lines = get_lines ( ) in let index = Zed_lines . line_index lines position in let bol = Zed_lines . line_start lines index in let column = position - bol in let width = Zed_lines . force_width lines bol column in ( index , column , bol , width ) width in let coordinates_and_display = S . map compute_coordinates_and_display position in let coordinates = S . map ( fun ( row , column , _ , _ ) _ -> ( row , column ) column ) column coordinates_and_display in let coordinates_display = S . map ( fun ( row , _ , _ , width ) width -> ( row , width ) width ) width coordinates_and_display in let line = S . map fst coordinates in let column = S . map snd coordinates in let column_display = S . map snd coordinates_display in let wanted_column , set_wanted_column = S . create wanted_column in { position ; send ; length ; changes ; get_lines ; coordinates ; coordinates_display ; line ; column ; column_display ; wanted_column ; set_wanted_column ; } |
let copy cursor = create ( ! cursor . length ) length cursor . changes cursor . get_lines ( S . value cursor . position ) position ( S . value cursor . wanted_column ) wanted_column |
let position cursor = cursor . position |
let get_position cursor = S . value cursor . position |
let line cursor = cursor . line |
let get_line cursor = S . value cursor . line |
let column cursor = cursor . column |
let column_display cursor = cursor . column_display |
let get_column cursor = S . value cursor . column |
let get_column_display cursor = S . value cursor . column_display |
let coordinates cursor = cursor . coordinates |
let coordinates_display cursor = cursor . coordinates |
let get_coordinates cursor = S . value cursor . coordinates |
let get_coordinates_display cursor = S . value cursor . coordinates_display |
let wanted_column cursor = cursor . wanted_column |
let get_wanted_column cursor = S . value cursor . wanted_column |
let set_wanted_column cursor column = cursor . set_wanted_column column |
let move cursor ( ? set_wanted_column = true ) true delta = let new_position = S . value cursor . position + delta in if new_position < 0 || new_position > ( ! cursor . length ) length then raise Out_of_bounds else begin cursor . send ( User_move new_position ) new_position ; if set_wanted_column then cursor . set_wanted_column ( S . value cursor . column_display ) column_display end |
let goto cursor ( ? set_wanted_column = true ) true position = if position < 0 || position > ( ! cursor . length ) length then raise Out_of_bounds else begin cursor . send ( User_move position ) position ; if set_wanted_column then cursor . set_wanted_column ( S . value cursor . column_display ) column_display end |
type clipboard = { clipboard_get : unit -> Zed_rope . t ; clipboard_set : Zed_rope . t -> unit ; } |
type ' a t = { mutable data : ' a option ; mutable text : Zed_rope . t ; mutable lines : Zed_lines . t ; changes : Zed_cursor . changes event ; send_changes : Zed_cursor . changes -> unit ; erase_mode : bool signal ; set_erase_mode : bool -> unit ; editable : int -> int -> bool ; clipboard : clipboard ; mutable mark : Zed_cursor . t ; selection : bool signal ; set_selection : bool -> unit ; match_word : Zed_rope . t -> int -> int option ; locale : string option signal ; undo : ( Zed_rope . t * Zed_lines . t * int * int * int * int * int * int ) int array ; undo_size : int ; mutable undo_start : int ; mutable undo_index : int ; mutable undo_count : int ; } |
let dummy_cursor = Zed_cursor . create 0 E . never ( fun ( ) -> Zed_lines . empty ) empty 0 0 |
let match_by_regexp_core re rope idx = match Zed_re . Core . regexp_match ~ sem ` : Longest re rope idx with | None -> None | Some arr -> match arr ( . 0 ) 0 with | Some ( Some_zip1 , zip2 ) zip2 -> Some ( SomeZed_rope . Zip . offset zip2 ) zip2 | None -> None |
let match_by_regexp_raw re rope idx = match Zed_re . Raw . regexp_match ~ sem ` : Longest re rope idx with | None -> None | Some arr -> match arr ( . 0 ) 0 with | Some ( Some_zip1 , zip2 ) zip2 -> Some ( SomeZed_rope . Zip_raw . offset zip2 ) zip2 | None -> None |
let regexp_word_core = let set = UCharInfo . load_property_set ` Alphabetic in let set = List . fold_left ( fun set ch -> USet . add ( UChar . of_char ch ) ch set ) set set [ ' 0 ' ; ' 1 ' ; ' 2 ' ; ' 3 ' ; ' 4 ' ; ' 5 ' ; ' 6 ' ; ' 7 ' ; ' 8 ' ; ' 9 ' ] ' 9 ' in Zed_re . Core . compile ( ` Repn ( Repn ` Set set , 1 , None ) None ) None |
let regexp_word_raw = let set = UCharInfo . load_property_set ` Alphabetic in let set = List . fold_left ( fun set ch -> USet . add ( UChar . of_char ch ) ch set ) set set [ ' 0 ' ; ' 1 ' ; ' 2 ' ; ' 3 ' ; ' 4 ' ; ' 5 ' ; ' 6 ' ; ' 7 ' ; ' 8 ' ; ' 9 ' ] ' 9 ' in Zed_re . Raw . compile ( ` Repn ( Repn ` Set set , 1 , None ) None ) None |
let new_clipboard ( ) = let r = ref ( Zed_rope . empty ( ) ) in { clipboard_get = ( fun ( ) -> ! r ) r ; clipboard_set = ( fun x -> r := x ) x } |
let create ( ? editable = fun _pos _len -> true ) true ( ? move = ( ) ) + ? clipboard ( ? match_word = match_by_regexp_core regexp_word_core ) regexp_word_core ( ? locale = S . const None ) None ( ? undo_size = 1000 ) 1000 ( ) = let _ = move in let changes , send_changes = E . create ( ) in let erase_mode , set_erase_mode = S . create false in let selection , set_selection = S . create false in let clipboard = match clipboard with | Some clipboard -> clipboard | None -> new_clipboard ( ) in let edit = { data = None ; text = Zed_rope . empty ( ) ; lines = Zed_lines . empty ; changes ; send_changes ; erase_mode ; set_erase_mode ; editable ; clipboard ; mark = dummy_cursor ; selection ; set_selection ; match_word ; locale ; undo = Array . make undo_size ( Zed_rope . empty ( ) , Zed_lines . empty , 0 , 0 , 0 , 0 , 0 , 0 ) 0 ; undo_size ; undo_start = 0 ; undo_index = 0 ; undo_count = 0 ; } in edit . mark <- Zed_cursor . create 0 changes ( fun ( ) -> edit . lines ) lines 0 0 ; edit |
let get_data engine = match engine . data with | Some data -> data | None -> raise Not_found |
let set_data engine data = engine . data <- Some data |
let clear_data engine = engine . data <- None |
let text engine = engine . text |
let lines engine = engine . lines |
let changes engine = engine . changes |
let erase_mode engine = engine . erase_mode |
let get_erase_mode engine = S . value engine . erase_mode |
let set_erase_mode engine state = engine . set_erase_mode state |
let mark engine = engine . mark |
let selection engine = engine . selection |
let get_selection engine = S . value engine . selection |
let set_selection engine state = engine . set_selection state |
let get_line e i = let txt = text e in let lines = lines e in let start = Zed_lines . line_start lines i in let stop = Zed_lines . line_stop lines i in Zed_rope . sub txt start ( stop - start ) start |
let update engine cursors = E . select ( E . stamp engine . changes ( ) :: E . stamp ( S . changes engine . selection ) selection ( ) :: E . stamp ( S . changes ( Zed_cursor . position engine . mark ) mark ) mark ( ) :: List . map ( fun cursor -> E . stamp ( S . changes ( Zed_cursor . position cursor ) cursor ) cursor ( ) ) cursors ) |
let new_cursor engine = Zed_cursor . create ( Zed_rope . length engine . text ) text engine . changes ( fun ( ) -> engine . lines ) lines 0 0 |
type ' a context = { edit : ' a t ; cursor : Zed_cursor . t ; check : bool ; } |
let context ( ? check = true ) true edit cursor = { edit ; cursor ; check } |
let edit ctx = ctx . edit |
let cursor ctx = ctx . cursor |
let check ctx = ctx . check |
let with_check check ctx = { ctx with check } |
let goto ctx ? set_wanted_column new_position = Zed_cursor . goto ctx . cursor ? set_wanted_column new_position |
let set_position ctx new_position = Zed_cursor . goto ctx . cursor ~ set_wanted_column : false new_position |
let move ctx ? set_wanted_column delta = Zed_cursor . move ctx . cursor ? set_wanted_column delta |
let next_line_n ctx n = let index = Zed_cursor . get_line ctx . cursor in if index + n > Zed_lines . count ctx . edit . lines then goto ctx ~ set_wanted_column : false ( Zed_rope . length ctx . edit . text ) text else begin let stop = if index + n = Zed_lines . count ctx . edit . lines then Zed_rope . length ctx . edit . text else Zed_lines . line_start ctx . edit . lines ( index + n + 1 ) 1 - 1 in let wanted_idx = Zed_lines . get_idx_by_width ctx . edit . lines ( index + n ) n ( Zed_cursor . get_wanted_column ctx . cursor ) cursor in goto ctx ~ set_wanted_column : false ( min wanted_idx stop ) stop end |
let prev_line_n ctx n = let index = Zed_cursor . get_line ctx . cursor in if index - n < 0 then begin goto ctx ~ set_wanted_column : false 0 end else begin let stop = Zed_lines . line_start ctx . edit . lines ( index - ( n - 1 ) 1 ) 1 - 1 in let wanted_idx = Zed_lines . get_idx_by_width ctx . edit . lines ( index - n ) n ( Zed_cursor . get_wanted_column ctx . cursor ) cursor in goto ctx ~ set_wanted_column : false ( min wanted_idx stop ) stop end |
let move_line ctx delta = match delta with | _ when delta < 0 -> prev_line_n ctx ( - delta ) delta | _ when delta > 0 -> next_line_n ctx delta | _ -> ( ) |
let position ctx = Zed_cursor . get_position ctx . cursor |
let line ctx = Zed_cursor . get_line ctx . cursor |
let column ctx = Zed_cursor . get_column ctx . cursor |
let column_display ctx = Zed_cursor . get_column_display ctx . cursor |
let at_bol ctx = Zed_cursor . get_column ctx . cursor = 0 |
let at_eol ctx = let position = Zed_cursor . get_position ctx . cursor in let index = Zed_cursor . get_line ctx . cursor in if index = Zed_lines . count ctx . edit . lines then position = Zed_rope . length ctx . edit . text else position = Zed_lines . line_start ctx . edit . lines ( index + 1 ) 1 - 1 |
let at_bot ctx = Zed_cursor . get_position ctx . cursor = 0 |
let at_eot ctx = Zed_cursor . get_position ctx . cursor = Zed_rope . length ctx . edit . text |
let modify { edit ; _ } text lines position new_position added removed added_width removed_width = if edit . undo_size > 0 then begin edit . undo ( . edit . undo_index ) undo_index <- ( text , lines , position , new_position , added , removed , added_width , removed_width ) removed_width ; edit . undo_index <- ( edit . undo_index + 1 ) 1 mod edit . undo_size ; if edit . undo_count = edit . undo_size then edit . undo_start <- ( edit . undo_start + 1 ) 1 mod edit . undo_size else edit . undo_count <- edit . undo_count + 1 end ; edit . send_changes { position ; added ; removed ; added_width ; removed_width } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.