···11+The MIT License
22+33+Copyright (c) 2016 Jane Street Group, LLC <opensource@janestreet.com>
44+55+Permission is hereby granted, free of charge, to any person obtaining a copy
66+of this software and associated documentation files (the "Software"), to deal
77+in the Software without restriction, including without limitation the rights
88+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99+copies of the Software, and to permit persons to whom the Software is
1010+furnished to do so, subject to the following conditions:
1111+1212+The above copyright notice and this permission notice shall be included in all
1313+copies or substantial portions of the Software.
1414+1515+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121+SOFTWARE.
+17
vendor/opam/ocaml-syntax-shims/README.md
···11+ocaml-syntax-shims
22+==================
33+44+This package provides a small utility that backports some of the newer
55+OCaml syntax to older OCaml compilers. This allows adopting new
66+features such as `let+` while still keeping compatibility with older
77+OCaml compiler.
88+99+To use it, simply depend on this package and add the following field
1010+to your `library` or `executable` stanzas in your `dune` files:
1111+`(preprocess future_syntax)`. For instance:
1212+1313+```scheme
1414+(library
1515+ (name mylib)
1616+ (preprocess future_syntax))
1717+```
+20
vendor/opam/ocaml-syntax-shims/dune-project
···11+(lang dune 2.0)
22+(name ocaml-syntax-shims)
33+44+(generate_opam_files true)
55+66+(license "MIT")
77+(maintainers jeremie@dimino.org)
88+(authors "Jérémie Dimino <jeremie@dimino.org>")
99+(source (github ocaml-ppx/ocaml-syntax-shims))
1010+(documentation https://ocaml-ppx.github.io/ocaml-syntax-shims/)
1111+1212+(package
1313+ (name ocaml-syntax-shims)
1414+ (depends
1515+ ("ocaml" (>= "4.02.3")))
1616+ (synopsis "Backport new syntax to older OCaml versions")
1717+ (description "\
1818+This packages backports new features of the language to older
1919+compilers, such as let+.
2020+"))
···11+open StdLabels
22+open Shims
33+44+let prog_name = Filename.basename Sys.executable_name
55+66+let dump_ast = ref false
77+88+(* Table from positions to custom operators at these positions *)
99+let custom_operators = Hashtbl.create 128
1010+1111+module Wrap_lexer = struct
1212+ let save_loc = Location.curr
1313+1414+ let restore_loc (lexbuf : Lexing.lexbuf) (loc : Location.t) =
1515+ lexbuf.lex_start_p <- loc.loc_start;
1616+ lexbuf.lex_curr_p <- loc.loc_end
1717+1818+ let encode_op (tok : Parser.token) op =
1919+ (match tok with LET -> "let__" | AND -> "and__" | _ -> assert false) ^ op
2020+2121+ let pending = Queue.create ()
2222+2323+ let add (x : Parser.token * _) = Queue.push x pending
2424+2525+ let register_custom_operator tok op (loc1 : Location.t) (loc2 : Location.t) =
2626+ let op = encode_op tok op in
2727+ Hashtbl.add custom_operators loc1.loc_start
2828+ ({ loc1 with loc_end = loc2.loc_end }, op)
2929+3030+ let wrap (lexer : Lexing.lexbuf -> Parser.token) lb =
3131+ if not (Queue.is_empty pending) then (
3232+ let tok, loc = Queue.pop pending in
3333+ restore_loc lb loc;
3434+ tok )
3535+ else
3636+ match lexer lb with
3737+ | (LET | AND) as tok ->
3838+ let loc = save_loc lb in
3939+ ( match Let_trail.op lb with
4040+ | None -> ()
4141+ | Some op -> register_custom_operator tok op loc (save_loc lb) );
4242+ restore_loc lb loc;
4343+ tok
4444+ | LPAREN ->
4545+ let loc1 = save_loc lb in
4646+ let tok2 = lexer lb in
4747+ let loc2 = save_loc lb in
4848+ let tok, loc =
4949+ match tok2 with
5050+ | LET | AND -> (
5151+ match Let_trail.op lb with
5252+ | None ->
5353+ add (tok2, loc2);
5454+ (Parser.LPAREN, loc1)
5555+ | Some op -> (
5656+ let loc3 = save_loc lb in
5757+ match lexer lb with
5858+ | RPAREN ->
5959+ ( LIDENT (encode_op tok2 op),
6060+ { loc2 with loc_end = loc3.loc_end } )
6161+ | tok4 ->
6262+ let loc4 = save_loc lb in
6363+ add (tok2, loc2);
6464+ add (tok4, loc4);
6565+ register_custom_operator tok2 op loc2 loc3;
6666+ (LPAREN, loc1) ) )
6767+ | _ ->
6868+ add (tok2, loc2);
6969+ (LPAREN, loc1)
7070+ in
7171+ restore_loc lb loc;
7272+ tok
7373+ | tok -> tok
7474+7575+ let () = Lexer.set_preprocessor (fun () -> Queue.clear pending) wrap
7676+end
7777+7878+module Map_ast = struct
7979+ open Ast_mapper
8080+ open Asttypes
8181+ open Parsetree
8282+ open Ast_helper
8383+8484+ let get_op vb =
8585+ match Hashtbl.find custom_operators vb.pvb_loc.loc_start with
8686+ | exception Not_found -> None
8787+ | loc, op -> Some (Exp.ident ~loc { txt = Lident op; loc })
8888+8989+ let mapper =
9090+ let super = default_mapper in
9191+ let expr self expr =
9292+ let expr =
9393+ match expr.pexp_desc with
9494+ | Pexp_let (rf, (vb :: _ as vbs), body) -> (
9595+ match get_op vb with
9696+ | None -> expr
9797+ | Some op ->
9898+ if rf = Recursive then
9999+ Location.raise_errorf ~loc:expr.pexp_loc
100100+ "Custom 'let' operators cannot be recursive";
101101+ let patts, exprs =
102102+ List.map vbs ~f:(fun vb ->
103103+ let {
104104+ pvb_pat = patt;
105105+ pvb_expr = expr;
106106+ pvb_attributes = attrs;
107107+ pvb_loc = loc;
108108+ } =
109109+ vb
110110+ in
111111+ ( match attrs with
112112+ | [] -> ()
113113+ | ({ loc; _ }, _) :: _ ->
114114+ Location.raise_errorf ~loc
115115+ "This attribute will be discarded" );
116116+ let op =
117117+ match get_op vb with
118118+ | Some op ->
119119+ Hashtbl.remove custom_operators vb.pvb_loc.loc_start;
120120+ op
121121+ | None ->
122122+ Location.raise_errorf ~loc
123123+ "Custom 'and' operator expected, got stantard \
124124+ 'and' keyword"
125125+ in
126126+ (patt, (loc, op, expr)))
127127+ |> List.split
128128+ in
129129+ let patt =
130130+ List.fold_left (List.tl patts) ~init:(List.hd patts)
131131+ ~f:(fun acc patt ->
132132+ let loc = patt.ppat_loc in
133133+ Pat.tuple ~loc [ acc; patt ])
134134+ in
135135+ let vars =
136136+ List.mapi exprs ~f:(fun i _ ->
137137+ Printf.sprintf "__future_syntax__%d__" i)
138138+ in
139139+ let pvars =
140140+ List.map2 vars patts ~f:(fun v p ->
141141+ let loc = { p.ppat_loc with loc_ghost = true } in
142142+ Pat.var ~loc { txt = v; loc })
143143+ in
144144+ let evars =
145145+ List.map2 vars exprs ~f:(fun v (_, _, e) ->
146146+ let loc = { e.pexp_loc with loc_ghost = true } in
147147+ Exp.ident ~loc { txt = Lident v; loc })
148148+ in
149149+ let expr =
150150+ List.fold_left2 (List.tl evars) (List.tl exprs)
151151+ ~init:(List.hd evars) ~f:(fun acc var (loc, op, _) ->
152152+ Exp.apply ~loc op [ (nolabel, acc); (nolabel, var) ])
153153+ in
154154+ let body =
155155+ let loc = expr.pexp_loc in
156156+ Exp.apply ~loc op
157157+ [
158158+ (nolabel, expr);
159159+ (nolabel, Exp.fun_ ~loc nolabel None patt body);
160160+ ]
161161+ in
162162+ List.fold_right2 pvars exprs ~init:body
163163+ ~f:(fun var (loc, _, expr) acc ->
164164+ Exp.let_ Nonrecursive ~loc [ Vb.mk ~loc var expr ] acc) )
165165+ | _ -> expr
166166+ in
167167+ super.expr self expr
168168+ in
169169+ { super with expr }
170170+171171+ let map f ast =
172172+ let ast = f mapper ast in
173173+ let fail _ (loc, _) =
174174+ Location.raise_errorf ~loc "Invalid use of custom 'let' or 'and' operator"
175175+ in
176176+ Hashtbl.iter fail custom_operators;
177177+ ast
178178+179179+ let structure = mapper.structure mapper
180180+181181+ let signature = mapper.signature mapper
182182+end
183183+184184+let process_file fn ~magic ~parse ~print ~map ~mk_ext =
185185+ let lexbuf = Lexing.from_channel (open_in_bin fn) in
186186+ Location.init lexbuf fn;
187187+ Location.input_lexbuf := Some lexbuf;
188188+ let ast =
189189+ try map (parse lexbuf)
190190+ with exn -> (
191191+ match error_of_exn exn with
192192+ | Some error ->
193193+ if !dump_ast then
194194+ [
195195+ mk_ext ?loc:None ?attrs:None (Ast_mapper.extension_of_error error);
196196+ ]
197197+ else (
198198+ Location.report_error Format.err_formatter error;
199199+ exit 1 )
200200+ | None -> raise exn )
201201+ in
202202+ if !dump_ast then (
203203+ set_binary_mode_out stdout true;
204204+ output_string stdout magic;
205205+ output_value stdout fn;
206206+ output_value stdout ast;
207207+ flush stdout )
208208+ else Format.printf "%a@?" print ast
209209+210210+let process_file fn =
211211+ let ext =
212212+ match String.rindex fn '.' with
213213+ | exception Not_found -> ""
214214+ | i -> String.sub fn ~pos:i ~len:(String.length fn - i)
215215+ in
216216+ match ext with
217217+ | ".ml" ->
218218+ process_file fn ~magic:Config.ast_impl_magic_number
219219+ ~parse:Parse.implementation ~print:Pprintast.structure
220220+ ~map:Map_ast.structure ~mk_ext:Ast_helper.Str.extension
221221+ | ".mli" ->
222222+ process_file fn ~magic:Config.ast_intf_magic_number ~parse:Parse.interface
223223+ ~print:Pprintast.signature ~map:Map_ast.signature
224224+ ~mk_ext:Ast_helper.Sig.extension
225225+ | _ ->
226226+ Printf.eprintf "%s: Don't know what to do with %s.\n%!" prog_name fn;
227227+ exit 2
228228+229229+let () =
230230+ let args =
231231+ Arg.align
232232+ [
233233+ ( "-dump-ast",
234234+ Arg.Set dump_ast,
235235+ " Output a binary AST rather than a pretty-printed source file" );
236236+ ]
237237+ in
238238+ let usage = Printf.sprintf "Usage: %s [-dump-ast] FILES" prog_name in
239239+ Arg.parse args process_file usage
+9
vendor/opam/ocaml-syntax-shims/src/select-impl
···11+(* -*- tuareg -*- *)
22+33+let v = Scanf.sscanf Sys.argv.(1) "%d.%d" (fun a b -> a, b) in
44+print_string (
55+ if v < (4, 08) then
66+ "real"
77+ else
88+ "nop"
99+)
+13
vendor/opam/ocaml-syntax-shims/src/select-shims
···11+(* -*- tuareg -*- *)
22+33+let v = Scanf.sscanf Sys.argv.(1) "%d.%d" (fun a b -> a, b) in
44+print_string (
55+ if v < (4, 03) then
66+ "402"
77+ else if v < (4, 06) then
88+ "403"
99+ else if v < (4, 08) then
1010+ "406"
1111+ else
1212+ "nop"
1313+)
···11+let ( let+ ) x f = `Let (x, f)
22+33+let ( and+ ) a b = `And (a, b)
44+55+let t =
66+ let+ x = 1 and+ y = 2 and+ z = 3 in
77+ (x, y, z)
88+99+let () =
1010+ match t with
1111+ | `Let (`And (`And (1, 2), 3), f) -> assert (f ((1, 2), 3) = (1, 2, 3))
1212+ | _ -> assert false
1313+1414+(* Make sure the evaluation order is the same as with OCaml >= 4.08 *)
1515+1616+let ( let+ ) x f = f x
1717+1818+let ( and+ ) a b = (a, b)
1919+2020+let () =
2121+ let q1 = Queue.create () in
2222+ let q2 = Queue.create () in
2323+ let () = Queue.add 1 q1 and () = Queue.add 2 q1 and () = Queue.add 3 q1 in
2424+ let+ () = Queue.add 1 q2 and+ () = Queue.add 2 q2 and+ () = Queue.add 3 q2 in
2525+ let l1 = Queue.fold (fun l x -> x :: l) [] q1 in
2626+ let l2 = Queue.fold (fun l x -> x :: l) [] q2 in
2727+ assert (l1 = l2)