Skip to content

Commit 01132d3

Browse files
tsnobipzth
authored andcommitted
fix pattern matching of dict
1 parent d4df2f8 commit 01132d3

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

jscomp/syntax/src/res_grammar.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let is_signature_item_start = function
131131

132132
let is_atomic_pattern_start = function
133133
| Token.Int _ | String _ | Codepoint _ | Backtick | Lparen | Lbracket | Lbrace
134-
| Underscore | Lident _ | Uident _ | List | Exception | Percent ->
134+
| Underscore | Lident _ | Uident _ | List | Dict | Exception | Percent ->
135135
true
136136
| _ -> false
137137

@@ -170,8 +170,8 @@ let is_structure_item_start = function
170170

171171
let is_pattern_start = function
172172
| Token.Int _ | Float _ | String _ | Codepoint _ | Backtick | True | False
173-
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Underscore | Lident _
174-
| Uident _ | Hash | Exception | Percent | Module | At ->
173+
| Minus | Plus | Lparen | Lbracket | Lbrace | List | Dict | Underscore
174+
| Lident _ | Uident _ | Hash | Exception | Percent | Module | At ->
175175
true
176176
| _ -> false
177177

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,41 @@
1-
2-
Syntax error!
3-
tests/parsing/grammar/pattern/dict.res:28:12-16
4-
5-
26 │ let decodeUser = (json: json): option<user> => {
6-
27 │ switch json {
7-
28 │ | Object(dict{
8-
29 │ "name": String(name),
9-
30 │ "age": ageJson
10-
11-
Did you forget a `)` here?
12-
13-
14-
Syntax error!
15-
tests/parsing/grammar/pattern/dict.res:31:6
16-
17-
29 ┆ "name": String(name),
18-
30 ┆ "age": ageJson
19-
31 ┆ }) =>
20-
32 ┆ Some({
21-
33 ┆ name,
22-
23-
I'm not sure what to parse here when looking at ")".
24-
25-
26-
Syntax error!
27-
tests/parsing/grammar/pattern/dict.res:41:3
28-
29-
39 ┆ },
30-
40 ┆ })
31-
41 ┆ | _ =>
32-
42 ┆ Js.log("Not an object.")
33-
43 ┆ None
34-
35-
I'm not sure what to parse here when looking at "|".
36-
1+
let someDict = Primitive_dict.make [|("one", {js|one|js})|]
2+
let (({ one = ((one)[@res.optional ]);_})[@res.dictPattern ]) = someDict
3+
let foo =
4+
((Function$
5+
(fun () ->
6+
match someDict with
7+
| (({ one = {js|one|js};_})[@res.dictPattern ]) ->
8+
Js.log {js|one|js}
9+
| _ -> Js.log {js|not one|js}))
10+
[@res.arity 1])
11+
type json =
12+
| Boolean of bool
13+
| Null [@as null]
14+
| String of string
15+
| Number of float
16+
| Object of json dict
17+
| Array of t array [@@unboxed ]
18+
type nonrec user = {
19+
name: string ;
20+
age: float [@res.optional ]}
21+
let decodeUser =
22+
((Function$
23+
(fun (json : json) ->
24+
(((match json with
25+
| Object
26+
(({ name = String name; age = ageJson;_})[@res.dictPattern ])
27+
->
28+
Some
29+
{
30+
name;
31+
age =
32+
(((match ageJson with
33+
| Number age -> Some age
34+
| _ -> None))[@res.optional ])
35+
}
36+
| _ -> (Js.log {js|Not an object.|js}; None))
37+
[@res.braces ]) : user option)))
38+
[@res.arity 1])
39+
;;Js.log
40+
(decodeUser
41+
(jsonParse (({js|{"name": "John", "age": 30}|js})[@res.template ])))

0 commit comments

Comments
 (0)