Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion handrolled_parser/parser.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,24 @@ fn State::parse_foreach_loop(
})
self.expect_token(TK_IN)
let expr = self.parse_expr()
let acc_binders = if self.peek_token() is SEMI(_) {
self.skip()
let (acc_binders, _) = self.series_with_follow(
delim=TK_COMMA,
follow_set=[TK_LBRACE],
fn(state) {
let binder = state.parse_binder(
context="accumulator variable name of `for`",
)
state.expect_token(TK_EQUAL)
let value = state.parse_expr()
(binder, value)
},
)
@list.from_array(acc_binders)
} else {
@list.new()
}
let body = self.parse_block_expr()
let else_block = if self.peek_token() is NOBREAK {
self.skip()
Expand All @@ -2915,7 +2933,7 @@ fn State::parse_foreach_loop(
None
}
let loc = self.loc_start_with(spos)
ForEach(binders~, expr~, body~, else_block~, loc~, label~)
ForEach(binders~, expr~, acc_binders~, body~, else_block~, loc~, label~)
}

///|
Expand Down
1 change: 1 addition & 0 deletions syntax/ast.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ pub(all) enum Expr {
ForEach(
binders~ : @list.List[Binder?],
expr~ : Expr,
acc_binders~ : @list.List[(Binder, Expr)],
body~ : Expr,
else_block~ : Expr?,
label~ : Label?,
Expand Down
14 changes: 13 additions & 1 deletion syntax/ast_json_repr.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,26 @@ pub fn Expr::json_repr(self : Expr) -> Json {
"where_clause": option_json_repr(where_clause, WhereClause::json_repr),
"label": option_json_repr(label, Label::json_repr),
})
ForEach(binders~, expr~, body~, else_block~, label~, loc~) =>
ForEach(binders~, expr~, acc_binders~, body~, else_block~, label~, loc~) =>
tagged_node("Expr::ForEach", loc, {
"binders": list_json_repr("Expr::ForEach::BinderList", loc, binders, fn(
b,
) {
option_json_repr(b, Binder::json_repr)
}),
"expr": expr.json_repr(),
"acc_binders": list_json_repr(
"Expr::ForEach::AccBinderList",
loc,
acc_binders,
fn(b) {
let (binder, expr) = b
tagged_node("ForEach::AccBinder", loc, {
"binder": binder.json_repr(),
"expr": expr.json_repr(),
})
},
),
"body": body.json_repr(),
"else_block": option_json_repr(else_block, Expr::json_repr),
"label": option_json_repr(label, Label::json_repr),
Expand Down
11 changes: 10 additions & 1 deletion syntax/iter_visitor.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ pub(open) trait IterVisitor {
Self,
binders~ : @list.List[Binder?],
expr~ : Expr,
acc_binders~ : @list.List[(Binder, Expr)],
body~ : Expr,
else_block~ : Expr?,
label~ : Label?,
Expand Down Expand Up @@ -1372,6 +1373,7 @@ impl IterVisitor with visit_Expr_ForEach(
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand All @@ -1381,6 +1383,7 @@ impl IterVisitor with visit_Expr_ForEach(
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand Down Expand Up @@ -4575,13 +4578,18 @@ pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_Expr_For
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
loc~,
) {
ignore(loc)
binders.each(binder => binder.each(x => env.0.visit_Binder(x)))
acc_binders.each(p => {
env.0.visit_Binder(p.0)
env.0.visit_Expr(p.1)
})
env.0.visit_Expr(expr)
env.0.visit_Expr(body)
else_block.each(else_block => env.0.visit_Expr(else_block))
Expand Down Expand Up @@ -4773,10 +4781,11 @@ pub impl[T : IterVisitor] IterVisitor for IterVisitorBase[T] with visit_Expr(
loc~,
where_clause~,
)
ForEach(binders~, expr~, body~, else_block~, label~, loc~) =>
ForEach(binders~, expr~, acc_binders~, body~, else_block~, label~, loc~) =>
env.0.visit_Expr_ForEach(
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand Down
9 changes: 7 additions & 2 deletions syntax/map_visitor.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ pub(open) trait MapVisitor {
Self,
binders~ : @list.List[Binder?],
expr~ : Expr,
acc_binders~ : @list.List[(Binder, Expr)],
body~ : Expr,
else_block~ : Expr?,
label~ : Label?,
Expand Down Expand Up @@ -1372,6 +1373,7 @@ impl MapVisitor with visit_Expr_ForEach(
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand All @@ -1381,6 +1383,7 @@ impl MapVisitor with visit_Expr_ForEach(
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand Down Expand Up @@ -4720,6 +4723,7 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Expr_ForEac
env,
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand All @@ -4730,7 +4734,7 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Expr_ForEac
let body = env.0.visit_Expr(body)
let else_block = else_block.map(else_block => env.0.visit_Expr(else_block))
let label = label.map(x => env.0.visit_Label(x))
Expr::ForEach(binders~, expr~, body~, else_block~, label~, loc~)
Expr::ForEach(binders~, expr~, acc_binders~, body~, else_block~, label~, loc~)
}

///|
Expand Down Expand Up @@ -4923,10 +4927,11 @@ pub impl[T : MapVisitor] MapVisitor for MapVisitorBase[T] with visit_Expr(
loc~,
where_clause~,
)
ForEach(binders~, expr~, body~, else_block~, label~, loc~) =>
ForEach(binders~, expr~, acc_binders~, body~, else_block~, label~, loc~) =>
env.0.visit_Expr_ForEach(
binders~,
expr~,
acc_binders~,
body~,
else_block~,
label~,
Expand Down
6 changes: 3 additions & 3 deletions syntax/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub(all) enum Expr {
Continue(args~ : @list.List[Expr], label~ : Label?, loc~ : @basic.Location)
Loop(arg~ : Expr, body~ : @list.List[Case], label~ : Label?, loop_loc~ : @basic.Location, loc~ : @basic.Location)
For(binders~ : @list.List[(Binder, Expr)], condition~ : Expr?, continue_block~ : @list.List[(Binder, Expr)], body~ : Expr, for_else~ : Expr?, where_clause~ : WhereClause?, label~ : Label?, loc~ : @basic.Location)
ForEach(binders~ : @list.List[Binder?], expr~ : Expr, body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location)
ForEach(binders~ : @list.List[Binder?], expr~ : Expr, acc_binders~ : @list.List[(Binder, Expr)], body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location)
Try(body~ : Expr, catch_~ : @list.List[Case], catch_all~ : Bool, try_else~ : @list.List[Case]?, has_try~ : Bool, try_loc~ : @basic.Location, catch_loc~ : @basic.Location, else_loc~ : @basic.Location, loc~ : @basic.Location)
TryOperator(body~ : Expr, kind~ : TryOperatorKind, try_loc~ : @basic.Location, loc~ : @basic.Location)
Map(elems~ : @list.List[MapExprElem], loc~ : @basic.Location)
Expand Down Expand Up @@ -847,7 +847,7 @@ pub(open) trait IterVisitor {
visit_Expr_Continue(Self, args~ : @list.List[Expr], label~ : Label?, loc~ : @basic.Location) -> Unit = _
visit_Expr_Loop(Self, arg~ : Expr, body~ : @list.List[Case], label~ : Label?, loop_loc~ : @basic.Location, loc~ : @basic.Location) -> Unit = _
visit_Expr_For(Self, binders~ : @list.List[(Binder, Expr)], condition~ : Expr?, continue_block~ : @list.List[(Binder, Expr)], body~ : Expr, for_else~ : Expr?, label~ : Label?, loc~ : @basic.Location, where_clause~ : WhereClause?) -> Unit = _
visit_Expr_ForEach(Self, binders~ : @list.List[Binder?], expr~ : Expr, body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location) -> Unit = _
visit_Expr_ForEach(Self, binders~ : @list.List[Binder?], expr~ : Expr, acc_binders~ : @list.List[(Binder, Expr)], body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location) -> Unit = _
visit_Expr_Try(Self, body~ : Expr, catch_~ : @list.List[Case], catch_all~ : Bool, try_else~ : @list.List[Case]?, has_try~ : Bool, try_loc~ : @basic.Location, catch_loc~ : @basic.Location, else_loc~ : @basic.Location, loc~ : @basic.Location) -> Unit = _
visit_Expr_TryOperator(Self, body~ : Expr, kind~ : TryOperatorKind, try_loc~ : @basic.Location, loc~ : @basic.Location) -> Unit = _
visit_Expr_Map(Self, elems~ : @list.List[MapExprElem], loc~ : @basic.Location) -> Unit = _
Expand Down Expand Up @@ -1060,7 +1060,7 @@ pub(open) trait MapVisitor {
visit_Expr_Continue(Self, args~ : @list.List[Expr], label~ : Label?, loc~ : @basic.Location) -> Expr = _
visit_Expr_Loop(Self, arg~ : Expr, body~ : @list.List[Case], label~ : Label?, loop_loc~ : @basic.Location, loc~ : @basic.Location) -> Expr = _
visit_Expr_For(Self, binders~ : @list.List[(Binder, Expr)], condition~ : Expr?, continue_block~ : @list.List[(Binder, Expr)], body~ : Expr, for_else~ : Expr?, label~ : Label?, loc~ : @basic.Location, where_clause~ : WhereClause?) -> Expr = _
visit_Expr_ForEach(Self, binders~ : @list.List[Binder?], expr~ : Expr, body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location) -> Expr = _
visit_Expr_ForEach(Self, binders~ : @list.List[Binder?], expr~ : Expr, acc_binders~ : @list.List[(Binder, Expr)], body~ : Expr, else_block~ : Expr?, label~ : Label?, loc~ : @basic.Location) -> Expr = _
visit_Expr_Try(Self, body~ : Expr, catch_~ : @list.List[Case], catch_all~ : Bool, try_else~ : @list.List[Case]?, has_try~ : Bool, try_loc~ : @basic.Location, catch_loc~ : @basic.Location, else_loc~ : @basic.Location, loc~ : @basic.Location) -> Expr = _
visit_Expr_TryOperator(Self, body~ : Expr, kind~ : TryOperatorKind, try_loc~ : @basic.Location, loc~ : @basic.Location) -> Expr = _
visit_Expr_Map(Self, elems~ : @list.List[MapExprElem], loc~ : @basic.Location) -> Expr = _
Expand Down
10 changes: 9 additions & 1 deletion syntax/utils.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,15 @@ pub fn loc_of_expression(expr : Expr) -> Location {
loc~,
where_clause=_
) => loc
ForEach(binders=_, expr=_, body=_, else_block=_, label=_, loc~) => loc
ForEach(
binders=_,
expr=_,
acc_binders=_,
body=_,
else_block=_,
label=_,
loc~
) => loc
Try(
body=_,
catch_=_,
Expand Down
10 changes: 10 additions & 0 deletions test/manual_test/__snapshot__/nobreak.json
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1011,6 +1016,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down
30 changes: 30 additions & 0 deletions test/sync_test/__snapshot__/parse_test_iter_range.json
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1305,6 +1310,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1437,6 +1447,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1581,6 +1596,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1725,6 +1745,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down Expand Up @@ -1871,6 +1896,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down
5 changes: 5 additions & 0 deletions test/sync_test/__snapshot__/pipeline_test_011.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down
5 changes: 5 additions & 0 deletions test/sync_test/__snapshot__/pipeline_test_any_to_string.json
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Apply",
"loc": null,
Expand Down
10 changes: 10 additions & 0 deletions test/sync_test/__snapshot__/pipeline_test_array_valtype_ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::DotApply",
"loc": null,
Expand Down Expand Up @@ -1996,6 +2001,11 @@
}
}
},
"acc_binders": {
"kind": "Expr::ForEach::AccBinderList",
"loc": null,
"children": []
},
"body": {
"kind": "Expr::Let",
"loc": null,
Expand Down
Loading