Skip to content

Commit a4b33a2

Browse files
committed
Add parsing and cppfront emit lambdas in alternatives
1 parent 148d1ea commit a4b33a2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

source/cppfront.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,10 +1240,16 @@ class cppfront
12401240
auto id = std::string{};
12411241
printer.emit_to_string(&id);
12421242
assert(alt->id_expression || alt->literal);
1243-
if (alt->id_expression)
1243+
if (alt->id_expression) {
12441244
emit(*alt->id_expression);
1245-
else if (alt->literal)
1245+
if (alt->expr) {
1246+
push_need_expression_list_parens(true);
1247+
emit(*alt->expr);
1248+
pop_need_expression_list_parens();
1249+
}
1250+
} else if (alt->literal) {
12461251
emit(*alt->literal);
1252+
}
12471253
printer.emit_to_string();
12481254

12491255
assert (*alt->is_as_keyword == "is" || *alt->is_as_keyword == "as");

source/parse.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ struct alternative_node
607607
source_position equal_sign;
608608
std::unique_ptr<statement_node> statement;
609609
token const* literal;
610+
std::unique_ptr<expression_list_node> expr;
610611

611612
auto position() const -> source_position
612613
{
@@ -2249,6 +2250,25 @@ class parser
22492250
return {};
22502251
}
22512252

2253+
if (curr().type() == lexeme::LeftParen) {
2254+
auto open_paren = curr().position();
2255+
next();
2256+
auto expr_list = expression_list(open_paren);
2257+
if (!expr_list) {
2258+
error("unexpected text - ( is not followed by an expression-list");
2259+
next();
2260+
return {};
2261+
}
2262+
if (curr().type() != lexeme::RightParen) {
2263+
error("unexpected text - expression-list is not terminated by )");
2264+
next();
2265+
return {};
2266+
}
2267+
expr_list->close_paren = curr().position();
2268+
next();
2269+
n->expr = std::move(expr_list);
2270+
}
2271+
22522272
if (curr().type() != lexeme::Assignment) {
22532273
error("expected = at start of inspect alternative body");
22542274
return {};

0 commit comments

Comments
 (0)