text
stringlengths
0
601k
let parseTemplateConstant ~ prefix ( p : Parser . t ) = let startPos = p . startPos in Parser . nextTemplateLiteralToken p ; match p . token with | TemplateTail txt -> Parser . next p ; let txt = if p . mode = ParseForTypeChecker then parseTemplateStringLiteral txt else txt in Parsetree . Pconst_string ( txt , prefix )...
let parseCommaDelimitedRegion p ~ grammar ~ closing ~ f = Parser . leaveBreadcrumb p grammar ; let rec loop nodes = match f p with | Some node -> begin match p . Parser . token with | Comma -> Parser . next p ; loop ( node :: nodes ) | token when token = closing || token = Eof -> List . rev ( node :: nodes ) | _ when G...
let parseCommaDelimitedReversedList p ~ grammar ~ closing ~ f = Parser . leaveBreadcrumb p grammar ; let rec loop nodes = match f p with | Some node -> begin match p . Parser . token with | Comma -> Parser . next p ; loop ( node :: nodes ) | token when token = closing || token = Eof -> ( node :: nodes ) | _ when Gramma...
let parseDelimitedRegion p ~ grammar ~ closing ~ f = Parser . leaveBreadcrumb p grammar ; let rec loop nodes = match f p with | Some node -> loop ( node :: nodes ) | None -> if ( p . Parser . token = Token . Eof || p . token = closing || Recover . shouldAbortListParse p ) then List . rev nodes else ( Parser . err p ( D...
let parseRegion p ~ grammar ~ f = Parser . leaveBreadcrumb p grammar ; let rec loop nodes = match f p with | Some node -> loop ( node :: nodes ) | None -> if p . Parser . token = Token . Eof || Recover . shouldAbortListParse p then List . rev nodes else ( Parser . err p ( Diagnostics . unexpected p . token p . breadcru...
let rec parsePattern ( ? alias = true ) ( ? or_ = true ) p = let startPos = p . Parser . startPos in let attrs = parseAttributes p in let pat = match p . Parser . token with | ( True | False ) as token -> let endPos = p . endPos in Parser . next p ; let loc = mkLoc startPos endPos in Ast_helper . Pat . construct ~ loc ...
type category = | Unexpected of { token : Token . t ; context : ( Grammar . t * Lexing . position ) list } | Expected of { context : Grammar . t option ; pos : Lexing . position ; token : Token . t } | Message of string | Uident of Token . t | Lident of Token . t | UnclosedString | UnclosedTemplate | UnclosedComment | ...
type t = { startPos : Lexing . position ; endPos : Lexing . position ; category : category ; }
type report = t list
let getStartPos t = t . startPos
let getEndPos t = t . endPos
let defaultUnexpected token = " I ' m not sure what to parse here when looking at " " \ ^ ( Token . toString token ) ^ " " . " \
let reservedKeyword token = let tokenTxt = Token . toString token in " ` " ^ tokenTxt ^ " ` is a reserved keyword . Keywords need to be escaped : " " \\\ ^ tokenTxt ^ " " " \
let explain t = match t . category with | Uident currentToken -> begin match currentToken with | Lident lident -> let guess = String . capitalize_ascii lident in " Did you mean ` " ^ guess " ` ^ instead of ` " ^ lident ^ " ` " ? | t when Token . isKeyword t -> let token = Token . toString t in " ` " ^ token ^ " ` is a ...
let make ~ startPos ~ endPos category = { startPos ; endPos ; category }
let printReport diagnostics src = let rec print diagnostics src = match diagnostics with | [ ] -> ( ) | d :: rest -> Res_diagnostics_printing_utils . Super_location . super_error_reporter Format . err_formatter src ~ loc : Location . { loc_start = d . startPos ; loc_end = d . endPos ; loc_ghost = false } ( explain d ) ...
let unexpected token context = Unexpected { token ; context }
let expected ? grammar pos token = Expected { context = grammar ; pos ; token }
let uident currentToken = Uident currentToken
let lident currentToken = Lident currentToken
let unknownUchar code = UnknownUchar code
let message txt = Message txt
let digits_count n = let rec loop n base count = if n >= base then loop n ( base * 10 ) ( count + 1 ) else count in loop ( abs n ) 1 0
let seek_2_lines_before src pos = let open Lexing in let original_line = pos . pos_lnum in let rec loop current_line current_char = if current_line + 2 >= original_line then ( current_char , current_line ) else loop ( if ( src . [ current_char ] [ @ doesNotRaise ] ) = ' \ n ' then current_line + 1 else current_line ) (...
let seek_2_lines_after src pos = let open Lexing in let original_line = pos . pos_lnum in let rec loop current_line current_char = if current_char = String . length src then ( current_char , current_line ) else match src . [ current_char ] [ @ doesNotRaise ] with | ' \ n ' when current_line = original_line + 2 -> ( cur...
let leading_space_count str = let rec loop i count = if i = String . length str then count else if str . [ i ] [ @ doesNotRaise ] != ' ' then count else loop ( i + 1 ) ( count + 1 ) in loop 0 0
let break_long_line max_width line = let rec loop pos accum = if pos = String . length line then accum else let chunk_length = min max_width ( String . length line - pos ) in let chunk = ( String . sub [ @ doesNotRaise ] ) line pos chunk_length in loop ( pos + chunk_length ) ( chunk :: accum ) in loop 0 [ ] |> List . r...
let filter_mapi f l = let rec loop f l i accum = match l with | [ ] -> accum | head :: rest -> let accum = match f i head with | None -> accum | Some result -> result :: accum in loop f rest ( i + 1 ) accum in loop f l 0 [ ] |> List . rev
module Color = struct type color = | Dim | Err | Warn | NoColor let dim = " \ x1b [ 2m " let err = " \ x1b [ 1 ; 31m " let warn = " \ x1b [ 1 ; 33m " let reset = " \ x1b [ 0m " external isatty : out_channel -> bool = " caml_sys_isatty " let should_enable_color ( ) = let term = try Sys . getenv " TERM " with Not_found -...
type gutter = Number of int | Elided
type highlighted_string = { s : string ; start : int ; end_ : int }
type line = { gutter : gutter ; content : highlighted_string list ; }
let print ~ is_warning ~ src ~ startPos ~ endPos = let open Lexing in let indent = 2 in let highlight_line_start_line = startPos . pos_lnum in let highlight_line_end_line = endPos . pos_lnum in let ( start_line_line_offset , first_shown_line ) = seek_2_lines_before src startPos in let ( end_line_line_end_offset , last_...
let setup_colors ( ) = Misc . Color . setup ! Clflags . color ; Super_code_frame . setup ! Clflags . color
let print_loc ~ normalizedRange ppf ( loc : Location . t ) = setup_colors ( ) ; let dim_loc ppf = function | None -> ( ) | Some ( ( start_line , start_line_start_char ) , ( end_line , end_line_end_char ) ) -> if start_line = end_line then if start_line_start_char = end_line_end_char then fprintf ppf " { :@< dim >% i :%...
let print ~ message_kind intro src ppf ( loc : Location . t ) = begin match message_kind with | ` warning -> fprintf ppf " [ { @@< info >% s } ] , " @@@ intro | ` warning_as_error -> fprintf ppf " [ { @@< error >% s } @ ( configured as error ) ] , " @@ intro | ` error -> fprintf ppf " [ { @@< error >% s } ] , " @@@ int...
let super_error_reporter ppf src ~ loc msg = setup_colors ( ) ; Format . fprintf ppf " [ @< v , >@ % a , @ % s , ] " @@ ( print ~ message_kind ` : error " Syntax error " ! src ) loc msg ; end
type mode = Break | Flat
type lineStyle = | Classic | Soft | Hard | Literal
type t = | Nil | Text of string | Concat of t list | Indent of t | IfBreaks of { yes : t ; no : t ; mutable broken : bool } | LineSuffix of t | LineBreak of lineStyle | Group of { mutable shouldBreak : bool ; doc : t } | CustomLayout of t list | BreakParent
let line = LineBreak Classic
let hardLine = LineBreak Hard
let softLine = LineBreak Soft
let literalLine = LineBreak Literal
let text s = Text s
let rec _concat acc l = match l with | Text s1 :: Text s2 :: rest -> Text ( s1 ^ s2 ) :: _concat acc rest | Nil :: rest -> _concat acc rest | Concat l2 :: rest -> _concat ( _concat acc rest ) l2 | x :: rest -> let rest1 = _concat acc rest in if rest1 == rest then l else x :: rest1 | [ ] -> acc
let concat l = Concat ( _concat [ ] l )
let indent d = Indent d
let ifBreaks t f = IfBreaks { yes = t ; no = f ; broken = false }
let lineSuffix d = LineSuffix d
let group d = Group { shouldBreak = false ; doc = d }
let breakableGroup ~ forceBreak d = Group { shouldBreak = forceBreak ; doc = d }
let customLayout gs = CustomLayout gs
let space = Text " "
let comma = Text " , "
let dot = Text " . "
let dotdot = Text " . . "
let dotdotdot = Text " . . . "
let lessThan = Text " " <
let greaterThan = Text " " >
let lbrace = Text " { "
let rbrace = Text " } "
let lparen = Text " ( "
let rparen = Text " ) "
let lbracket = Text " [ "
let rbracket = Text " ] "
let question = Text " " ?
let tilde = Text " " ~
let equal = Text " " =
let trailingComma = ifBreaks comma nil
let doubleQuote = Text " " " \
let propagateForcedBreaks doc = let rec walk doc = match doc with | Text _ | Nil | LineSuffix _ -> false | BreakParent -> true | LineBreak ( Hard | Literal ) -> true | LineBreak ( Classic | Soft ) -> false | Indent children -> let childForcesBreak = walk children in childForcesBreak | IfBreaks ( { yes = trueDoc ; no = ...
let rec willBreak doc = match doc with | LineBreak ( Hard | Literal ) | BreakParent | Group { shouldBreak = true } -> true | Group { doc } | Indent doc | CustomLayout ( doc :: _ ) -> willBreak doc | Concat docs -> List . exists willBreak docs | IfBreaks { yes ; no } -> willBreak yes || willBreak no | _ -> false
let join ~ sep docs = let rec loop acc sep docs = match docs with | [ ] -> List . rev acc | [ x ] -> List . rev ( x :: acc ) | x :: xs -> loop ( sep :: x :: acc ) sep xs in concat ( loop [ ] sep docs )
let fits w stack = let width = ref w in let result = ref None in let rec calculate indent mode doc = match mode , doc with | _ when result . contents != None -> ( ) | _ when width . contents < 0 -> result := Some false | _ , Nil | _ , LineSuffix _ | _ , BreakParent -> ( ) | _ , Text txt -> width := width . contents - (...
let toString ~ width doc = propagateForcedBreaks doc ; let buffer = MiniBuffer . create 1000 in let rec process ~ pos lineSuffices stack = match stack with | ( ( ind , mode , doc ) as cmd ) :: rest -> begin match doc with | Nil | BreakParent -> process ~ pos lineSuffices rest | Text txt -> MiniBuffer . add_string buffe...
let debug t = let rec toDoc = function | Nil -> text " nil " | BreakParent -> text " breakparent " | Text txt -> text ( " text ( " " \ ^ txt ^ " " ) " ) \ | LineSuffix doc -> group ( concat [ text " linesuffix ( " ; indent ( concat [ line ; toDoc doc ] ) ; line ; text " ) " ] ) | Concat [ ] -> text " concat ( ) " | Con...
type ( ' ast , ' diagnostics ) parseResult = { filename : string ; [ @ live ] source : string ; parsetree : ' ast ; diagnostics : ' diagnostics ; invalid : bool ; comments : Res_comment . t list }
type ( ' diagnostics ) parsingEngine = { parseImplementation : forPrinter : bool -> filename : string -> ( Parsetree . structure , ' diagnostics ) parseResult ; parseInterface : forPrinter : bool -> filename : string -> ( Parsetree . signature , ' diagnostics ) parseResult ; stringOfDiagnostics : source : string -> fil...
type printEngine = { printImplementation : width : int -> filename : string -> comments : Res_comment . t list -> Parsetree . structure -> unit ; printInterface : width : int -> filename : string -> comments : Res_comment . t list -> Parsetree . signature -> unit ; }
let setup ~ filename ~ forPrinter ( ) = let src = IO . readFile ~ filename in let mode = if forPrinter then Res_parser . Default else ParseForTypeChecker in Res_parser . make ~ mode src filename
let parsingEngine = { parseImplementation = begin fun ~ forPrinter ~ filename -> let engine = setup ~ filename ~ forPrinter ( ) in let structure = Res_core . parseImplementation engine |> To_current . copy_structure in let ( invalid , diagnostics ) = match engine . diagnostics with | [ ] as diagnostics -> ( false , dia...
let printEngine = { printImplementation = begin fun ~ width ~ filename : _ ~ comments structure -> print_string ( Res_printer . printImplementation ~ width ( From_current . copy_structure structure ) ~ comments ) end ; printInterface = begin fun ~ width ~ filename : _ ~ comments signature -> print_string ( Res_printer ...
let parse_implementation sourcefile = Location . input_name := sourcefile ; let parseResult = parsingEngine . parseImplementation ~ forPrinter : false ~ filename : sourcefile in if parseResult . invalid then begin Res_diagnostics . printReport parseResult . diagnostics parseResult . source ; exit 1 end ; parseResult . ...
let parse_interface sourcefile = Location . input_name := sourcefile ; let parseResult = parsingEngine . parseInterface ~ forPrinter : false ~ filename : sourcefile in if parseResult . invalid then begin Res_diagnostics . printReport parseResult . diagnostics parseResult . source ; exit 1 end ; parseResult . parsetree
let setup ~ filename = if String . length filename > 0 then ( Location . input_name := filename ; IO . readFile ~ filename |> Lexing . from_string ) else Lexing . from_channel stdin
let extractOcamlConcreteSyntax filename = let lexbuf = if String . length filename > 0 then IO . readFile ~ filename |> Lexing . from_string else Lexing . from_channel stdin in let stringLocs = ref [ ] in let commentData = ref [ ] in let rec next ( prevTokEndPos : Lexing . position ) ( ) = let token = Lexer . token_wit...
let parsingEngine = { Res_driver . parseImplementation = begin fun ~ forPrinter : _ ~ filename -> let lexbuf = setup ~ filename in let ( stringData , comments ) = extractOcamlConcreteSyntax ! Location . input_name in let structure = Parse . implementation lexbuf |> From_current . copy_structure |> Res_ast_conversion . ...
let printEngine = Res_driver . { printImplementation = begin fun ~ width : _ ~ filename : _ ~ comments : _ structure -> Pprintast . structure Format . std_formatter structure end ; printInterface = begin fun ~ width : _ ~ filename : _ ~ comments : _ signature -> Pprintast . signature Format . std_formatter signature en...
let isReasonDocComment ( comment : Res_comment . t ) = let content = Res_comment . txt comment in let len = String . length content in if len = 0 then true else if len >= 2 && ( String . unsafe_get content 0 = ' ' * && String . unsafe_get content 1 = ' ' ) * then false else if len >= 1 && ( String . unsafe_get content ...
let extractConcreteSyntax filename = let commentData = ref [ ] in let stringData = ref [ ] in let src = IO . readFile ~ filename in let scanner = Res_scanner . make src ~ filename in let rec next prevEndPos scanner = let ( startPos , endPos , token ) = Res_scanner . scan scanner in match token with | Eof -> ( ) | Comme...
let parsingEngine = { Res_driver . parseImplementation = begin fun ~ forPrinter : _ ~ filename -> let ( chan , close ) = if ( String . length filename ) == 0 then ( stdin , fun _ -> ( ) ) else let file_chan = open_in_bin filename in let ( ) = seek_in file_chan 0 in file_chan , close_in_noerr in let magic = Config . ast...
type t = | OpenDescription | ModuleLongIdent [ @ live ] | Ternary | Es6ArrowExpr | Jsx | JsxAttribute | JsxChild [ @ live ] | ExprOperand | ExprUnary | ExprSetField | ExprBinaryAfterOp of Token . t | ExprBlock | ExprCall | ExprList | ExprArrayAccess | ExprArrayMutation | ExprIf | ExprFor | IfCondition | IfBranch | Else...
let toString = function | OpenDescription -> " an open description " | ModuleLongIdent -> " a module path " | Ternary -> " a ternary expression " | Es6ArrowExpr -> " an es6 arrow function " | Jsx -> " a jsx expression " | JsxAttribute -> " a jsx attribute " | ExprOperand -> " a basic expression " | ExprUnary -> " a una...
let isSignatureItemStart = function | Token . At | Let | Typ | External | Exception | Open | Include | Module | AtAt | Export | PercentPercent -> true | _ -> false
let isAtomicPatternStart = function | Token . Int _ | String _ | Character _ | Backtick | Lparen | Lbracket | Lbrace | Underscore | Lident _ | Uident _ | List | Exception | Lazy | Percent -> true | _ -> false
let isAtomicExprStart = function | Token . True | False | Int _ | String _ | Float _ | Character _ | Backtick | Uident _ | Lident _ | Hash | Lparen | List | Lbracket | Lbrace | LessThan | Module | Percent -> true | _ -> false
let isAtomicTypExprStart = function | Token . SingleQuote | Underscore | Lparen | Lbrace | Uident _ | Lident _ | Percent -> true | _ -> false
let isExprStart = function | Token . True | False | Int _ | String _ | Float _ | Character _ | Backtick | Underscore | Uident _ | Lident _ | Hash | Lparen | List | Module | Lbracket | Lbrace | LessThan | Minus | MinusDot | Plus | PlusDot | Bang | Percent | At | If | Switch | While | For | Assert | Lazy | Try -> true | ...
let isJsxAttributeStart = function | Token . Lident _ | Question -> true | _ -> false