Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 8a0d5db

Browse files
committed
Explore surface syntax for optional attributes.
1 parent 021922b commit 8a0d5db

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/res_core.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
161161
let uncurryAttr = (Location.mknoloc "bs", Parsetree.PStr [])
162162
let ternaryAttr = (Location.mknoloc "ns.ternary", Parsetree.PStr [])
163163
let ifLetAttr = (Location.mknoloc "ns.iflet", Parsetree.PStr [])
164+
let optionalAttr = (Location.mknoloc "optional", Parsetree.PStr [])
165+
164166
let suppressFragileMatchWarningAttr =
165167
( Location.mknoloc "warning",
166168
Parsetree.PStr
@@ -2923,6 +2925,14 @@ and parseRecordExprRowWithStringKey p =
29232925
| _ -> Some (field, Ast_helper.Exp.ident ~loc:field.loc field))
29242926
| _ -> None
29252927

2928+
and parseOptionalExpression p =
2929+
match p.Parser.token with
2930+
| Token.Question ->
2931+
Parser.next p;
2932+
let e = parseExpr p in
2933+
{e with pexp_attributes = optionalAttr :: e.pexp_attributes}
2934+
| _ -> parseExpr p
2935+
29262936
and parseRecordExprRow p =
29272937
let attrs = parseAttributes p in
29282938
let () =
@@ -2939,7 +2949,7 @@ and parseRecordExprRow p =
29392949
match p.Parser.token with
29402950
| Colon ->
29412951
Parser.next p;
2942-
let fieldExpr = parseExpr p in
2952+
let fieldExpr = parseOptionalExpression p in
29432953
Some (field, fieldExpr)
29442954
| _ ->
29452955
let value = Ast_helper.Exp.ident ~loc:field.loc ~attrs field in

tests/parsing/grammar/expressions/record.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let r = {...expr, pexp_attributes: [],} // trailing comma
2424
let r = {...make() : myRecord, foo: bar}
2525
let r = {...(make() : myRecord), foo: bar} // parens optional
2626

27-
let r = {x: @optional None, y: @optional None, z: @optional (None:tt)}
27+
let r = {x: @optional None, y: ?None, z: @optional (None:tt)}
2828

2929
let z = name => { name : @optional name, x: 3}
3030

0 commit comments

Comments
 (0)