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
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ Follow these steps to upgrade the vendored Flow parser within Melange:
```shell
$ cp \
${FLOW_SRC}/src/parser/*.ml{,i} \
${FLOW_SRC}/src/hack_forked/utils/third_party/flow_{set,map}.ml \
${FLOW_SRC}/src/hack_forked/utils/collections/third-party/flow_{set,map}.ml \
${FLOW_SRC}/src/third-party/sedlex/flow_sedlexing.ml{,i} \
${MELANGE_SRC}/jscomp/js_parser
```

Expand All @@ -159,6 +160,7 @@ $ cp \
For `.ml` files:

```ocaml
(* (executable (name x) (libraries compiler-libs.common)) *)
let () =
let ast = Pparse.read_ast Structure Sys.argv.(1) in
Format.printf "%a" Pprintast.structure ast
Expand All @@ -167,6 +169,7 @@ let () =
For `.mli` files:

```ocaml
(* (executable (name x) (libraries compiler-libs.common)) *)
let () =
let ast = Pparse.read_ast Signature Sys.argv.(1) in
Format.printf "%a" Pprintast.signature ast
Expand Down
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Unreleased

- Support `Sys.opaque_identity` to turn off optimizations
([#1276](https://github.com/melange-re/melange/pull/1276))
- Upgrade the Melange JS parser to [Flow
v0.266.0](https://github.com/facebook/flow/releases/tag/v0.266.0)
([#1380](https://github.com/melange-re/melange/pull/1380))

5.1.0-53 2025-03-23
---------------
Expand Down
14 changes: 14 additions & 0 deletions jscomp/js_parser/comment_attachment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ let statement_add_comments
DeclareModule { s with DeclareModule.comments = merge_comments comments }
| DeclareModuleExports ({ DeclareModuleExports.comments; _ } as s) ->
DeclareModuleExports { s with DeclareModuleExports.comments = merge_comments comments }
| DeclareNamespace ({ DeclareNamespace.comments; _ } as s) ->
DeclareNamespace { s with DeclareNamespace.comments = merge_comments comments }
| DeclareTypeAlias ({ TypeAlias.comments; _ } as s) ->
DeclareTypeAlias { s with TypeAlias.comments = merge_comments comments }
| DeclareOpaqueType ({ OpaqueType.comments; _ } as s) ->
Expand Down Expand Up @@ -571,6 +573,8 @@ let statement_add_comments
InterfaceDeclaration { s with Interface.comments = merge_comments comments }
| Labeled ({ Labeled.comments; _ } as s) ->
Labeled { s with Labeled.comments = merge_comments comments }
| Match ({ Match.comments; _ } as s) ->
Match { s with Match.comments = merge_comments comments }
| Return ({ Return.comments; _ } as s) ->
Return { s with Return.comments = merge_comments comments }
| Switch ({ Switch.comments; _ } as s) ->
Expand Down Expand Up @@ -741,6 +745,16 @@ let object_pattern_property_comment_bounds loc property =
ignore (collector#pattern_object_p property);
collect_without_trailing_line_comment collector

let match_expression_case_comment_bounds (loc, case) =
let collector = new comment_bounds_collector ~loc in
ignore (collector#match_case ~on_case_body:collector#expression (loc, case));
collector#comment_bounds

let match_statement_case_comment_bounds (loc, case) =
let collector = new comment_bounds_collector ~loc in
ignore (collector#match_case ~on_case_body:collector#statement (loc, case));
collector#comment_bounds

let switch_case_comment_bounds (loc, case) =
let collector = new comment_bounds_collector ~loc in
ignore (collector#switch_case (loc, case));
Expand Down
92 changes: 25 additions & 67 deletions jscomp/js_parser/declaration_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,14 @@
* LICENSE file in the root directory of this source tree.
*)

module Ast = Flow_ast
open Token
open Parser_common
open Parser_env
open Flow_ast
open Comment_attachment

module type DECLARATION = sig
val async : env -> bool * Loc.t Comment.t list

val generator : env -> bool * Loc.t Comment.t list

val variance : env -> parse_readonly:bool -> bool -> bool -> Loc.t Variance.t option

val function_params : await:bool -> yield:bool -> env -> (Loc.t, Loc.t) Ast.Function.Params.t

val function_body :
env ->
async:bool ->
generator:bool ->
expression:bool ->
simple_params:bool ->
(Loc.t, Loc.t) Function.body * bool

val check_unique_formal_parameters : env -> (Loc.t, Loc.t) Ast.Function.Params.t -> unit

val check_unique_component_formal_parameters :
env -> (Loc.t, Loc.t) Ast.Statement.ComponentDeclaration.Params.t -> unit

val strict_function_post_check :
env ->
contains_use_strict:bool ->
(Loc.t, Loc.t) Identifier.t option ->
(Loc.t, Loc.t) Ast.Function.Params.t ->
unit

val strict_component_post_check :
env ->
contains_use_strict:bool ->
(Loc.t, Loc.t) Identifier.t ->
(Loc.t, Loc.t) Ast.Statement.ComponentDeclaration.Params.t ->
unit

val let_ :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list

val const :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list

val var :
env ->
(Loc.t, Loc.t) Statement.VariableDeclaration.Declarator.t list
* Loc.t Ast.Comment.t list
* (Loc.t * Parse_error.t) list

val _function : env -> (Loc.t, Loc.t) Statement.t

val enum_declaration : ?leading:Loc.t Comment.t list -> env -> (Loc.t, Loc.t) Statement.t

val component : env -> (Loc.t, Loc.t) Statement.t
end

module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DECLARATION = struct
module Declaration (Parse : Parser_common.PARSER) (Type : Parser_common.TYPE) :
Parser_common.DECLARATION = struct
module Enum = Enum_parser.Enum (Parse)

let check_param =
Expand Down Expand Up @@ -371,12 +310,22 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DE
let _function =
with_loc (fun env ->
let (async, leading_async) = async env in
let (sig_loc, (generator, tparams, id, params, return, predicate, leading)) =
let (sig_loc, (generator, effect_, tparams, id, params, return, predicate, leading)) =
with_loc
(fun env ->
let leading_function = Peek.comments env in
Expect.token env T_FUNCTION;
let (generator, leading_generator) = generator env in
let (effect_, (generator, leading_generator)) =
match Peek.token env with
| T_FUNCTION ->
Eat.token env;
(Function.Arbitrary, generator env)
| T_IDENTIFIER { raw = "hook"; _ } when not async ->
Eat.token env;
(Function.Hook, (false, []))
| t ->
Expect.error env t;
(Function.Arbitrary, generator env)
in
let leading = List.concat [leading_async; leading_function; leading_generator] in
let (tparams, id) =
match (in_export_default env, Peek.token env) with
Expand Down Expand Up @@ -423,7 +372,7 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DE
| None -> (return_annotation_remove_trailing env return, predicate)
| Some _ -> (return, predicate_remove_trailing env predicate)
in
(generator, tparams, id, params, return, predicate, leading))
(generator, effect_, tparams, id, params, return, predicate, leading))
env
in
let simple_params = is_simple_parameter_list params in
Expand All @@ -437,6 +386,7 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DE
params;
body;
generator;
effect_;
async;
predicate;
return;
Expand Down Expand Up @@ -567,6 +517,13 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DE
let name = Statement.ComponentDeclaration.Param.Identifier (identifier_name env) in
Expect.identifier env "as";
(name, Parse.pattern env Parse_error.StrictParamName, false)
| (T_LCURLY, _) ->
error env Parse_error.InvalidComponentParamName;
let fake_name_loc = Peek.loc env in
let fallback_ident = (fake_name_loc, { Ast.Identifier.name = ""; comments = None }) in
let name = Statement.ComponentDeclaration.Param.Identifier fallback_ident in
let local = Parse.pattern env Parse_error.StrictParamName in
(name, local, false)
| (_, _) ->
let id = Parse.identifier_with_type env Parse_error.StrictParamName in
(match id with
Expand All @@ -592,6 +549,7 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Type_parser.TYPE) : DE
let rest =
rest_param env t
|> Option.map (fun (loc, id, comments) ->
if Peek.token env = T_COMMA then Eat.token env;
(loc, { Statement.ComponentDeclaration.RestParam.argument = id; comments })
)
in
Expand Down
8 changes: 8 additions & 0 deletions jscomp/js_parser/declaration_parser.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(*
* 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 Declaration (_ : Parser_common.PARSER) (_ : Parser_common.TYPE) : Parser_common.DECLARATION
46 changes: 25 additions & 21 deletions jscomp/js_parser/enum_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ type explicit_type =
| String
| Symbol
| BigInt [@@deriving ord]
let rec compare_explicit_type :
explicit_type -> explicit_type -> int =
((
fun lhs ->
fun rhs ->
match (lhs, rhs) with
| (Boolean, Boolean) -> 0
| (Number, Number) -> 0
| (String, String) -> 0
| (Symbol, Symbol) -> 0
| (BigInt, BigInt) -> 0
| _ ->
let to_int =
function
| Boolean -> 0
| Number -> 1
| String -> 2
| Symbol -> 3
| BigInt -> 4 in
compare (to_int lhs) (to_int rhs))
[@ocaml.warning "-A"])[@@ocaml.warning "-39"]
include
struct
let _ = fun (_ : explicit_type) -> ()
let rec compare_explicit_type :
explicit_type -> explicit_type -> int =
((
fun lhs rhs ->
match (lhs, rhs) with
| (Boolean, Boolean) -> 0
| (Number, Number) -> 0
| (String, String) -> 0
| (Symbol, Symbol) -> 0
| (BigInt, BigInt) -> 0
| _ ->
let to_int =
function
| Boolean -> 0
| Number -> 1
| String -> 2
| Symbol -> 3
| BigInt -> 4 in
compare (to_int lhs) (to_int rhs))
[@ocaml.warning "-39"][@ocaml.warning "-A"])[@@ocaml.warning "-39"]
let _ = compare_explicit_type
end[@@ocaml.doc "@inline"][@@merlin.hide ]
let string_of_explicit_type =
function
| Boolean -> "boolean"
Expand Down
66 changes: 47 additions & 19 deletions jscomp/js_parser/enum_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,37 @@ end = struct
true
| _ -> false

let number_init env loc ~neg ~leading ~kind ~raw =
let value = Parse.number env kind raw in
let (value, raw) =
if neg then
(-.value, "-" ^ raw)
else
(value, raw)
in
let trailing = Eat.trailing_comments env in
if end_of_member_init env then
NumberInit
( loc,
{
NumberLiteral.value;
raw;
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
}
)
else
InvalidInit loc

let member_init env =
let loc = Peek.loc env in
let leading = Peek.comments env in
match Peek.token env with
| T_NUMBER { kind; raw } ->
let value = Parse.number env kind raw in
let trailing = Eat.trailing_comments env in
if end_of_member_init env then
NumberInit
( loc,
{
NumberLiteral.value;
raw;
comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing ();
}
)
else
InvalidInit loc
| T_MINUS ->
Eat.token env;
(match Peek.token env with
| T_NUMBER { kind; raw } -> number_init env loc ~neg:true ~leading ~kind ~raw
| _ -> InvalidInit loc)
| T_NUMBER { kind; raw } -> number_init env loc ~neg:false ~leading ~kind ~raw
| T_STRING (loc, value, raw, octal) ->
if octal then strict_error env Parse_error.StrictOctalLiteral;
Eat.token env;
Expand Down Expand Up @@ -404,6 +417,7 @@ end = struct
| None ->
let bools_len = List.length members.boolean_members in
let nums_len = List.length members.number_members in
let bigints_len = List.length members.bigint_members in
let strs_len = List.length members.string_members in
let defaulted_len = List.length members.defaulted_members in
let empty () =
Expand All @@ -416,9 +430,9 @@ end = struct
}
in
begin
match (bools_len, nums_len, strs_len, defaulted_len) with
| (0, 0, 0, 0) -> empty ()
| (0, 0, _, _) ->
match (bools_len, nums_len, bigints_len, strs_len, defaulted_len) with
| (0, 0, 0, 0, 0) -> empty ()
| (0, 0, 0, _, _) ->
string_body
~env
~enum_name
Expand All @@ -427,7 +441,7 @@ end = struct
members.string_members
members.defaulted_members
comments
| (_, 0, 0, _) when bools_len >= defaulted_len ->
| (_, 0, 0, 0, _) when bools_len >= defaulted_len ->
List.iter
(fun (loc, { DefaultedMember.id = (_, { Identifier.name = member_name; _ }) }) ->
error_at
Expand All @@ -441,7 +455,7 @@ end = struct
has_unknown_members;
comments;
}
| (0, _, 0, _) when nums_len >= defaulted_len ->
| (0, _, 0, 0, _) when nums_len >= defaulted_len ->
List.iter
(fun (loc, { DefaultedMember.id = (_, { Identifier.name = member_name; _ }) }) ->
error_at
Expand All @@ -455,6 +469,20 @@ end = struct
has_unknown_members;
comments;
}
| (0, 0, _, 0, _) when bigints_len >= defaulted_len ->
List.iter
(fun (loc, { DefaultedMember.id = (_, { Identifier.name = member_name; _ }) }) ->
error_at
env
(loc, Parse_error.EnumNumberMemberNotInitialized { enum_name; member_name }))
members.defaulted_members;
BigIntBody
{
BigIntBody.members = members.bigint_members;
explicit_type = false;
has_unknown_members;
comments;
}
| _ ->
error_at env (name_loc, Parse_error.EnumInconsistentMemberValues { enum_name });
empty ()
Expand Down
Loading