Skip to content

Remove ast_desc and ast_info from jane-syntax #1488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2023
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
12,885 changes: 6,408 additions & 6,477 deletions ocaml/boot/menhir/parser.ml

Large diffs are not rendered by default.

25 changes: 10 additions & 15 deletions ocaml/parsing/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ module T = struct
match Jane_syntax.Core_type.of_ast typ with
| Some (jtyp, attrs) -> begin
let attrs = sub.attributes sub attrs in
Jane_syntax_parsing.Core_type.wrap_desc ~loc ~info:attrs @@
match sub.typ_jane_syntax sub jtyp with
| _ -> .
let typ = match sub.typ_jane_syntax sub jtyp with
| _ -> .
in
{ typ with ptyp_attributes = attrs @ typ.ptyp_attributes }
end
| None ->
let attrs = sub.attributes sub attrs in
Expand Down Expand Up @@ -254,16 +255,14 @@ module T = struct
let name = map_loc sub pext_name in
match Jane_syntax.Extension_constructor.of_ast ext with
| Some (jext, attrs) -> begin
let attrs = sub.attributes sub attrs in
Jane_syntax_parsing.Extension_constructor.wrap_desc
~loc ~info:(attrs, name) @@
let _attrs = sub.attributes sub attrs in
match sub.extension_constructor_jane_syntax sub jext with
| _ -> .
end
| None ->
let attrs = sub.attributes sub pext_attributes in
Te.constructor ~loc ~attrs
(map_loc sub pext_name)
name
(map_extension_constructor_kind sub pext_kind)

end
Expand Down Expand Up @@ -321,9 +320,8 @@ module MT = struct
match Jane_syntax.Module_type.of_ast mty with
| Some (jmty, attrs) -> begin
let attrs = sub.attributes sub attrs in
Jane_syntax_parsing.Module_type.wrap_desc ~loc ~info:attrs @@
match sub.module_type_jane_syntax sub jmty with
| Jmty_strengthen smty -> Jane_syntax.Strengthen.mty_of ~loc smty
| Jmty_strengthen smty -> Jane_syntax.Strengthen.mty_of ~loc ~attrs smty
end
| None ->
let attrs = sub.attributes sub attrs in
Expand Down Expand Up @@ -373,7 +371,6 @@ module MT = struct
let loc = sub.location sub loc in
match Jane_syntax.Signature_item.of_ast sigi with
| Some jsigi -> begin
Jane_syntax_parsing.Signature_item.wrap_desc ~loc ~info:() @@
match sub.signature_item_jane_syntax sub jsigi with
| Jsig_include_functor incl ->
Jane_syntax.Include_functor.sig_item_of ~loc incl
Expand Down Expand Up @@ -453,7 +450,6 @@ module M = struct
let loc = sub.location sub loc in
match Jane_syntax.Structure_item.of_ast stri with
| Some jstri -> begin
Jane_syntax_parsing.Structure_item.wrap_desc ~loc ~info:() @@
match sub.structure_item_jane_syntax sub jstri with
| Jstr_include_functor incl ->
Jane_syntax.Include_functor.str_item_of ~loc incl
Expand Down Expand Up @@ -540,8 +536,8 @@ module E = struct
match Jane_syntax.Expression.of_ast exp with
| Some (jexp, attrs) -> begin
let attrs = sub.attributes sub attrs in
Jane_syntax_parsing.Expression.wrap_desc ~loc ~info:attrs
(Jane_syntax.Expression.expr_of ~loc (sub.expr_jane_syntax sub jexp))
Jane_syntax.Expression.expr_of ~loc ~attrs
(sub.expr_jane_syntax sub jexp)
end
| None ->
let attrs = sub.attributes sub attrs in
Expand Down Expand Up @@ -658,8 +654,7 @@ module P = struct
match Jane_syntax.Pattern.of_ast pat with
| Some (jpat, attrs) -> begin
let attrs = sub.attributes sub attrs in
Jane_syntax_parsing.Pattern.wrap_desc ~loc ~info:attrs @@
Jane_syntax.Pattern.pat_of ~loc (sub.pat_jane_syntax sub jpat)
Jane_syntax.Pattern.pat_of ~loc ~attrs (sub.pat_jane_syntax sub jpat)
end
| None ->
let attrs = sub.attributes sub attrs in
Expand Down
46 changes: 22 additions & 24 deletions ocaml/parsing/jane_syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ open Jane_syntax_parsing
future syntax features to remember to do this wrapping.
*)

module With_attributes = With_attributes

(** List and array comprehensions *)
module Comprehensions = struct
let feature : Feature.t = Language_extension Comprehensions
Expand Down Expand Up @@ -94,9 +92,7 @@ module Comprehensions = struct
v}
*)

let comprehension_expr names x =
Expression.wrap_desc ~info:[] ~loc:x.pexp_loc @@
Expression.make_jane_syntax feature names x
let comprehension_expr names x = Expression.make_jane_syntax feature names x

(** First, we define how to go from the nice AST to the OCaml AST; this is
the [expr_of_...] family of expressions, culminating in
Expand Down Expand Up @@ -143,9 +139,9 @@ module Comprehensions = struct
clauses
(comprehension_expr ["body"] body)))

let expr_of ~loc cexpr =
let expr_of ~loc ~attrs cexpr =
(* See Note [Wrapping with make_entire_jane_syntax] *)
Expression.make_entire_jane_syntax ~loc feature (fun () ->
let expr = Expression.make_entire_jane_syntax ~loc feature (fun () ->
match cexpr with
| Cexp_list_comprehension comp ->
expr_of_comprehension ~type_:["list"] comp
Expand All @@ -157,6 +153,8 @@ module Comprehensions = struct
| Immutable -> "immutable"
]
comp)
in
{ expr with pexp_attributes = expr.pexp_attributes @ attrs }

(** Then, we define how to go from the OCaml AST to the nice AST; this is
the [..._of_expr] family of expressions, culminating in
Expand Down Expand Up @@ -286,22 +284,22 @@ module Immutable_arrays = struct

let feature : Feature.t = Language_extension Immutable_arrays

let expr_of ~loc = function
let expr_of ~loc ~attrs = function
| Iaexp_immutable_array elts ->
(* See Note [Wrapping with make_entire_jane_syntax] *)
Expression.make_entire_jane_syntax ~loc feature (fun () ->
Ast_helper.Exp.array elts)
Ast_helper.Exp.array ~attrs elts)

(* Returns remaining unconsumed attributes *)
let of_expr expr = match expr.pexp_desc with
| Pexp_array elts -> Iaexp_immutable_array elts, expr.pexp_attributes
| _ -> failwith "Malformed immutable array expression"

let pat_of ~loc = function
let pat_of ~loc ~attrs = function
| Iapat_immutable_array elts ->
(* See Note [Wrapping with make_entire_jane_syntax] *)
Pattern.make_entire_jane_syntax ~loc feature (fun () ->
Ast_helper.Pat.array elts)
Ast_helper.Pat.array ~attrs elts)

(* Returns remaining unconsumed attributes *)
let of_pat pat = match pat.ppat_desc with
Expand Down Expand Up @@ -351,10 +349,10 @@ module Strengthen = struct
the [(module M)] is a [Pmty_alias]. This isn't syntax we can write, but
[(module M)] can be the inferred type for [M], so this should be fine. *)

let mty_of ~loc { mty; mod_id } =
let mty_of ~loc ~attrs { mty; mod_id } =
(* See Note [Wrapping with make_entire_jane_syntax] *)
Module_type.make_entire_jane_syntax ~loc feature (fun () ->
Ast_helper.Mty.functor_ (Named (Location.mknoloc None, mty))
Ast_helper.Mty.functor_ ~attrs (Named (Location.mknoloc None, mty))
(Ast_helper.Mty.alias mod_id))

(* Returns remaining unconsumed attributes *)
Expand Down Expand Up @@ -404,15 +402,15 @@ module Unboxed_constants = struct
| Float (x, suffix) -> Pconst_float (x, suffix)
| Integer (x, suffix) -> Pconst_integer (x, Some suffix)

let expr_of ~loc t =
let expr_of ~loc ~attrs t =
let constant = constant_of t in
Expression.make_entire_jane_syntax ~loc feature (fun () ->
Ast_helper.Exp.constant constant)
Ast_helper.Exp.constant ~attrs constant)

let pat_of ~loc t =
let pat_of ~loc ~attrs t =
let constant = constant_of t in
Pattern.make_entire_jane_syntax ~loc feature (fun () ->
Ast_helper.Pat.constant constant)
Ast_helper.Pat.constant ~attrs constant)
end

(******************************************************************************)
Expand Down Expand Up @@ -463,10 +461,10 @@ module Expression = struct

let of_ast = Expression.make_of_ast ~of_ast_internal

let expr_of ~loc = function
| Jexp_comprehension x -> Comprehensions.expr_of ~loc x
| Jexp_immutable_array x -> Immutable_arrays.expr_of ~loc x
| Jexp_unboxed_constant x -> Unboxed_constants.expr_of ~loc x
let expr_of ~loc ~attrs = function
| Jexp_comprehension x -> Comprehensions.expr_of ~loc ~attrs x
| Jexp_immutable_array x -> Immutable_arrays.expr_of ~loc ~attrs x
| Jexp_unboxed_constant x -> Unboxed_constants.expr_of ~loc ~attrs x
end

module Pattern = struct
Expand All @@ -485,9 +483,9 @@ module Pattern = struct

let of_ast = Pattern.make_of_ast ~of_ast_internal

let pat_of ~loc = function
| Jpat_immutable_array x -> Immutable_arrays.pat_of ~loc x
| Jpat_unboxed_constant x -> Unboxed_constants.pat_of ~loc x
let pat_of ~loc ~attrs = function
| Jpat_immutable_array x -> Immutable_arrays.pat_of ~loc ~attrs x
| Jpat_unboxed_constant x -> Unboxed_constants.pat_of ~loc ~attrs x
end

module Module_type = struct
Expand Down
42 changes: 17 additions & 25 deletions ocaml/parsing/jane_syntax.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@
(*********************************************)
(* Individual features *)

(** An AST desc together with its attributes, including the attributes that
signal that the AST should be interpreted as a Jane Syntax AST.
*)
module With_attributes : sig
type 'desc t = 'desc Jane_syntax_parsing.With_attributes.t = private
{ jane_syntax_attributes : Parsetree.attributes
; desc : 'desc
}
end

(** The ASTs for list and array comprehensions *)
module Comprehensions : sig
type iterator =
Expand Down Expand Up @@ -72,7 +62,8 @@ module Comprehensions : sig
(only allowed with [-extension immutable_arrays]) *)

val expr_of :
loc:Location.t -> expression -> Parsetree.expression_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes ->
expression -> Parsetree.expression
end

(** The ASTs for immutable arrays. When we merge this upstream, we'll merge
Expand All @@ -88,10 +79,11 @@ module Immutable_arrays : sig
(** [: P1; ...; Pn :] **)

val expr_of :
loc:Location.t -> expression -> Parsetree.expression_desc With_attributes.t

loc:Location.t -> attrs:Parsetree.attributes ->
expression -> Parsetree.expression
val pat_of :
loc:Location.t -> pattern -> Parsetree.pattern_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes ->
pattern -> Parsetree.pattern
end

(** The ASTs for [include functor]. When we merge this upstream, we'll merge
Expand All @@ -104,10 +96,8 @@ module Include_functor : sig
type structure_item =
| Ifstr_include_functor of Parsetree.include_declaration

val sig_item_of :
loc:Location.t -> signature_item -> Parsetree.signature_item_desc
val str_item_of :
loc:Location.t -> structure_item -> Parsetree.structure_item_desc
val sig_item_of : loc:Location.t -> signature_item -> Parsetree.signature_item
val str_item_of : loc:Location.t -> structure_item -> Parsetree.structure_item
end

(** The ASTs for module type strengthening. *)
Expand All @@ -116,9 +106,8 @@ module Strengthen : sig
{ mty : Parsetree.module_type; mod_id : Longident.t Location.loc }

val mty_of :
loc:Location.t
-> module_type
-> Parsetree.module_type_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes ->
module_type -> Parsetree.module_type
end

(** The ASTs for unboxed literals, like #4.0 *)
Expand All @@ -131,10 +120,12 @@ module Unboxed_constants : sig
type pattern = t

val expr_of :
loc:Location.t -> expression -> Parsetree.expression_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes ->
expression -> Parsetree.expression

val pat_of :
loc:Location.t -> pattern -> Parsetree.pattern_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes ->
pattern -> Parsetree.pattern
end

(******************************************)
Expand Down Expand Up @@ -244,7 +235,7 @@ module Expression : sig
and type ast := Parsetree.expression

val expr_of :
loc:Location.t -> t -> Parsetree.expression_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes -> t -> Parsetree.expression
end

(** Novel syntax in patterns *)
Expand All @@ -258,7 +249,7 @@ module Pattern : sig
and type ast := Parsetree.pattern

val pat_of :
loc:Location.t -> t -> Parsetree.pattern_desc With_attributes.t
loc:Location.t -> attrs:Parsetree.attributes -> t -> Parsetree.pattern
end

(** Novel syntax in module types *)
Expand Down Expand Up @@ -287,6 +278,7 @@ module Structure_item : sig
include AST with type t := t and type ast := Parsetree.structure_item
end

(** Novel syntax in extension constructors *)
module Extension_constructor : sig
type t = |

Expand Down
Loading