Skip to content

Commit

Permalink
flambda-backend: Parsing new mode syntax (ocaml-flambda#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
riaqn authored Mar 8, 2024
1 parent 536d7fe commit db11d61
Show file tree
Hide file tree
Showing 23 changed files with 41,369 additions and 15,975 deletions.
56,131 changes: 40,358 additions & 15,773 deletions boot/menhir/parser.ml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions boot/menhir/parser.mli
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ type token =
| BAR
| BANG
| BACKQUOTE
| ATAT
| AT
| ASSERT
| AS
| ANDOP of (string)
Expand Down
7 changes: 6 additions & 1 deletion parsing/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,11 @@ let default_iterator =
value_description =
(fun this {pval_name; pval_type; pval_prim = _; pval_loc;
pval_attributes} ->
let modes, ptyp_attributes =
Jane_syntax.Mode_expr.maybe_of_attrs pval_type.ptyp_attributes
in
Option.iter (this.modes this) modes;
let pval_type = {pval_type with ptyp_attributes} in
iter_loc this pval_name;
this.typ this pval_type;
this.location this pval_loc;
Expand Down Expand Up @@ -962,7 +967,7 @@ let default_iterator =
this.location this a.attr_loc
);
attributes = (fun this l -> List.iter (this.attribute this) l);
(* [ast_iterator] should not know about the structure of mode expressions *)
(* CR zqian: should go into the modes and at least iterate the locations. *)
modes = (fun _this _m -> ());
payload =
(fun this -> function
Expand Down
23 changes: 22 additions & 1 deletion parsing/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type mapper = {
Jane_syntax.Structure_item.t -> Jane_syntax.Structure_item.t;
typ_jane_syntax: mapper -> Jane_syntax.Core_type.t -> Jane_syntax.Core_type.t;

modes : mapper -> Jane_syntax.Mode_expr.t -> Jane_syntax.Mode_expr.t;
}

let map_fst f (x, y) = (f x, y)
Expand Down Expand Up @@ -925,9 +926,26 @@ let default_mapper =
value_description =
(fun this {pval_name; pval_type; pval_prim; pval_loc;
pval_attributes} ->
let modes, ptyp_attributes =
Jane_syntax.Mode_expr.maybe_of_attrs pval_type.ptyp_attributes
in
let pval_type = { pval_type with ptyp_attributes } in
let pval_type = this.typ this pval_type in
let attr =
match modes with
| None -> None
| Some modes ->
let modes = this.modes this modes in
Jane_syntax.Mode_expr.attr_of modes
in
let pval_type =
match attr with
| None -> pval_type
| Some attr -> {pval_type with ptyp_attributes = attr :: pval_type.ptyp_attributes}
in
Val.mk
(map_loc this pval_name)
(this.typ this pval_type)
pval_type
~attrs:(this.attributes this pval_attributes)
~loc:(this.location this pval_loc)
~prim:pval_prim
Expand Down Expand Up @@ -1096,6 +1114,9 @@ let default_mapper =
signature_item_jane_syntax = MT.map_signature_item_jst;
structure_item_jane_syntax = M.map_structure_item_jst;
typ_jane_syntax = T.map_jst;

(* CR zqian: should go into the modes and at least map the locations. *)
modes = (fun _this m -> m);
}

let extension_of_error {kind; main; sub} =
Expand Down
1 change: 1 addition & 0 deletions parsing/ast_mapper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type mapper = {
Jane_syntax.Structure_item.t -> Jane_syntax.Structure_item.t;
typ_jane_syntax: mapper -> Jane_syntax.Core_type.t -> Jane_syntax.Core_type.t;

modes : mapper -> Jane_syntax.Mode_expr.t -> Jane_syntax.Mode_expr.t;
}
(** A mapper record implements one "method" per syntactic category,
using an open recursion style: each method takes as its first
Expand Down
4 changes: 4 additions & 0 deletions parsing/jane_syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ module Mode_expr = struct
let const' = (const : Const.t :> _ Location.loc) in
Location.mkloc [const] const'.loc

let concat mode0 mode1 =
let txt = mode0.txt @ mode1.txt in
Location.mknoloc txt

let feature : Feature.t = Language_extension Mode

let attribute_or_extension_name =
Expand Down
5 changes: 5 additions & 0 deletions parsing/jane_syntax.mli
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ module Mode_expr : sig
(** The mode expression containing a single mode constant. *)
val singleton : Const.t -> t

(** Merging two mode expressions. This will be hard to define as mode
expressions gets complex. Currently it's for merging legacy and new syntax
*)
val concat : t -> t -> t

(** Extract the mode attribute (if any) from a list of attributes; also
returns the rest of the attributes; Raises if multiple relevant attributes
are found *)
Expand Down
2 changes: 2 additions & 0 deletions parsing/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ rule token = parse
{ PREFIXOP op }
| ['=' '<' '>' '|' '&' '$'] symbolchar * as op
{ INFIXOP0 op }
| "@" { AT }
| "@@" { ATAT }
| ['@' '^'] symbolchar * as op
{ INFIXOP1 op }
| ['+' '-'] symbolchar * as op
Expand Down
Loading

0 comments on commit db11d61

Please sign in to comment.