text
stringlengths
0
601k
let rec sequentialIfBlocks x = match x with | Some ( { pexp_desc = Pexp_ifthenelse ( e1 , e2 , els ) } ) -> ( let ( nestedIfs , finalExpression ) = ( sequentialIfBlocks els ) in ( ( e1 , e2 ) :: nestedIfs , finalExpression ) ) | Some e -> ( [ ] , Some e ) | None -> ( [ ] , None )
let almost_simple_prefix_symbols = [ ' ' ; ! ' ' ; ? ' ' ] ~ ; ;
let unary_minus_prefix_symbols = [ " " ; ~- " . " ] ~- ; ;
let unary_plus_prefix_symbols = [ " " ; ~+ " . " ~+ ] ; ;
let infix_symbols = [ ' ' ; = ' ' ; < ' ' ; > ' ' ; @ ' ' ; ^ ' ' ; | ' ' ; & ' ' ; + ' ' ; - ' ' ; * ' ' ; / ' ' ; $ ' ' ; % ' ' ; \\ ' ' # ]
let special_infix_strings = [ " asr " ; " land " ; " lor " ; " lsl " ; " lsr " ; " lxor " ; " mod " ; " or " ; " " ; := " " ; != " " ] !==
let requireIndentFor = [ updateToken ; " " ] :=
let requireNoSpaceFor tok = tok = pipeFirstToken || ( tok . [ 0 ] = ' ' # && tok <> " " ) #=
let getPrintableUnaryIdent s = if List . mem s unary_minus_prefix_symbols || List . mem s unary_plus_prefix_symbols then String . sub s 1 ( String . length s - 1 ) else s
let printedStringAndFixity = function | s when List . mem s special_infix_strings -> Infix s | " " ^ -> UnaryPostfix " " ^ | s when List . mem s . [ 0 ] infix_symbols -> Infix s | s when List . mem s . [ 0 ] almost_simple_prefix_symbols && not ( List . mem s special_infix_strings ) && not ( s = " " ) ? -> ( if List . m...
let isSimplePrefixToken s = match printedStringAndFixity s with | AlmostSimplePrefix _ | UnaryPostfix " " ^ -> true | _ -> false
let rules = [ [ ( TokenPrecedence , ( fun s -> ( Left , s = pipeFirstToken ) ) ) ; ( TokenPrecedence , ( fun s -> ( Left , s . [ 0 ] = ' ' # && s <> sharpOpEqualToken && s <> " " ) ) ) ; # ( TokenPrecedence , ( fun s -> ( Left , s = " . " ) ) ) ; ( CustomPrecedence , ( fun s -> ( Left , s = " prec_lbracket " ) ) ) ; ] ...
let without_prefixed_backslashes str = if str = " " then str else if String . get str 0 = ' ' \\ then String . sub str 1 ( String . length str - 1 ) else str
let indexOfFirstMatch ~ prec lst = let rec aux n = function | [ ] -> None | [ ] :: tl -> aux ( n + 1 ) tl | ( ( kind , tester ) :: hdTl ) :: tl -> match prec , kind with | Token str , TokenPrecedence | Custom str , CustomPrecedence -> let associativity , foundMatch = tester str in if foundMatch then Some ( associativit...
let precedenceInfo ~ prec = let prec = match prec with | Token str -> Token ( without_prefixed_backslashes str ) | Custom _ -> prec in indexOfFirstMatch ~ prec rules
let isLeftAssociative ~ prec = match precedenceInfo ~ prec with | None -> false | Some ( Left , _ ) -> true | Some ( Right , _ ) -> false | Some ( Nonassoc , _ ) -> false
let isRightAssociative ~ prec = match precedenceInfo ~ prec with | None -> false | Some ( Right , _ ) -> true | Some ( Left , _ ) -> false | Some ( Nonassoc , _ ) -> false
let higherPrecedenceThan c1 c2 = match ( ( precedenceInfo ~ prec : c1 ) , ( precedenceInfo ~ prec : c2 ) ) with | ( _ , None ) | ( None , _ ) -> let ( str1 , str2 ) = match ( c1 , c2 ) with | ( Token s1 , Token s2 ) -> ( " Token " ^ s1 , " Token " ^ s2 ) | ( Token s1 , Custom s2 ) -> ( " Token " ^ s1 , " Custom " ^ s2 ...
let printedStringAndFixityExpr = function | { pexp_desc = Pexp_ident { txt = Lident l } } -> printedStringAndFixity l | _ -> Normal
let needs_parens txt = match printedStringAndFixity txt with | Infix _ -> true | UnaryPostfix _ -> true | UnaryPlusPrefix _ -> true | UnaryMinusPrefix _ -> true | UnaryNotPrefix _ -> true | AlmostSimplePrefix _ -> true | Normal -> false
let needs_spaces txt = txt . [ 0 ] ' ' =* || txt . [ String . length txt - 1 ] = ' ' *
let rec orList = function | { ppat_desc = Ppat_or ( p1 , p2 ) } -> ( orList p1 ) @ ( orList p2 ) | x -> [ x ]
let override = function | Override -> " " ! | Fresh -> " "
let type_variance = function | Invariant -> " " | Covariant -> " " + | Contravariant -> " " -
type construct = [ ` cons of expression list | ` list of expression list | ` nil | ` normal | ` simple of Longident . t | ` tuple ]
let view_expr x = match x . pexp_desc with | Pexp_construct ( { txt = Lident " ( ) " } , _ ) -> ` tuple | Pexp_construct ( { txt = Lident " [ ] " } , _ ) -> ` nil | Pexp_construct ( { txt = Lident " " } , :: Some _ ) -> let rec loop exp acc = match exp with | { pexp_desc = Pexp_construct ( { txt = Lident " [ ] " } , _ ...
let is_simple_list_expr x = match view_expr x with | ` list _ | ` cons _ -> true | _ -> false
let is_simple_construct : construct -> bool = function | ` nil | ` tuple | ` list _ | ` simple _ | ` cons _ -> true | ` normal -> false
let uncurriedTable = Hashtbl . create 42
let is_single_unit_construct exprList = match exprList with | x [ ] :: -> let view = view_expr x in ( match view with | ` tuple -> true | _ -> false ) | _ -> false
let detectTernary l = match l with | [ { pc_lhs { = ppat_desc = Ppat_construct ( { txt = Lident " true " } , _ ) } ; pc_guard = None ; pc_rhs = ifTrue } ; { pc_lhs { = ppat_desc = Ppat_construct ( { txt = Lident " false " } , _ ) } ; pc_guard = None ; pc_rhs = ifFalse } ] -> Some ( ifTrue , ifFalse ) | _ -> None
type funcApplicationLabelStyle = | NeverWrapFinalItem | WrapFinalListyItemIfFewerThan of int
type formatSettings = { constructorTupleImplicitArity : bool ; space : int ; listsRecordsIndent : int ; indentWrappedPatternArgs : int ; indentMatchCases : int ; indentAfterLabels : int ; trySwitchIndent : int ; funcApplicationLabelStyle : funcApplicationLabelStyle ; funcCurriedPatternStyle : funcApplicationLabelStyle ...
let defaultSettings = { constructorTupleImplicitArity = false ; space = 1 ; listsRecordsIndent = 2 ; indentWrappedPatternArgs = 2 ; indentMatchCases = 2 ; indentAfterLabels = 2 ; trySwitchIndent = 0 ; funcApplicationLabelStyle = WrapFinalListyItemIfFewerThan 3 ; funcCurriedPatternStyle = NeverWrapFinalItem ; width = 80...
let configuredSettings = ref defaultSettings
let configure ~ width ~ assumeExplicitArity ~ constructorLists = ( configuredSettings := { defaultSettings with width ; assumeExplicitArity ; constructorLists } )
let module Formatter = struct
let isArityClear attrs = ( ! configuredSettings ) . assumeExplicitArity || List . exists ( function | ( { txt " = explicit_arity " } , _ ) -> true | _ -> false ) attrs
let default_indent_body = settings . listsRecordsIndent * settings . space
let makeList ? listConfigIfCommentsInterleaved ? listConfigIfEolCommentsInterleaved ( ? break = Layout . Never ) ( ? wrap ( " " , = " " ) ) ( ? inline ( = true , false ) ) ( ? sep = Layout . NoSep ) ( ? indent = default_indent_body ) ( ? sepLeft = true ) ( ? preSpace = false ) ( ? postSpace = false ) ( ? pad ( = false ...
let makeAppList = function | [ hd ] -> hd | l -> makeList ~ inline ( : true , true ) ~ postSpace : true ~ break : IfNeed l
let makeTup ( ? wrap ( " " , = " " ) ) ( ? trailComma = true ) ( ? uncurried = false ) l = let ( lwrap , rwrap ) = wrap in let lparen = lwrap ^ ( if uncurried then " ( . " else " ( " ) in makeList ~ wrap ( : lparen , " ) " ^ rwrap ) ~ sep ( : if trailComma then commaTrail else commaSep ) ~ postSpace : true ~ break : If...
let ensureSingleTokenSticksToLabel x = let listConfigIfCommentsInterleaved cfg = let inline = ( true , true ) and postSpace = true and indent = 0 in { cfg with Layout . break = Always_rec ; postSpace ; indent ; inline } in makeList ~ listConfigIfCommentsInterleaved [ x ]
let unbreakLabelFormatter formatter = let newFormatter labelTerm term = match formatter labelTerm term with | Easy_format . Label ( ( labelTerm , settings ) , term ) -> Easy_format . Label ( ( labelTerm , { settings with label_break = ` Never } ) , term ) | _ -> failwith " not a label " in newFormatter
let inlineLabel labelTerm term = let settings = { label_break = ` Never ; space_after_label = true ; indent_after_label = 0 ; label_style = Some " inlineLabel " ; } in Easy_format . Label ( ( labelTerm , settings ) , term )
let debugWithHtml = ref false
let html_escape_string s = let buf = Buffer . create ( 2 * String . length s ) in for i = 0 to String . length s - 1 do match s . [ i ] with ' ' & -> Buffer . add_string buf " & amp ; " | ' ' < -> Buffer . add_string buf " & lt ; " | ' ' > -> Buffer . add_string buf " & gt ; " | c -> Buffer . add_char buf c done ; Buff...
let html_escape = ` Escape_string html_escape_string
let html_style = [ " atom " , { Easy_format . tag_open = " < a " ; > tag_close = " </ a " > } ; " body " , { tag_open = " < lb " ; > tag_close = " </ lb " > } ; " list " , { tag_open = " < l " ; > tag_close = " </ l " > } ; " op " , { tag_open = " < op " ; > tag_close = " </ op " > } ; " cl " , { tag_open = " < cl " ; ...
let easyLabel ( ? break ` = Auto ) ( ? space = false ) ( ? indent = settings . indentAfterLabels ) labelTerm term = let settings = { label_break = break ; space_after_label = space ; indent_after_label = indent ; label_style = Some " label " ; } in Easy_format . Label ( ( labelTerm , settings ) , term )
let label ? break ? space ? indent ( labelTerm : Layout . t ) ( term : Layout . t ) = Layout . Label ( easyLabel ? break ? indent ? space , labelTerm , term )
let atom ? loc str = let style = { Easy_format . atom_style = Some " atomClss " } in source_map ? loc ( Layout . Easy ( Easy_format . Atom ( str , style ) ) )
let makeES6List ? wrap ( ( : lwrap , rwrap ) ( " " , = " " ) ) lst last = makeList ~ wrap ( : lwrap ^ " [ " , " ] " ^ rwrap ) ~ break : IfNeed ~ postSpace : true ~ sep : commaTrail ( lst @ [ makeList [ atom " . . . " ; last ] ] )
let makeNonIndentedBreakingList lst = makeList ~ break : Always_rec ~ indent : 0 ~ inline ( : true , true ) lst
let makeSpacedBreakableInlineList lst = makeList ~ break : IfNeed ~ inline ( : true , true ) ~ postSpace : true lst
let makeCommaBreakableListSurround opn cls lst = makeList ~ break : IfNeed ~ postSpace : true ~ sep ( : Sep " , " ) ~ wrap ( : opn , cls ) lst
let formatPrecedence ( ? inline = false ) ( ? wrap ( " ( " , = " ) " ) ) ? loc formattedTerm = source_map ? loc ( makeList ~ inline ( : true , inline ) ~ wrap ~ break : IfNeed [ formattedTerm ] )
let wrap fn term = ignore ( Format . flush_str_formatter ( ) ) ; fn Format . str_formatter term ; atom ( Format . flush_str_formatter ( ) )
let rec beginsWithStar_ line length idx = if idx = length then false else match String . get line idx with | ' ' * -> true | ' \ t ' | ' ' -> beginsWithStar_ line length ( idx + 1 ) | _ -> false
let beginsWithStar line = beginsWithStar_ line ( String . length line ) 0
let rec numLeadingSpace_ line length idx accum = if idx = length then accum else match String . get line idx with | ' \ t ' | ' ' -> numLeadingSpace_ line length ( idx + 1 ) ( accum + 1 ) | _ -> accum
let numLeadingSpace line = numLeadingSpace_ line ( String . length line ) 0 0
let smallestLeadingSpaces strs = let rec smallestLeadingSpaces curMin strs = match strs with | [ ] -> curMin | " " :: tl -> smallestLeadingSpaces curMin tl | hd :: tl -> let leadingSpace = numLeadingSpace hd in let nextMin = min curMin leadingSpace in smallestLeadingSpaces nextMin tl in smallestLeadingSpaces 99999 strs
let rec isSequencey = function | Layout . SourceMap ( _ , sub ) -> isSequencey sub | Layout . Sequence _ -> true | Layout . Label ( _ , _ , _ ) -> false | Layout . Easy ( Easy_format . List _ ) -> true | Layout . Easy _ -> false | Layout . Whitespace ( _ , sub ) -> isSequencey sub
let inline ( ? preSpace = false ) ( ? postSpace = false ) labelTerm term = makeList [ labelTerm ; term ] ~ inline ( : true , true ) ~ postSpace ~ preSpace ~ indent : 0 ~ break : Layout . Never
let breakline labelTerm term = makeList [ labelTerm ; term ] ~ inline ( : true , true ) ~ indent : 0 ~ break : Always_rec
let insertBlankLines n term = if n = 0 then term else makeList ~ inline ( : true , true ) ~ indent : 0 ~ break : Always_rec ( Array . to_list ( Array . make n ( atom " " ) ) @ [ term ] )
let string_after s n = String . sub s n ( String . length s - n )
let rec lineZeroMeaningfulContent_ line length idx accum = if idx = length then None else let ch = String . get line idx in if ch = ' \ t ' || ch = ' ' || ch = ' ' * then lineZeroMeaningfulContent_ line length ( idx + 1 ) ( accum + 1 ) else Some accum
let lineZeroMeaningfulContent line = lineZeroMeaningfulContent_ line ( String . length line ) 1 0
let formatComment_ txt = let commLines = Reason_syntax_util . split_by ~ keep_empty : true ( fun x -> x = ' \ n ' ) ( Comment . wrap txt ) in match commLines with | [ ] -> atom " " | [ hd ] -> atom hd | zero :: one :: tl -> let attemptRemoveCount = ( smallestLeadingSpaces ( one :: tl ) ) in let leftPad = if beginsWithS...
let formatComment comment = source_map ~ loc ( : Comment . location comment ) ( formatComment_ comment )
let rec append ( ? space = false ) txt = function | Layout . SourceMap ( loc , sub ) -> Layout . SourceMap ( loc , append ~ space txt sub ) | Sequence ( config , l ) when snd config . wrap <> " " -> let sep = if space then " " else " " in Sequence ( { config with wrap ( = fst config . wrap , snd config . wrap ^ sep ^ t...
let appendSep spaceBeforeSep sep layout = append ( if spaceBeforeSep then " " ^ sep else sep ) layout
let rec flattenCommentAndSep ? spaceBeforeSep ( : spaceBeforeSep = false ) ? sepStr = function | Layout . SourceMap ( loc , sub ) -> Layout . SourceMap ( loc , flattenCommentAndSep ~ spaceBeforeSep ? sepStr sub ) | Layout . Whitespace ( info , sub ) -> Layout . Whitespace ( info , flattenCommentAndSep ~ spaceBeforeSep ...
let rec preOrderWalk f layout = match f layout with | Layout . Sequence ( listConfig , sublayouts ) -> let newSublayouts = List . map ( preOrderWalk f ) sublayouts in Layout . Sequence ( listConfig , newSublayouts ) | Layout . Label ( formatter , left , right ) -> let newLeftLayout = preOrderWalk f left in let newRight...
let unbreaklayout = preOrderWalk ( function | Layout . Sequence ( listConfig , sublayouts ) -> Layout . Sequence ( { listConfig with break = Layout . Never } , sublayouts ) | Layout . Label ( formatter , left , right ) -> Layout . Label ( unbreakLabelFormatter formatter , left , right ) | layout -> layout )
let consolidateSeparator l = preOrderWalk ( function | Sequence ( listConfig , sublayouts ) when listConfig . sep != NoSep && listConfig . sepLeft -> let sublayoutsLen = List . length sublayouts in let mapSublayout i layout = match ( listConfig . sep , ( i + 1 = sublayoutsLen ) ) with | ( NoSep , _ ) -> raise ( NotPoss...
let insertLinesAboveItems items = preOrderWalk ( function | Whitespace ( region , sub ) -> insertBlankLines ( WhitespaceRegion . newlines region ) sub | layout -> layout
let insertCommentIntoWhitespaceRegion comment region subLayout = let cl = Comment . location comment in let range = WhitespaceRegion . range region in let nextRegion = WhitespaceRegion . addComment region comment in let formattedComment = formatComment comment in match WhitespaceRegion . comments region with | [ ] -> i...
let rec prependSingleLineComment comment layout = match layout with | Layout . SourceMap ( loc , sub ) -> Layout . SourceMap ( loc , prependSingleLineComment comment sub ) | Sequence ( config , hd :: tl ) when config . break = Always_rec -> Sequence ( config , ( prependSingleLineComment comment hd ) :: tl ) | Whitespac...
let appendComment ~ breakAncestors layout comment = let text = Comment . wrap comment in let layout = match layout with | Layout . Whitespace ( info , sublayout ) -> Layout . Whitespace ( info , makeList ~ break : Layout . Never ~ postSpace : true [ sublayout ; atom text ] ) | layout -> makeList ~ break : Layout . Neve...
let rec looselyAttachComment ~ breakAncestors layout comment = let location = Comment . location comment in match layout with | Layout . SourceMap ( loc , sub ) -> Layout . SourceMap ( loc , looselyAttachComment ~ breakAncestors sub comment ) | Layout . Whitespace ( info , sub ) -> Layout . Whitespace ( info , looselyA...
let rec insertSingleLineComment layout comment = let location = Comment . location comment in match layout with | Layout . SourceMap ( loc , sub ) -> Layout . SourceMap ( loc , insertSingleLineComment sub comment ) | Layout . Whitespace ( info , sub ) -> let range = WhitespaceRegion . range info in if Range . containsL...
let rec attachCommentToNodeRight layout comment = match layout with | Layout . Sequence ( config , sub ) when snd config . wrap <> " " -> let lwrap , rwrap = config . wrap in let rwrap = rwrap ^ " " ^ Comment . wrap comment in Layout . Sequence ( { config with wrap ( = lwrap , rwrap ) } , sub ) | Layout . SourceMap ( l...
let rec attachCommentToNodeLeft comment layout = match layout with | Layout . Sequence ( config , sub ) when snd config . wrap <> " " -> let lwrap , rwrap = config . wrap in let lwrap = Comment . wrap comment ^ " " ^ lwrap in Layout . Sequence ( { config with wrap = ( lwrap , rwrap ) } , sub ) | Layout . SourceMap ( lo...
let rec tryPerfectlyAttachComment layout = function | None -> ( layout , None ) | Some comment -> perfectlyAttachComment comment layout | Layout . Sequence ( listConfig , subLayouts ) -> let distributeCommentIntoSubLayouts ( i , processed , newComment ) layout = let ( layout , newComment ) = tryPerfectlyAttachComment l...
let insertRegularComment layout comment = match perfectlyAttachComment comment layout with | ( layout , None ) -> layout | ( layout , Some _ ) -> looselyAttachComment ~ breakAncestors : false layout comment
let insertEndOfLineComment layout comment = looselyAttachComment ~ breakAncestors : true layout comment
let rec partitionComments_ ( ( singleLines , endOfLines , regulars ) as soFar ) = function | [ ] -> soFar | com :: tl -> match Comment . category com with | Comment . EndOfLine -> partitionComments_ ( singleLines , ( com :: endOfLines ) , regulars ) tl | Comment . SingleLine -> partitionComments_ ( ( com :: singleLines...
let partitionComments comments = let ( singleLines , endOfLines , regulars ) = partitionComments_ ( [ ] , [ ] , [ ] ) comments in ( singleLines , List . rev endOfLines , regulars )
let partitionSingleLineComments loc singleLineComments = let ( before , after ) = List . fold_left ( fun ( before , after ) comment -> let cl = Comment . location comment in let isAfter = loc . loc_end . pos_lnum < cl . loc_start . pos_lnum in if isAfter then ( before , comment :: after ) else ( comment :: before , aft...
let appendSingleLineCommentsToEnd loc layout singleLineComments = let rec aux prevLoc layout i = function | comment :: cs -> let loc = Comment . location comment in let formattedComment = formatComment comment in let commentLayout = if Reason_location . hasSpaceBetween loc prevLoc then insertBlankLines 1 formattedComme...
let attachSingleLineComments singleLineComments = function | Layout . SourceMap ( loc , subLayout ) -> let ( before , after ) = partitionSingleLineComments loc singleLineComments in let layout = List . fold_left insertSingleLineComment subLayout before in appendSingleLineCommentsToEnd loc layout after | layout -> List ...
let format_layout ? comments ppf layout = let easy = match comments with | None -> Layout . to_easy_format layout | Some comments -> let ( singleLines , endOfLines , regulars ) = partitionComments comments in let layout = List . fold_left insertRegularComment layout regulars in let layout = consolidateSeparator layout ...
let partitionFinalWrapping listTester wrapFinalItemSetting x = let rev = List . rev x in match ( rev , wrapFinalItemSetting ) with | ( [ ] , _ ) -> raise ( NotPossible " shouldnt be partitioning 0 label attachments " ) | ( _ , NeverWrapFinalItem ) -> None | ( last :: revEverythingButLast , WrapFinalListyItemIfFewerThan...
let semiTerminated term = makeList [ term ; atom " ; " ]
let makeLetSequence letItems = makeList ~ break : Always_rec ~ inline ( : true , false ) ~ wrap ( " { " , : " } " ) ~ postSpace : true ~ sep ( : SepFinal ( " ; " , " ; " ) ) letItems
let makeLetSequenceSingleLine letItems = makeList ~ break : IfNeed ~ inline ( : true , false ) ~ wrap ( " { " , : " } " ) ~ preSpace : true ~ postSpace : true ~ sep ( : Sep " ; " ) letItems
let makeUnguardedLetSequence ( ? sep ( = Layout . SepFinal ( " ; " , " ; " ) ) ) letItems = makeList ~ break : Always_rec ~ inline ( : true , true ) ~ wrap ( " " , : " " ) ~ indent : 0 ~ postSpace : true ~ sep letItems