-
Notifications
You must be signed in to change notification settings - Fork 469
Add dict literal syntax #6774
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
Merged
Merged
Add dict literal syntax #6774
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b034ea
Handle dict literal syntax
bloodyowl 3687e70
Update test
bloodyowl 7479265
Move isTupleArray to res_parsetree_viewer
bloodyowl fb9b020
Map locations correctly
bloodyowl 086b062
Fix location in parsing comments printing
bloodyowl 12b758f
Make Dict limit to List grammar-wise
bloodyowl 81075b0
Add changelog entry
bloodyowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,6 +222,7 @@ let get_closing_token = function | |
| Lbrace -> Rbrace | ||
| Lbracket -> Rbracket | ||
| List -> Rbrace | ||
| Dict -> Rbrace | ||
| LessThan -> GreaterThan | ||
| _ -> assert false | ||
|
||
|
@@ -233,7 +234,7 @@ let rec go_to_closing closing_token state = | |
| GreaterThan, GreaterThan -> | ||
Parser.next state; | ||
() | ||
| ((Token.Lbracket | Lparen | Lbrace | List | LessThan) as t), _ -> | ||
| ((Token.Lbracket | Lparen | Lbrace | List | Dict | LessThan) as t), _ -> | ||
Parser.next state; | ||
go_to_closing (get_closing_token t) state; | ||
go_to_closing closing_token state | ||
|
@@ -1896,6 +1897,9 @@ and parse_atomic_expr p = | |
| List -> | ||
Parser.next p; | ||
parse_list_expr ~start_pos p | ||
| Dict -> | ||
Parser.next p; | ||
parse_dict_expr ~start_pos p | ||
| Module -> | ||
Parser.next p; | ||
parse_first_class_module_expr ~start_pos p | ||
|
@@ -3122,6 +3126,20 @@ and parse_record_expr_row p = | |
| _ -> None) | ||
| _ -> None | ||
|
||
and parse_dict_expr_row p = | ||
match p.Parser.token with | ||
| String s -> ( | ||
let loc = mk_loc p.start_pos p.end_pos in | ||
Parser.next p; | ||
let field = Location.mkloc (Longident.Lident s) loc in | ||
match p.Parser.token with | ||
| Colon -> | ||
Parser.next p; | ||
let fieldExpr = parse_expr p in | ||
Some (field, fieldExpr) | ||
| _ -> Some (field, Ast_helper.Exp.ident ~loc:field.loc field)) | ||
| _ -> None | ||
|
||
and parse_record_expr_with_string_keys ~start_pos first_row p = | ||
let rows = | ||
first_row | ||
|
@@ -3903,6 +3921,36 @@ and parse_list_expr ~start_pos p = | |
loc)) | ||
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc list_exprs)] | ||
|
||
and parse_dict_expr ~start_pos p = | ||
let rows = | ||
parse_comma_delimited_region ~grammar:Grammar.DictRows ~closing:Rbrace | ||
~f:parse_dict_expr_row p | ||
in | ||
let loc = mk_loc start_pos p.end_pos in | ||
let to_key_value_pair | ||
(record_item : Longident.t Location.loc * Parsetree.expression) = | ||
match record_item with | ||
| ( {Location.txt = Longident.Lident key; loc = keyLoc}, | ||
({pexp_loc = value_loc} as value_expr) ) -> | ||
Some | ||
(Ast_helper.Exp.tuple | ||
~loc:(mk_loc keyLoc.loc_start value_loc.loc_end) | ||
[ | ||
Ast_helper.Exp.constant ~loc:keyLoc (Pconst_string (key, None)); | ||
value_expr; | ||
]) | ||
| _ -> None | ||
in | ||
let key_value_pairs = List.filter_map to_key_value_pair rows in | ||
Parser.expect Rbrace p; | ||
Ast_helper.Exp.apply ~loc | ||
(Ast_helper.Exp.ident ~loc | ||
(Location.mkloc | ||
(Longident.Ldot | ||
(Longident.Ldot (Longident.Lident "Js", "Dict"), "fromArray")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but this can be done independently later. |
||
loc)) | ||
[(Asttypes.Nolabel, Ast_helper.Exp.array ~loc key_value_pairs)] | ||
|
||
and parse_array_exp p = | ||
let start_pos = p.Parser.start_pos in | ||
Parser.expect Lbracket p; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// empty dict | ||
let x = dict{} | ||
|
||
// one value | ||
let x = dict{"foo": "bar"} | ||
|
||
// two values | ||
let x = dict{"foo": "bar", "bar": "baz"} | ||
|
||
let baz = "foo" | ||
let x = dict{"foo": "bar", "bar": "baz", "baz": baz} |
7 changes: 7 additions & 0 deletions
7
jscomp/syntax/tests/parsing/grammar/expressions/expected/dict.res.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
let x = Js.Dict.fromArray [||] | ||
let x = Js.Dict.fromArray [|("foo", {js|bar|js})|] | ||
let x = Js.Dict.fromArray [|("foo", {js|bar|js});("bar", {js|baz|js})|] | ||
let baz = {js|foo|js} | ||
let x = | ||
Js.Dict.fromArray | ||
[|("foo", {js|bar|js});("bar", {js|baz|js});("baz", baz)|] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.