text
stringlengths
0
601k
let marshal2 name m n ( matrix : int list list ) = let matrix : int array array = Array . of_list ( List . map Array . of_list matrix ) in let ( displacement : int array ) , ( data : int array ) = MenhirLib . RowDisplacement . compress ( ) = ( fun x -> x = hole ) hole m n matrix in Error . logC 1 ( fun f -> fprintf f "...
let action node t = match Default . has_default_reduction node with | Some _ -> hole | None -> try let target = SymbolMap . find ( Symbol . T t ) ( Lr1 . transitions node ) in match Default . has_default_reduction target with | Some ( _ , toks ) when TerminalSet . mem Terminal . sharp toks -> assert ( TerminalSet . car...
let ( ) = assert ( Terminal . t2i Terminal . sharp = Terminal . n - 1 )
let goto node nt = try let target = SymbolMap . find ( Symbol . N nt ) ( Lr1 . transitions node ) in encode_Goto target with Not_found -> encode_NoGoto
let error node t = if action node t = encode_Fail then encode_Error else encode_NoError
let default_reduction node = match Default . has_default_reduction node with | Some ( prod , _ ) -> encode_DefRed prod | None -> encode_NoDefRed
let action = define_and_measure ( " action " , marshal2 " action " Lr1 . n ( Terminal . n - 1 ) ( Lr1 . map ( fun node -> Terminal . mapx ( fun t -> action node t ) ) ) )
let goto = define_and_measure ( " goto " , marshal2 " goto " Lr1 . n Nonterminal . n ( Lr1 . map ( fun node -> Nonterminal . map ( fun nt -> goto node nt ) ) ) )
let error = define_and_measure ( " error " , flatten_and_marshal11_list ( Lr1 . map ( fun node -> Terminal . mapx ( fun t -> error node t ) ) ) )
let default_reduction = define_and_measure ( " default_reduction " , marshal1_list ( Lr1 . map ( fun node -> default_reduction node ) ) )
let lhs = define_and_measure ( " lhs " , marshal1 ( Production . amap ( fun prod -> Nonterminal . n2i ( Production . nt prod ) ) ) )
let semantic_action = define ( " semantic_action " , EArray ( Production . mapx semantic_action ) )
let stringwrap f x = EStringConst ( f x )
let reduce_or_accept prod = match Production . classify prod with | Some _ -> " Accepting " | None -> " Reducing production " ^ ( Production . print prod )
let trace = define_and_measure ( " trace " , if Settings . trace then EData ( " Some " , [ ETuple [ EArray ( Terminal . map ( stringwrap Terminal . print ) ) ; EArray ( Production . map ( stringwrap reduce_or_accept ) ) ; ] ] ) else EData ( " None " , [ ] ) )
let token2terminal = destructuretokendef " token2terminal " tint false ( fun tok -> EIntConst ( Terminal . t2i tok ) )
let token2value = destructuretokendef " token2value " tobj true ( fun tok -> ERepr ( match Terminal . ocamltype tok with | None -> EUnit | Some _ -> EVar semv ) )
let strategy = match Settings . strategy with | ` Legacy -> EData ( " ` Legacy " , [ ] ) | ` Simplified -> EData ( " ` Simplified " , [ ] )
let monolithic_entry_point state nt t = define ( Nonterminal . print true nt , let lexer = " lexer " and lexbuf = " lexbuf " in EFun ( [ PVar lexer ; PVar lexbuf ] , annotate ( EMagic ( EApp ( EVar entry , [ strategy ; EIntConst ( Lr1 . number state ) ; EVar lexer ; EVar lexbuf ] ) ) ) ( TypTextual t ) ) )
let monolithic_api : IL . valdef list = Lr1 . fold_entry ( fun _prod state nt t api -> monolithic_entry_point state nt t :: api ) [ ]
let incremental_entry_point state nt t = let initial = " initial_position " in define ( Nonterminal . print true nt , EFun ( [ PVar initial ] , annotate ( EMagic ( EApp ( EVar start , [ EIntConst ( Lr1 . number state ) ; EVar initial ; ] ) ) ) ( checkpoint ( TypTextual t ) ) ) )
let incremental_api : IL . valdef list = Lr1 . fold_entry ( fun _prod state nt t api -> incremental_entry_point state nt t :: api ) [ ]
let eterminal ( t : Terminal . t ) : expr = EData ( tokengadtdata ( Terminal . print t ) , [ ] )
let enonterminal ( nt : Nonterminal . t ) : expr = EData ( tnonterminalgadtdata ( Nonterminal . print false nt ) , [ ] )
let esymbol ( symbol : Symbol . t ) : expr = match symbol with | Symbol . T t -> EData ( dataT , [ eterminal t ] ) | Symbol . N nt -> EData ( dataN , [ enonterminal nt ] )
let xsymbol ( symbol : Symbol . t ) : expr = EData ( dataX , [ esymbol symbol ] )
let terminal ( ) = assert Settings . inspection ; let t = " t " in define ( " terminal " , EFun ( [ PVar t ] , EMatch ( EVar t , Terminal . mapx ( fun tok -> branch ( pint ( Terminal . t2i tok ) ) ( xsymbol ( Symbol . T tok ) ) ) @ [ branch PWildcard ( EComment ( " This terminal symbol does not exist . " , EApp ( EVar ...
let nonterminal ( ) = assert Settings . inspection ; let nt = " nt " in define ( " nonterminal " , EFun ( [ PVar nt ] , EMatch ( EVar nt , Nonterminal . foldx ( fun nt branches -> branch ( pint ( Nonterminal . n2i nt ) ) ( xsymbol ( Symbol . N nt ) ) :: branches ) [ branch PWildcard ( EComment ( " This nonterminal symb...
let lr0_incoming ( ) = assert Settings . inspection ; define_and_measure ( " lr0_incoming " , marshal1 ( Array . init Lr0 . n ( fun node -> encode_symbol_option ( Lr0 . incoming_symbol node ) ) ) )
let rhs ( ) = assert Settings . inspection ; let productions : int array array = Production . amap ( fun prod -> Array . map encode_symbol ( Production . rhs prod ) ) in define_and_measure ( " rhs " , linearize_and_marshal1 productions )
let lr0_core ( ) = assert Settings . inspection ; define_and_measure ( " lr0_core " , marshal1_list ( Lr1 . map ( fun ( node : Lr1 . node ) -> Lr0 . core ( Lr1 . state node ) ) ) )
let lr0_items ( ) = assert Settings . inspection ; let items : int array array = Array . init Lr0 . n ( fun node -> Array . map Item . marshal ( Array . of_list ( Item . Set . elements ( Lr0 . items node ) ) ) ) in define_and_measure ( " lr0_items " , linearize_and_marshal1 items )
let nullable ( ) = assert Settings . inspection ; define_and_measure ( " nullable " , marshal11_list ( Nonterminal . map ( fun nt -> encode_bool ( Analysis . nullable nt ) ) ) )
let first ( ) = assert Settings . inspection ; define_and_measure ( " first " , flatten_and_marshal11_list ( Nonterminal . map ( fun nt -> Terminal . mapx ( fun t -> encode_bool ( TerminalSet . mem t ( Analysis . first nt ) ) ) ) ) )
let versiondef = { valpublic = true ; valpat = PUnit ; valval = EVar ( staticVersion ^ " . require_ " ^ Version . version ) ; }
let program = [ SIFunctor ( grammar . parameters , SIComment " This generated code requires the following version of MenhirLib " : :: SIValDefs ( false , [ versiondef ] ) :: mbasics grammar @ SIStretch grammar . preludes :: SIModuleDef ( tables , MStruct [ SIInclude ( MVar basics ) ; SIValDefs ( false , [ token2termina...
let ( ) = Time . tick " Producing tables and abstract syntax " end
module Result = struct include TableclothResult ] } ) * let pp ( okf : Format . formatter -> ' ok -> unit ) unit ( errf : Format . formatter -> ' error -> unit ) unit ( fmt : Format . formatter ) formatter ( r : ( ' ok , ' error ) ' error t ) t : unit = match r with | Ok ok -> Format . pp_print_string fmt " < ok : " ; ...
type ' a t = ' a array
let singleton a = [ | a ] |
let clone t = Array . map TableclothFun . identity t
let isEmpty a = length a = 0
let initialize length ~ f = Belt . Array . makeBy length f
let range ( ? from = 0 ) 0 to_ = Belt . Array . makeBy ( to_ - from ) from ( fun i -> i + from ) from
let toList : ' a array -> ' a list = Belt . List . fromArray
let toIndexedList ( array : ' a array ) array : ( int * ' a ) ' a list = Belt . Array . reduceReverse array ( length array - 1 , [ ] ) ( fun ( i , acc ) acc x -> ( i - 1 , ( i , x ) x :: acc ) acc ) acc . | snd
let getAt t ~ index = Belt . Array . get t index
let ( ( ) . ? ) ( array : ' element t ) t ( index : int ) int : ' element option = getAt array ~ index
let first t = getAt t ~ index : 0
let last t = getAt t ~ index ( : Array . length t - 1 ) 1
let set t index value = t ( . index ) index <- value
let setAt t ~ index ~ value = t ( . index ) index <- value
let filter t ~ f = Belt . Array . keep t f
let swap t i j = let temp = t ( . i ) i in t ( . i ) i <- t ( . j ) j ; t ( . j ) j <- temp ; ( )
let fold t ~ initial ~ f = Belt . Array . reduce t initial f
let foldRight t ~ initial ~ f = Belt . Array . reduceReverse t initial f
let maximum t ~ compare = fold t ~ initial : None ~ f ( : fun max element -> match max with | None -> Some element | Some current -> ( match compare element current > 0 with | true -> Some element | false -> max ) )
let minimum t ~ compare = fold t ~ initial : None ~ f ( : fun min element -> match min with | None -> Some element | Some current -> ( match compare element current < 0 with | true -> Some element | false -> min ) )
let extent t ~ compare = fold t ~ initial : None ~ f ( : fun range element -> match range with | None -> Some ( element , element ) element | Some ( min , max ) max -> Some ( ( match compare element min < 0 with | true -> element | false -> min ) , match compare element max > 0 with | true -> element | false -> max ) )
let sum ( type a ) a t ( module M : TableclothContainer . Sum with type t = a ) a : a = Array . fold_left M . add M . zero t
let map t ~ f = Belt . Array . map t f
let mapWithIndex t ~ f = Belt . Array . mapWithIndex t f
let map2 a b ( ~ f : ' a -> ' b -> ' c ) ' c : ' c array = Belt . Array . zipBy a b f
let map3 as_ bs ( cs : ' c t ) t ~ f = let minLength = Belt . Array . reduce [ | length bs ; length cs ] | ( length as_ ) as_ min in Belt . Array . makeBy minLength ( fun i -> f as_ ( . i ) i bs ( . i ) i cs ( . i ) i ) i
let zip = map2 ~ f ( : fun a b -> ( a , b ) b ) b
let flatMap t ~ f = Belt . Array . map t f . | Belt . Array . concatMany
let sliding ( ? step = 1 ) 1 a ~ size = let n = Array . length a in if size > n then [ ] || else initialize ( 1 + ( ( n - size ) size / step ) step ) step ~ f ( : fun i -> initialize size ~ f ( : fun j -> a ( ( . i * step ) step + j ) j ) j ) j
let find t ~ f = let rec find_loop t ~ f ~ length i = if i >= length then None else if f t ( . i ) i then Some t ( . i ) i else find_loop t ~ f ~ length ( i + 1 ) 1 in find_loop t ~ f ~ length ( : length t ) t 0
let findIndex array ~ f = let rec loop index = if index >= length array then None else if f index array ( . index ) index then Some ( index , array ( . index ) index ) index else loop ( index + 1 ) 1 in loop 0
let any t ~ f = Belt . Array . some t f
let all t ~ f = Belt . Array . every t f
let includes t v ~ equal = any t ~ f ( : equal v ) v
let append a a ' = Belt . Array . concat a a '
let flatten ( ars : ' a array array ) array = Belt . Array . concatMany ars
let intersperse t ~ sep = Belt . Array . makeBy ( max 0 ( ( length t * 2 ) 2 - 1 ) 1 ) 1 ( fun i -> if i mod 2 <> 0 then sep else t ( . i / 2 ) 2 ) 2
let slice ? to_ array ~ from = let defaultTo = match to_ with None -> length array | Some i -> i in let sliceFrom = if from >= 0 then min ( length array ) array from else max 0 ( min ( length array ) array ( length array + from ) from ) from in let sliceTo = if defaultTo >= 0 then min ( length array ) array defaultTo e...
let count t ~ f = fold t ~ initial : 0 ~ f ( : fun total element -> total + match f element with true -> 1 | false -> 0 )
let chunksOf t ~ size = sliding t ~ step : size ~ size
let forEach t ~ f : unit = Belt . Array . forEach t f
let forEachWithIndex t ~ f : unit = for i = 0 to length t - 1 do f i t ( . i ) i done
let partition t ~ f = let left , right = foldRight t ~ initial ( [ ] , : [ ] ) ~ f ( : fun ( lefts , rights ) rights element -> if f element then ( element :: lefts , rights ) rights else ( lefts , element :: rights ) rights ) in ( fromList left , fromList right ) right
let splitAt t ~ index = ( slice t ~ from : 0 ~ to_ : index , slice t ~ from : index ~ to_ ( : length t ) t ) t
let splitWhen t ~ f = match findIndex t ~ f ( : fun _ e -> f e ) e with | None -> ( t , [ ] ) || | Some ( index , _ ) _ -> splitAt t ~ index
let unzip t = ( Array . init ( length t ) t ( fun i -> fst t ( . i ) i ) i , Array . init ( length t ) t ( fun i -> snd t ( . i ) i ) i )
let repeat element ~ length = Array . init ( max length 0 ) 0 ( fun _ -> element ) element
let filterMap t ~ f = let result = fold t ~ initial [ ] : ~ f ( : fun results element -> match f element with None -> results | Some value -> value :: results ) . | fromList in reverse result ; result
let sort a ~ compare = Array . sort compare a
let values t = let result = fold t ~ initial [ ] : ~ f ( : fun results element -> match element with None -> results | Some value -> value :: results ) . | fromList in reverse result ; result
let join t ~ sep = Js . Array . joinWith sep t
let groupBy t comparator ~ f = fold t ~ initial ( : TableclothMap . empty comparator ) comparator ~ f ( : fun map element -> let key = f element in TableclothMap . update map ~ key ~ f ( : function | None -> Some [ element ] | Some elements -> Some ( element :: elements ) elements ) )
let equal equal a b = if length a <> length b then false else if length a = 0 then true else let rec loop index = if index = length a then true else equal a ( . index ) index b ( . index ) index && loop ( index + 1 ) 1 in loop 0
let compare compare a b = match TableclothInt . compare ( length a ) a ( length b ) b with | 0 -> if length a == 0 then 0 else let rec loop index = if index = length a then 0 else match compare a ( . index ) index b ( . index ) index with | 0 -> loop ( index + 1 ) 1 | result -> result in loop 0 | result -> result
type t = char type nonrec t = t let compare = compare end ) end
let toCode ( c : char ) char = Char . code c
let fromCode i : char option = if 0 <= i && i <= 255 then Some ( Char . chr i ) i else None
let toString c = String . make 1 c
let fromString str : char option = match String . length str with 1 -> Some str [ . 0 ] 0 | _ -> None
let toDigit char = match char with ' 0 ' . . ' 9 ' -> Some ( toCode char - toCode ' 0 ' ) ' 0 ' | _ -> None
let toLowercase char = match char with | ' A ' . . ' Z ' -> Char . chr ( toCode ' a ' + ( toCode char - toCode ' A ' ) ' A ' ) ' A ' | _ -> char
let toUppercase char = match char with | ' a ' . . ' z ' -> Char . chr ( toCode ' A ' + ( toCode char - toCode ' a ' ) ' a ' ) ' a ' | _ -> char