Skip to content
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

Safe call operators ?. #2142

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Handle JS property access ##
  • Loading branch information
Gregoirevda committed Aug 9, 2018
commit 7358ad602835c577150a1af8f6388ada764b8cfb
27 changes: 20 additions & 7 deletions src/reason-parser/reason_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -2990,12 +2990,8 @@ parenthesized_expr:
let pat_record_name = match $1.pexp_desc with Pexp_ident({txt = Lident(str); loc;}) -> str | _ -> "x" in
let exp_record_name = match $1.pexp_desc with Pexp_ident({txt = Lident(str); loc;}) -> Lident(str) | _ -> Lident("x") in
let exp_prop_name = match $3.pexp_desc with Pexp_ident({txt = Lident(str); loc;}) -> Lident(str) | _ -> Lident("__error__") in
let some_pat =
Pat.mk ~loc:loc_exp (Ppat_construct({ txt = Lident("Some"); loc = loc_exp}, Some(mkpat (Ppat_var(mkloc pat_record_name loc_exp))))) in
let none_pat =
Pat.mk ~loc:loc_exp (Ppat_construct({ txt = Lident("None"); loc = loc_exp;}, None)) in
let some_case =
Exp.case some_pat (mkexp (Pexp_construct ({txt = Lident("Some"); loc = loc_exp}, Some(
let access_property = match $2 with
| "?." ->
mkexp (Pexp_field (
mkexp (Pexp_ident({
txt = exp_record_name;
Expand All @@ -3004,7 +3000,24 @@ parenthesized_expr:
txt = exp_prop_name;
loc = loc_exp;
}))
))));
| "?#" ->
mkexp (Pexp_apply(
mkexp (Pexp_ident({
txt = Lident("##");
loc = loc_exp;
})), [
Nolabel, mkexp (Pexp_ident({ txt = exp_record_name; loc = loc_exp}));
Nolabel, mkexp (Pexp_ident({ txt = exp_prop_name; loc = loc_exp }));
]
))
| _ -> syntax_error ()
in
let some_pat =
Pat.mk ~loc:loc_exp (Ppat_construct({ txt = Lident("Some"); loc = loc_exp}, Some(mkpat (Ppat_var(mkloc pat_record_name loc_exp))))) in
let none_pat =
Pat.mk ~loc:loc_exp (Ppat_construct({ txt = Lident("None"); loc = loc_exp;}, None)) in
let some_case =
Exp.case some_pat (mkexp (Pexp_construct ({txt = Lident("Some"); loc = loc_exp}, Some(access_property))));
in
let none_case =
Exp.case none_pat (mkexp (Pexp_construct({ txt = Lident("None"); loc = loc_exp;}, None)));
Expand Down