Skip to content

Commit 3d0c49c

Browse files
committed
Add ?opt props syntax (with punning)
1 parent 49e5c29 commit 3d0c49c

File tree

9 files changed

+8254
-7982
lines changed

9 files changed

+8254
-7982
lines changed

mlx/jsx_helper.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ let make_jsx_element ~raise ~loc:_ ~tag ~end_tag ~props ~children () =
6262
mkexp ~loc (Pexp_ident { loc = make_loc loc; txt })
6363
in
6464
let props =
65+
let prop_exp ~loc name =
66+
let id = mkloc (Lident name) (make_loc loc) in
67+
mkexp ~loc (Pexp_ident id)
68+
in
6569
List.map
6670
(function
67-
| loc, `Prop_punned name ->
68-
let id = mkloc (Lident name) (make_loc loc) in
69-
Labelled name, mkexp ~loc (Pexp_ident id)
70-
| _loc, `Prop (name, expr) -> Labelled name, expr)
71+
| loc, `Prop_punned name -> Labelled name, prop_exp ~loc name
72+
| loc, `Prop_opt_punned name -> Optional name, prop_exp ~loc name
73+
| _loc, `Prop (name, expr) -> Labelled name, expr
74+
| _loc, `Prop_opt (name, expr) -> Optional name, expr)
7175
props
7276
in
7377
let unit =

mlx/parser.ml

Lines changed: 3198 additions & 3078 deletions
Large diffs are not rendered by default.

mlx/parser.mly

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,9 @@ jsx_element:
25232523
;
25242524
jsx_prop:
25252525
name=LIDENT { $loc(name), `Prop_punned name }
2526+
| QUESTION name=LIDENT { $loc(name), `Prop_opt_punned name }
25262527
| name=LIDENT EQUAL expr=simple_expr { $loc(name), `Prop (name, expr) }
2528+
| QUESTION name=LIDENT EQUAL expr=simple_expr { $loc(name), `Prop_opt (name, expr) }
25272529
;
25282530
labeled_simple_expr:
25292531
simple_expr %prec below_HASH
1.22 KB
Binary file not shown.

ocamlmerlin_mlx/preprocess/parser_raw.ml

Lines changed: 2887 additions & 2761 deletions
Large diffs are not rendered by default.

ocamlmerlin_mlx/preprocess/parser_raw.mli

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ module MenhirInterpreter : sig
390390
| N_reversed_llist_simple_expr_ : (Parsetree.expression list) nonterminal
391391
| N_reversed_llist_preceded_CONSTRAINT_constrain__ : ((Parsetree.core_type * Parsetree.core_type * Warnings.loc) list) nonterminal
392392
| N_reversed_llist_jsx_prop_ : (((Lexing.position * Lexing.position) *
393-
[ `Prop of string * Parsetree.expression | `Prop_punned of string ])
393+
[ `Prop of string * Parsetree.expression
394+
| `Prop_opt of string * Parsetree.expression
395+
| `Prop_opt_punned of string
396+
| `Prop_punned of string ])
394397
list) nonterminal
395398
| N_reversed_bar_llist_extension_constructor_declaration_ : (Parsetree.extension_constructor list) nonterminal
396399
| N_reversed_bar_llist_extension_constructor_ : (Parsetree.extension_constructor list) nonterminal
@@ -500,7 +503,10 @@ module MenhirInterpreter : sig
500503
| N_label_declaration_semi : (Parsetree.label_declaration) nonterminal
501504
| N_label_declaration : (Parsetree.label_declaration) nonterminal
502505
| N_jsx_prop : ((Lexing.position * Lexing.position) *
503-
[ `Prop of string * Parsetree.expression | `Prop_punned of string ]) nonterminal
506+
[ `Prop of string * Parsetree.expression
507+
| `Prop_opt of string * Parsetree.expression
508+
| `Prop_opt_punned of string
509+
| `Prop_punned of string ]) nonterminal
504510
| N_jsx_longident_JSX_UIDENT_E_JSX_LIDENT_E_ : ([> `Module | `Value ] * (Lexing.position * Lexing.position) * Longident.t) nonterminal
505511
| N_jsx_longident_JSX_UIDENT_JSX_LIDENT_ : ([ `Module | `Value ] * (Lexing.position * Lexing.position) * Longident.t) nonterminal
506512
| N_jsx_element : (Parsetree.expression_desc) nonterminal

ocamlmerlin_mlx/preprocess/parser_raw.mly

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,9 @@ jsx_element:
26972697
;
26982698
jsx_prop:
26992699
name=LIDENT { $loc(name), `Prop_punned name }
2700+
| QUESTION name=LIDENT { $loc(name), `Prop_opt_punned name }
27002701
| name=LIDENT EQUAL expr=simple_expr { $loc(name), `Prop (name, expr) }
2702+
| QUESTION name=LIDENT EQUAL expr=simple_expr { $loc(name), `Prop_opt (name, expr) }
27012703
;
27022704
labeled_simple_expr:
27032705
simple_expr %prec below_HASH

ocamlmerlin_mlx/preprocess/parser_recover.ml

Lines changed: 2143 additions & 2137 deletions
Large diffs are not rendered by default.

test/mlx.t

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
MERLIN
1818
let _ = div () ~children:[] ~attr ~with_value:1 [@JSX]
1919

20+
$ echo 'let _ = <div ?opt ?opt_value=some />' | ./mlx
21+
BATCH
22+
let _ = div () ~children:[] ?opt ?opt_value:some [@JSX]
23+
MERLIN
24+
let _ = div () ~children:[] ?opt ?opt_value:some [@JSX]
25+
2026
$ echo 'let _ = <Hello attr with_value=1 />' | ./mlx
2127
BATCH
2228
let _ = Hello.createElement () ~children:[] ~attr ~with_value:1 [@JSX]

0 commit comments

Comments
 (0)