text
stringlengths
0
601k
let formatSimpleAttributed x y = makeList ~ wrap ( " ( " , : " ) " ) ~ break : IfNeed ~ indent : 0 ~ postSpace : true ( List . concat [ y ; [ x ] ] )
let formatAttributed ( ? labelBreak ` = Auto ) x y = label ~ break : labelBreak ~ indent : 0 ~ space : true ( makeList ~ inline ( : true , true ) ~ postSpace : true y ) x
let formatJustTheTypeConstraint typ = makeList ~ postSpace : false ~ sep ( : Sep " " ) [ atom " " ; : typ ]
let formatTypeConstraint one two = label ~ space : true ( makeList ~ postSpace : false [ one ; atom " " ] ) : two
let formatCoerce expr optType coerced = match optType with | None -> label ~ space : true ( makeList ~ postSpace : true [ expr ; atom " " ] ) :> coerced | Some typ -> label ~ space : true ( makeList ~ postSpace : true [ formatTypeConstraint expr typ ; atom " " ] ) :> coerced
let formatIndentedApplication headApplicationItem argApplicationItems = makeList ~ inline ( : true , true ) ~ postSpace : true ~ break : IfNeed [ label ~ space : true headApplicationItem ( makeAppList argApplicationItems ) ]
let formatAttachmentApplication finalWrapping ( attachTo : ( bool * Layout . t ) option ) ( appTermItems , loc ) = let partitioning = finalWrapping appTermItems in match partitioning with | None -> ( match ( appTermItems , attachTo ) with | ( [ ] , _ ) -> raise ( NotPossible " No app terms " ) | ( [ hd ] , None ) -> so...
let applicationFinalWrapping x = partitionFinalWrapping isSequencey settings . funcApplicationLabelStyle x
let curriedFunctionFinalWrapping x = partitionFinalWrapping isSequencey settings . funcCurriedPatternStyle x
let typeApplicationFinalWrapping typeApplicationItems = partitionFinalWrapping isSequencey settings . funcApplicationLabelStyle typeApplicationItems
let protectIdentifier txt = if not ( needs_parens txt ) then atom txt else if needs_spaces txt then makeList ~ wrap ( " ( " , : " ) " ) ~ pad ( : true , true ) [ atom txt ] else atom ( " ( " ^ txt ^ " ) " )
let protectLongIdentifier longPrefix txt = makeList [ longPrefix ; atom " . " ; protectIdentifier txt ]
let paren b fu ppf x = if b then Format . fprintf ppf " ( % a ) " fu x else fu ppf x
let constant_string ppf s = Format . fprintf ppf " % S " s
let tyvar ppf str = Format . fprintf ppf " ' % s " str
let constant ? raw_literal ( ? parens = true ) ppf = function | Pconst_char i -> Format . fprintf ppf " % C " i | Pconst_string ( i , None ) -> begin match raw_literal with | Some text -> Format . fprintf ppf " " \% s " " \ text | None -> Format . fprintf ppf " " \% s " " \ ( Reason_syntax_util . escape_string i ) end ...
let is_punned_labelled_expression e lbl = match e . pexp_desc with | Pexp_ident { txt } | Pexp_constraint ( { pexp_desc = Pexp_ident { txt } } , _ ) | Pexp_coerce ( { pexp_desc = Pexp_ident { txt } } , _ , _ ) -> txt = Longident . parse lbl | _ -> false
let is_punned_labelled_pattern p lbl = match p . ppat_desc with | Ppat_constraint ( { ppat_desc = Ppat_var _ ; ppat_attributes = _ :: _ } , _ ) -> false | Ppat_constraint ( { ppat_desc = Ppat_var { txt } } , _ ) | Ppat_var { txt } -> txt = lbl | _ -> false
let isLongIdentWithDot = function | Ldot _ -> true | _ -> false
let isJsDotTLongIdent ident = match ident with | Ldot ( Lident " Js " , " t " ) -> true | _ -> false
let recordRowIsPunned pld = let name = pld . pld_name . txt in ( match pld . pld_type with | { ptyp_desc = ( Ptyp_constr ( { txt } , [ ] ) ) ; ptyp_attributes = [ ] ; _ } when ( Longident . last txt = name && isLongIdentWithDot txt == false ) -> true | _ -> false )
let isPunnedJsxArg lbl ident = not ( isLongIdentWithDot ident . txt ) && ( Longident . last ident . txt ) = lbl
let is_unit_pattern x = match x . ppat_desc with | Ppat_construct ( { txt = Lident " ( ) " } , None ) -> true | _ -> false
let is_ident_pattern x = match x . ppat_desc with | Ppat_var _ -> true | _ -> false
let is_any_pattern x = x . ppat_desc = Ppat_any
let is_direct_pattern x = x . ppat_attributes == [ ] && match x . ppat_desc with | Ppat_construct ( { txt = Lident " ( ) " } , None ) -> true | _ -> false
let isJSXComponent expr = match expr with | ( { pexp_desc = Pexp_apply ( { pexp_desc = Pexp_ident _ } , args ) ; pexp_attributes } ) | ( { pexp_desc = Pexp_apply ( { pexp_desc = Pexp_letmodule ( _ , _ , _ ) } , args ) ; pexp_attributes } ) -> let { jsxAttrs } = partitionAttributes pexp_attributes in let hasLabelledChil...
let isSingleArgParenApplication = function | [ { pexp_attributes = [ ] ; pexp_desc = Pexp_record _ } ] | [ { pexp_attributes = [ ] ; pexp_desc = Pexp_tuple _ } ] | [ { pexp_attributes = [ ] ; pexp_desc = Pexp_array _ } ] | [ { pexp_attributes = [ ] ; pexp_desc = Pexp_object _ } ] -> true | [ { pexp_attributes = [ ] ; p...
let isSingleArgParenPattern = function | [ { ppat_attributes = [ ] ; ppat_desc = Ppat_record _ } ] | [ { ppat_attributes = [ ] ; ppat_desc = Ppat_array _ } ] | [ { ppat_attributes = [ ] ; ppat_desc = Ppat_tuple _ } ] -> true | [ { ppat_attributes = [ ] ; ppat_desc = Ppat_construct ( ( { txt = Lident " " } ) , :: _ ) } ...
let rec computeInfixChain = function | LayoutNode layoutNode -> [ Layout layoutNode ] | InfixTree ( op , leftResolvedRule , rightResolvedRule ) -> ( computeInfixChain leftResolvedRule ) @ [ InfixToken op ] @ ( computeInfixChain rightResolvedRule )
let equalityOperators = [ " " ; != " " ; !== " " ; === " " ; == " " ; >= " " ; <= " " ; < " " ] >
let formatComputedInfixChain infixChainList = let layout_of_group group currentToken = if List . length group < 2 then makeList ~ inline ( : true , true ) ~ sep ( : Sep " " ) group else if List . mem currentToken equalityOperators then let hd = List . hd group in let tl = makeList ~ inline ( : true , true ) ~ sep ( : S...
let groupAndPrint ~ xf ~ getLoc ~ comments items = let rec group prevLoc curr acc = function | x :: xs -> let item = xf x in let loc = getLoc x in let range = Range . makeRangeBetween prevLoc loc in if Range . containsWhitespace ~ range ~ comments ( ) then group loc [ ( range , item ) ] ( ( List . rev curr ) :: acc ) x...
let printer = object ( self ' : self ) val pipe = false val semi = false val inline_braces = false val preserve_braces = true val mutable comments = [ ] method comments = comments method trackComment comment = comments <- comment :: comments method under_pipe = { < pipe = true } > method under_semi = { < semi = true } ...
let toplevel_phrase ppf x = match x with | Ptop_def s -> format_layout ppf ( printer # structure s ) | Ptop_dir _ -> print_string " "
let case_list ppf x = List . iter ( format_layout ppf ) ( printer # case_list x )
let longident_for_arity lid = let rec toplevel = function | Lident s -> [ s ] | Ldot ( lid , s ) -> let append_s x = x ^ " . " ^ s in s :: ( List . map append_s ( toplevel lid ) ) | Lapply ( _ , s ) -> toplevel s in match lid with | Lident s -> ( " . " ^ s ) :: toplevel lid | _ -> toplevel lid
let add_explicit_arity loc attributes = ( { txt " = explicit_arity " ; loc } , PStr [ ] ) :: normalized_attributes " explicit_arity " attributes
let explicit_arity_not_exists attributes = not ( attribute_exists " explicit_arity " attributes )
let wrap_expr_with_tuple exp = { exp with pexp_desc = Pexp_tuple [ exp ] }
let wrap_pat_with_tuple pat = { pat with ppat_desc = Ppat_tuple [ pat ] }
let built_in_explicit_arity_constructors = [ " Some " ; " Assert_failure " ; " Match_failure " ]
let explicit_arity_constructors = StringSet . of_list ( built_in_explicit_arity_constructors @ ( ! configuredSettings ) . constructorLists )
let add_explicit_arity_mapper super = let super_expr = super . Ast_mapper . expr in let super_pat = super . Ast_mapper . pat in let expr mapper expr = let expr = match expr with | { pexp_desc = Pexp_construct ( lid , Some sp ) ; pexp_loc ; pexp_attributes } when List . exists ( fun c -> StringSet . mem c explicit_arity...
let preprocessing_mapper = ml_to_reason_swap_operator_mapper ( escape_stars_slashes_mapper ( add_explicit_arity_mapper Ast_mapper . default_mapper ) )
let core_type ppf x = format_layout ppf ( printer # core_type ( apply_mapper_to_type x preprocessing_mapper ) )
let pattern ppf x = format_layout ppf ( printer # pattern ( apply_mapper_to_pattern x preprocessing_mapper ) )
let signature ( comments : Comment . t list ) ppf x = List . iter ( fun comment -> printer # trackComment comment ) comments ; format_layout ppf ~ comments ( printer # signature ( apply_mapper_to_signature x preprocessing_mapper ) )
let structure ( comments : Comment . t list ) ppf x = List . iter ( fun comment -> printer # trackComment comment ) comments ; format_layout ppf ~ comments ( printer # structure ( apply_mapper_to_structure x preprocessing_mapper ) )
let expression ppf x = format_layout ppf ( printer # unparseExpr ( apply_mapper_to_expr x preprocessing_mapper ) )
let case_list = case_list end in object method core_type = Formatter . core_type method pattern = Formatter . pattern method signature = Formatter . signature method structure = Formatter . structure method toplevel_phrase = Formatter . toplevel_phrase method expression = Formatter . expression method case_list = Forma...
module R = Merlin_recovery . Make ( Reason_parser . MenhirInterpreter ) ( struct include Reason_parser_recover let default_value loc x = Default . default_loc := loc ; default_value x let guide _ = false end )
type ' a parser = | Correct of ' a M . parser | Recovering of ' a R . candidates * Reason_lexer . invalid_docstrings
let initial entry_point position = Correct ( M . initial entry_point position )
type ' a step = | Intermediate of ' a parser | Success of ' a * Reason_lexer . invalid_docstrings | Error
let step parser token = match parser with | Correct parser -> begin match M . step parser token with | M . Intermediate parser -> Intermediate ( Correct parser ) | M . Success ( x , ds ) -> Success ( x , ds ) | M . Error -> let ( _ , loc_start , loc_end ) = token in let loc = { Location . loc_start ; loc_end ; loc_ghos...
module Step : sig type ' a parser type ' a step = | Intermediate of ' a parser | Success of ' a * invalid_docstrings | Error val initialize : ' a I . checkpoint -> ' a step val offer : ' a parser -> token Reason_lexer . positioned -> ' a step val add_docstring : string -> Lexing . position -> Lexing . position -> ' a p...
type ' a parser = ' a Step . parser
type ' a step = ' a Step . step = | Intermediate of ' a parser | Success of ' a * invalid_docstrings | Error
let initial entry position = match Step . initialize ( entry position ) with | Step . Intermediate parser -> parser | _ -> assert false
let rec offer_many parser = function | [ ] -> Step . Intermediate parser | [ token ] -> Step . offer parser token | token :: tokens -> match Step . offer parser token with | Step . Intermediate parser -> offer_many parser tokens | other -> other
let try_insert_semi_on = function | Reason_parser . LET | Reason_parser . TYPE | Reason_parser . MODULE | Reason_parser . OPEN | Reason_parser . EXCEPTION | Reason_parser . INCLUDE | Reason_parser . DOCSTRING _ | Reason_parser . LIDENT _ | Reason_parser . UIDENT _ | Reason_parser . IF | Reason_parser . WHILE | Reason_p...
let advance p n = { p with Lexing . pos_cnum = p . Lexing . pos_cnum + n }
let rec split_greaters acc pcur = function | ' ' > :: tl -> let pnext = ( advance pcur 1 ) in split_greaters ( ( Reason_parser . GREATER , pcur , pnext ) :: acc ) pnext tl | nonGts -> ( List . rev acc ) , nonGts , pcur
let common_remaining_infix_token pcur = let pnext = advance pcur 1 in function | [ ' ' ] - -> Some ( Reason_parser . MINUS , pcur , pnext ) | [ ' ' ; - ' . ' ] -> Some ( Reason_parser . MINUSDOT , pcur , advance pnext 1 ) | [ ' ' ] + -> Some ( Reason_parser . PLUS , pcur , pnext ) | [ ' ' ; + ' . ' ] -> Some ( Reason_p...
let rec decompose_token pos0 split = let pcur = advance pos0 1 in let pnext = advance pos0 2 in match split with | [ ] -> None | ' ' = :: tl -> let eq = ( Reason_parser . EQUAL , pcur , pnext ) in let ( revFirstTwo , tl , pcur , _pnext ) = match tl with | ' ' ? :: tlTl -> [ ( Reason_parser . QUESTION , pcur , pnext ) ;...
let rec init_tailrec_aux acc i n f = if i >= n then acc else init_tailrec_aux ( f i :: acc ) ( i + 1 ) n f
let list_init len f = List . rev ( init_tailrec_aux [ ] 0 len f )
let explode s = list_init ( String . length s ) ( String . get s )
let try_split_label ( tok_kind , pos0 , _posn ) = match tok_kind with | Reason_parser . INFIXOP0 s -> ( match decompose_token pos0 ( explode s ) with | None -> [ ] | Some ( l ) -> l ) | _ -> [ ]
let step parser token = match Step . offer parser token with | ( Success _ | Intermediate _ ) as step -> step | Error -> let try_alternative_tokens = function | [ ] -> Error | tokens -> match offer_many parser tokens with | ( Step . Intermediate _ | Step . Success _ ) as result -> result | Step . Error -> Error in let ...
let rename_labels = ref false
let potentially_conflicts_with ~ keyword s = let s_length = String . length s in let keyword_length = String . length keyword in s_length >= keyword_length && ( try for i = 0 to keyword_length - 1 do if keyword . [ i ] <> s . [ i ] then raise Exit ; done ; for i = keyword_length to s_length - 1 do if s . [ i ] <> ' _ '...
let string_add_suffix x = x ^ " _ "
let string_drop_suffix x = String . sub x 0 ( String . length x - 1 )
let reason_to_ml_swap = function | " " ! -> " not " | " " ^ -> " " ! | " " ++ -> " " ^ | " " === -> " " == | " " == -> " " = | " " \\!== -> " " !== | " " \\=== -> " " === | " " != -> " " <> | " " !== -> " " != | x when ( potentially_conflicts_with ~ keyword " : match " x || potentially_conflicts_with ~ keyword " : meth...
let ml_to_reason_swap = function | " not " -> " " ! | " " ! -> " " ^ | " " ^ -> " " ++ | " " == -> " " === | " " = -> " " == | " " !== -> " " \\!== | " " === -> " " \\=== | " " <> -> " " != | " " != -> " " !== | x when ( potentially_conflicts_with ~ keyword " : match_ " x || potentially_conflicts_with ~ keyword " : met...
let escape_string str = let buf = Buffer . create ( String . length str ) in String . iter ( fun c -> match c with | ' \ t ' -> Buffer . add_string buf " \\ t " | ' \ r ' -> Buffer . add_string buf " \\ r " | ' \ n ' -> Buffer . add_string buf " \\ n " | ' ' \\ -> Buffer . add_string buf " " \\\\ | ' " ' -> Buffer . ad...
module TrailingCommaMarker = struct let char = Char . chr 249 let string = String . make 1 char end
module EOLMarker = struct let char = Char . chr 248 let string = String . make 1 char end
let is_prefixed prefix str i = let len = String . length prefix in let j = ref 0 in while ! j < len && String . unsafe_get prefix ! j = String . unsafe_get str ( i + ! j ) do incr j done ; ( ! j = len )
let rec pick_while p = function | [ ] -> [ ] , [ ] | hd :: tl when p hd -> let ( satisfied , not_satisfied ) = pick_while p tl in hd :: satisfied , not_satisfied | l -> ( [ ] , l )
let find_substring sub str i = let len = String . length str - String . length sub in let found = ref false and i = ref i in while not ! found && ! i <= len do if is_prefixed sub str ! i then found := true else incr i ; done ; if not ! found then raise Not_found ; ! i
let replace_string old_str new_str str = match find_substring old_str str 0 with | exception Not_found -> str | occurrence -> let buffer = Buffer . create ( String . length str + 15 ) in let rec loop i j = Buffer . add_substring buffer str i ( j - i ) ; Buffer . add_string buffer new_str ; let i = j + String . length o...
let split_by ( ? keep_empty = false ) is_delim str = let len = String . length str in let rec loop acc last_pos pos = if pos = - 1 then if last_pos = 0 && not keep_empty then acc else String . sub str 0 last_pos :: acc else if is_delim str . [ pos ] then let new_len = ( last_pos - pos - 1 ) in if new_len <> 0 || keep_e...
let rec trim_right_idx str idx = if idx = - 1 then 0 else match String . get str idx with | ' \ t ' | ' ' | ' \ n ' | ' \ r ' -> trim_right_idx str ( idx - 1 ) | _ -> idx + 1
let trim_right str = let length = String . length str in if length = 0 then " " else let index = trim_right_idx str ( length - 1 ) in if index = 0 then " " else if index = length then str else String . sub str 0 index
let processLine line = let rightTrimmed = trim_right line in let trimmedLen = String . length rightTrimmed in if trimmedLen = 0 then rightTrimmed else let segments = split_by ~ keep_empty : false ( fun c -> c = TrailingCommaMarker . char ) rightTrimmed in let hadTrailingCommaMarkerBeforeNewline = String . get rightTrim...
let processLineEndingsAndStarts str = split_by ~ keep_empty : true ( fun x -> x = ' \ n ' ) str |> List . map processLine |> String . concat " \ n " |> String . trim
let isLineComment str = match String . index str ' \ n ' with | exception Not_found -> false | n -> n = String . length str - 1
let map_lident f lid = let swapped = match lid . txt with | Lident s -> Lident ( f s ) | Ldot ( longPrefix , s ) -> Ldot ( longPrefix , f s ) | Lapply ( y , s ) -> Lapply ( y , s ) in { lid with txt = swapped }
let map_arg_label f = function | Nolabel -> Nolabel | Labelled lbl -> Labelled ( f lbl ) | Optional lbl -> Optional ( f lbl )
let map_class_expr f class_expr = { class_expr with pcl_desc = match class_expr . pcl_desc with | Pcl_constr ( lid , ts ) -> Pcl_constr ( map_lident f lid , ts ) | e -> e }
let map_class_type f class_type = { class_type with pcty_desc = match class_type . pcty_desc with | Pcty_constr ( lid , ct ) -> Pcty_constr ( map_lident f lid , ct ) | Pcty_arrow ( arg_lbl , ct , cls_type ) -> Pcty_arrow ( map_arg_label f arg_lbl , ct , cls_type ) | x -> x }
let map_core_type f typ = { typ with ptyp_desc = match typ . ptyp_desc with | Ptyp_var var -> Ptyp_var ( f var ) | Ptyp_arrow ( lbl , t1 , t2 ) -> let lbl ' = match lbl with | Labelled s when ! rename_labels -> Labelled ( f s ) | Optional s when ! rename_labels -> Optional ( f s ) | lbl -> lbl in Ptyp_arrow ( lbl ' , t...
let identifier_mapper f super =
let map_fields fields = List . map ( fun ( lid , x ) -> ( map_lident f lid , x ) ) fields in
let map_name ( { txt } as name ) = { name with txt ( = f txt ) } in
let map_lid lid = map_lident f lid in
let map_label label = map_arg_label f label in expr = begin fun mapper expr -> let expr = match expr with | { pexp_desc = Pexp_ident lid } -> { expr with pexp_desc = Pexp_ident ( map_lid lid ) } | { pexp_desc = Pexp_fun ( label , eo , pat , e ) } when ! rename_labels -> { expr with pexp_desc = Pexp_fun ( map_label labe...