Skip to content

Commit 42f7eb6

Browse files
committed
Add support for literals in inspect alternatives
1 parent cfa45f6 commit 42f7eb6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

source/cppfront.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,11 @@ class cppfront
12391239

12401240
auto id = std::string{};
12411241
printer.emit_to_string(&id);
1242-
assert(alt->id_expression);
1243-
emit(*alt->id_expression);
1242+
assert(alt->id_expression || alt->literal);
1243+
if (alt->id_expression)
1244+
emit(*alt->id_expression);
1245+
else if (alt->literal)
1246+
emit(*alt->literal);
12441247
printer.emit_to_string();
12451248

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

source/parse.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ struct alternative_node
606606
std::unique_ptr<id_expression_node> id_expression;
607607
source_position equal_sign;
608608
std::unique_ptr<statement_node> statement;
609+
token const* literal;
609610

610611
auto position() const -> source_position
611612
{
@@ -718,8 +719,11 @@ auto alternative_node::visit(auto& v, int depth) -> void
718719
}
719720
assert (is_as_keyword);
720721
v.start(*is_as_keyword, depth+1);
721-
assert (id_expression);
722-
id_expression->visit(v, depth+1);
722+
assert (id_expression || literal);
723+
if (id_expression)
724+
id_expression->visit(v, depth+1);
725+
else if (literal)
726+
literal->visit(v, depth+1);
723727
assert (statement);
724728
statement->visit(v, depth+1);
725729
v.end(*this, depth);
@@ -2236,8 +2240,12 @@ class parser
22362240
if (auto id = id_expression()) {
22372241
n->id_expression = std::move(id);
22382242
}
2243+
else if (is_literal(curr().type())) {
2244+
n->literal = &curr();
2245+
next();
2246+
}
22392247
else {
2240-
error("expected id-expression after 'is' in inspect alternative");
2248+
error("expected id-expression or literal after 'is' in inspect alternative");
22412249
return {};
22422250
}
22432251

0 commit comments

Comments
 (0)