File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -641,6 +641,13 @@ module SexpAst = struct
641641 longident longident_loc.Asttypes. txt;
642642 expression expr2;
643643 ]
644+ | Pexp_index (e1 , e2 , e3 ) ->
645+ Sexp. list
646+ ([Sexp. atom " Pexp_index" ; expression e1; expression e2]
647+ @
648+ match e3 with
649+ | None -> []
650+ | Some e -> [expression e])
644651 | Pexp_array exprs ->
645652 Sexp. list
646653 [Sexp. atom " Pexp_array" ; Sexp. list (map_empty ~f: expression exprs)]
Original file line number Diff line number Diff line change @@ -476,6 +476,7 @@ let rec is_block_expr expr =
476476 | Pexp_constraint (expr , _ ) when is_block_expr expr -> true
477477 | Pexp_field (expr , _ ) when is_block_expr expr -> true
478478 | Pexp_setfield (expr , _ , _ ) when is_block_expr expr -> true
479+ | Pexp_index (expr , _ , _ ) when is_block_expr expr -> true
479480 | _ -> false
480481
481482let is_if_then_else_expr expr =
@@ -1313,6 +1314,12 @@ and walk_expression expr t comments =
13131314 attach t.leading expr2.pexp_loc leading;
13141315 walk_expression expr2 t inside;
13151316 attach t.trailing expr2.pexp_loc trailing
1317+ | Pexp_index (container , index , value_opt ) -> (
1318+ walk_expression container t comments;
1319+ walk_expression index t comments;
1320+ match value_opt with
1321+ | None -> ()
1322+ | Some value -> walk_expression value t comments)
13161323 | Pexp_ifthenelse (if_expr , then_expr , else_expr ) -> (
13171324 let leading, rest = partition_leading_trailing comments expr.pexp_loc in
13181325 attach t.leading expr.pexp_loc leading;
Original file line number Diff line number Diff line change @@ -3374,6 +3374,31 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33743374 | Pexp_setfield (expr1 , longident_loc , expr2 ) ->
33753375 print_set_field_expr ~state e.pexp_attributes expr1 longident_loc expr2
33763376 e.pexp_loc cmt_tbl
3377+ | Pexp_index (container , index , value_opt ) -> (
3378+ let container_doc =
3379+ let doc = print_expression_with_comments ~state container cmt_tbl in
3380+ match Parens. field_expr container with
3381+ | Parens. Parenthesized -> add_parens doc
3382+ | Braced braces -> print_braces doc container braces
3383+ | Nothing -> doc
3384+ in
3385+ let index_doc = print_expression_with_comments ~state index cmt_tbl in
3386+ match value_opt with
3387+ | None ->
3388+ (* Read: container[index] *)
3389+ Doc. concat [container_doc; Doc. lbracket; index_doc; Doc. rbracket]
3390+ | Some value ->
3391+ (* Write: container[index] = value *)
3392+ let value_doc = print_expression_with_comments ~state value cmt_tbl in
3393+ Doc. concat
3394+ [
3395+ container_doc;
3396+ Doc. lbracket;
3397+ index_doc;
3398+ Doc. rbracket;
3399+ Doc. text " = " ;
3400+ value_doc;
3401+ ])
33773402 | Pexp_ifthenelse (_ifExpr, _thenExpr, _elseExpr)
33783403 when ParsetreeViewer. is_ternary_expr e ->
33793404 let parts, alternate = ParsetreeViewer. collect_ternary_parts e in
You can’t perform that action at this time.
0 commit comments