Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compiler/flow_parser/**
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#### :house: Internal

- Vendor the Flow parser 0.267.0 sources used by the compiler, removing the external `flow_parser` dependency and establishing a maintained baseline for future OCaml upgrades. https://github.com/rescript-lang/rescript/pull/8587
- Store processed external declarations as structured data instead of serialized values in `pval_prim`, and lower external calls during Lambda translation. This removes `Pccall`, `external_spec`, and the unsupported `%absfloat` primitive. The AST, CMI, and CMT magic numbers are bumped (`ResImpl01301`/`ResIntf01301`, `Caml1999I025`, `Caml1999T026`). https://github.com/rescript-lang/rescript/pull/8581
- Resolve dynamic-import targets during Lambda translation and store the module and export path directly in `Pimport`. This removes the `dynamic_import` flags from `Pjs_call` and `Lglobal_module`, along with backend expression-shape detection. https://github.com/rescript-lang/rescript/pull/8582
- Give nominal variants one canonical runtime layout: compute their JavaScript representation once after typing each declaration, replace positional constructor tags with semantic runtime descriptors, and make construction, matching, coercion, printing, diagnostics, and GenType consume the stored representation instead of reinterpreting annotations. Pattern matching keeps occurrence-specific plans local without adding another Lambda or Lam expression form. https://github.com/rescript-lang/rescript/pull/8579
Expand Down
21 changes: 21 additions & 0 deletions compiler/flow_parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Meta Platforms, Inc. and affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions compiler/flow_parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Vendored Flow parser

This directory contains the OCaml Flow parser used by the ReScript compiler.

- Upstream fork: https://github.com/rescript-lang/flow
- Flow parser version: `0.267.0`
- Source commit: `9ea4062c0b7e037415c4413a7634c459ebd5c31b`
- Original source directories: `src/parser`, `src/third-party/sedlex`,
`src/third-party/sedlex-ppx`, and `src/hack_forked/utils/collections`

The Dune files were adapted to build these sources as private libraries inside
the ReScript repository. Sources used only by the upstream JavaScript and C API
targets are not included. One ambiguous Sedlex documentation comment was
converted to a regular comment so the vendored sources build with ReScript's
warning settings.

Vendored sources are excluded from the repository-wide OCamlformat check so
that they remain comparable with their upstream versions.

The Flow sources are licensed under the MIT licence in `LICENSE`. Vendored
Sedlex and collections sources retain their own licence files.
6 changes: 6 additions & 0 deletions compiler/flow_parser/collections/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(include_subdirs unqualified)

(library
(name collections)
(wrapped false)
(libraries base))
13 changes: 13 additions & 0 deletions compiler/flow_parser/collections/iMap.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

include WrappedMap.Make (IntKey)

let pp : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit =
(fun pp_data -> make_pp IntKey.pp pp_data)

let show pp_data x = Format.asprintf "%a" (pp pp_data) x
14 changes: 14 additions & 0 deletions compiler/flow_parser/collections/iSet.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

include Flow_set.Make (IntKey)

let pp = make_pp IntKey.pp

let show iset = Format.asprintf "%a" pp iset

let to_string = show
54 changes: 54 additions & 0 deletions compiler/flow_parser/collections/immQueue.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

type 'a t = {
incoming: 'a list;
outgoing: 'a list;
length: int;
}

let empty = { incoming = []; outgoing = []; length = 0 }

let length t = t.length

let is_empty t = length t = 0

let push t x = { t with incoming = x :: t.incoming; length = t.length + 1 }

let prepare_for_read t =
match t.outgoing with
| [] -> { t with incoming = []; outgoing = List.rev t.incoming }
| _ -> t

let pop t =
let t = prepare_for_read t in
match t.outgoing with
| [] -> (None, t)
| hd :: tl -> (Some hd, { t with outgoing = tl; length = t.length - 1 })

let peek t =
let t = prepare_for_read t in
match t.outgoing with
| [] -> (None, t)
| hd :: _ -> (Some hd, t)

let exists t ~f = List.exists f t.outgoing || List.exists f t.incoming

let iter t ~f =
List.iter f t.outgoing;
List.iter f (List.rev t.incoming)

let from_list x = { incoming = []; outgoing = x; length = List.length x }

let to_list x = x.outgoing @ List.rev x.incoming

let concat t =
{
incoming = [];
outgoing = Base.List.concat_map ~f:to_list t;
length = List.map (fun u -> u.length) t |> List.fold_left ( + ) 0;
}
37 changes: 37 additions & 0 deletions compiler/flow_parser/collections/immQueue.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

(*
* Immutable queue implementation. Modeled loosely after the mutable stdlib
* Queue. push, pop, etc. are amortized O(1).
*)

type 'a t

val empty : 'a t

val push : 'a t -> 'a -> 'a t

val pop : 'a t -> 'a option * 'a t

val peek : 'a t -> 'a option * 'a t

val is_empty : 'a t -> bool

val length : 'a t -> int

val exists : 'a t -> f:('a -> bool) -> bool

val iter : 'a t -> f:('a -> unit) -> unit

(* from_list: the head of the list is the first one to be popped *)
val from_list : 'a list -> 'a t

(* to_list: the head of the list is the first one to be popped *)
val to_list : 'a t -> 'a list

val concat : 'a t list -> 'a t
12 changes: 12 additions & 0 deletions compiler/flow_parser/collections/intKey.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

type t = int

let compare = ( - )

let pp = Format.pp_print_int
101 changes: 101 additions & 0 deletions compiler/flow_parser/collections/priorityQueue.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

module Make (Ord : Set.OrderedType) = struct
type elt = Ord.t

type t = {
mutable __queue: elt option array;
mutable size: int;
}

let rec make_empty n = { __queue = Array.make n None; size = 0 }

and is_empty t = t.size = 0

and pop t =
if t.size = 0 then failwith "Popping from an empty priority queue";
let v = t.__queue.(0) in
t.size <- t.size - 1;

if t.size <> 0 then (
let last = t.__queue.(t.size) in
t.__queue.(t.size) <- None;
__bubble_down t.__queue t.size last 0
);

match v with
| None -> failwith "Attempting to return a null value"
| Some v -> v

and push t element =
if Array.length t.__queue = t.size then (
let new_queue = Array.make ((Array.length t.__queue * 2) + 1) None in
Array.blit t.__queue 0 new_queue 0 (Array.length t.__queue);
t.__queue <- new_queue
);

t.__queue.(t.size) <- Some element;
__bubble_up t.__queue t.size;
t.size <- t.size + 1;
()

and __swap arr i j =
let tmp = arr.(i) in
arr.(i) <- arr.(j);
arr.(j) <- tmp

and __bubble_up arr index =
if index = 0 then ();
let pindex = (index - 1) / 2 in
match (arr.(index), arr.(pindex)) with
| (None, _)
| (_, None) ->
failwith "Unexpected null index found when calling __bubble_up"
| (Some e, Some p) ->
if Ord.compare e p < 0 then (
__swap arr index pindex;
__bubble_up arr pindex
)

and __bubble_down arr size value index =
let right_child_index = (index * 2) + 2 in
let left_child_index = right_child_index - 1 in
if right_child_index < size then
match (arr.(right_child_index), arr.(left_child_index), value) with
| (None, _, _)
| (_, None, _)
| (_, _, None) ->
failwith "Unexpected null index found when calling __bubble_down"
| (Some r, Some l, Some v) ->
let (smaller_child, smaller_child_index) =
if Ord.compare r l < 0 then
(r, right_child_index)
else
(l, left_child_index)
in
if Ord.compare v smaller_child <= 0 then
arr.(index) <- value
else (
arr.(index) <- arr.(smaller_child_index);
__bubble_down arr size value smaller_child_index
)
else if left_child_index < size then
match (arr.(left_child_index), value) with
| (None, _)
| (_, None) ->
failwith "Unexpected null index found when calling __bubble_down"
| (Some l, Some v) ->
if Ord.compare v l <= 0 then
arr.(index) <- value
else (
arr.(index) <- arr.(left_child_index);
arr.(left_child_index) <- value
)
else
arr.(index) <- value
end
94 changes: 94 additions & 0 deletions compiler/flow_parser/collections/reordered_argument_collections.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

module Reordered_argument_map (S : WrappedMap.S) = struct
include S

let add m ~key ~data = add key data m

let filter m ~f = filter f m

let fold m ~init ~f = fold f m init

let find_opt m k = find_opt k m

let find m k = find k m

let iter m ~f = iter f m

let map m ~f = map f m

let mapi m ~f = mapi f m

let mem m v = mem v m

let remove m v = remove v m

let exists m ~f = exists f m

let merge m1 m2 ~f = merge f m1 m2

let filter m ~f = filter m ~f

let partition m ~f = partition f m
end

module Reordered_argument_set (S : Flow_set.S) = struct
include S

let add s v = add v s

let filter s ~f = filter f s

let fold s ~init ~f = fold f s init

let iter s ~f = iter f s

let mem s v = mem v s

let remove s v = remove v s

let exists s ~f = exists f s

let of_list l = List.fold_left add S.empty l

let make_pp pp fmt x =
Format.fprintf fmt "@[<hv 2>{";
let elts = elements x in
(match elts with
| [] -> ()
| _ -> Format.fprintf fmt " ");
ignore
(List.fold_left
(fun sep elt ->
if sep then Format.fprintf fmt ";@ ";
let () = pp fmt elt in
true)
false
elts
);
(match elts with
| [] -> ()
| _ -> Format.fprintf fmt " ");
Format.fprintf fmt "}@]"
end

module SSet = struct
include Reordered_argument_set (SSet)

let pp = SSet.pp

let show = SSet.show
end

module SMap = struct
include Reordered_argument_map (SMap)

let pp = SMap.pp

let show = SMap.show
end
Loading
Loading