Skip to content

Commit 1bb321c

Browse files
committed
Add parsing and cppfront emit lambdas in alternatives
1 parent cd03b8e commit 1bb321c

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
@@ -1283,10 +1283,16 @@ class cppfront
12831283
auto id = std::string{};
12841284
printer.emit_to_string(&id);
12851285
assert(alt->id_expression || alt->literal);
1286-
if (alt->id_expression)
1286+
if (alt->id_expression) {
12871287
emit(*alt->id_expression);
1288-
else if (alt->literal)
1288+
if (alt->expr) {
1289+
push_need_expression_list_parens(true);
1290+
emit(*alt->expr);
1291+
pop_need_expression_list_parens();
1292+
}
1293+
} else if (alt->literal) {
12891294
emit(*alt->literal);
1295+
}
12901296
printer.emit_to_string();
12911297

12921298
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
@@ -668,6 +668,7 @@ struct alternative_node
668668
source_position equal_sign;
669669
std::unique_ptr<statement_node> statement;
670670
token const* literal;
671+
std::unique_ptr<expression_list_node> expr;
671672

672673
auto position() const -> source_position
673674
{
@@ -2359,6 +2360,25 @@ class parser
23592360
return {};
23602361
}
23612362

2363+
if (curr().type() == lexeme::LeftParen) {
2364+
auto open_paren = curr().position();
2365+
next();
2366+
auto expr_list = expression_list(open_paren);
2367+
if (!expr_list) {
2368+
error("unexpected text - ( is not followed by an expression-list");
2369+
next();
2370+
return {};
2371+
}
2372+
if (curr().type() != lexeme::RightParen) {
2373+
error("unexpected text - expression-list is not terminated by )");
2374+
next();
2375+
return {};
2376+
}
2377+
expr_list->close_paren = curr().position();
2378+
next();
2379+
n->expr = std::move(expr_list);
2380+
}
2381+
23622382
if (curr().type() != lexeme::Assignment) {
23632383
error("expected = at start of inspect alternative body");
23642384
return {};

0 commit comments

Comments
 (0)